deepbiop_core/
seq.rs

1use needletail::Sequence;
2use pyo3::prelude::*;
3use pyo3_stub_gen::derive::*;
4
5/// Normalize a DNA sequence by converting any non-standard nucleotides to standard ones.
6///
7/// This function takes a DNA sequence as a `String` and a boolean flag `iupac` indicating whether to normalize using IUPAC ambiguity codes.
8/// It returns a normalized DNA sequence as a `String`.
9///
10/// # Arguments
11///
12/// * `seq` - A DNA sequence as a `String`.
13/// * `iupac` - A boolean flag indicating whether to normalize using IUPAC ambiguity codes.
14///
15/// # Returns
16///
17/// A normalized DNA sequence as a `String`.
18#[gen_stub_pyfunction(module = "deepbiop.core")]
19#[pyfunction]
20pub fn normalize_seq(seq: String, iupac: bool) -> String {
21    String::from_utf8_lossy(&seq.as_bytes().normalize(iupac)).to_string()
22}
23
24/// Generate the reverse complement of a DNA sequence.
25///
26/// This function takes a DNA sequence as a `String` and returns its reverse complement.
27/// The reverse complement is generated by reversing the sequence and replacing each nucleotide
28/// with its complement (A<->T, C<->G).
29///
30/// # Arguments
31///
32/// * `seq` - A DNA sequence as a `String`
33///
34/// # Returns
35///
36/// The reverse complement sequence as a `String`
37///
38/// # Example
39///
40/// ```
41/// use deepbiop_core::seq::reverse_complement;
42///
43/// let seq = String::from("ATCG");
44/// let rev_comp = reverse_complement(seq);
45/// assert_eq!(rev_comp, "CGAT");
46/// ```
47#[gen_stub_pyfunction(module = "deepbiop.core")]
48#[pyfunction]
49pub fn reverse_complement(seq: String) -> String {
50    String::from_utf8(seq.as_bytes().reverse_complement()).unwrap()
51}