bamnostic
bamnostic copied to clipboard
Writing out to a new bam file
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)
Thank you for bringing this up. This has been an ongoing feature addition for BAMnostic. For transparency's sake, I will shine a light on the issue.
Taking a read from one BAM file and writing it to another is the simplest case of a write function. This is because BAMnostic can just use the read's original unmodified bytes and just "transplant" them.
However, many users may want to create (and/or modify) a read. This is where a write function gets complicated. In this event; the read sequence, quality scores, and CIGAR string have to be re-validated for quality control reasons. Then the whole read has to be recompressed into bytes.
This is why BAMnostic only had very minimal features for writing out to BAM files (https://github.com/betteridiot/bamnostic/issues/5) and wasn't developed with that in mind.
However, a number of colleagues have asked for this feature set. Due to this new interest, a version should be pushed out within the next 1-2 weeks that will have this feature added.
I apologize for the inconvenience.
To be clear, what you wrote should have worked, but BAMnostic has some bugs that are making it not. It's not you, it's me.