use crate::alphabets::Alphabet;
use std::borrow::Borrow;
use std::sync::LazyLock;
pub fn alphabet() -> Alphabet {
Alphabet::new(b"ACGUacgu")
}
pub fn n_alphabet() -> Alphabet {
Alphabet::new(b"ACGUNacgun")
}
pub fn iupac_alphabet() -> Alphabet {
Alphabet::new(b"ACGURYSWKMBDHVNZacguryswkmbdhvnz")
}
static COMPLEMENT: LazyLock<[u8; 256]> = LazyLock::new(|| {
let mut comp = [0; 256];
comp.iter_mut().enumerate().for_each(|(v, a)| {
*a = v as u8;
});
b"AGCUYRWSKMDVHBNZ"
.iter()
.zip(b"UCGARYWSMKHBDVNZ".iter())
.for_each(|(&a, &b)| {
comp[a as usize] = b;
comp[a as usize + 32] = b + 32;
});
comp
});
#[inline]
pub fn complement(a: u8) -> u8 {
COMPLEMENT[a as usize]
}
pub fn revcomp<C, T>(text: T) -> Vec<u8>
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
T::IntoIter: DoubleEndedIterator,
{
text.into_iter()
.rev()
.map(|a| complement(*a.borrow()))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_word() {
assert!(alphabet().is_word(b"GAUUACA"));
}
#[test]
fn is_no_word() {
assert!(!alphabet().is_word(b"gaTTaca"));
}
#[test]
fn symbol_is_no_word() {
assert!(!alphabet().is_word(b"#"));
}
#[test]
fn number_is_no_word() {
assert!(!alphabet().is_word(b"42"));
}
#[test]
fn test_reverse_complement() {
assert_eq!(revcomp(b"GAUUACA"), b"UGUAAUC");
}
}