A sequencing machine does not hand back a finished genome. It hands back millions of short pieces of DNA or RNA from the sample. Each piece is a string of chemical letters, called bases (A, C, G, and T, or U in RNA), plus a confidence number for every letter. Together, the letters and the confidences are a read. On its own, a read does not say which chromosome it came from or where along that chromosome it belongs.
An aligner compares each read with a reference genome, a previously assembled sequence that stands in as the map for that species. The result is an alignment: the read, the place it landed, and a note of how the letters line up, including matches, gaps, extra bases, and ends that were left off the match.
SAM and BAM are the two standard ways to keep those alignments. They hold the same records. SAM writes them as text a person can open. BAM writes them as a compressed binary file a program can search by genomic location, which is what you want once the file is tens of gigabytes. The original reads, before anyone has placed them, usually live in a third file, FASTQ. SAM and BAM come after alignment. Later steps use them as the evidence for a variant call, a coverage count, or the picture in a genome browser.
The rules for both files are the SAMv1 specification, published with the other HTS format specifications. This library pins format 1.6. See specification notes. Words used below are defined in the glossary.
The two forms
SAM is tab-separated text. The header lines start with @ and name the reference sequences, their lengths, the read groups, and the programs that touched the file. Each alignment line has eleven fixed columns and then optional tags:
| Column | Role |
|---|---|
| QNAME | Read name |
| FLAG | Bit field: paired, unmapped, reverse strand, duplicate, and so on |
| RNAME | Reference sequence name, or * when unplaced |
| POS | One-based position on that reference, or 0 when unplaced |
| MAPQ | Mapping quality, an estimate of how likely the placement is wrong |
| CIGAR | How the read lines up: matches, insertions, deletions, clipping |
| RNEXT, PNEXT, TLEN | Where the mate sits, for paired reads |
| SEQ, QUAL | The bases and their Phred qualities |
| TAGS | Optional typed fields such as NM:i:0 |
BAM stores those same fields in binary. Reference names live in a dictionary, and each record points at a name by integer id. Positions on disk are zero-based. The bytes are wrapped in BGZF, which is gzip cut into independent blocks of at most 64 KiB uncompressed. An index (BAI, or CSI for references longer than 512 Mbp) maps a genomic interval to the blocks that might contain it. A tool can then read the gene it cares about and leave the rest of the file alone.
SAM and BAM describe the same records. A file can be converted from one to the other without changing the alignments. SAM is the form a person or a text pipeline can read. BAM is the form a large analysis stores and queries.
What the files are used for
Alignment files are the working evidence between the sequencer and most downstream conclusions.
Variant calling walks the reads that overlap a site and decides whether the sample differs from the reference. Coverage and RNA-seq counting ask how many reads land in an exon or across a splice. ChIP-seq and ATAC-seq look for piles of reads at binding sites and open chromatin. Methylation and some long-read analyses keep the read sequence and the tags that say how each base was modified. Quality control counts unmapped reads, duplicates, and failed pairs. A genome browser such as IGV draws the reads so a reviewer can see the evidence under a call.
The file is also the archive of an aligned run. Given the BAM, the reference, and the aligner version, a later pipeline can re-call variants without sequencing the sample again.
When they appear
The aligner writes them. Common aligners include BWA-MEM and Bowtie 2 for short reads, STAR and HISAT2 for RNA-seq, and minimap2 for long reads. The usual next commands sort the file by coordinate and write an index. Everything after that expects a coordinate-sorted, indexed BAM: GATK, DeepVariant, bcftools, freebayes, mosdepth, and samtools itself.
SAM shows up while a pipeline is being built, when a file is small, or when one program's stdout is the next program's stdin. BAM shows up as soon as the file is large enough that a text scan is the wrong tool, which for a human genome is almost immediately. A 30× human short-read BAM is on the order of tens of gigabytes. Region queries against an index are the reason to keep that size in BAM rather than in one gzip stream of SAM text.
This library sits in that middle. An Elixir program can open a BAM, read the header, stream records, or fetch one interval, and it can write a BAM that samtools will accept. It does not replace the aligner or the variant caller.
Who uses them
Sequencing cores and clinical laboratories deliver aligned BAMs to the groups that ordered the run. Research groups in rare disease, cancer, agricultural genomics, and pathogen surveillance keep BAMs as the record of each sample. Bioinformaticians write the pipelines, often in Nextflow, Snakemake, or WDL, that sort, mark duplicates, and call variants. People reviewing a result open the same file in a browser and look at the reads under one locus. Tool authors read and write the format so their program can sit in that pipeline.
A wet-lab scientist rarely edits a BAM by hand. They meet the file as a download, a track in a browser, or an input path in a workflow.
How a record is produced and consumed
- The sequencer writes FASTQ.
- The aligner matches reads to a reference and emits SAM or BAM.
samtools sortorders records by reference and position.samtools indexwritesfile.bam.baiorfile.bam.csi.- A downstream tool either streams every record or seeks to the intervals it needs.
- A variant caller or a counter writes a smaller file, such as VCF or a coverage track. The BAM remains the evidence.
In this library the same path is BAM.open/2, BAM.info/1, BAM.stream/2, and BAM.read/3. BAM.write/4 emits BGZF and, for a path, a BAI or CSI sidecar. BAM.SAM parses and formats the text form. Coordinates in BAM.Alignment.pos stay zero-based, matching the BAM record. BAM.SAM.region/1 converts a one-based samtools region such as chr1:1-1000.
Other formats, and when they fit better
SAM and BAM are the right record of linear alignments. Several neighboring formats answer a different question, and some of them are the better file for a particular job.
CRAM stores the differences from a reference instead of every matching base, and it can bin quality scores. For a stable reference such as GRCh38, a CRAM file is often several times smaller than the BAM of the same reads, which makes it the better archive and the better object to ship. The reader must have the same reference the file was built against. CRAM is a separate specification. This library does not read or write it. Tutorial 9 discusses that boundary.
SAM text is the better form for inspection, fixtures, and pipes. It is the worse form for a whole genome: it is larger, and a plain gzip SAM file cannot seek to a locus.
FASTQ and FASTA are the better starting point when the work does not need a placement. Assembly, k-mer screens, and some quality reports never align. They carry sequence and, for FASTQ, qualities. They do not carry CIGAR, flags, or a reference position. FASTQ has no single formal specification. The Cock et al. paper is the reference people cite for the Sanger form of the file.
VCF and BCF are the better record of the conclusion. A variant call says "this sample has this allele at this site." The BAM says which reads supported that call. Re-analysis and manual review go back to the BAM. A VCF cannot reconstruct the reads. BCF is the binary encoding of the same VCF records.
BED, GFF3, and GTF describe intervals and annotations: genes, exons, peaks, target capture regions. They are the better way to say where a gene is. They do not contain the reads.
bigWig and bedGraph are the better way to store depth or a signal track once the individual reads are no longer required. A coverage track is much smaller than the BAM it came from. It cannot answer which read had the mismatch.
GAF and GAM are the better fit when the reference is a pangenome graph rather than one linear sequence. GAF is the text alignment format described with the GFA graph specification. GAM is the binary alignment format used by vg. A linear BAM flattens that graph onto a single path and drops the places where the sample simply does not match that path.
For a linear reference, a coordinate-sorted indexed BAM is still the format most existing tools expect. Choose CRAM when storage and transfer dominate and every consumer can see the reference. Choose SAM when a person has to read the file. Choose FASTQ, VCF, or a coverage track when the question is no longer "where did each read land."
Glossary
| Term | Meaning | Reference |
|---|---|---|
| Base | One letter of a DNA or RNA sequence. DNA uses A, C, G, and T. RNA uses U in place of T. | SAMv1 sequence field |
| Read | The bases from one fragment that came off the sequencer, plus a quality score for each base. | FASTQ, Cock et al. 2010 |
| Quality score | A number saying how likely that base call is wrong. SAM stores the printable form of a Phred score. | Phred quality score |
| FASTQ | Text file of unplaced reads: a name, the bases, and the quality characters. This is the usual input to an aligner. | Cock et al. 2010 |
| FASTA | An older text file of names and sequences, without per-base qualities. Reference genomes are often distributed this way. | NCBI FASTA format |
| Reference genome | The assembled sequence an aligner uses as the map. Placements are coordinates on this sequence, not a new assembly of the sample. | Genome Reference Consortium |
| Alignment | The recorded placement of one read: where it landed, how the bases line up, and how confident that placement is. | SAMv1 |
| Aligner | The program that searches the reference for each read. BWA-MEM, Bowtie 2, STAR, HISAT2, and minimap2 are common ones. | SAMv1 |
| SAM | The text form of an alignment file. A header names the references. Each following line is one alignment. | SAMv1 |
| BAM | The same records as SAM, stored in binary and compressed so a program can jump to a genomic interval. | SAMv1 |
| CIGAR | A short code for how the read lines up with the reference: matches, insertions, deletions, and clipped ends. 8M means eight bases consumed on both. | SAMv1 |
| FLAG | A bit field on each alignment. The bits say whether the read is paired, unmapped, on the reverse strand, a duplicate, and so on. | SAMv1 |
| MAPQ | Mapping quality. A Phred-scaled estimate that the aligner put the read in the wrong place. | SAMv1 |
| Mate | The other read from the same paired fragment. Paired-end sequencing reads both ends of one piece of DNA. | SAMv1 |
| Tag | An optional typed field after the eleven fixed columns, such as NM:i:0 for the number of differences from the reference. | SAMtags |
| BGZF | The compression wrapped around a BAM file: gzip cut into independent blocks so a reader can start at a block in the middle of the file. | SAMv1 |
| BAI | The usual index beside a BAM file. It maps a genomic interval to the blocks that might hold overlapping reads. It fits references up to 512 Mbp. | SAMv1 |
| CSI | The index used when a reference is too long for BAI, and for some other coordinate-sorted files. | CSI v1 |
| CRAM | A compressed alignment file that stores differences from a reference instead of every matching base. The reader needs that same reference. | CRAM v3 |
| VCF | A text file of variant calls: sites where the sample differs from the reference. It is the conclusion, not the reads. | VCF v4.5 |
| BCF | The binary form of a VCF. | BCF v2 layout |
| BED | A simple text file of genomic intervals, such as regions to capture or peaks to inspect. | BED v1 |
| GFF3 | A text file of annotated features, such as genes and exons, with a controlled set of types and attributes. | GFF3 specification |
| GTF | A gene-annotation file used widely by Ensembl and by RNA-seq counters. It is a constrained form of GFF. | Ensembl GFF/GTF |
| bigWig | A binary track of a numeric signal along the genome, such as depth, that a browser can draw without the reads. | UCSC bigWig |
| bedGraph | A text track of a numeric signal on intervals. bigWig is the indexed form of the same idea. | UCSC bedGraph |
| GFA / GAF | GFA describes a genome graph. GAF describes how a read aligns to that graph. | GFA specification |
| GAM | A binary graph-alignment file used by vg. | vg file formats |
| Coverage | How many reads overlap a base or an interval. It is a summary of a BAM, not a replacement for it. | SAMv1 |
| samtools | The reference toolkit that reads, writes, sorts, and indexes SAM, BAM, and CRAM. | HTSlib and samtools |
| IGV | A genome browser used to look at the reads in a BAM under one locus. | Integrative Genomics Viewer |