This is a test version of Biostars. For the public version, visit https://www.biostars.org.
BWA mem or samtools have any parameter to filter the mismatch reads?

I'm looking for the parameter of BWA mem or samtools to filter out the mapped reads contain N% mismatched bases in or after reads mapping to the reference genome.

However, I can't figure out how to combain the scoring parameters of BWA mem, nither find the filtering parameters of samtools view to deal with it.

Dose anyone could give some advises?

Thanks a lot!

mapping samtools reads bwa

1 answer

use the edit distance : NM ?

samtools view -e '[NM]>5'

This is the first time that I heard about this feature. Extremely useful.

Looks like it was introduced in samtools version 1.12 (March 2021)

https://github.com/samtools/samtools/releases/

samtools view now works with the filtering expressions introduced by HTSlib. The filtering expression is passed to the program using the specific option -e or the global long option --input-fmt-option. E.g.

samtools view -e 'qname =~ "#49$" && mrefid != refid && refid != -1 &&  mrefid != -1' align.bam

for those with older versions of samtools:

mamba install samtools==1.12

Thanks!

But It seemed like the [NM] just counts the number of mismatched bases. How could I calculate the percentage of mismatched bases of each reads considering their length.

I simply tried samtools view -e '[NM]/qlen < 0.05' but I got errors [E::sam_passes_filter] Couldn't process filter expression

How to correct the filtering expression?

Log in to answer this question.