<p>Given a <a href='http://samtools.sourceforge.net/SAM1.pdf'>sam</a> file which was produced by using <a href='http://bio-bwa.sourceforge.net/'>bwa</a> to align a fastq of reads to a reference genome, how can I enumerate the number of reads which were assigned to each chromosome of the reference genome?</p>

<p>The solution based on <code> samtools idxstats aln.bam</code> should be used with caution. This is because AFAIK the numbers reported by samtools idxstats (&amp; flagstat) represent the <strong>number of alignments</strong> of reads that are mapped to chromosomes, not the (non-redundant) number of reads, as stated in the documentation. I stumbled across this by observing that the total numbers reported by samtools idxstats (&amp; flagstat) exceeded the total number of reads in my input fastq files. See further discussion on this issue in the comments on this post: http://left.subtree.org/2012/04/13/counting-the-number-of-reads-in-a-bam-file/</p>
