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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! BAM record data and fields.

mod bounds;
pub mod field;
mod fields;

pub(crate) use self::bounds::Bounds;
pub use self::{field::Field, fields::Fields};

use std::{error, fmt, io};

use noodles_sam::{self as sam, record::data::field::Tag};

/// BAM record data.
///
/// This is also called optional fields.
#[derive(Clone, Default, Eq, PartialEq)]
pub struct Data {
    data: Vec<u8>,
    bounds: Bounds,
}

impl Data {
    /// Returns the number of data fields.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bam::record::Data;
    /// let data = Data::default();
    /// assert_eq!(data.len(), 0);
    /// ```
    pub fn len(&self) -> usize {
        self.bounds.len()
    }

    /// Returns whether there are any data fields.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bam::record::Data;
    /// let data = Data::default();
    /// assert!(data.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Removes all fields from the data map.
    ///
    /// This does not affect the capacity of the map.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::Data;
    ///
    /// let mut data = Data::try_from(vec![
    ///     b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
    ///     b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
    /// ])?;
    ///
    /// assert_eq!(data.len(), 2);
    ///
    /// data.clear();
    ///
    /// assert!(data.is_empty());
    /// # Ok::<_, io::Error>(())
    /// ```
    pub fn clear(&mut self) {
        self.data.clear();
        self.bounds.clear();
    }

    /// Returns a field by the given tag.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::{data::{field::Value, Field}, Data};
    /// use noodles_sam::record::data::field::Tag;
    ///
    /// let data = Data::try_from(vec![
    ///     b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
    ///     b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
    /// ])?;
    ///
    /// let rg = data.get(Tag::ReadGroup).transpose()?;
    /// assert_eq!(rg, Some(Field::new(Tag::ReadGroup, Value::String(String::from("rg0")))));
    ///
    /// assert!(data.get(Tag::AlignmentScore).is_none());
    /// # Ok::<_, io::Error>(())
    /// ```
    pub fn get(&self, tag: Tag) -> Option<io::Result<Field>> {
        for (i, result) in self.keys().enumerate() {
            match result {
                Ok(t) => {
                    if t == tag {
                        return self.get_index(i);
                    }
                }
                Err(e) => return Some(Err(e)),
            }
        }

        None
    }

    /// Returns a field by an index.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::{data::{field::Value, Field}, Data};
    /// use noodles_sam::record::data::field::Tag;
    ///
    /// let data = Data::try_from(vec![
    ///     b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
    ///     b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
    /// ])?;
    ///
    /// let rg = data.get_index(1).transpose()?;
    /// assert_eq!(rg, Some(Field::new(Tag::ReadGroup, Value::String(String::from("rg0")))));
    ///
    /// assert!(data.get_index(2).is_none());
    /// # Ok::<_, io::Error>(())
    /// ```
    pub fn get_index(&self, i: usize) -> Option<io::Result<Field>> {
        use crate::reader::record::data::read_field;

        self.bounds.get(i).map(|range| {
            let mut reader = &self.data[range];
            read_field(&mut reader).transpose().unwrap()
        })
    }

    /// Inserts a field into the data map.
    ///
    /// This uses the field tag as the key and field as the value.
    ///
    /// If the tag already exists in the map, the existing field is replaced by the new one, and
    /// the existing field is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::{data::{field::Value, Field}, Data};
    /// use noodles_sam::record::data::field::Tag;
    ///
    /// let mut data = Data::default();
    ///
    /// let nh = Field::new(Tag::AlignmentHitCount, Value::UInt8(1));
    /// data.insert(nh.clone()).transpose()?;
    ///
    /// assert_eq!(data.len(), 1);
    /// assert_eq!(data.get_index(0).transpose()?, Some(nh));
    /// # Ok::<_, io::Error>(())
    /// ```
    pub fn insert(&mut self, field: Field) -> Option<io::Result<Field>> {
        for (i, result) in self.values().enumerate() {
            match result {
                Ok(f) => {
                    if f.tag() == field.tag() {
                        match self.splice(i, field) {
                            Ok(_) => return Some(Ok(f)),
                            Err(e) => return Some(Err(e)),
                        }
                    }
                }
                Err(e) => return Some(Err(e)),
            }
        }

        if let Err(e) = self.push(field) {
            Some(Err(e))
        } else {
            None
        }
    }

