get the dictionary in the sam header and test one chromosome
$ samtools view -H file.bam |\
grep -E 'SN\:(chr)?22' |\
awk '{if(index($0,"LN:51304566")!=0) printf("hg19\n"); else if(index($0,"LN:49691432")!=0) printf("hg18\n"); else printf("??\n");}'
hg19
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.