Samtools Tutorial


Go to to chat with a life sciences focused ChatGPT.

Samtools is a powerful software suite designed for manipulating high-throughput sequencing data. It provides a collection of utilities that work with alignments in the SAM (Sequence Alignment/Map), BAM (Binary Alignment/Map), and CRAM (Compressed Reference Alignment/Map) formats. These tools are essential for bioinformatics workflows, as they allow researchers to convert between these formats, sort and merge alignment files, index data for fast retrieval, and perform a variety of other tasks.

The beauty of Samtools lies in its ability to work efficiently with large sequencing datasets. It can handle files on remote servers, only downloading the necessary parts of a file when required. This is particularly useful when working with large genomic datasets that are often stored on distributed networks.

Samtools is part of a larger ecosystem that includes BCFtools for variant calling and manipulation of VCF (Variant Call Format) and BCF (Binary Call Format) files, and HTSlib, a C library for reading and writing high-throughput sequencing data. These tools are designed to work together seamlessly, providing a comprehensive toolkit for genomic data analysis.

One of the key features of Samtools is its stream-based processing capability. It can read from standard input (stdin) and write to standard output (stdout), allowing it to be combined with Unix pipes for efficient data processing. This means that multiple commands can be chained together to form complex workflows without the need for intermediate files, saving both time and disk space.

Samtools is widely used in the bioinformatics community and is continually updated to keep pace with the evolving field of genomics. It is open-source software, which means that it is freely available for anyone to use, modify, and distribute.

In the following sections, we'll go through how to install Samtools, get started with some basic commands, and explore some popular code examples to demonstrate its capabilities.


Before we can dive into using Samtools, we need to install it on our system. The installation process is straightforward, but it does require some familiarity with the command line. Samtools can be installed from source code or via package managers such as apt for Debian-based systems or brew for macOS.

To install Samtools from source, you would typically follow these steps:

  1. Download the latest release of Samtools from the official GitHub repository or the Samtools website.
  2. Extract the downloaded archive.
  3. Navigate to the extracted directory in the terminal.
  4. Run the ./configure command to configure the build system for your environment.
  5. Run make to compile the software.
  6. Optionally, run make install to install the software on your system.

For those who prefer using a package manager, the installation can be as simple as running a command like sudo apt-get install samtools on Ubuntu or brew install samtools on macOS.

It's important to note that Samtools depends on the HTSlib library, which is usually included with the Samtools source code. If you're installing from a package manager, the dependencies should be handled automatically.

Quick Start

Once Samtools is installed, you can begin using it immediately. Here's a quick start guide to some of the basic commands:

  • To view the contents of a SAM/BAM/CRAM file, you can use the view command:
    samtools view input.bam
  • To sort an alignment file, use the sort command:
    samtools sort unsorted.bam -o sorted.bam
  • To index a sorted BAM file for fast random access, use the index command:
    samtools index sorted.bam
  • To generate alignment statistics, use the flagstat command:
    samtools flagstat aligned.bam
  • To convert a SAM file to BAM format, you can use the view command with the -b option:
    samtools view -b input.sam > output.bam

These commands represent just the tip of the iceberg when it comes to Samtools' capabilities. As you become more familiar with the tool, you'll discover a wide range of options and subcommands that can be tailored to your specific needs.

Code Examples Of Popular Commands

Let's explore some popular commands in Samtools and provide code examples for each.

1. Converting BAM to CRAM

CRAM is a compressed version of the BAM format that can significantly reduce the size of alignment files. To convert a BAM file to CRAM, use the following command:

samtools view -C -T reference.fasta -o output.cram input.bam

2. Extracting Reads from a Specific Region

If you're interested in reads from a specific region of the genome, you can use the view command with a region specifier:

samtools view input.bam 'chr1:100000-200000' > region.bam

3. Merging Multiple BAM Files

To combine multiple BAM files into a single file, use the merge command:

samtools merge output.bam input1.bam input2.bam input3.bam

4. Removing Duplicates

Duplicate reads can be removed using the markdup command, which marks duplicates that can then be filtered out:

samtools markdup input.bam output.markdup.bam

5. Creating a FASTA Index

The faidx command creates an index for a FASTA file, allowing for fast retrieval of sequence data:

samtools faidx reference.fasta

These examples showcase some of the most common tasks that Samtools can perform. The software is incredibly versatile and can be adapted to a wide range of bioinformatics challenges.

In conclusion, Samtools is an indispensable tool for anyone working with genomic data. Its ability to handle large datasets, combined with its comprehensive set of features, makes it a go-to solution for bioinformaticians around the world. Whether you're sorting, merging, indexing, or converting sequencing data, Samtools has you covered.