Samtools is a set of tools for working with SAM and BAM files.
view
samtools view is used to convert between .SAM and the compressed .BAM format.
$ samtools view -b <input.sam> -o <output.bam>
- the -b option specifies that output should be in BAM format.
It can also be used to view compressed BAM files.
sort
sorts BAM file. Improves compression and allows indexing for faster access.
$ samtools sort <input.bam> <output.sorted.bam>
index
Indexes a sorted BAM file, increases the speed of access.
$ samtools index <input.sorted.bam>
mpileup
mpileup outputs a consensus sequence with a per-base summary of read depth and quality. It can also be used to generate a VCF file, either of the entire sequence (with one line per base) or of variants.
Requires an indexed fasta file. index can be created using the samtools faidx <fasta file> command.
raw pileup
$ samtools mpileup -f <fasta reference> -o <output file> <sorted BAM file>
VCF pileup
$ samtools mpileup -uvf <fasta reference> <sorted BAM file> > <output.vcf>
Pileup file can be converted to a FASTA consensus using the pileup2fasta Perl script.
tview
views a BAM file against a reference, showing the pileup of all the reads.