1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::borrow::Borrow;
use crate::alphabets::Alphabet;
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")
}
lazy_static! {
static ref COMPLEMENT: [u8; 256] = {
let mut comp = [0; 256];
for (v, a) in comp.iter_mut().enumerate() {
*a = v as u8;
}
for (&a, &b) in b"AGCUYRWSKMDVHBNZ".iter().zip(b"UCGARYWSMKHBDVNZ".iter()) {
comp[a as usize] = b;
comp[a as usize + 32] = b + 32;
}
comp
};
}
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");
}
}