# 02. From a Library to FASTQ

> How paired-end sequencing reads both ends of a cDNA fragment and how Illumina SBS records nucleotide sequences and Phred quality scores in FASTQ.

Putting a cDNA library into a sequencer produces FASTQ files containing **nucleotide sequences and confidence for each character**. This lesson follows only the process by which one fragment becomes two FASTQ reads.

> **Question for this lesson**: How does a sequencer read a cDNA fragment, and how does it record how confident each reading is?

Think of a large parallel job that stores a result and execution metadata for every input record. A sequencer processes millions of fragments at once and serializes not only the string read from each fragment but also its **physical address and confidence for every character**.

| Systems view | Sequencing equivalent |
| --- | --- |
| Work unit | One cluster on the flow cell |
| Repeated loop | Call one base in every cycle |
| Result data | A, C, G, T, and N string |
| Observation metadata | Cluster address and per-base Phred score |
| Output format | FASTQ record |

## 1. Reading both ends of a fragment

Suppose an instrument reads 100 bp from each end of a 500 bp fragment. It reads the first 100 bp and the last 100 bp, leaving the middle 300 bp unread.

Reading both ends of one fragment separately is called **paired-end sequencing**. The two reads share an ID indicating that they came from the same fragment. The first is saved in the R1 file and the second in the R2 file.

The two files are not independent logs; they contain pairs from the same jobs. Records at the same position in R1 and R2 represent opposite ends of one fragment. Deleting or reordering records in only one file breaks that relationship.

Read length is not the length of the original mRNA. It is the length the instrument actually read from the end of the fragment, usually 100 to 150 bp from each end.

This short animation gives an intuitive view of how paired-end sequencing reads both ends of one fragment (Simple Science):

<div style="position:relative;padding-bottom:56.25%;height:0;overflow:hidden;margin:1rem 0;border-radius:8px;">
  <iframe style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;" src="https://www.youtube-nocookie.com/embed/0vqajoP08Jg" title="NGS: How does paired-end sequencing work? (Simple Science)" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>


## 2. Reading in parallel on a flow cell

A sequencer does not hold one fragment and read fragments in sequence. It attaches millions of fragments to the surface of a glass chip called a **flow cell** and photographs them together during the same cycle.

The fluorescence from one fragment molecule is too weak for a camera to distinguish. The attached fragment is therefore copied thousands of times in place, creating a bright spot containing the same sequence. This spot is a **cluster**. In paired-end sequencing, R1 and R2 are produced from the one library fragment attached in each cluster.

A flow cell is divided into **lanes**, independent channels through which reagents flow. The camera does not capture an entire lane in one image. It divides the lane into smaller imaging areas called **tiles**. Use the widget below to move step by step down to the physical address where one fragment sits.



A string such as `C5GAPACXX:4:1101:2723:1997` in a FASTQ header records this address.

| Header segment | Location it identifies |
| --- | --- |
| `C5GAPACXX` | flow cell ID |
| `4` | lane number |
| `1101` | tile number |
| `2723:1997` | x:y coordinates of the cluster within the tile |

Multiple samples can share one lane. Each sample is given a unique **index sequence** before they are mixed. After reading, FASTQ files are separated again according to the index. These processes are called multiplexing and demultiplexing. The sample-specific FASTQ files we receive have usually already been demultiplexed.

The structure resembles repartitioning distributed-job results by `sample_id`. An index is itself a short DNA sequence that the instrument reads, so calling errors can occur. Indexes that are too similar can cause a record to be assigned to the wrong sample.

## 3. Selecting one base from fluorescence signals

Suppose only the T signal glows strongly at the first position in a fragment. The instrument records the first character as T. If A and C glow with similar strength at the next position, the instrument still chooses one but records lower confidence.

Illumina instruments repeat a cycle of adding one nucleotide and photographing its fluorescence. Because the sequence is read while it is synthesized, this method is called **SBS (sequencing by synthesis)**.

Illumina's official animation makes the process inside the instrument much clearer:

<div style="position:relative;padding-bottom:56.25%;height:0;overflow:hidden;margin:1rem 0;border-radius:8px;">
  <iframe style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;" src="https://www.youtube-nocookie.com/embed/fCd6B5HRaZ8" title="Overview of Illumina Sequencing by Synthesis Workflow (Illumina)" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

> **Photograph fluorescence → select the strongest base → record the probability that the selection is wrong**

Choosing one of A, C, G, and T from the fluorescence is called **base calling**. If no base can be selected confidently, it is recorded as `N`.

### One expected error in 1,000 bases

The **Phred quality score Q** represents the probability that a base call is wrong. Q30 means an average probability of about one error for every 1,000 bases read under the same conditions.

| Phred Q | Error probability | Accuracy |
| --- | --- | --- |
| Q20 | 1/100 | 99% |
| **Q30** | **1/1000** | **99.9%** |
| Q40 | 1/10000 | 99.99% |

[Probability and Phred scores](/en/reference/probability-and-phred/) explains why a 10-point increase in Q reduces the error probability by a factor of ten, along with the logarithmic formula.

FASTQ stores Q values as ASCII characters. Because every base has one corresponding quality score, the sequence and quality strings have the same length.

A FASTQ record has four lines.

```text
@read42/1
TACGTTAG
+
IIIII5II
```

The eight bases on the second line correspond position by position to the eight quality characters on the fourth. You can read `sequence[i]` together with `quality[i]` as parallel arrays. The header stores the record ID and instrument address.

The [raw FASTQ inspection stage of the RNA-seq exercise](/en/practice/rna-seq-from-fastq/#3-1-inspect-raw-fastq-structure-and-quality) examines these four-line records and their quality distributions in real paired-end files.

Instruments such as PacBio and Oxford Nanopore produce long reads. This note focuses on Illumina instruments that produce short paired-end reads.


## Summary

A sequencer reads both ends of a fragment over multiple cycles. In every cycle, it selects one base from the fluorescence signal and calculates a Phred score for the probability that the choice is wrong.

> **cDNA fragment → repeated fluorescence imaging and base calling → nucleotide sequence + quality scores → FASTQ**

The next lesson, [FASTQ quality control and trimming](/en/lessons/rna-seq-to-matrix/), checks FASTQ quality and prepares reads for alignment.

---

### Sources

- Method for removing ALT, HLA, and decoy sequences from the reference genome: [GTEx / TOPMed RNA-seq pipeline](https://github.com/broadinstitute/gtex-pipeline/blob/master/TOPMed_RNAseq_pipeline.md)
- Normal-tissue expression reference (GTEx): [GTEx Portal: bulk tissue expression](https://gtexportal.org/home/downloads/adult-gtex/bulk_tissue_expression) · [Nature Genetics 2013](https://www.nature.com/articles/ng.2653)
- Cancer expression and genome reference (PCAWG): [Pan-cancer analysis of whole genomes, *Nature* 2020](https://www.nature.com/articles/s41586-020-1969-6) · [ICGC/ARGO data access](https://docs.icgc-argo.org/docs/data-access/icgc-25k-data)
- Sid's public expression viewer: [osteosarc.com/rnaseq](https://osteosarc.com/rnaseq/)