Skip to content

Writing out to a new bam file #42

@claudiadast

Description

@claudiadast

I'm trying to use bamnostic to basically read in a BAM, filter out reads where any of the bases have a quality < 10, and then write out the remaining lines to a new BAM file. However, I'm a bit confused as to how you write things out using bamnostic. Here's what I have so far:

import bamnostic as bs
input_bam = bs.AlignmentFile("inputbam.bam", 'rb')
output_bam = bs.AlignmentFile("outputbam.bam", 'wb')

for read in bam:
        qual = read.query_qualities
        if all(q >= 10 for q in qual):
                output_bam.write(read)

But this fails. What's the correct way to write out the lines to a new bam file?

I even tried with the to_bam() method, but it fails as well due to TypeError: to_bam() takes exactly 1 argument (2 given):

out = bs.bam.BamWriter("filteredBam.bam", 'wb')
for read in bam:
        qual = read.query_qualities
        if all(q >= 10 for q in qual):
                   read.to_bam(out)

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