# Reading One SAM/BAM Line

> What SAM/BAM alignment files record, and how to read the 11 required columns, FLAG, CIGAR, and optional tags from a real read pair.

**SAM and BAM are file formats that store alignment results for the short sequences, or reads, produced by a sequencer.** SAM is human-readable text, while BAM is a compressed binary representation of the same information. Each record describes where and in what state one read aligns to the genome.

When an expression value looks wrong, reading this record lets you trace the final number back to the alignment rather than inspecting only the output table.

> **Question for this reference**: How do you interpret the position, orientation, and alignment state in one SAM line?

## 1. Alignment output: the SAM/BAM format

An aligner such as STAR calculates where and how each read aligns to the genome, then writes the result as SAM or BAM. For an overview of the formats, see the [data formats reference](/en/reference/data-formats/).

To understand SAM records for paired-end reads, you need the **insert size** and orientation. After alignment, the two reads from the ends of one fragment usually face each other. The complete range covered by both reads is the insert size. FLAG and TLEN record this relationship.

A SAM file has **two parts**.

- **Header** (starts with `@`): metadata such as the reference genome used for alignment.
- **Alignment lines**: one line per read, with **11 required tab-separated columns** followed by optional fields.

### The 11 required columns

| # | Column | Meaning |
| --- | --- | --- |
| 1 | **QNAME** | Read ID, or query name |
| 2 | **FLAG** | Bit value encoding alignment states, described below |
| 3 | **RNAME** | Name of the aligned reference sequence, such as a chromosome |
| 4 | **POS** | Leftmost alignment start, 1-based |
| 5 | **MAPQ** | Mapping quality, or confidence in this location |
| 6 | **CIGAR** | String describing sequence-level alignment state |
| 7 | **RNEXT** | Reference containing the mate; `=` means the same reference |
| 8 | **PNEXT** | Position of the mate |
| 9 | **TLEN** | Insert size, the fragment length estimated from both read positions |
| 10 | **SEQ** | Read sequence from FASTQ |
| 11 | **QUAL** | Per-base read quality from FASTQ, Phred+33 |

Columns 10 and 11, SEQ and QUAL, come directly from [FASTQ](/en/reference/data-formats/). A SAM line therefore combines **the read information from FASTQ with where and how that read aligned to the genome**.

### Reading a real line

Here is an example paired-end read. Spaces represent tabs.

```text
A01055:...:7607   99    chr21   33426891   255   76M   =   33426961   145    CGGGCCTCC…   FFFF…   NH:i:1 HI:i:1 AS:i:149 nM:i:0 NM:i:0 RG:Z:boston
A01055:...:7607   147   chr21   33426961   255   75M   =   33426891   -145   GACATCGCT…   FFFF…   NH:i:1 HI:i:1 AS:i:149 nM:i:0 NM:i:0 RG:Z:tempus
```

- Two lines with the same **QNAME**, or read ID, are the two ends of one fragment.
- Both align to `chr21`; RNEXT is `=`, and their positions are 33,426,891 and 33,426,961.
- **TLEN** is `+145` and `-145`: the fragment is **145 bp** long, and the sign identifies which read lies on the left.
- The first read has FLAG **99** and its mate has FLAG **147**, decoded below.

### FLAG: alignment states encoded as bits

FLAG combines **12 on/off states** into one integer. You do not need to memorize them. Enter a number in Broad's [explain-flags tool](https://broadinstitute.github.io/picard/explain-flags.html) to decode it.

In the example, **99** expands to `read paired` (0x1) + `proper pair` (0x2) + `mate reverse strand` (0x20) + `first in pair` (0x40). Its mate, **147**, means `read paired` + `proper pair` + `read reverse strand` (0x10) + `second in pair` (0x80).

> 🧩 **Conditions for a proper pair**: (1) both reads align to the **same chromosome**, (2) one is forward and the other reverse, and (3) the **insert size is in an appropriate range**. The example is chr21, forward + reverse, and 145 bp, so it qualifies ✅.

Several especially useful FLAG states are:

- **PCR/optical duplicate**: a duplicate read produced during experimental amplification, with exactly the same start and end. Usually excluded from quantification.
- **not primary alignment** (secondary): a lower-ranked alignment beyond the **primary alignment** when one read maps to multiple locations.
- **supplementary alignment**: a read split across multiple locations, such as in a splice or structural variant.

### CIGAR: sequence-level alignment state

CIGAR records **how** a read aligns to the reference as a string.

- `76M`: 76 bases are aligned. **M means only that the positions align; it does not guarantee identical bases**, so M includes both matches and mismatches.
- `76=`: 76 bases are aligned and **every base matches the reference**.
- `40M1523N36M`: an **N** in the middle, a skipped reference region that is usually an intron, marks a splice read common in RNA-seq.
- Operators include **M** (aligned), **I** (insertion), **D** (deletion), **N** (skip an intron), **S** (soft clip, an unaligned end), **=** (match), and **X** (mismatch). The sum of operators that consume SEQ, **M/I/S/=/X**, equals the SEQ length.

The separate [CIGAR string reference](/en/reference/cigar/) explains all nine operators, splicing, indels, clipping, and reference-span calculations.

### Optional fields

From column 12 onward, any number of fields can appear as **`TAG:Type:Value`**. Type is the data type, such as `i` for integer and `Z` for string. Common tags include:

| Tag | Meaning |
| --- | --- |
| **NM** | Number of characters different from the reference, mismatches + indels |
| **AS** | Alignment score |
| **NH** | Number of hits, or locations to which this read aligned |
| **HI** | Hit index, the rank among multiple locations |
| **nM** | Mismatch count added by STAR |
| **SA** | Supplementary-alignment information for a split alignment |
| **RG** | Read group, identifying a sample or batch such as `boston` or `tempus` in the example |

> 💡 **Why learn to read SAM?** When an expression value looks wrong or only one gene stands out, inspect whether the reads aligned **properly**, as proper pairs with few mismatches and primary alignments. Opening a region with `samtools view` and checking FLAG, CIGAR, and NM is a basic debugging technique.

## Summary

SAM combines read information from FASTQ with genomic positions and alignment states. Instead of memorizing every FLAG value, begin with three questions.

1. Do the read and its mate align at the expected positions and orientations?
2. How does CIGAR record alignment, insertion, deletion, and clipping?
3. What do tags such as NM and NH say about mismatches and multi-mapping?

Continue the conceptual flow in [Finding read positions with splice-aware alignment](/en/lessons/rna-seq-alignment/).