noodles_sam/alignment/record/
sequence.rs

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
/// An alignment record sequence.
pub trait Sequence {
    /// Returns whether there are any bases.
    fn is_empty(&self) -> bool;

    /// Returns the number of bases.
    fn len(&self) -> usize;

    /// Returns the base at the given index.
    fn get(&self, i: usize) -> Option<u8>;

    /// Splits the subsequence into two subsequences at the given index.
    fn split_at_checked(
        &self,
        mid: usize,
    ) -> Option<(Box<dyn Sequence + '_>, Box<dyn Sequence + '_>)>;

    /// Returns an iterator over bases.
    fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_>;
}

impl<'a> IntoIterator for &'a dyn Sequence {
    type Item = u8;
    type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl Sequence for Box<dyn Sequence + '_> {
    fn is_empty(&self) -> bool {
        (**self).is_empty()
    }

    fn len(&self) -> usize {
        (**self).len()
    }

    fn get(&self, i: usize) -> Option<u8> {
        (**self).get(i)
    }

    fn split_at_checked(
        &self,
        mid: usize,
    ) -> Option<(Box<dyn Sequence + '_>, Box<dyn Sequence + '_>)> {
        (**self).split_at_checked(mid)
    }

    fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
        (**self).iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_into_iter() {
        struct T(Vec<u8>);

        impl Sequence for T {
            fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            fn len(&self) -> usize {
                self.0.len()
            }

            fn get(&self, i: usize) -> Option<u8> {
                self.0.get(i).copied()
            }

            fn split_at_checked(
                &self,
                mid: usize,
            ) -> Option<(Box<dyn Sequence + '_>, Box<dyn Sequence + '_>)> {
                let (left, right) = self.0.split_at(mid);
                Some((Box::new(T(left.to_vec())), Box::new(T(right.to_vec()))))
            }

            fn iter(&self) -> Box<dyn Iterator<Item = u8> + '_> {
                Box::new(self.0.iter().copied())
            }
        }

        let sequence: &dyn Sequence = &T(vec![b'N', b'D', b'L', b'S']);

        assert_eq!(
            sequence.into_iter().collect::<Vec<_>>(),
            [b'N', b'D', b'L', b'S']
        );
    }
}