How to you find read statistics for bam files with terminal mismatch? I have a bam file aligned to a reference sequence. I am interested in finding the number of reads for read length 18 to 34, where the last base is mis-matched. I got this bam file from bowtie alignment allowing 3 mismatches (-v 0) option, so I am expecting some of the reads to have terminal bases mis-matched. How can we count the number of sequences of a specific length with terminal mismatch in bam file?

**UPDATE:

I have created a bowtie aligned .sam file with this command: 
bowtie -S -p 18 /media/owner/ref.fa test1.fastq test1.sam

which I then converted to .bam file like this: 
samtools view -bS -o test1.bam test1.sam

From this test1.bam, I was able to get the total number of plus and minus strand of reads aligned to the reference loci using bedtools coverage maping (for positives reads and negative reads) like this: 
bedtools coverage -a my_region.bed -b test1.bam -bed -d -s | gzip &gt; test1_region_pos.tsv.gz

Since I used default bowtie option which allows for three mismatches, I want to know how many reads are aligned with terminal mismatches (i.e,last three, last two and last one base mismatched at 3' region of the aligned reads at their loci position).
Could you provide some examples of lines that should be counted? I'm not sure how mismatches are encoded in the version of Bowtie you're using. As far as I remember, Bowtie doesn't distinguish between = and X in CIGARs (and uses M for both), therefore, mismatches must be detected from the MD tag. 

You can use SAMsift for filtering the alignments. The following filtering criterion should keep only reads with  read length from 18 to 34 that have an MD tag with 0 as the last character, which must be preceded by a non-number (this should signalise a mismatch at the last position).

samsift \
  -i test1.bam \
  -f '18 &lt;= len(SEQ) &lt;= 34 and MD[-1]=="0" and not MD[-2].isdigit()' \
  -m nonstop-remove


The obtained alignments can be counted by adding | samtools view | wc -l.

Explanation:


-f ... – filtering criterion
18 &lt;= len(SEQ) &lt;= 34 – the allowed read lengths
MD[-1]=="0" – the last character of the MD tag is 0
not MD[-2].isdigit() – the penultimate character of the MD tag is not a digit
-m nonstop-remove – remove alignments without any MD tag (probably unaligned, they would cause an error).