    fn splice(&mut self, i: usize, field: Field) -> io::Result<()> {
        let range = self.bounds.get(i).expect("index out of bounds");
        let buf = <Vec<u8>>::try_from(field)?;
        self.data.splice(range, buf);

        self.index()?;

        Ok(())
    }

    fn push(&mut self, field: Field) -> io::Result<()> {
        let buf = <Vec<u8>>::try_from(field)?;
        self.data.extend_from_slice(&buf);
        self.bounds.push(self.data.len());
        Ok(())
    }

    /// Returns an iterator over data fields.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::{data::{field::Value, Field}, Data};
    /// use noodles_sam::record::data::field::Tag;
    ///
    /// let data = Data::try_from(vec![
    ///     b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
    ///     b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
    /// ])?;
    ///
    /// let mut fields = data.values();
    ///
    /// assert_eq!(
    ///     fields.next().transpose()?,
    ///     Some(Field::new(Tag::AlignmentHitCount, Value::Int32(1)))
    /// );
    ///
    /// assert_eq!(
    ///     fields.next().transpose()?,
    ///     Some(Field::new(Tag::ReadGroup, Value::String(String::from("rg0"))))
    /// );
    ///
    /// assert!(fields.next().is_none());
    /// # Ok::<(), io::Error>(())
    /// ```
    #[deprecated(since = "0.8.0", note = "Use `Data::values` instead.")]
    pub fn fields(&self) -> Fields<&[u8]> {
        self.values()
    }

    /// Returns an iterator over all tags.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::Data;
    /// use noodles_sam::record::data::field::Tag;
    ///
    /// let data = Data::try_from(vec![
    ///     b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
    ///     b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
    /// ])?;
    ///
    /// let mut keys = data.keys();
    ///
    /// assert_eq!(keys.next().transpose()?, Some(Tag::AlignmentHitCount));
    /// assert_eq!(keys.next().transpose()?, Some(Tag::ReadGroup));
    /// assert!(keys.next().is_none());
    /// # Ok::<(), io::Error>(())
    /// ```
    pub fn keys(&self) -> impl Iterator<Item = io::Result<Tag>> + '_ {
        let mut start = 0;

        self.bounds.as_ref().iter().map(move |&end| {
            let buf = &self.data[start..end];
            let raw_tag = [buf[0], buf[1]];

            start = end;

            Tag::try_from(raw_tag).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
        })
    }

    /// Returns an iterator over all fields.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bam::record::{data::{field::Value, Field}, Data};
    /// use noodles_sam::record::data::field::Tag;
    ///
    /// let data = Data::try_from(vec![
    ///     b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
    ///     b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
    /// ])?;
    ///
    /// let mut values = data.values();
    ///
    /// assert_eq!(
    ///     values.next().transpose()?,
    ///     Some(Field::new(Tag::AlignmentHitCount, Value::Int32(1)))
    /// );
    ///
    /// assert_eq!(
    ///     values.next().transpose()?,
    ///     Some(Field::new(Tag::ReadGroup, Value::String(String::from("rg0"))))
    /// );
    ///
    /// assert!(values.next().is_none());
    /// # Ok::<(), io::Error>(())
    /// ```
    pub fn values(&self) -> Fields<&[u8]> {
        Fields::new(&self.data)
    }

    pub(crate) fn index(&mut self) -> io::Result<()> {
        self.bounds.update(&self.data[..])
    }
}

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

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

impl fmt::Debug for Data {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.values()).finish()
    }
}

