Samtools: Merge

Upload multiple BAM/SAM/CRAM files to merge into a single file.

Accepted formats: .sam, .bam, .cram (Minimum 2 files)

samtools merge

Function:

samtools merge is a command-line utility used to combine multiple SAM, BAM, or CRAM alignment files into a single output file. This is particularly useful when sequencing data originates from multiple lanes, flow cells, or sequencing runs and needs to be consolidated before downstream analysis.

Input Format:

  • BAM Files (.bam)
  • SAM Files (.sam)
  • CRAM Files (.cram)

Input files should generally be coordinate-sorted and generated against the same reference genome to ensure compatibility.

Output Format:

  • Merged BAM File containing reads from all input files.
  • Optional index file (.bai) can be generated after merging.

Example Usage:

# Merge two BAM files
samtools merge merged.bam lane1.bam lane2.bam

# Merge multiple BAM files
samtools merge merged.bam lane1.bam lane2.bam lane3.bam

# Force overwrite existing output
samtools merge -f merged.bam input1.bam input2.bam

# Merge while preserving read groups
samtools merge -r merged.bam input1.bam input2.bam

Applications:

  • Combine sequencing data from multiple lanes.
  • Merge replicate sequencing runs.
  • Prepare unified datasets for variant calling.
  • Consolidate BAM files before downstream QC analysis.
  • Simplify storage and workflow management.
  • Create a single alignment file for visualization tools such as IGV and JBrowse.

Output Example:

Input:
sample_lane1.bam
sample_lane2.bam
sample_lane3.bam

Output:
merged.bam

The resulting merged BAM file contains all alignments from the input files while preserving alignment information and read metadata.

Important Notes:

  • Input files should be sorted consistently.
  • All files should use the same reference genome build.
  • Indexing the merged BAM file is recommended:
samtools index merged.bam

Suggested Reading:

Official Samtools Documentation: Samtools Merge Manual

Citation:

If you use samtools merge in your work, please cite:

Li, H., Handsaker, B., Wysoker, A., Fennell, T., Ruan, J., Homer, N., et al. (2009). The Sequence Alignment/Map (SAM) format and SAMtools. Bioinformatics, 25(16), 2078–2079. DOI: 10.1093/bioinformatics/btp352