Samtools: Sort

Sort BAM/SAM/CRAM files by coordinate for downstream analysis.

Accepted formats: .sam, .bam, .cram

samtools sort

Function:

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.

Input Format:

  • SAM File (.sam) – text-based alignment format.
  • BAM File (.bam) – binary alignment format.
  • CRAM File (.cram) – compressed alignment format.

Output Format:

  • Sorted BAM File (.sorted.bam)
  • Sorted CRAM File (.sorted.cram)

The output file is typically coordinate-sorted and ready for indexing using samtools index.

Example Usage:

# 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

Applications:

  • Preparation of BAM files for indexing.
  • Required preprocessing step for variant calling workflows.
  • Visualization in genome browsers such as IGV and JBrowse.
  • Coverage and depth analysis.
  • RNA-Seq, WGS, WES, ChIP-Seq, and metagenomics pipelines.
  • Improved performance of downstream bioinformatics tools.

Output Example:

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.

Important Notes:

  • Sorting is required before indexing BAM/CRAM files.
  • Most variant callers require coordinate-sorted input.
  • Sorting large files may require significant temporary disk space.
  • Multi-threading can significantly improve performance.

Suggested Reading:

Official Samtools Documentation: Samtools Sort Manual

Citation:

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