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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! BAM record CIGAR operation.

use std::{convert::TryFrom, error, fmt, mem};

use byteorder::{ByteOrder, LittleEndian};
use noodles_sam::record::cigar::op::Kind;

const MAX_LENGTH: u32 = 268_435_455; // 2^28 - 1

/// A BAM record CIGAR operation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Op {
    kind: Kind,
    len: u32,
}

/// An error returned when the operation length is > 2^28.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LengthError(u32);

impl error::Error for LengthError {}

impl fmt::Display for LengthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "expected operation length to be <= {}, got {}",
            MAX_LENGTH, self.0
        )
    }
}

impl Op {
    /// Creates a CIGAR operation.
    ///
    /// # Errors
    ///
    /// [`LengthError`] is returned if the length is > 2^28 - 1.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bam::record::cigar::Op;
    /// use noodles_sam::record::cigar::op::Kind;
    /// let op = Op::new(Kind::Match, 13)?;
    /// # Ok::<(), noodles_bam::record::cigar::op::LengthError>(())
    /// ```
    pub fn new(kind: Kind, len: u32) -> Result<Self, LengthError> {
        if len <= MAX_LENGTH {
            Ok(Self { kind, len })
        } else {
            Err(LengthError(len))
        }
    }

    /// Returns the kind of the operation.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bam::record::cigar::Op;
    /// use noodles_sam::record::cigar::op::Kind;
    /// let op = Op::new(Kind::Match, 13)?;
    /// assert_eq!(op.kind(), Kind::Match);
    /// # Ok::<(), noodles_bam::record::cigar::op::LengthError>(())
    /// ```
    pub fn kind(self) -> Kind {
        self.kind
    }

    /// Returns the length of the operation.
    ///
    /// The length is guaranteed to be <= 2^28 - 1.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bam::record::cigar::Op;
    /// use noodles_sam::record::cigar::op::Kind;
    /// let op = Op::new(Kind::Match, 13)?;
    /// assert_eq!(op.len(), 13);
    /// # Ok::<(), noodles_bam::record::cigar::op::LengthError>(())
    /// ```
    pub fn len(self) -> u32 {
        self.len
    }

    /// Returns whether the operation is a no-op.
    ///
    /// That is, whether the operation has a length of 0.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bam::record::cigar::Op;
    /// use noodles_sam::record::cigar::op::Kind;
    ///
    /// let op = Op::new(Kind::Match, 0)?;
    /// assert!(op.is_empty());
    ///
    /// let op = Op::new(Kind::Match, 13)?;
    /// assert!(!op.is_empty());
    /// # Ok::<(), noodles_bam::record::cigar::op::LengthError>(())
    /// ```
    pub fn is_empty(self) -> bool {
        self.len == 0
    }
}

/// An error returned when a raw u32 fails to convert.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TryFromUIntError {
    /// The operation is invalid.
    InvalidOp(u32),
    /// The length is invalid.
    InvalidLength(LengthError),
}

impl error::Error for TryFromUIntError {}

impl fmt::Display for TryFromUIntError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidOp(n) => write!(f, "invalid op: expected 0..=8, got {}", n),
            Self::InvalidLength(e) => write!(f, "invalid length: {}", e),
        }
    }
}

impl TryFrom<u32> for Op {
    type Error = TryFromUIntError;

    fn try_from(u: u32) -> Result<Self, Self::Error> {
        let len = u >> 4;

        let kind = match u & 0x0f {
            0 => Kind::Match,
            1 => Kind::Insertion,
            2 => Kind::Deletion,
            3 => Kind::Skip,
            4 => Kind::SoftClip,
            5 => Kind::HardClip,
            6 => Kind::Pad,
            7 => Kind::SeqMatch,
            8 => Kind::SeqMismatch,
            n => return Err(TryFromUIntError::InvalidOp(n)),
        };

        Self::new(kind, len).map_err(TryFromUIntError::InvalidLength)
    }
}

/// An error returned when a raw byte slice fails to convert.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TryFromByteSliceError {
    /// The input is invalid.
    Invalid(usize),
    /// The value is invalid.
    InvalidUInt(TryFromUIntError),
}

impl error::Error for TryFromByteSliceError {}

impl fmt::Display for TryFromByteSliceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid(len) => write!(f, "expected length to >= 4, got {}", len),
            Self::InvalidUInt(e) => write!(f, "invalid u32: {}", e),
        }
    }
}

