read-structure 0.3.0

Library for parsing and working with read structure descriptions
Documentation
//! Segment Types
//!
//! Type [`SegmentType`] represents the types of segments that can show
//! up in a read structure ([`crate::read_structure::ReadStructure`]: trait.ReadStructure).

use std::{convert::TryFrom, mem, str::FromStr};

use strum::IntoEnumIterator;
use strum_macros::EnumIter;

use crate::ReadStructureError;

/// The `SegmentType` type. See [the module level documentation](self) for more.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, EnumIter, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)]
pub enum SegmentType {
    /// Template: the bases in the segment are reads of template (e.g. genomic dna, rna, etc.)
    Template = b'T',
    /// Sample Barcode: the bases in the segment are an index sequence used to identify the sample being sequenced
    SampleBarcode = b'B',
    /// Molecular Barcode: the bases in the segment are an index sequence used to identify the unique source molecule being sequence (i.e. a UMI)
    MolecularBarcode = b'M',
    /// Skip: the bases in the segment should be skipped or ignored, for example if they are monotemplate sequence generated by the library preparation
    Skip = b'S',
    /// Cellular Barcode: the bases in the segment are an index sequence used to identify the unique cell being sequenced
    CellularBarcode = b'C',
}

impl SegmentType {
    /// Returns the character representation of this segment type.
    pub fn value(&self) -> char {
        let value = *self as u8;
        value as char
    }
}

impl TryFrom<char> for SegmentType {
    type Error = ReadStructureError;

    /// Returns the segment type given the character representation.
    ///
    /// # Errors
    ///
    /// - If `SegmentType` not valid
    fn try_from(value: char) -> Result<Self, Self::Error> {
        match value {
            'T' => Ok(SegmentType::Template),
            'B' => Ok(SegmentType::SampleBarcode),
            'M' => Ok(SegmentType::MolecularBarcode),
            'S' => Ok(SegmentType::Skip),
            'C' => Ok(SegmentType::CellularBarcode),
            _ => Err(ReadStructureError::ReadSegmentTypeInvalid(value)),
        }
    }
}

impl TryFrom<u8> for SegmentType {
    type Error = ReadStructureError;

    /// Returns the segment type given the byte representation.
    ///
    /// # Errors
    ///
    /// - If `SegmentType` not valid
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(value as char)
    }
}

impl FromStr for SegmentType {
    type Err = ReadStructureError;

    /// Returns the segment type given the string representation.
    ///
    /// # Errors
    ///
    /// - If `SegmentType` not valid
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.len() == 1 {
            Self::try_from(value.chars().next().unwrap())
        } else {
            Err(ReadStructureError::ReadSegmentTypeStringInvalid(value.to_owned()))
        }
    }
}

#[cfg(test)]
mod test {
    use std::convert::TryFrom;
    use std::str::FromStr;

    use crate::{ReadStructureError, segment_type::SegmentType};
    use strum::IntoEnumIterator;

    #[test]
    fn test_segment_type_round_trip() -> Result<(), ReadStructureError> {
        assert_eq!(SegmentType::iter().len(), 5);
        for tpe in SegmentType::iter() {
            assert_eq!(SegmentType::try_from(tpe.value())?, tpe);
        }
        Ok(())
    }

    #[test]
    fn test_invalid_segment_type() {
        assert!(SegmentType::try_from(b'G').is_err());
    }

    #[test]
    fn test_segment_type_from_str() -> Result<(), ReadStructureError> {
        let segment_types_char: [char; 5] = ['T', 'B', 'M', 'S', 'C'];
        let segment_types_str: [&str; 5] = ["T", "B", "M", "S", "C"];
        let mut iter = segment_types_str.iter().zip(segment_types_char.iter());
        for (s, c) in iter {
            assert_eq!(SegmentType::from_str(s)?, SegmentType::try_from(*c)?);
        }
        Ok(())
    }

    #[test]
    fn test_invalid_segment_type_string() {
        assert!(SegmentType::from_str("").is_err());
        assert!(SegmentType::from_str("GG").is_err());
        assert!(SegmentType::from_str("TG").is_err());
        assert!(!SegmentType::from_str("T").is_err());
    }
}