|
| 1 | +use numpy::ndarray::Array1; |
| 2 | +use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1}; |
| 3 | +use pyo3::prelude::*; |
| 4 | +use pyo3::types::PyTuple; |
| 5 | + |
| 6 | +/// A codec for encoding/decoding between ASCII symbols and integer codes, |
| 7 | +/// based on a given alphabet of allowed symbols. |
| 8 | +#[pyclass(module = "biotite.rust.sequence")] |
| 9 | +pub struct AlphabetCodec { |
| 10 | + /// Maps ASCII byte value -> symbol code. `illegal_code` for unmapped symbols. |
| 11 | + symbol_to_code: [u8; 256], |
| 12 | + /// The alphabet symbols (as bytes), indexed by code. |
| 13 | + code_to_symbol: Vec<u8>, |
| 14 | + /// The sentinel code that marks an illegal/unmapped symbol. |
| 15 | + illegal_code: u8, |
| 16 | +} |
| 17 | + |
| 18 | +#[pymethods] |
| 19 | +impl AlphabetCodec { |
| 20 | + /// Create a new codec from the alphabet's symbols. |
| 21 | + /// |
| 22 | + /// This class can only be used if the symbols are ASCII characters. |
| 23 | + /// |
| 24 | + /// Parameters |
| 25 | + /// ---------- |
| 26 | + /// symbols : ndarray, dtype=uint8 |
| 27 | + /// The ASCII characters as bytes. |
| 28 | + /// The index of each symbol becomes its code. |
| 29 | + #[new] |
| 30 | + fn new(symbols: PyReadonlyArray1<u8>) -> PyResult<Self> { |
| 31 | + let symbols = symbols.as_slice()?; |
| 32 | + if symbols.len() > 255 { |
| 33 | + return Err(pyo3::exceptions::PyValueError::new_err( |
| 34 | + "Alphabet must have at most 255 symbols", |
| 35 | + )); |
| 36 | + } |
| 37 | + // As the symbol code `symbols.len()` is always illegal, |
| 38 | + // it can be used later to check for invalid input symbols |
| 39 | + let illegal_code = symbols.len() as u8; |
| 40 | + // An array based map that maps from symbol to code |
| 41 | + // Since the maximum value of a char is 256 |
| 42 | + // the size of the map is known at compile time |
| 43 | + // Initially fill the map with the illegal symbol |
| 44 | + // Consequently, the map will later return the illegal symbol |
| 45 | + // when indexed with a character that is not part of the alphabet |
| 46 | + let mut symbol_to_code = [illegal_code; 256]; |
| 47 | + for (i, &symbol) in symbols.iter().enumerate() { |
| 48 | + symbol_to_code[symbol as usize] = i as u8; |
| 49 | + } |
| 50 | + Ok(AlphabetCodec { |
| 51 | + symbol_to_code, |
| 52 | + code_to_symbol: symbols.to_vec(), |
| 53 | + illegal_code, |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + /// Encode an array of ASCII symbols into an array of symbol codes. |
| 58 | + /// |
| 59 | + /// Parameters |
| 60 | + /// ---------- |
| 61 | + /// symbols : ndarray, dtype=uint8 |
| 62 | + /// The symbols (as bytes) to encode. |
| 63 | + /// |
| 64 | + /// Returns |
| 65 | + /// ------- |
| 66 | + /// code : ndarray, dtype=uint8 |
| 67 | + /// The encoded symbol codes. |
| 68 | + /// |
| 69 | + /// Raises |
| 70 | + /// ------ |
| 71 | + /// AlphabetError |
| 72 | + /// If any symbol is not in the alphabet. |
| 73 | + fn encode<'py>( |
| 74 | + &self, |
| 75 | + py: Python<'py>, |
| 76 | + symbols: PyReadonlyArray1<u8>, |
| 77 | + ) -> PyResult<Bound<'py, PyArray1<u8>>> { |
| 78 | + let symbols = symbols.as_slice()?; |
| 79 | + let mut code = Array1::<u8>::uninit(symbols.len()); |
| 80 | + |
| 81 | + for (&sym, out_code) in symbols.iter().zip( |
| 82 | + code.as_slice_mut() |
| 83 | + .expect("Array not contiguous") |
| 84 | + .iter_mut(), |
| 85 | + ) { |
| 86 | + let c = self.symbol_to_code[sym as usize]; |
| 87 | + if c == self.illegal_code { |
| 88 | + let alphabet_error = py |
| 89 | + .import("biotite.sequence.alphabet")? |
| 90 | + .getattr("AlphabetError")?; |
| 91 | + return Err(PyErr::from_value(alphabet_error.call1((format!( |
| 92 | + "Symbol {} is not in the alphabet", |
| 93 | + repr_char(sym) |
| 94 | + ),))?)); |
| 95 | + } |
| 96 | + out_code.write(c); |
| 97 | + } |
| 98 | + // SAFETY: All n elements have been written above |
| 99 | + let code = unsafe { code.assume_init() }; |
| 100 | + Ok(code.into_pyarray(py)) |
| 101 | + } |
| 102 | + |
| 103 | + /// Decode an array of symbol codes into an array of ASCII symbols. |
| 104 | + /// |
| 105 | + /// Parameters |
| 106 | + /// ---------- |
| 107 | + /// code : ndarray, dtype=uint8 |
| 108 | + /// The symbol codes to decode. |
| 109 | + /// |
| 110 | + /// Returns |
| 111 | + /// ------- |
| 112 | + /// symbols : ndarray, dtype=uint8 |
| 113 | + /// The decoded symbols as bytes. |
| 114 | + /// |
| 115 | + /// Raises |
| 116 | + /// ------ |
| 117 | + /// AlphabetError |
| 118 | + /// If any code is not valid in the alphabet. |
| 119 | + fn decode<'py>( |
| 120 | + &self, |
| 121 | + py: Python<'py>, |
| 122 | + code: PyReadonlyArray1<u8>, |
| 123 | + ) -> PyResult<Bound<'py, PyArray1<u8>>> { |
| 124 | + let code = code.as_slice()?; |
| 125 | + let mut symbols = Array1::<u8>::uninit(code.len()); |
| 126 | + |
| 127 | + for (&c, out_symbol) in code.iter().zip( |
| 128 | + symbols |
| 129 | + .as_slice_mut() |
| 130 | + .expect("Array not contiguous") |
| 131 | + .iter_mut(), |
| 132 | + ) { |
| 133 | + if (c as usize) >= self.code_to_symbol.len() { |
| 134 | + let alphabet_error = py |
| 135 | + .import("biotite.sequence.alphabet")? |
| 136 | + .getattr("AlphabetError")?; |
| 137 | + return Err(PyErr::from_value( |
| 138 | + alphabet_error.call1((format!("'{}' is not a valid code", c),))?, |
| 139 | + )); |
| 140 | + } |
| 141 | + out_symbol.write(self.code_to_symbol[c as usize]); |
| 142 | + } |
| 143 | + // SAFETY: All n elements have been written above |
| 144 | + let symbols = unsafe { symbols.assume_init() }; |
| 145 | + Ok(symbols.into_pyarray(py)) |
| 146 | + } |
| 147 | + |
| 148 | + fn __reduce__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> { |
| 149 | + let cls = py |
| 150 | + .import("biotite.rust.sequence")? |
| 151 | + .getattr("AlphabetCodec")?; |
| 152 | + let code_to_symbol = self.code_to_symbol.clone().into_pyarray(py); |
| 153 | + let args = PyTuple::new(py, [code_to_symbol.into_any()])?; |
| 154 | + PyTuple::new(py, [cls.unbind(), args.into_any().unbind()]) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/// Format a byte value as a Python-style repr for error messages. |
| 159 | +fn repr_char(byte: u8) -> String { |
| 160 | + let c = byte as char; |
| 161 | + if c.is_ascii_graphic() || c == ' ' { |
| 162 | + format!("'{}'", c) |
| 163 | + } else { |
| 164 | + format!("'\\x{:02x}'", byte) |
| 165 | + } |
| 166 | +} |
0 commit comments