This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filter Bam : Only Sens Alignment

Hi,

How can I filter a bam file to only have reads that are aligning on the sens strand of my reference. I've singe-end reads and I have aligned my reads with bowtie 0.12.7.

Thanks a lot,

EDIT > I found it myself :

sens reads : awk '$2=="0" {print $0}' input.sam > output.sam

anti-sens reads : awk '$2=="16" {print $0}' input.sam > output.sam

N.

bam filter

2 answers

Your solution won't work if , for example, the flag " read fails platform/vendor quality checks" is set (see http://picard.sourceforge.net/explain-flags.html )

You'd better use samtools view with (filtering)

samtools view -F 16 your.bam

or (required)

samtools view -f 16 your.bam

While it's correct that a bitwise flag of 16 (second column in the samfile) would mean reverse strand mapped, it is not the only possible bitwise flag that would indicate reverse strand mapped (5th bit set). For example if your read is mapped on the anti-sense strand and it is also mate paired, then you could have a flag of 17 or 49 depending on what strand your mate is mapped.

To properly check if your mapping is reverse strand, you need to check whether the 5th bit of the flag is set. You can do this by using the bitwise AND operator.

In python:

if flag & 16:
   return 'negative strand'
else:
   return 'positive strand'

The '&' bitwise AND operator also works in perl.

Or you can just use the samtools -f or -F options.

I wrote an explanation of the bitwise flags in a blog post a while back: http://blog.nextgenetics.net/?e=18

Log in to answer this question.