impl TryFrom<&[u8]> for Op {
    type Error = TryFromByteSliceError;

    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        if buf.len() < mem::size_of::<u32>() {
            return Err(TryFromByteSliceError::Invalid(buf.len()));
        }

        let u = LittleEndian::read_u32(buf);
        Self::try_from(u).map_err(TryFromByteSliceError::InvalidUInt)
    }
}

impl fmt::Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.len(), self.kind())
    }
}

impl From<Op> for u32 {
    fn from(op: Op) -> Self {
        let i = op.kind() as u32;
        op.len() << 4 | i
    }
}

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

    #[test]
    fn test_try_from_u32() -> Result<(), LengthError> {
        assert_eq!(Op::try_from(1 << 4), Ok(Op::new(Kind::Match, 1)?));
        assert_eq!(Op::try_from(2 << 4 | 1), Ok(Op::new(Kind::Insertion, 2)?));
        assert_eq!(Op::try_from(3 << 4 | 2), Ok(Op::new(Kind::Deletion, 3)?));
        assert_eq!(Op::try_from(4 << 4 | 3), Ok(Op::new(Kind::Skip, 4)?));
        assert_eq!(Op::try_from(5 << 4 | 4), Ok(Op::new(Kind::SoftClip, 5)?));
        assert_eq!(Op::try_from(6 << 4 | 5), Ok(Op::new(Kind::HardClip, 6)?));
        assert_eq!(Op::try_from(7 << 4 | 6), Ok(Op::new(Kind::Pad, 7)?));
        assert_eq!(Op::try_from(8 << 4 | 7), Ok(Op::new(Kind::SeqMatch, 8)?));
        assert_eq!(Op::try_from(9 << 4 | 8), Ok(Op::new(Kind::SeqMismatch, 9)?));

        assert_eq!(
            Op::try_from(10 << 4 | 9),
            Err(TryFromUIntError::InvalidOp(9))
        );

        Ok(())
    }

    #[test]
    fn test_try_from_u8_slice() -> Result<(), LengthError> {
        let buf = [0x40, 0x02, 0x00, 0x00];
        assert_eq!(Op::try_from(&buf[..]), Ok(Op::new(Kind::Match, 36)?));

        let buf = [0x40, 0x02, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00];
        assert_eq!(Op::try_from(&buf[..]), Ok(Op::new(Kind::Match, 36)?));

        let buf = [];
        assert_eq!(
            Op::try_from(&buf[..]),
            Err(TryFromByteSliceError::Invalid(0))
        );

        let buf = [0x49, 0x02, 0x00, 0x00];
        assert_eq!(
            Op::try_from(&buf[..]),
            Err(TryFromByteSliceError::InvalidUInt(
                TryFromUIntError::InvalidOp(9)
            ))
        );

        Ok(())
    }

    #[test]
    fn test_fmt() -> Result<(), LengthError> {
        assert_eq!(Op::new(Kind::Match, 32)?.to_string(), "32M");
        assert_eq!(Op::new(Kind::Deletion, 7)?.to_string(), "7D");
        assert_eq!(Op::new(Kind::Skip, 11)?.to_string(), "11N");
        assert_eq!(Op::new(Kind::Pad, 188)?.to_string(), "188P");
        Ok(())
    }

    #[test]
    fn test_from_op_for_u32() -> Result<(), LengthError> {
        assert_eq!(u32::from(Op::new(Kind::Match, 1)?), 1 << 4);
        assert_eq!(u32::from(Op::new(Kind::Insertion, 2)?), 2 << 4 | 1);
        assert_eq!(u32::from(Op::new(Kind::Deletion, 3)?), 3 << 4 | 2);
        assert_eq!(u32::from(Op::new(Kind::Skip, 4)?), 4 << 4 | 3);
        assert_eq!(u32::from(Op::new(Kind::SoftClip, 5)?), 5 << 4 | 4);
        assert_eq!(u32::from(Op::new(Kind::HardClip, 6)?), 6 << 4 | 5);
        assert_eq!(u32::from(Op::new(Kind::Pad, 7)?), 7 << 4 | 6);
        assert_eq!(u32::from(Op::new(Kind::SeqMatch, 8)?), 8 << 4 | 7);
        assert_eq!(u32::from(Op::new(Kind::SeqMismatch, 9)?), 9 << 4 | 8);
        Ok(())
    }
}