This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to extract alignments with 0 mismatch from sam or bam file by using samtools?

I have a single-ended sam files generated from bwa with default settings for NIPT calculation. How can I extract a sam or bam files that contains only alignments that have 0 mismatch by using samtools?

bam samtools sam

2 answers

samtools view -e '[NM]==0' -O BAM -o out.bam   in.bam

requires a recent version of samtools.

Oops those were from my notes dates 5 years back my bad. edited my answer.

You could use this samtools view -h bwa.bam | grep "NM:i:0" | samtools view -O BAM -o bwa.filtered.bam

Thanks, can I replace bwa.bam with bwa.sam? Or I need to convert it to bam first?

this is wrong, the last command is trying to read a file bwa.filtered.bam. You want .. | samtools view -O BAM -o bwa.filtered.bam

Hi, by somehow it shows error: no header when type together but worked when I type them separately

Log in to answer this question.