I am using samtools v1.6 to get some stats on my bam file. Here, I merged my two paired end reads and ran bwa twice, once on merged reads and once on unmerged reads and then merged the two .bam files using samtools merge. I then used both samtools stats and samtools flagstat to get some stats but interestingly, I get different results. Can you help me understand why?
samtools flagstat Sample_sorted.bam
60970 + 0 in total (QC-passed reads + QC-failed reads)
0 + 0 secondary
8465 + 0 supplementary
0 + 0 duplicates
45003 + 0 mapped (73.81% : N/A)
27856 + 0 paired in sequencing
13046 + 0 read1
14810 + 0 read2
0 + 0 properly paired (0.00% : N/A)
22138 + 0 with itself and mate mapped
1051 + 0 singletons (3.77% : N/A)
19562 + 0 with mate mapped to a different chr
4 + 0 with mate mapped to a different chr (mapQ>=5)
samtools stats Sample_sorted.bam |grep ^SN | cut -f 2-
raw total sequences: 52505
filtered sequences: 0
sequences: 52505
is sorted: 1
1st fragments: 37695
last fragments: 14810
reads mapped: 36538
reads mapped and paired: 22138 # paired-end technology bit set + both mates mapped
reads unmapped: 15967
reads properly paired: 0 # proper-pair bit set
reads paired: 27856 # paired-end technology bit set
reads duplicated: 0 # PCR or optical duplicate bit set
reads MQ0: 36523 # mapped and MQ=0
reads QC failed: 0
non-primary alignments: 0
total length: 7949149 # ignores clipping
bases mapped: 6010513 # ignores clipping
bases mapped (cigar): 5909812 # more accurate
bases trimmed: 0
bases duplicated: 0
mismatches: 484745 # from NM fields
error rate: 8.202376e-02 # mismatches / bases mapped (cigar)
average length: 151
maximum length: 293
average quality: 35.5
insert size average: 267.3
insert size standard deviation: 229.1
inward oriented pairs: 1005
outward oriented pairs: 134
pairs with other orientation: 3
pairs on different chromosomes: 9781
I also did a simple count on my bam file and it aligns with samtools flagstat results but not stats results.
samtools view Sample_sorted.bam | wc -l
60970