Sort BAM/SAM/CRAM files by coordinate for downstream analysis.
samtools sort
samtools sort is a command-line utility used to
sort alignment files by genomic coordinates or read names.
Sorting organizes reads in a defined order, making alignment
files compatible with downstream analyses such as indexing,
variant calling, coverage calculation, and genome browser
visualization.
.sam) – text-based alignment format.
.bam) – binary alignment format.
.cram) – compressed alignment format.
.sorted.bam)
.sorted.cram)
The output file is typically coordinate-sorted and ready
for indexing using samtools index.
# Sort a BAM file
samtools sort input.bam -o sorted.bam
# Sort a SAM file
samtools sort input.sam -o sorted.bam
# Sort by read name
samtools sort -n input.bam -o sorted_name.bam
# Use multiple threads
samtools sort -@ 8 input.bam -o sorted.bam
Input:
sample.bam
Output:
sample.sorted.bam
The sorted BAM file contains the same alignments as the original file, but organized by genomic coordinates, enabling efficient access and processing.
Official Samtools Documentation: Samtools Sort Manual
If you use samtools sort 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