Samtools merge error

I am trying to merge two bam files using Samtools,
After merging the files, it showed that “An error occurred setting the metadata for this dataset” and the info showed as below
“[bam_merge_core] Order of targets in file 1.sam caused coordinate sort to be lost
[E::hts_idx_push] Chromosome blocks not continuous”

Both bam files have been sorted using Samtools sort but the error still occur… Can someone help me with this? Many thanks in advance!!

Hello @CFLiu115

This sounds like a genome mismatch problem. Or, the BAM are not really coordinate sorted.

  • Are the BAM headers identical between the two inputs?
  • If you assigned a “database”, is the BAM data actually a match for that specific built-in genome?

How to check is explained in this FAQ: Mismatched Chromosome identifiers (and how to avoid them)

Plus this FAQ has more help. Tool error? Try Sorting Your Inputs Most is not needed anymore (Galaxy automatically coordinate sorts BAMs), but there are many ways to sort BAMs in other ways, intentionally or not, and this can help you to get back to a coordinate sort.


Hi Jennifer,

Thank you for the email.

Two data sets were aligned using different versions of Botiew2 but to the same build (hg19)

Will this cause this problem? Is there any way to fix it without re do the alignment? Thank you in advance!


1 Like

Hi @CFLiu115

Hard to know.

The quickest way forward, ensuring a correct result, is probably to rerun the alignments.

There is a tool to replace a header in a BAM dataset based on the header of a different BAM dataset, but that could easily introduce data content problems. The tool is intended to replace headers back onto datasets that have lost their original header due to manipulations. That is a different use case than yours.

Better to get each of your BAMs created correctly and matched up near the start of an analysis, with minimal extra manipulations. Hunting for and fixing problems, later on, is non-trivial and not always successful or even possible. Example: You could just end up with “odd results” that are not obvious to detect.