I would like to return reads with a flag of 0x02 similar to this
$ samtools view -f 0x02 in.bam 1:100-200
without using system calls in pysam (which are slower to my knowledge)
#!/usr/bin/python
import pysam
pos = "1:100-200"
# open bam
bam = pysam.AlignmentFile(bam_fh,"rb")
# I would like to do something like this, syntax is not correct I imagine
reads = bam.fetch(region=pos,until_eof=True)
good_reads = reads.flag("0x02")
print len(good_reads)
or is it impossible to subset reads using a flag in pysam without iterating? In that case I'll default to the csamtools commands.
Could you use named pipes instead?
Filtering Stream Bam Output Idealy From Pysam/Python
Basically what I'm thinking is that you could create the named pipe programmatically (using Python's subprocess library, for example) but outside the context of pysam. Then you can open that named pipe using the pysam API, just like any other BAM file.
I ended up using the pipes
Even samtools will do the subsetting by iterating over the alignments, there's no other way to do it.