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
271
bitflags::bitflags! {
    /// Alignment record flags.
    #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
    pub struct Flags: u16 {
        /// Read is segmented (`0x01`).
        const SEGMENTED = 0x01;
        /// Each segment in the read is properly aligned (`0x02`).
        #[deprecated(since = "0.54.0", note = "Use `PROPERLY_SEGMENTED` instead.")]
        const PROPERLY_ALIGNED = 0x02;
        /// Each segment in the read is properly aligned (`0x02`).
        const PROPERLY_SEGMENTED = 0x02;
        /// Read is unmapped (`Ox04`).
        const UNMAPPED = 0x04;
        /// The mate is unmapped (`0x08`).
        const MATE_UNMAPPED = 0x08;
        /// The sequence is reverse complemented (`0x10`).
        const REVERSE_COMPLEMENTED = 0x10;
        /// The sequence of the mate is reverse complemented (`0x20`).
        const MATE_REVERSE_COMPLEMENTED = 0x20;
        /// First segment in the read (`0x40`).
        const FIRST_SEGMENT = 0x40;
        /// Last segment in the read (`0x80`).
        const LAST_SEGMENT = 0x80;
        /// Secondary read (`0x0100`).
        const SECONDARY = 0x0100;
        /// Read failed quality checks (`0x0200`).
        const QC_FAIL = 0x0200;
        /// PCR or optical duplicate read (`0x0400`).
        const DUPLICATE = 0x0400;
        /// Supplementary alignment (`0x0800`).
        const SUPPLEMENTARY = 0x0800;
    }
}

impl Flags {
    /// Returns whether the `SEGMENTED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::SEGMENTED.is_segmented());
    /// assert!(!Flags::UNMAPPED.is_segmented());
    /// ```
    pub fn is_segmented(self) -> bool {
        self.contains(Self::SEGMENTED)
    }

    /// Returns whether the `PROPERLY_ALIGNED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::PROPERLY_ALIGNED.is_properly_aligned());
    /// assert!(!Flags::UNMAPPED.is_properly_aligned());
    /// ```
    #[allow(deprecated)]
    #[deprecated(since = "0.54.0", note = "Use `Flags::is_properly_segmented` instead.")]
    pub fn is_properly_aligned(self) -> bool {
        self.contains(Self::PROPERLY_ALIGNED)
    }

    /// Returns whether the `PROPERLY_SEGMENTED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::PROPERLY_SEGMENTED.is_properly_segmented());
    /// assert!(!Flags::UNMAPPED.is_properly_segmented());
    /// ```
    pub fn is_properly_segmented(self) -> bool {
        self.contains(Self::PROPERLY_SEGMENTED)
    }

    /// Returns whether the `UNMAPPED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::UNMAPPED.is_unmapped());
    /// assert!(!Flags::SEGMENTED.is_unmapped());
    /// ```
    pub fn is_unmapped(self) -> bool {
        self.contains(Self::UNMAPPED)
    }

    /// Returns whether the `MATE_UNMAPPED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::MATE_UNMAPPED.is_mate_unmapped());
    /// assert!(!Flags::UNMAPPED.is_mate_unmapped());
    /// ```
    pub fn is_mate_unmapped(self) -> bool {
        self.contains(Self::MATE_UNMAPPED)
    }

    /// Returns whether the `REVERSE_COMPLEMENTED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::REVERSE_COMPLEMENTED.is_reverse_complemented());
    /// assert!(!Flags::UNMAPPED.is_reverse_complemented());
    /// ```
    pub fn is_reverse_complemented(self) -> bool {
        self.contains(Self::REVERSE_COMPLEMENTED)
    }

    /// Returns whether the `MATE_REVERSE_COMPLEMENTED` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::MATE_REVERSE_COMPLEMENTED.is_mate_reverse_complemented());
    /// assert!(!Flags::UNMAPPED.is_mate_reverse_complemented());
    /// ```
    pub fn is_mate_reverse_complemented(self) -> bool {
        self.contains(Self::MATE_REVERSE_COMPLEMENTED)
    }

