noodles_sam/alignment/record_buf/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::ops::{Index, IndexMut};

use noodles_core::position::SequenceIndex;

/// An alignment record sequence buffer.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Sequence(Vec<u8>);

impl Sequence {
    /// Returns whether there are any bases.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the number of bases.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns the base at the given index.
    pub fn get(&self, i: usize) -> Option<u8> {
        self.0.get(i).copied()
    }
}

impl AsRef<[u8]> for Sequence {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl AsMut<Vec<u8>> for Sequence {
    fn as_mut(&mut self) -> &mut Vec<u8> {
        &mut self.0
    }
}

impl From<&[u8]> for Sequence {
    fn from(buf: &[u8]) -> Self {
        Self::from(Vec::from(buf))
    }
}

impl<const N: usize> From<&[u8; N]> for Sequence {
    fn from(buf: &[u8; N]) -> Self {
        Self::from(buf.as_slice())
    }
}

impl From<Vec<u8>> for Sequence {
    fn from(bases: Vec<u8>) -> Self {
        Self(bases)
    }
}

impl<I> Index<I> for Sequence
where
    I: SequenceIndex<u8>,
{
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        index.index(&self.0)
    }
}

impl<I> IndexMut<I> for Sequence
where
    I: SequenceIndex<u8>,
{
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        index.index_mut(&mut self.0)
    }
}

impl From<Sequence> for Vec<u8> {
    fn from(sequence: Sequence) -> Self {
        sequence.0
    }
}

impl crate::alignment::record::Sequence for &Sequence {
    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 crate::alignment::record::Sequence + '_>,
        Box<dyn crate::alignment::record::Sequence + '_>,
    )> {
        if mid > self.len() {
            let (left, right) = self.0.split_at(mid);
            Some((
                Box::new(crate::record::Sequence::new(left)),
                Box::new(crate::record::Sequence::new(right)),
            ))
        } else {
            None
        }
    }

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