noodles_sam/record/data/field/value/
base_modifications.rs

1//! SAM record data field value for base modifications.
2
3pub mod group;
4mod parser;
5
6pub use self::group::Group;
7
8use crate::alignment::record_buf::Sequence;
9
10/// Base modifications.
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct BaseModifications(Vec<Group>);
13
14impl BaseModifications {
15    /// Parses base modifications from a string.
16    ///
17    /// # Examples
18    ///
19    /// ```
20    /// use noodles_sam::{
21    ///     alignment::record_buf::Sequence,
22    ///     record::data::field::value::{
23    ///         base_modifications::{
24    ///             group::{modification, Strand, UnmodifiedBase},
25    ///             Group,
26    ///         },
27    ///         BaseModifications,
28    ///     },
29    /// };
30    ///
31    /// let is_reverse_complemented = false;
32    /// let sequence = Sequence::from(b"CACCCGATGACCGGCT");
33    /// let base_modifications = BaseModifications::parse(
34    ///     "C+m,1,3,0;",
35    ///     is_reverse_complemented,
36    ///     &sequence,
37    /// )?;
38    ///
39    /// assert_eq!(base_modifications, BaseModifications::from(vec![
40    ///     Group::new(
41    ///         UnmodifiedBase::C,
42    ///         Strand::Forward,
43    ///         vec![modification::FIVE_METHYLCYTOSINE],
44    ///         None,
45    ///         vec![2, 11, 14],
46    ///     )
47    /// ]));
48    /// # Ok::<_, Box<dyn std::error::Error>>(())
49    /// ```
50    pub fn parse(
51        s: &str,
52        is_reverse_complemented: bool,
53        sequence: &Sequence,
54    ) -> Result<Self, parser::ParseError> {
55        parser::parse(s, is_reverse_complemented, sequence)
56    }
57}
58
59impl AsRef<[Group]> for BaseModifications {
60    fn as_ref(&self) -> &[Group] {
61        &self.0
62    }
63}
64
65impl AsMut<Vec<Group>> for BaseModifications {
66    fn as_mut(&mut self) -> &mut Vec<Group> {
67        &mut self.0
68    }
69}
70
71impl From<Vec<Group>> for BaseModifications {
72    fn from(groups: Vec<Group>) -> Self {
73        Self(groups)
74    }
75}
76
77impl From<BaseModifications> for Vec<Group> {
78    fn from(base_modifications: BaseModifications) -> Self {
79        base_modifications.0
80    }
81}