    /// Returns whether the `FIRST_SEGMENT` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::FIRST_SEGMENT.is_first_segment());
    /// assert!(!Flags::UNMAPPED.is_first_segment());
    /// ```
    pub fn is_first_segment(self) -> bool {
        self.contains(Self::FIRST_SEGMENT)
    }

    /// Returns whether the `LAST_SEGMENT` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::LAST_SEGMENT.is_last_segment());
    /// assert!(!Flags::UNMAPPED.is_last_segment());
    /// ```
    pub fn is_last_segment(self) -> bool {
        self.contains(Self::LAST_SEGMENT)
    }

    /// Returns whether the `SECONDARY` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::SECONDARY.is_secondary());
    /// assert!(!Flags::UNMAPPED.is_secondary());
    /// ```
    pub fn is_secondary(self) -> bool {
        self.contains(Self::SECONDARY)
    }

    /// Returns whether the `QC_FAIL` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::QC_FAIL.is_qc_fail());
    /// assert!(!Flags::UNMAPPED.is_qc_fail());
    /// ```
    pub fn is_qc_fail(self) -> bool {
        self.contains(Self::QC_FAIL)
    }

    /// Returns whether the `DUPLICATE` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::DUPLICATE.is_duplicate());
    /// assert!(!Flags::UNMAPPED.is_duplicate());
    /// ```
    pub fn is_duplicate(self) -> bool {
        self.contains(Self::DUPLICATE)
    }

    /// Returns whether the `SUPPLEMENTARY` flag is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::alignment::record::Flags;
    /// assert!(Flags::SUPPLEMENTARY.is_supplementary());
    /// assert!(!Flags::UNMAPPED.is_supplementary());
    /// ```
    pub fn is_supplementary(self) -> bool {
        self.contains(Self::SUPPLEMENTARY)
    }
}

impl From<u16> for Flags {
    fn from(value: u16) -> Self {
        Self::from_bits_truncate(value)
    }
}

impl From<Flags> for u16 {
    fn from(flags: Flags) -> Self {
        flags.bits()
    }
}

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

    #[test]
    fn test_default() {
        let flags = Flags::default();

        assert!(flags.is_empty());
        assert!(!flags.is_segmented());
        assert!(!flags.is_properly_aligned());
        assert!(!flags.is_properly_segmented());
        assert!(!flags.is_unmapped());
        assert!(!flags.is_mate_unmapped());
        assert!(!flags.is_reverse_complemented());
        assert!(!flags.is_mate_reverse_complemented());
        assert!(!flags.is_first_segment());
        assert!(!flags.is_last_segment());
        assert!(!flags.is_secondary());
        assert!(!flags.is_qc_fail());
        assert!(!flags.is_duplicate());
        assert!(!flags.is_supplementary());
    }

    #[test]
    fn test_contains() {
        assert!(Flags::SEGMENTED.is_segmented());
        assert!(Flags::PROPERLY_ALIGNED.is_properly_aligned());
        assert!(Flags::PROPERLY_SEGMENTED.is_properly_segmented());
        assert!(Flags::UNMAPPED.is_unmapped());
        assert!(Flags::MATE_UNMAPPED.is_mate_unmapped());
        assert!(Flags::REVERSE_COMPLEMENTED.is_reverse_complemented());
        assert!(Flags::MATE_REVERSE_COMPLEMENTED.is_mate_reverse_complemented());
        assert!(Flags::FIRST_SEGMENT.is_first_segment());
        assert!(Flags::LAST_SEGMENT.is_last_segment());
        assert!(Flags::SECONDARY.is_secondary());
        assert!(Flags::QC_FAIL.is_qc_fail());
        assert!(Flags::DUPLICATE.is_duplicate());
        assert!(Flags::SUPPLEMENTARY.is_supplementary());
    }

    #[test]
    fn test_from_u16_for_flags() {
        assert_eq!(Flags::from(0x04), Flags::UNMAPPED);
    }

    #[test]
    fn test_from_flags_for_u16() {
        assert_eq!(u16::from(Flags::UNMAPPED), 0x04);
    }
}