SAM, BAM and CRAMare the same data
An aligner tells you where each read landed. That answer is written in one of three encodings, and SAMtools reads all of them.
Readable, greppable, and enormous. Useful for looking at, and almost never for keeping.
The same records, compressed and indexable. The working format for essentially everything.
Stores only differences from a reference genome. Much smaller, but you must keep that reference.
Six subcommandsdo nearly everything
SAMtools has dozens. In practice a pipeline uses the same handful, in roughly the same order, every time.
$ samtools sort -@ 8 -o aln.sorted.bam aln.bam
$ samtools index aln.sorted.bam
$ samtools flagstat aln.sorted.bam
Sort first,then index
Almost every SAMtools problem is one of these two steps missing, and the error message rarely says so plainly.
A BAM straight out of an aligner is in read order, which is the order the sequencer produced them in. Nearly everything downstream wants coordinate order instead — sorted by chromosome, then position. Once sorted, an index lets a tool jump straight to a region without reading the file, which is what makes samtools view aln.bam chr1:1-1000 instant rather than a full scan.
IGV, variant callers and coverage tools all assume both steps have happened. If a genome browser shows you nothing, or a caller reports no reads in an obviously covered region, check for the .bai file before checking anything else.
The numberin the second column
SAM flags are a bitfield, which is why filtering reads feels cryptic until you see how it works.
One integer holding a dozen yes/no answers about how that read aligned.
Filter on the bits rather than the number. -F 0x904 is the standard cleanup.
The explain-flags page exists for a reason. Nobody memorises what 163 means.
Each bit means one thing: paired, properly paired, unmapped, reverse strand, secondary, duplicate. A flag of 99 is several of those at once. -f keeps reads with a bit set, -F excludes them, so -F 0x904 is the usual way to drop unmapped, secondary and supplementary alignments in one go.
SAMtools isalready built
Nothing here needs compiling. Pick a machine, and these two commands put it on there.
$ dxflow workflow create --identity samtools hub://samtools
$ dxflow workflow start samtools