Closed zaeleus closed 7 months ago
This is now available in noodles 0.68.0 / noodles-sam 0.56.0, e.g.,
// cargo add noodles@0.68.0 --features sam
use std::io;
use noodles::sam::{
alignment::{
record::{
cigar::{op::Kind, Op},
data::field::Tag,
},
record_buf::{data::field::Value, Cigar, Data},
},
io::writer::record::write_cigar,
};
fn main() -> io::Result<()> {
let mut buf = Vec::new();
let cigar: Cigar = [Op::new(Kind::Match, 4)].into_iter().collect();
write_cigar(&mut buf, &cigar)?;
let mut data = Data::default();
data.insert(Tag::MATE_CIGAR, Value::String(buf.into()));
eprintln!("{data:?}");
// => Data([(Tag("MC"), String("4M"))])
Ok(())
}
That was quick. Amazing! Thanks!
Originally posted by @theJasonFan in https://github.com/zaeleus/noodles/issues/233#issuecomment-2027767346
The request here is to provide a method to serialize alignment record CIGAR operations to SAM CIGAR strings. This would enable using the serialized form in, e.g., record data string values.
@theJasonFan, the alignment record CIGAR buffer (
sam::alignment::record_buf::Cigar
) does not use a byte buffer for its internal representation, so your proposed methods won't quite work here. I think the better solution is to increase the visibility of the SAM record CIGAR field serializer, which will allow any implementation ofalignment::record::Cigar
to be serialized to a SAM CIGAR string.