zaeleus / noodles

Bioinformatics I/O libraries in Rust
MIT License
477 stars 53 forks source link

How to write custom bam records #274

Closed SteampunkIslande closed 1 month ago

SteampunkIslande commented 1 month ago

Hello !

First, thank you to all the contributors for this awesome rust crate :)

Sadly, I'm struggling with some basic BAM file editing... Below is a minimal example:

fn main()
{
    let mut reader = bam::io::reader::Builder::default().build_from_path(input_path)?;
    let mut writer = bam::io::writer::Builder::default().build_from_path(output_path)?;
    let header = reader.read_header()?;

    writer.write_header(&header)?;

    for result in reader.record_bufs(&header) {
        let mut record = result?;
        let tag = record.data().get(tag_name).unwrap();
        let mbc = match tag {
            noodles::sam::alignment::record_buf::data::field::Value::String(val) => {
                format!("{}", val)
            }
            _ = { String::from("") }
        };
        *record.name_mut() = Some(noodles::sam::alignment::record_buf::Name::from(
            format!("{:?}{}", record.name(), mbc).as_bytes(),
        ));
        writer.write_record(&header, record);
}

Here, record is of type RecordBuf, since according to what I found this is the only way to mutate BAM records (see this closed issue you answered a while ago https://github.com/zaeleus/noodles/issues/258)

Now, how can I write these modified records into a bam file? I found no way to turn RecordBuf into Record in the documentation.

Thank you for your time!

SteampunkIslande commented 1 month ago

Anyway, found it

https://github.com/zaeleus/noodles/discussions/240