Easy BAM Sort with samtools sort



This short tutorial demonstrates how samtools sort is used to sort BAM genomic coordinates. Moreover, how to pipe samtool sort when running bwa alignment, and how to sort by subject name. Enjoy it!

1. Sorting BAM File

Assuming that you already have generated the BAM file that you want to sort the genomic coordinates, thus run:

$ samtools sort {YOUR_BAM}.bam -o {SORTED_BAM}.bam

2. samtools sort pipe: Sorting with BWA

Do you have your BAM file already? If not, you can run bwa and sort the BAM with a pipe at the same time:

$ bwa mem  -t 16 {REFERENCE_GENOME.FASTA} {READS_INPUT} | samtools sort -o {SORTED_BAM}.bam

This should speed up the combo bwa alignment and bam sorting.

3. samtools sort by Name

Sort by subject names rather than by reference coordinates.

$ samtools sort -n {YOUR_BAM}.bam -o {SORTED_BAM}.bam

More Resources

Here are three of my favorite Python Bioinformatics Books in case you want to learn more about it.


In summary, this tutorial shows different flavors of samtool sort. I hope was helpful for you. Let me know if there are different ways to sort that you would like to see.

Related Posts