<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>

Here's how I count the number per sequence of primary alignments with proper read pairs:

https://gist.github.com/slowkow/52a8911e18a873a51dcb