Skip to content

Commit

Permalink
sam/writer/record/cigar: Extract kind writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jan 8, 2024
1 parent ab5b88d commit dba2039
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions noodles-sam/src/writer/record/cigar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ where
} else {
for result in cigar.iter() {
let op = result?;

num::write_usize(writer, op.len())?;

let c = match op.kind() {
Kind::Match => b'M',
Kind::Insertion => b'I',
Kind::Deletion => b'D',
Kind::Skip => b'N',
Kind::SoftClip => b'S',
Kind::HardClip => b'H',
Kind::Pad => b'P',
Kind::SequenceMatch => b'=',
Kind::SequenceMismatch => b'X',
};

writer.write_all(&[c])?;
write_kind(writer, op.kind())?;
}
}

Ok(())
}

fn write_kind<W>(writer: &mut W, kind: Kind) -> io::Result<()>
where
W: Write,
{
let c = match kind {
Kind::Match => b'M',
Kind::Insertion => b'I',
Kind::Deletion => b'D',
Kind::Skip => b'N',
Kind::SoftClip => b'S',
Kind::HardClip => b'H',
Kind::Pad => b'P',
Kind::SequenceMatch => b'=',
Kind::SequenceMismatch => b'X',
};

writer.write_all(&[c])
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -59,4 +64,28 @@ mod tests {

Ok(())
}

#[test]
fn test_write_kind() -> io::Result<()> {
fn t(buf: &mut Vec<u8>, kind: Kind, expected: &[u8]) -> io::Result<()> {
buf.clear();
write_kind(buf, kind)?;
assert_eq!(buf, expected);
Ok(())
}

let mut buf = Vec::new();

t(&mut buf, Kind::Match, b"M")?;
t(&mut buf, Kind::Insertion, b"I")?;
t(&mut buf, Kind::Deletion, b"D")?;
t(&mut buf, Kind::Skip, b"N")?;
t(&mut buf, Kind::SoftClip, b"S")?;
t(&mut buf, Kind::HardClip, b"H")?;
t(&mut buf, Kind::Pad, b"P")?;
t(&mut buf, Kind::SequenceMatch, b"=")?;
t(&mut buf, Kind::SequenceMismatch, b"X")?;

Ok(())
}
}

0 comments on commit dba2039

Please sign in to comment.