This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filter out reads with hidden tags in bam files

Hello,

I saw this post filter reads in BAM having a tag / but I couldn't figure out how to filter out the reads having hidden tags (SA) - like the screen shot below. Is there an easy way of removing only those reads?

I tried samtools view -e/-d but it didn't work for me. Any advice would be appreciated. enter image description here

bam samtools

2 answers

samtools view --expr '![SA]' -O BAM  -o SA.removed.bam  in.bam

You can combine with this --unmap too which changes it to marking the unselected reads as unmapped instead of removing them. This an be a useful way of retaining all data in the BAM so you can still use it for going back to the original FASTQ if required (potentially saving on disk space for a second copy).

Thanks! I will try it too.

samtools view -d "SA" in.bam -o SA.only.bam

samtools view SA.only.bam | cut -f 1 | sort | uniq > SA.id.list

samtools view -N SA.id.list -U SA.removed.bam -o /dev/null in.bam

found one way to deal with.

this is wrong. If one read in a pair carries the tag but not the other, you'll remove both reads.

Oh I forgot to say mine are single end reads from Ion torrent sequencers.

Log in to answer this question.