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
//! SAM record data field value for base modifications.

pub mod group;
mod parser;

pub use self::group::Group;

use crate::alignment::record_buf::Sequence;

/// Base modifications.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BaseModifications(Vec<Group>);

impl BaseModifications {
    /// Parses base modifications from a string.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_sam::{
    ///     alignment::record_buf::Sequence,
    ///     record::data::field::value::{
    ///         base_modifications::{
    ///             group::{modification, Strand, UnmodifiedBase},
    ///             Group,
    ///         },
    ///         BaseModifications,
    ///     },
    /// };
    ///
    /// let is_reverse_complemented = false;
    /// let sequence = Sequence::from(b"CACCCGATGACCGGCT");
    /// let base_modifications = BaseModifications::parse(
    ///     "C+m,1,3,0;",
    ///     is_reverse_complemented,
    ///     &sequence,
    /// )?;
    ///
    /// assert_eq!(base_modifications, BaseModifications::from(vec![
    ///     Group::new(
    ///         UnmodifiedBase::C,
    ///         Strand::Forward,
    ///         vec![modification::FIVE_METHYLCYTOSINE],
    ///         None,
    ///         vec![2, 11, 14],
    ///     )
    /// ]));
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn parse(
        s: &str,
        is_reverse_complemented: bool,
        sequence: &Sequence,
    ) -> Result<Self, parser::ParseError> {
        parser::parse(s, is_reverse_complemented, sequence)
    }
}

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

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

impl From<Vec<Group>> for BaseModifications {
    fn from(groups: Vec<Group>) -> Self {
        Self(groups)
    }
}

impl From<BaseModifications> for Vec<Group> {
    fn from(base_modifications: BaseModifications) -> Self {
        base_modifications.0
    }
}