I don't agree that it is necessary to have your chromosome names in any particular order, but if you absolutely must do this the only way I can think of it is convert to SAM format (not BAM), replace the header with a reordered one, convert back to BAM again and re-sort to get the alignments in the same order as the header. Some of these steps can be amalgamated. You cannot replace the header while in BAM format as internally BAM stored chromosome by the Nth SQ number in the header, thus it would simply migrate alignments to the wrong chromosomes.
That's all very messy, and IMO pointless, but something like this (untested) ought to work:
(cat new_header; samtools view in.bam) | samtools sort -o out.bam -
I think you should go for variant calling and then sort the resulted vcf file as per your liking..!!
Why would you do that? Which larger problem are you trying to solve by changing the order of chromosomes in your bam file?
I need to do variant calling with GATK and GATK may give incorrect results if the order of my bam file is chr1 ... chr22 chrX chrY chrM
No it wouldn't. What makes you think that?
cause I found that someone else had a bam file with the order of chrM, chr1, chr2, ... ,chrX, chrY and he mentioned it's very important. I will check it again and find the original information about the order of bam files.
I found it. These are defined by historical karotyping of largest to smallest chromosomes, followed by the X, Y, and MT for the b3x references; the order is thus 1, 2, 3, ..., 10, 11, 12, ... 20, 21, 22, X, Y, MT. The hg1x references differ in that the chromosome names are prefixed with "chr" and chrM appears first instead of last hg