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
/// An infinite iterator producing random bases (A, C, G, T).
pub struct RandomBases {}

/// An infinite iterator producing random A or T.
pub struct RandomAT {}

impl Iterator for RandomBases {
    type Item = char;
    fn next(&mut self) -> Option<Self::Item> {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        let between = rand::distributions::Uniform::from(0..4);
        Some(match rng.sample(&between) {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            _ => 'T',
        })
    }
}

impl Iterator for RandomAT {
    type Item = char;
    fn next(&mut self) -> Option<Self::Item> {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        if rng.gen() {
            Some('A')
        } else {
            Some('T')
        }
    }
}

impl RandomBases {
    /// Create the iterator.
    pub fn new() -> Self {
        RandomBases {}
    }
}

impl RandomAT {
    /// Create the iterator.
    pub fn new() -> Self {
        RandomAT {}
    }
}

/// Complement of a DNA base.
pub fn complement(base: char) -> char {
    match base {
        'A' | 'a' => 'T',
        'T' | 't' => 'A',
        'C' | 'c' => 'G',
        'G' | 'g' => 'C',
        // '*' => '*',
        _ => {
            panic!("unexpected character");
        }
    }
}