spdi/
util.rs

1// Copyright Ryangguk Kim @ Oak Bioinformatics, LLC
2// 
3// This software is available under a dual licensing model, offering users the choice between the Affero General Public License version 3 (AGPL-3) for open-source use and a commercial license for proprietary or commercial use. 
4// 
5// To obtain a commercial license, please contact info@oakbioinformatics.com.
6
7use crate::error;
8use anyhow::{Error, Result};
9use noodles::vcf::record::reference_bases::Base;
10
11pub fn get_bases_of_string(s: &str) -> Result<Vec<Base>> {
12    let mut bases: Vec<Base> = Vec::with_capacity(s.len());
13    for c in s.chars() {
14        let base = get_base_of_char(c)?;
15        match base {
16            None => {}
17            Some(v) => bases.push(v),
18        }
19    }
20    Ok(bases)
21}
22
23pub fn get_base_of_char(c: char) -> Result<Option<Base>> {
24    match c {
25        'A' => Ok(Some(Base::A)),
26        'T' => Ok(Some(Base::T)),
27        'G' => Ok(Some(Base::G)),
28        'C' => Ok(Some(Base::C)),
29        'N' => Ok(Some(Base::N)),
30        '-' => Ok(None),
31        _ => {
32            let e = error::InvalidBase { base: c.to_string() };
33            Err(Error::new(e))
34        }
35    }
36}
37
38pub fn get_char_of_base(base: &Base) -> char {
39    match base {
40        Base::A => 'A',
41        Base::T => 'T',
42        Base::G => 'G',
43        Base::C => 'C',
44        Base::N => 'N',
45    }
46}
47
48pub fn get_string_of_bases(bases: &Vec<Base>) -> String {
49    let bases_len = bases.len();
50    match bases_len {
51        0 => "-".to_string(),
52        _ => {
53            let mut s: String = String::with_capacity(bases_len);
54            for base in bases {
55                s.push(get_char_of_base(base));
56            }
57            s
58        }
59    }
60}
61
62pub fn is_base_same_as_char(base: &Base, c: char) -> bool {
63    match base {
64        Base::A => {
65            if c == 'A' {
66                true
67            } else {
68                false
69            }
70        }
71        Base::T => {
72            if c == 'T' {
73                true
74            } else {
75                false
76            }
77        }
78        Base::G => {
79            if c == 'G' {
80                true
81            } else {
82                false
83            }
84        }
85        Base::C => {
86            if c == 'C' {
87                true
88            } else {
89                false
90            }
91        }
92        _ => {
93            eprint!("Base error. base={:#?}", base);
94            false
95        }
96    }
97}