|
1 | | -#![allow(clippy::borrow_deref_ref)] // TODO: broken clippy lint? |
2 | | - // Copyright 2021-2024 SecureDNA Stiftung (SecureDNA Foundation) <licensing@securedna.org> |
3 | | - // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 1 | +// Copyright 2021-2024 SecureDNA Stiftung (SecureDNA Foundation) <licensing@securedna.org> |
| 2 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
4 | 3 |
|
5 | | -use pyo3::{exceptions::PyValueError, prelude::*, types::PyBytes}; |
| 4 | +#[pyo3::pymodule] |
| 5 | +mod quickdna { |
6 | 6 |
|
7 | | -use crate::{ |
8 | | - errors::TranslationError, |
9 | | - trans_table::{reverse_complement_bytes, TranslationTable}, |
10 | | - Nucleotide, NucleotideAmbiguous, |
11 | | -}; |
| 7 | + use pyo3::{exceptions::PyValueError, prelude::*, types::PyBytes}; |
12 | 8 |
|
13 | | -impl From<TranslationError> for PyErr { |
14 | | - fn from(err: TranslationError) -> PyErr { |
15 | | - PyValueError::new_err(err.to_string()) |
16 | | - } |
17 | | -} |
18 | | - |
19 | | -#[pyfunction] |
20 | | -fn _check_table(table: u8) -> PyResult<()> { |
21 | | - let _ = TranslationTable::try_from(table)?; |
22 | | - Ok(()) |
23 | | -} |
| 9 | + use crate::{ |
| 10 | + errors::TranslationError, |
| 11 | + trans_table::{reverse_complement_bytes, TranslationTable}, |
| 12 | + Nucleotide, NucleotideAmbiguous, |
| 13 | + }; |
24 | 14 |
|
25 | | -/// Translate a bytestring of DNA nucleotides into a bytestring of amino acids. |
26 | | -/// |
27 | | -/// The input string is allowed to contain IUPAC ambiguity codes; ambiguous |
28 | | -/// codons are represented by `X` in the output. |
29 | | -/// |
30 | | -/// * `translate(b"CCNTACACK CATNCNAAT")` returns `b"PYTHXN"` |
31 | | -#[pyfunction] |
32 | | -fn _translate(py: Python, table: u8, dna: &PyBytes) -> PyResult<PyObject> { |
33 | | - let table = TranslationTable::try_from(table)?; |
34 | | - let bytes = table.translate_dna_bytes::<NucleotideAmbiguous>(dna.as_bytes())?; |
35 | | - Ok(PyBytes::new(py, &bytes).into()) |
36 | | -} |
| 15 | + impl From<TranslationError> for PyErr { |
| 16 | + fn from(err: TranslationError) -> PyErr { |
| 17 | + PyValueError::new_err(err.to_string()) |
| 18 | + } |
| 19 | + } |
37 | 20 |
|
38 | | -/// Translate a bytestring of DNA nucleotides into a bytestring of amino acids. |
39 | | -/// |
40 | | -/// The input string is validated to consist of unambiguous nucleotides (no IUPAC ambiguity codes). |
41 | | -/// |
42 | | -/// * `translate_strict(b"AAACCCTTTGGG")` returns `b"KPFG"` |
43 | | -/// * `translate_strict(b"AAACCCTTTGGN")` is an error. |
44 | | -#[pyfunction] |
45 | | -fn _translate_strict(py: Python, table: u8, dna: &PyBytes) -> PyResult<PyObject> { |
46 | | - let table = TranslationTable::try_from(table)?; |
47 | | - let bytes = table.translate_dna_bytes::<Nucleotide>(dna.as_bytes())?; |
48 | | - Ok(PyBytes::new(py, &bytes).into()) |
49 | | -} |
| 21 | + #[pyfunction] |
| 22 | + fn _check_table(table: u8) -> PyResult<()> { |
| 23 | + let _ = TranslationTable::try_from(table)?; |
| 24 | + Ok(()) |
| 25 | + } |
50 | 26 |
|
51 | | -/// Get the reverse complement of a bytestring of DNA nucleotides. |
52 | | -/// |
53 | | -/// The input string is allowed to contain IUPAC ambiguity codes. |
54 | | -/// |
55 | | -/// * `reverse_complement(b"AAAAABCCC")` returns `b"GGGVTTTTT"` |
56 | | -#[pyfunction] |
57 | | -fn _reverse_complement(py: Python, dna: &PyBytes) -> PyResult<PyObject> { |
58 | | - let bytes = reverse_complement_bytes::<NucleotideAmbiguous>(dna.as_bytes())?; |
59 | | - Ok(PyBytes::new(py, &bytes).into()) |
60 | | -} |
| 27 | + /// Translate a bytestring of DNA nucleotides into a bytestring of amino acids. |
| 28 | + /// |
| 29 | + /// The input string is allowed to contain IUPAC ambiguity codes; ambiguous |
| 30 | + /// codons are represented by `X` in the output. |
| 31 | + /// |
| 32 | + /// * `translate(b"CCNTACACK CATNCNAAT")` returns `b"PYTHXN"` |
| 33 | + #[pyfunction] |
| 34 | + fn _translate(py: Python, table: u8, dna: Bound<'_, PyBytes>) -> PyResult<Py<PyAny>> { |
| 35 | + let table = TranslationTable::try_from(table)?; |
| 36 | + let bytes = table.translate_dna_bytes::<NucleotideAmbiguous>(dna.as_bytes())?; |
| 37 | + Ok(PyBytes::new(py, &bytes).into()) |
| 38 | + } |
61 | 39 |
|
62 | | -/// Get the reverse complement of a bytestring of DNA nucleotides. |
63 | | -/// |
64 | | -/// The input string is validated to consist of unambiguous nucleotides (no IUPAC ambiguity codes). |
65 | | -/// |
66 | | -/// * `reverse_complement_strict(b"AAAAAACCC")` returns `b"GGGTTTTTT"` |
67 | | -/// * `reverse_complement_strict(b"AAAAAACCN")` is an error. |
68 | | -#[pyfunction] |
69 | | -fn _reverse_complement_strict(py: Python, dna: &PyBytes) -> PyResult<PyObject> { |
70 | | - let bytes = reverse_complement_bytes::<Nucleotide>(dna.as_bytes())?; |
71 | | - Ok(PyBytes::new(py, &bytes).into()) |
72 | | -} |
| 40 | + /// Translate a bytestring of DNA nucleotides into a bytestring of amino acids. |
| 41 | + /// |
| 42 | + /// The input string is validated to consist of unambiguous nucleotides (no IUPAC ambiguity codes). |
| 43 | + /// |
| 44 | + /// * `translate_strict(b"AAACCCTTTGGG")` returns `b"KPFG"` |
| 45 | + /// * `translate_strict(b"AAACCCTTTGGN")` is an error. |
| 46 | + #[pyfunction] |
| 47 | + fn _translate_strict(py: Python, table: u8, dna: Bound<'_, PyBytes>) -> PyResult<Py<PyAny>> { |
| 48 | + let table = TranslationTable::try_from(table)?; |
| 49 | + let bytes = table.translate_dna_bytes::<Nucleotide>(dna.as_bytes())?; |
| 50 | + Ok(PyBytes::new(py, &bytes).into()) |
| 51 | + } |
73 | 52 |
|
74 | | -#[pymodule] |
75 | | -fn quickdna(_py: Python, m: &PyModule) -> PyResult<()> { |
76 | | - m.add_function(wrap_pyfunction!(_check_table, m)?)?; |
77 | | - m.add_function(wrap_pyfunction!(_translate, m)?)?; |
78 | | - m.add_function(wrap_pyfunction!(_translate_strict, m)?)?; |
79 | | - m.add_function(wrap_pyfunction!(_reverse_complement, m)?)?; |
80 | | - m.add_function(wrap_pyfunction!(_reverse_complement_strict, m)?)?; |
| 53 | + /// Get the reverse complement of a bytestring of DNA nucleotides. |
| 54 | + /// |
| 55 | + /// The input string is allowed to contain IUPAC ambiguity codes. |
| 56 | + /// |
| 57 | + /// * `reverse_complement(b"AAAAABCCC")` returns `b"GGGVTTTTT"` |
| 58 | + #[pyfunction] |
| 59 | + fn _reverse_complement(py: Python, dna: Bound<'_, PyBytes>) -> PyResult<Py<PyAny>> { |
| 60 | + let bytes = reverse_complement_bytes::<NucleotideAmbiguous>(dna.as_bytes())?; |
| 61 | + Ok(PyBytes::new(py, &bytes).into()) |
| 62 | + } |
81 | 63 |
|
82 | | - Ok(()) |
| 64 | + /// Get the reverse complement of a bytestring of DNA nucleotides. |
| 65 | + /// |
| 66 | + /// The input string is validated to consist of unambiguous nucleotides (no IUPAC ambiguity codes). |
| 67 | + /// |
| 68 | + /// * `reverse_complement_strict(b"AAAAAACCC")` returns `b"GGGTTTTTT"` |
| 69 | + /// * `reverse_complement_strict(b"AAAAAACCN")` is an error. |
| 70 | + #[pyfunction] |
| 71 | + fn _reverse_complement_strict(py: Python, dna: Bound<'_, PyBytes>) -> PyResult<Py<PyAny>> { |
| 72 | + let bytes = reverse_complement_bytes::<Nucleotide>(dna.as_bytes())?; |
| 73 | + Ok(PyBytes::new(py, &bytes).into()) |
| 74 | + } |
83 | 75 | } |
0 commit comments