This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Remove supplementary alignments from a bam file

Hi!

Do you know how can I filter out supplementary alignments from a bam file? I was reviewing http://broadinstitute.github.io/picard/explain-flags.html and I am aware that the flag for this kind of alignments is "2048". However, depending on another features (ex: paired read, second in pair, etc), the flag can vary.

So, I am not sure about how can I filter out this kind of alignments.

alignment

2 answers

samtools view -F 2048 -bo filtered.bam original.bam

You don't have to care about what other flags are set, the -F option will filter any entry with bit 2048.

Hi all, I am very new to all these tools and analysis. I have a bam file and I wanted to eliminate the supplementary alignments but when I use samtools view -b -F 2145 tow.sort.bam > new.bam

The new.bam file is empty. However, if what I do is to select them in a new file:

samtools view -b -f 2145 tow.sort.bam > filtered.bam

The filtered.bam file contains the supplementary aligments I saw in flagstat.

Anyone can tell me why the first command is not working as I expect?

Thanks!

-F 2145 will remove all paired-end reads. You want -F 2048.

the SAM flag is a bit array https://en.wikipedia.org/wiki/Bit_array . Filtering with samtools view -F or samtools view -f uses Bit Wise operations https://en.wikipedia.org/wiki/Bitwise_operation so the other bits don't have any consequence on the filtering.

Beat you by 3 minutes :)

Log in to answer this question.