impl TryFrom<Vec<u8>> for Data {
    type Error = io::Error;

    fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
        let mut data = Self {
            data,
            bounds: Bounds::default(),
        };

        data.index()?;

        Ok(data)
    }
}

impl TryFrom<Vec<Field>> for Data {
    type Error = io::Error;

    fn try_from(fields: Vec<Field>) -> Result<Self, Self::Error> {
        let mut buf = Vec::new();

        for field in fields {
            let raw_field = <Vec<u8>>::try_from(field)?;
            buf.extend_from_slice(&raw_field);
        }

        Self::try_from(buf)
    }
}

/// An error returned when BAM data fails to convert to SAM data.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TryFromDataError {
    /// A field is invalid.
    InvalidField,
    /// The data is invalid.
    DuplicateTag(Tag),
}

impl error::Error for TryFromDataError {}

impl fmt::Display for TryFromDataError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidField => write!(f, "invalid field"),
            Self::DuplicateTag(tag) => write!(f, "duplicate tag: {}", tag),
        }
    }
}

impl TryFrom<&Data> for sam::record::Data {
    type Error = TryFromDataError;

    fn try_from(data: &Data) -> Result<Self, Self::Error> {
        let mut sam_data = Self::default();

        for result in data.values() {
            let field = result.map_err(|_| TryFromDataError::InvalidField)?;
            let sam_field = sam::record::data::Field::from(field);

            if let Some(f) = sam_data.insert(sam_field) {
                return Err(TryFromDataError::DuplicateTag(f.tag()));
            }
        }

        Ok(sam_data)
    }
}

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

    #[test]
    fn test_insert() -> io::Result<()> {
        use field::Value;

        let mut data = Data::try_from(vec![
            b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
            b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
        ])?;

        // push
        let as_ = Field::new(Tag::AlignmentScore, Value::UInt8(13));
        assert!(data.insert(as_).transpose()?.is_none());
        assert_eq!(
            data.as_ref(),
            [
                b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
                b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
                b'A', b'S', b'C', 0x0d, // AS:C:13
            ]
        );

        // replace
        let nh = Field::new(Tag::AlignmentHitCount, Value::UInt8(2));
        let expected_nh = Field::new(Tag::AlignmentHitCount, Value::Int32(1));
        assert_eq!(data.insert(nh).transpose()?, Some(expected_nh));
        assert_eq!(
            data.as_ref(),
            [
                b'N', b'H', b'C', 0x02, // NH:C:2
                b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
                b'A', b'S', b'C', 0x0d, // AS:C:13
            ]
        );

        Ok(())
    }

    #[test]
    fn test_try_from_vec_field_for_data() -> io::Result<()> {
        use field::Value;

        let fields = vec![
            Field::new(Tag::AlignmentHitCount, Value::UInt8(1)),
            Field::new(Tag::ReadGroup, Value::String(String::from("rg0"))),
        ];

        let data = Data::try_from(fields)?;

        assert_eq!(
            data.as_ref(),
            vec![
                b'N', b'H', b'C', 0x01, // NH:i:1
                b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
            ],
        );

        Ok(())
    }

    #[test]
    fn test_try_from_data_for_sam_record_data() -> Result<(), Box<dyn std::error::Error>> {
        use sam::record::data::field::Value;

        let data = Data::try_from(vec![
            b'N', b'H', b'i', 0x01, 0x00, 0x00, 0x00, // NH:i:1
            b'R', b'G', b'Z', b'r', b'g', b'0', 0x00, // RG:Z:rg0
        ])?;

        let actual = sam::record::Data::try_from(&data)?;
        let expected = sam::record::Data::try_from(vec![
            sam::record::data::Field::new(Tag::AlignmentHitCount, Value::Int(1)),
            sam::record::data::Field::new(Tag::ReadGroup, Value::String(String::from("rg0"))),
        ])?;

        assert_eq!(actual, expected);

        Ok(())
    }
}