SAMtools

Notitle

SAMtools implements various utilities for post-processing alignments in the SAM format, such as indexing, variant caller and alignment viewer, and thus provides universal tools for processing read alignments.

How to run the application

SAMtools is available as a module, to check all available versions of SAMtools run:

$ module avail samtools

samtools/0.1.18         samtools/1.1            samtools/1.3.1(default)

To load the latest version of SAMtools and set the SAMtools environment run:

module load samtools

Submission script example

#!/bin/sh
#$ -cwd              # Set the working directory for the job to the current directory
#$ -V                # Export environment to job
#$ -l h_rt=24:0:0    # Request 24 hour runtime
#$ -l h_vmem=1G      # Request 1GB RAM
#$ -m bea            # mail sent to job owner b|beginning e|end a|aborted

module load samtools/1.3.1

samtools view -b -S -o genome_reads_aligned.bam genome_reads_aligned.sam

samtools sort genome_reads_aligned.bam > genome_reads_aligned.sorted.bam

samtools index genome_reads_aligned.sorted.bam

samtools mpileup -g -f ref_genome_1K.fna genome_reads_aligned.sorted.bam \
    > genome_variants.bcf

samtools view

Will translate all the alignments in genome_reads_aligned.sam file in SAM format to BAM format and will write the output to genome_reads_aligned.bam file.

  • -b will translate the input to BAM format.
  • -S The input is a SAM file, indicating that the input file is in SAM format.
  • -o The output file name.

samtools sort

Will sort the alignments in the genome_reads_aligned.bam file by leftmost coordinates. The output will be redirected to genome_reads_aligned.sorted.bam file.

You can also specify the -o option instead of redirecting the output. like this:

samtools sort genome_reads_aligned.bam -o genome_reads_aligned.sorted.bam

samtools index

Will index the sorted BAM file genome_reads_aligned.sorted.bam for fast random access and the index file genome_reads_aligned.sorted.bam.bai will be created.

Example Commands

samtools view -bt ref_list.txt -o aln.bam aln.sam.gz
samtools sort -T /tmp/aln.sorted -o aln.sorted.bam aln.bam
samtools index aln.sorted.bam
samtools idxstats aln.sorted.bam
samtools flagstat aln.sorted.bam
samtools stats aln.sorted.bam
samtools bedcov aln.sorted.bam
samtools depth aln.sorted.bam
samtools view aln.sorted.bam chr2:20,100,000-20,200,000
samtools merge out.bam in1.bam in2.bam in3.bam
samtools faidx ref.fasta
samtools tview aln.sorted.bam ref.fasta
samtools split merged.bam
samtools quickcheck in1.bam in2.cram
samtools dict -a GRCh38 -s "Homo sapiens" ref.fasta
samtools fixmate in.namesorted.sam out.bam
samtools mpileup -C50 -gf ref.fasta -r chr3:1,000-2,000 in1.bam in2.bam
samtools flags PAIRED,UNMAP,MUNMAP
samtools fastq input.bam > output.fastq
samtools fasta input.bam > output.fasta
samtools addreplacerg -r 'ID:fish' -r 'LB:1334' -r 'SM:alpha' -o output.bam input.bam
samtools collate aln.sorted.bam aln.name_collated.bam
samtools depad input.bam

More information on SAMtools commands and usage is available in the documentation.