This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Samtools: remove reads whose mate does not map as expected

Is there a way to remove reads whose mate does not map as expected in a Bam file directly? I know we can run command line "samtools view -h -f 2 in.bam out.sam" to exclude the reads in the output sam file.

However, I wonder if I could exclude those reads directly from the in.bam file.

next-gen

Hi, Take some time to go through the samtools manual. It will definitely help you. Samtools works for both sam and bam format and can generate output files in both sam and bam format. Also, you can't just change the original file with samtools. You will have to create a new sam or bam file using the original sam/bam file. The command mentioned above can be used to generate a new "bam" output if -b parameter is used. Check http://samtools.sourceforge.net/samtools.shtml.

Thanks. I'm not sure whether "-b" works but will definitely try that.

I tried to read samtools manual carefully but failed to understand all of the details. I would like to see more examples.

It will work. But there will be a new bam file instead of modifying your original bam file.

1 answer

In other words, you want to know if it's possible to edit a BAM file in-place with samtools. The answer is no (well, samtools will let you by just specifying the same file for input and output). While it's theoretically possible, it'd be a good way to shoot yourself in the foot if not done correctly (such as if the writer file pointer caught up to and overwrote where the reader file pointer was pointed) so it's not something anyone will recommend you frequently do.

Log in to answer this question.