jean_core/sequence/
mod.rs

1mod seq;
2
3/// DNA/RNA codons (e.g. AAA, TAG, ATG)
4pub mod codon;
5
6/// DNA sequences + bases
7pub mod dna;
8
9/// Protein sequences + amino acids
10pub mod protein;
11
12/// RNA sequences + bases
13pub mod rna;
14
15pub use seq::*;
16
17/// Enum for differentiating between DNA, RNA, and Protein sequences
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
19pub enum Sequence {
20  Dna(Seq<dna::Base>),
21  Rna(Seq<rna::Base>),
22  Protein(Seq<protein::AminoAcid>),
23}
24
25impl TryFrom<&str> for Sequence {
26  type Error = String;
27
28  fn try_from(value: &str) -> Result<Self, Self::Error> {
29    match dna::Dna::try_from(value) {
30      Ok(dna) => Ok(Sequence::Dna(dna)),
31      Err(_) => match rna::Rna::try_from(value) {
32        Ok(rna) => Ok(Sequence::Rna(rna)),
33        Err(_) => match protein::Protein::try_from(value) {
34          Ok(protein) => Ok(Sequence::Protein(protein)),
35          Err(_) => Err(format!("Invalid sequence.")),
36        },
37      },
38    }
39  }
40}
41
42impl std::fmt::Display for Sequence {
43  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44    let s = match self {
45      Self::Dna(dna) => dna.to_string(),
46      Self::Rna(rna) => rna.to_string(),
47      Self::Protein(protein) => protein.to_string(),
48    };
49
50    s.chars()
51      .collect::<Vec<char>>()
52      .chunks(80)
53      .map(|c| {
54        c.iter()
55          .map(|c| write!(f, "{c}"))
56          .collect::<std::fmt::Result>()?;
57        writeln!(f)
58      })
59      .collect::<std::fmt::Result>()
60  }
61}