Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrating from 0.49 to 0.65: generating invalid BAM files #244

Closed
jamestwebber opened this issue Mar 12, 2024 · 2 comments
Closed

migrating from 0.49 to 0.65: generating invalid BAM files #244

jamestwebber opened this issue Mar 12, 2024 · 2 comments
Assignees
Labels
bam bug Something isn't working

Comments

@jamestwebber
Copy link
Contributor

Every once in a while I touch this code and find that noodles has gone up 20 versions 😁 This time, I'm going from noodles 0.49 / noodles-bam 0.43 to 0.65 / 0.56 respectively. I was able to get my code running by looking through the changelog--I just needed to not read/write reference sequences, and change how I parsed the tag I need. Now my code runs, but the output appears to be invalid for some reason.

Trying samtools view on the result gets me [E::sam_format1_append] Corrupted aux data for read [read name]. I'm not sure how to diagnose what's wrong here. If I try to read the output with pysam it gives me KeyError: "unknown type '71'" in the tags, so I'm guessing the tags are being mangled somehow.

I'm just filtering a BAM file based on a tag (an array of two uint16) and separating the reads into different output BAMs. I use async readers and writers to speed things up.

Code example
    info!("Reading from {}", input_bam.display());
    let (mut reader, header) = make_reader(&input_bam).await?;

    ...

    let mut writers = make_writers(&header, output_bams).await?;

    debug!("Reading records from BAM");
    let mut records = reader.records();

    while let Some(record) = records.try_next().await? {
        if let Some(Ok(Value::Array(Array::UInt16(bc_val)))) = record.data().get(&BC) {
            let bc_val: Vec<_> = bc_val.iter().filter_map(|s| s.ok()).collect();
            if bc_val.len() != 2 {
                debug!("bc array with length {}, that's weird!", bc_val.len());
            } else {
                if let Some(p) = barcode_map.get(&bc_val) {
                    if let Some(writer) = writers.get_mut(p) {
                        writer.write_record(&header, &record).await?;
                    }
                }
            }
        }
    }

    for writer in writers.values_mut() {
        writer.shutdown().await?;
    }

That code is slightly modified from before to extract the tag correctly, but not much has changed. What did I miss here?

@zaeleus zaeleus added bam bug Something isn't working labels Mar 12, 2024
@zaeleus zaeleus self-assigned this Mar 12, 2024
@zaeleus
Copy link
Owner

zaeleus commented Mar 12, 2024

It's a bug, sorry! There was a typo in how value counts were calculated. It affects noodles 0.61.0-0.65.0/noodles-bam 0.53.0-0.56.0 when using BAM format records (bam::Record) that have data array fields.

This is now fixed in noodles 0.66.0/noodles-bam 0.57.0. Thanks for reporting!

@jamestwebber
Copy link
Contributor Author

Thanks for the ultra-fast bugfix as usual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bam bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants