<p>Given an indexed BAM file, is the following command guaranteed to give the full and correct listing of (UCSC) chromosome names for reads in the BAM file?</p>

<pre>
$ samtools view -H foo.bam | cut -f2 | grep &#39;^SN:chr&#39; | sed s&#39;/SN://&#39;</pre>

<p>I&#39;m trying to think of any gotchas or custom implementations. Are there other tags that could be used for chromosome names in BAM file headers, or other issues I&#39;m not thinking of?</p>

If the file is already indexed, you can use the `idxstats` tool.

    samtools idxstats NA18152.bam | head -n 3
    chr1    247249719    123894    0
    chr2    242951149    97215    0
    chr3    199501827    81334    0

Then:

    samtools idxstats NA18152.bam | cut -f 1 | head -3
    chr1
    chr2
    chr3