whittle 1.0.0

Coordinate-consistent trimming of long-read uBAM and tagged FASTQ
Documentation
//! Serialization of parsed modifications back to `MM` text and `ML` bytes.

use super::{ModCode, Mods};
use crate::io::fastq::push_u64;

/// Serializes groups back to an `MM:Z` string and the concatenated `ML` bytes.
pub fn serialize(mods: &Mods) -> (Vec<u8>, Vec<u8>) {
    let mut mm = Vec::new();
    let mut ml = Vec::new();

    for g in &mods.groups {
        // A group with no positions is emitted as `C+m;`: it declares how the
        // unlisted bases of its kind are read, so it carries information.
        mm.push(g.base);
        mm.push(g.strand);
        for code in &g.codes {
            match code {
                ModCode::Char(c) => mm.push(*c),
                ModCode::Chebi(id) => push_u64(&mut mm, u64::from(*id)),
            }
        }
        if let Some(s) = g.status {
            mm.push(s);
        }
        for d in &g.deltas {
            mm.push(b',');
            push_u64(&mut mm, *d as u64);
        }
        mm.push(b';');
        ml.extend_from_slice(&g.ml);
    }

    (mm, ml)
}

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

    #[test]
    fn roundtrip_single_group() {
        let (mm, ml) = serialize(&parse(b"C+m?,5,12,0;", &[200, 10, 128]));
        assert_eq!(mm, b"C+m?,5,12,0;");
        assert_eq!(ml, vec![200, 10, 128]);
    }

    #[test]
    fn roundtrip_multi_group_and_chebi() {
        let input = b"C+mh,1,3;A+16061,2;".as_slice();
        let (mm, ml) = serialize(&parse(input, &[1, 2, 3, 4, 5]));
        assert_eq!(mm, input);
        assert_eq!(ml, vec![1, 2, 3, 4, 5]);
    }

    /// A zero-position group is valid SAM (assessed, none found) and is
    /// re-emitted rather than dropped; it contributes no ML bytes.
    #[test]
    fn emits_empty_groups() {
        let mut mods = parse(b"C+m,0;", &[7]);
        mods.groups.push(crate::mods::MmGroup {
            base: b'A',
            strand: b'+',
            codes: vec![crate::mods::ModCode::Char(b'a')],
            status: None,
            deltas: vec![],
            ml: vec![],
        });
        let (mm, ml) = serialize(&mods);
        assert_eq!(mm, b"C+m,0;A+a;");
        assert_eq!(ml, vec![7]);
    }
}