This is a test version of Biostars. For the public version, visit https://www.biostars.org.
simulating samtools -f option in pysam

I am trying to count BAM reads with flag 83 using pysam version 0.8.3. I tried to use bitwise operation AND:

counter = 0
for read in bam:
   if read.flag & 83 == 83:
      counter += 1

which shows 561 reads while using samtools:

samtools view -f 83 example.bam | wc -l

shows 565 reads. There are 4 extra reads with flag 371 listed by samtools option -f that are not captured by my pysam code.

Is this the right way to simulate option -f in samtools?

Here is the example.bam file used for this question (260 kb)

https://www.dropbox.com/s/c35kmfktgx7c5va/example.bam

Thanks

sequencing

with your example bam I'm getting the correct number: 565 with pysam (also v0.8.3) and still 565 with samtools. The bitwise operation should be correct for 371, because 371 & 83 == 83 is True.

Thanks Martomobo, I found a bug in my code and fixed it.

0 answers

No answers yet.

Log in to answer this question.