Tutorials

BAM and SAM files in plain language

The SAM header, alignment fields, flags and CIGAR strings, what BAM and CRAM add, and the checks to run before variant calling.

SAM is a text format for aligned sequencing reads. BAM is the same information compressed in a binary format, and CRAM is a further-compressed version that depends on a reference sequence.

The SAM header

The header lines start with @. They record the format version, the sorting order, the reference sequences and the programs used. A missing or wrong header can make a valid file unusable because tools rely on it to know chromosome lengths and coordinate order.

Common header lines:

  • @HD: version and sort order.
  • @SQ: reference sequence name and length.
  • @RG: read group, which usually identifies the sample, library and platform unit.
  • @PG: program name, version and command line.

Read groups matter more than they look. Variant callers use them to separate samples and to apply library-specific error models. A BAM without a read group can still align, but downstream tools may refuse it or produce misleading results.

The alignment lines

Each alignment line has eleven mandatory fields:

  1. Query name.
  2. Flag, a bit field that encodes pairing, strand, mapping status and more.
  3. Reference name.
  4. Leftmost reference position, one-based.
  5. Mapping quality.
  6. CIGAR string, which describes matches, insertions, deletions and clipping.
  7. Mate reference name.
  8. Mate position.
  9. Template length.
  10. Sequence.
  11. Base quality string.

Optional tags carry extra information such as the aligner's score, the read group, mismatches and tags used by specific tools.

The flag is compact but easy to misread. A few common values: 4 means unmapped, 16 means reverse strand, 1024 means duplicate, 2048 means supplementary alignment. Combinations are sums of bits, so a value of 99 describes a paired read in a proper pair on the forward strand with its mate on the reverse strand.

CIGAR strings

A CIGAR string such as 50M10I40M5S means 50 aligned bases, a 10-base insertion, 40 more aligned bases and 5 soft-clipped bases at the end. M does not distinguish a match from a mismatch; the NM tag and the reference sequence do that.

Soft clipping keeps the clipped bases in the read. Hard clipping removes them. A read with a long soft clip may be chimeric or may extend beyond the reference. A read with many insertions or deletions may be misaligned or may indicate a real structural event.

Sorting and indexing

Most tools expect a coordinate-sorted BAM with an index (.bai or .csi). Sorting brings alignments into chromosome and position order. The index allows a tool to jump to a region without reading the whole file.

A name-sorted BAM is used for some steps, such as duplicate marking or paired-end processing. If a tool says the file is not sorted, check which order it needs; converting between orders is cheap compared with debugging a wrong result.

BAM, CRAM and storage

BAM is widely supported and reasonably compact. CRAM is smaller because it stores differences from a reference, but it needs the same reference to decode. If the reference is missing or different, the file cannot be read correctly.

Keep the reference checksum with a CRAM. For long-term archiving, check that the file can be decoded by the intended tool before deleting the BAM.

What to check before using a BAM

  • Is it coordinate-sorted and indexed?
  • Does the header list the same reference used for alignment?
  • Does every read have a read group?
  • Are duplicate and unmapped reads flagged as expected?
  • Does the alignment statistics match the report?
  • Is the file truncated? A checksum or samtools quickcheck catches many problems.

A BAM is evidence of an alignment, not proof that the alignment is correct. A high mapping rate in a contaminated sample still gives a high mapping rate. Look at coverage, insert size, duplication and the fraction of reads on the expected chromosome.

Key points

  • SAM is text; BAM is binary; CRAM is reference-dependent compression.
  • Read groups carry sample and library identity.
  • The flag and CIGAR string encode most of the alignment logic.
  • Most tools need coordinate sorting and an index.
  • Keep the reference with a CRAM.

Common mistakes

  • Using a BAM without read groups for multi-sample calling.
  • Mixing a BAM and a BED file from different reference builds.
  • Treating M in a CIGAR as an exact match.
  • Deleting the BAM while keeping a CRAM without the reference.
  • Assuming a high mapping rate means a clean sample.

When to ask for help

If a BAM will not open, if the header and the reference disagree, or if the alignment statistics look implausible, ask before running more steps. You can order an analysis or read the variant calling pipeline overview. The Genome Analysis studio can inspect alignments, and the NGS pipeline overview shows the surrounding steps.

Sources and further reading

Reading alignment statistics

Alignment statistics answer different questions. The total number of reads says how much data arrived. The mapped fraction says how much aligned. The properly paired fraction says how much aligned as expected for the library. The duplicate fraction says how much of the data is redundant. The mean insert size says whether the library preparation behaved as expected.

No single number is a quality score. A low mapping rate can come from contamination, adapter read-through, a wrong reference or a bad library. A high duplicate rate can come from low input, over-amplification or a deliberately deep experiment. A short insert size can cause read-through and adapter problems.

Look at the numbers together and compare them with the provider's report. If the mapped fraction is high but coverage is uneven, the problem may be biological or reference-related. If the duplicate fraction is high but the library was intentionally deep, it may be expected. The context decides.

Finally, check the file with a second tool before a long run. A viewer, a statistics tool and the aligner that produced it may interpret a malformed header differently. Agreement between two independent tools is a cheap form of validation.

When to ask for help. If your data or design do not fit a tutorial, order an analysis and we will reply with a fixed price.

Written by the Solagene team for research and education. Found an error? Tell us and we will correct it.