# 04. Aligning RNA Reads to the Genome

> Find the original genomic positions of short RNA-seq reads and explain why splicing requires a splice-aware aligner.

Even cleaned FASTQ files do not say **which gene a read came from**. You must find each read's original position in the reference genome before you can count reads by gene. The output of this lesson is an **aligned BAM** file.

> **Question for this lesson**: How do you put short RNA reads back in the correct positions in the genome?

Imagine having a code fragment without knowing which repository and line it came from. You search the entire codebase for the closest match, then attach a file and line address. Alignment has the same structure: it is a **search and address-assignment** operation.

| Input | Search reference | Processing | Output |
| --- | --- | --- | --- |
| Short FASTQ reads | Reference genome + GTF | Search candidate locations and select the best match | BAM with coordinates and confidence |

## 1. Alignment: finding the original position of a short read

Alignment is the process of placing each FASTQ read back onto a reference genome. The introns have already been removed from mature mRNA, so one RNA-seq read can span two exons that are far apart in the genome. This requires a **splice-aware aligner** that can split a read across those positions.

Suppose a read joins the end of `EXON1` to the beginning of `EXON2`. The genome contains a long intron between those regions, so a simple substring search fails.

```text
read:    ...ACCTG | GAACT...
genome:  ...ACCTG | <long intron> | GAACT...
result:  align the first part to exon 1 and the second to exon 2
```

A splice-aware aligner permits a large gap during its search and uses known exon boundaries and candidate junctions to record the two pieces as one read. The structure resembles creating a source map across files, but repeats and base-calling errors can leave more than one plausible location.

![Splice-aware alignment diagram: an intron is removed from pre-mRNA to form mRNA, and a short read from that mRNA is split across two exons during alignment](/images/lessons/rna-seq-pipeline/alignment-splice.png)

*A read from mature mRNA can span two exons separated by an intron. Tools such as STAR and HISAT2 split the read and align each part to the correct position.*

STAR and HISAT2 are representative tools. More important than the tool name is **using matching reference-genome and GTF versions, and distinguishing reads that align confidently to one location from reads that align ambiguously to several**. Just as addresses fail when code and its source map come from different versions, genomic coordinates and gene boundaries can disagree when the genome and GTF versions do not match.

The [alignment stage of the RNA-seq exercise](/en/practice/rna-seq-from-fastq/#3-3-align-to-the-genome-and-transcriptome-with-star) runs STAR and inspects `Log.final.out`, the genome BAM, and the transcriptome BAM together.

> See the [RNA-seq aligners reference](/en/reference/aligners/) for the differences between STAR and HISAT2, how indexes work, multi-mapping, and how to read the output.


## Summary

Because RNA reads come from mature RNA with introns removed, one read may align in separate pieces to two exons in the genome. Splice-aware aligners such as STAR and HISAT2 handle this gap.

> **FASTQ for alignment + reference genome and GTF → splice-aware alignment → BAM**

The next lesson, [Converting reads into an expression matrix](/en/lessons/rna-seq-quantification/), turns the reads in a BAM file into gene-level expression values.