Skip to content

How to get tags #297

@Liripo

Description

@Liripo

How to get CB and UB tags:
my code:

use noodles::bam::record::data;
use noodles::bam::{self, record};
use anyhow::Result;
use std::fs::File;
use std::io::Write;

pub fn read_bam_tags(bam_file: &str,csv_out: &str) -> Result<()> {
    let mut reader = bam::io::indexed_reader::Builder::default().build_from_path(bam_file)?;

    // Skip the BAM header
    reader.read_header()?;

    let mut csv_writer = File::create(csv_out)?;
    writeln!(csv_writer, "CB,UB")?;


    for result in reader.records() {
        let record = result?;
        if record.flags().is_unmapped() || record.flags().is_secondary() {
            continue;
        }
        let cb = [b'C',b'B'];
        let cb_value = record.data().get(&cb);
        let ub = [b'U',b'B'];
        let ub_value = record.data().get(&ub);
        writeln!("{},{}",cb_value,ub_value);
    }

    Ok(())
}

fn main() -> Result<()> {
    read_bam_tags("Aligned.sortedByCoord.out.bam","test.csv")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions