The bioinformatics file-format zoo · Subtopic 1 of 3 · 8 min
SAM & BAM: how alignments are stored
In the file-format zoo overview we met FASTQ, the file that holds the raw text a sequencing machine spits out. This lesson zooms in on what happens next. A sequencing machine does not read a whole genome in one go; it reads millions of short snippets, and each snippet is called a read (think of a read as one short sentence torn out of a very long book). A reference genome is a finished, known copy of that book that scientists have already assembled and agreed on. Aligning (also called mapping) means sliding each short read against the reference until you find the spot where it fits best. So the big question aligning answers is: for every read, WHERE in the genome does it belong, and how confident are we? The answer gets saved in two closely related file types called SAM and BAM.
SAM: a plain-text record
SAM stands for Sequence Alignment/Map. It is a plain-text file, meaning you can open it and read it with your eyes, just like FASTQ. A SAM file has two parts. First comes a header: a block of lines at the top that all start with the @ symbol (the at sign). The header holds background information, such as which reference genome was used. After the header comes the body: one line for every single read, one read per line, so a file can have millions of these lines.
Each read line is split into columns separated by tabs (a tab is just the invisible gap you get from the Tab key). A few of those columns are: the read name (a label identifying that snippet), the reference name (which part of the genome it mapped to, for example chr1 meaning chromosome 1), the position (the exact spot along that reference where the read starts), and the CIGAR string, a short code like 100M that summarizes how the read lines up (M for stretches that match, I for extra letters inserted, D for letters deleted). You do not need to memorize the columns or decode CIGAR by hand. Just hold on to the idea: each line says where one read landed and how cleanly it fit.
BAM: the compressed twin
BAM is simply the compressed, binary version of SAM. It holds exactly the same information, but squeezed down so it takes far less disk space. Binary means it is stored in a computer-packed form, so if you tried to open a BAM file directly it would look like scrambled nonsense, not readable text. That trade is worth it because real files are huge. The standard tool for working with both is called samtools, and it can convert between the formats and let you peek at a BAM as if it were plain SAM.
samtools view aln.bam | head -2read00042 99 chr1 1004 60 100M = 1250 346 AGCTTAGCTAGCTACCTATATCTTG... FFFFFFFFFFFFFFFFFFFF... NM:i:1
read00043 99 chr1 1108 60 100M = 1355 347 TTGGTCTTCGATCCAGGATCAAGTC... FFFFFFFFFFFFFFFFFFFF... NM:i:0
The samtools view command translates the compressed BAM back into readable SAM text, and head -2 shows just the first two lines so the screen is not flooded. Read the first line left to right: read00042 is the read name, chr1 is the reference it mapped to, 1004 is the start position, and 100M is the CIGAR (a clean 100-letter match). The numbers sprinkled in between (like 99 and 60) are status flags and quality scores we are skipping for now, and the trailing dots just mean the line continues with more columns. To count how many alignment lines are in the whole file, add the -c flag, where c stands for count:
samtools view -c aln.bam5811221
Try it yourself: Install samtools (on a Mac with Homebrew that is: brew install samtools), then get any small example BAM file to practice on. Run samtools view -c example.bam and read the number it prints back. That number is how many read alignments the file contains. Next, run samtools view example.bam | head -2 and just eyeball the first two lines. See if you can spot the read name, the reference name, and the position without worrying about the rest. That is the whole muscle memory: a BAM is millions of these where-did-it-land lines, and samtools is how you look inside.