Skip to content

Commit 6ff9c1e

Browse files
authored
Refactor DNA translation functions to use PyAny
1 parent 7cdcbc4 commit 6ff9c1e

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/python_api.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ fn _check_table(table: u8) -> PyResult<()> {
2929
///
3030
/// * `translate(b"CCNTACACK CATNCNAAT")` returns `b"PYTHXN"`
3131
#[pyfunction]
32-
fn _translate(py: Python<'_>, table: u8, dna: &PyBytes) -> PyResult<Py<PyAny>> {
32+
fn _translate(py: Python, table: u8, dna: &PyAny) -> PyResult<Py<PyAny>> {
3333
let table = TranslationTable::try_from(table)?;
34-
let bytes = table.translate_dna_bytes::<NucleotideAmbiguous>(dna.as_bytes())?;
34+
let dna_bytes = dna.downcast::<PyBytes>()?;
35+
let bytes = table.translate_dna_bytes::<NucleotideAmbiguous>(dna_bytes.as_bytes())?;
3536
Ok(PyBytes::new(py, &bytes).into_py(py))
3637
}
3738

@@ -42,9 +43,10 @@ fn _translate(py: Python<'_>, table: u8, dna: &PyBytes) -> PyResult<Py<PyAny>> {
4243
/// * `translate_strict(b"AAACCCTTTGGG")` returns `b"KPFG"`
4344
/// * `translate_strict(b"AAACCCTTTGGN")` is an error.
4445
#[pyfunction]
45-
fn _translate_strict(py: Python<'_>, table: u8, dna: &PyBytes) -> PyResult<Py<PyAny>> {
46+
fn _translate_strict(py: Python, table: u8, dna: &PyAny) -> PyResult<Py<PyAny>> {
4647
let table = TranslationTable::try_from(table)?;
47-
let bytes = table.translate_dna_bytes::<Nucleotide>(dna.as_bytes())?;
48+
let dna_bytes = dna.downcast::<PyBytes>()?;
49+
let bytes = table.translate_dna_bytes::<Nucleotide>(dna_bytes.as_bytes())?;
4850
Ok(PyBytes::new(py, &bytes).into_py(py))
4951
}
5052

@@ -54,8 +56,9 @@ fn _translate_strict(py: Python<'_>, table: u8, dna: &PyBytes) -> PyResult<Py<Py
5456
///
5557
/// * `reverse_complement(b"AAAAABCCC")` returns `b"GGGVTTTTT"`
5658
#[pyfunction]
57-
fn _reverse_complement(py: Python<'_>, dna: &PyBytes) -> PyResult<Py<PyAny>> {
58-
let bytes = reverse_complement_bytes::<NucleotideAmbiguous>(dna.as_bytes())?;
59+
fn _reverse_complement(py: Python, dna: &PyAny) -> PyResult<Py<PyAny>> {
60+
let dna_bytes = dna.downcast::<PyBytes>()?;
61+
let bytes = reverse_complement_bytes::<NucleotideAmbiguous>(dna_bytes.as_bytes())?;
5962
Ok(PyBytes::new(py, &bytes).into_py(py))
6063
}
6164

@@ -66,13 +69,14 @@ fn _reverse_complement(py: Python<'_>, dna: &PyBytes) -> PyResult<Py<PyAny>> {
6669
/// * `reverse_complement_strict(b"AAAAAACCC")` returns `b"GGGTTTTTT"`
6770
/// * `reverse_complement_strict(b"AAAAAACCN")` is an error.
6871
#[pyfunction]
69-
fn _reverse_complement_strict(py: Python<'_>, dna: &PyBytes) -> PyResult<Py<PyAny>> {
70-
let bytes = reverse_complement_bytes::<Nucleotide>(dna.as_bytes())?;
72+
fn _reverse_complement_strict(py: Python, dna: &PyAny) -> PyResult<Py<PyAny>> {
73+
let dna_bytes = dna.downcast::<PyBytes>()?;
74+
let bytes = reverse_complement_bytes::<Nucleotide>(dna_bytes.as_bytes())?;
7175
Ok(PyBytes::new(py, &bytes).into_py(py))
7276
}
7377

7478
#[pymodule]
75-
fn quickdna(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
79+
fn quickdna(py: Python, m: &PyModule) -> PyResult<()> {
7680
m.add_function(wrap_pyfunction!(_check_table, m)?)?;
7781
m.add_function(wrap_pyfunction!(_translate, m)?)?;
7882
m.add_function(wrap_pyfunction!(_translate_strict, m)?)?;

0 commit comments

Comments
 (0)