LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 73 lessons

Lesson 4 of 7

Module 5 · Sequencing data

Aligning reads to a genome

Aligning reads to a genome

Aligning reads to a genome

Alignment finds where each read came from in the reference genome. RNA reads can span an intron, so RNA-seq needs a splice-aware aligner such as HISAT2 or STAR.

⏱ 12 min readModule 5 · Sequencing dataFree
Lesson at a glance6 steps · about 12 min
  1. Index, align, sort
  2. What this does
  3. Reading the output
  4. Inside a BAM file
  5. What this does
  6. Reading the output

Index, align, sort

The aligner first builds an index of the genome, once. Then it aligns each sample and writes SAM text, which samtools sorts by position into a compressed BAM file and indexes for fast access.

Some terms first. An index of the genome works like the index of a book: a lookup table, built once, that lets the aligner jump straight to the places where a short piece of sequence occurs instead of reading the whole genome for every read. HISAT2 builds a compressed index based on a method called the Burrows–Wheeler transform. SAM (Sequence Alignment/Map) is a tab-separated text format with one line per aligned read. BAM is the same information in compressed binary form: smaller and faster, but not readable without samtools. Sorting puts the reads in order of their position on the genome, and the BAM index, a small .bai file beside it, lets programs jump to any region.

RNA-seq needs a splice-aware aligner because of splicing. The mature RNA you sequenced has had its introns removed, so a read can contain the end of one exon joined directly to the start of the next. On the genome those two pieces sit hundreds or thousands of bases apart. An ordinary DNA aligner would try to match the read in one piece and fail; HISAT2 and STAR can split a read across an intron. STAR is very fast but needs a lot of memory, around 30 GB for the human genome; HISAT2 needs much less.

The question an aligner answers is the one from Module 4: where does this sequence match best, allowing for a few differences? What changes is the scale. Aligning each of millions of reads against a whole genome with the full dynamic programming method from Module 4 would take far too long. Instead, like the fast database search in Module 4, the aligner uses the index to find short exact matches first, called seeds, and only then checks the full read carefully at those few candidate places. The index is what makes that first step fast.

Terminal
hisat2-build -q data/practice_genome.fa index/practice      # once per genome

hisat2 -p 2 -x index/practice \
       -1 trimmed/ctrl_1_R1.fastq.gz -2 trimmed/ctrl_1_R2.fastq.gz 2> aligned/ctrl_1.log \
  | samtools sort -o aligned/ctrl_1.bam
samtools index aligned/ctrl_1.bam
cat aligned/ctrl_1.log
9000 reads; of these: 9000 (100.00%) were paired; of these: 37 (0.41%) aligned concordantly 0 times 8963 (99.59%) aligned concordantly exactly 1 time 0 (0.00%) aligned concordantly >1 times ---- 37 pairs aligned concordantly 0 times; of these: 3 (8.11%) aligned discordantly 1 time ---- 34 pairs aligned 0 times concordantly or discordantly; of these: 68 mates make up the pairs; of these: 36 (52.94%) aligned 0 times 32 (47.06%) aligned exactly 1 time 0 (0.00%) aligned >1 times 99.80% overall alignment rate

What this does

  • hisat2-build -q builds the index from the genome FASTA file, quietly (-q). index/practice is a prefix: HISAT2 writes several files whose names start with it and end in .ht2. You do this once per genome, not once per sample.
  • hisat2 -p 2 -x index/practice aligns using 2 processor threads and that index. -1 and -2 give the trimmed read 1 and read 2 files. The summary of the run goes to the error channel, which 2> saves to a log.
  • HISAT2 writes the alignments in SAM format to standard output. The pipe sends them straight into samtools sort, which sorts them by position and writes a BAM file with -o. No large SAM text file ever reaches the disk.
  • samtools index makes the .bai index, and cat prints the log.

The trimmed files from Trimming and filtering are the input here, not the raw files in data/. Giving the raw reads by mistake still works, so nothing warns you, but the leftover adapter makes some reads fail to align or align only in part. Keep -1 for read 1 and -2 for read 2 as well: HISAT2 uses the two to work out which way round each pair should face.

Reading the output

  • 9,000 read pairs went in. 8,963 pairs (99.59%) aligned concordantly to exactly one place; none aligned to more than one place.
  • Of the 37 pairs that did not align concordantly, 3 aligned discordantly: both reads aligned, but not in the expected orientation or distance from each other.
  • That leaves 34 pairs, 68 single reads (mates). Of these, 32 aligned on their own and 36 did not align at all.
  • The overall rate counts single reads: (8,963 + 3) pairs × 2 = 17,932 reads, plus the 32 lone mates, gives 17,964 of 18,000 reads, which is 99.80%.

Concordant pairs align with both reads facing each other at a sensible distance. A real human RNA-seq sample usually gives 85–95% overall alignment; the practice data are higher because they were simulated from this genome. A much lower rate suggests contamination, the wrong reference or poor quality.

For example, if mouse RNA were aligned to the human genome by mistake, many reads would still align, because the two species share many genes, but the rate would fall well below the usual range. A sample with a lot of bacterial contamination would show the same symptom. The alignment rate is one of the first numbers to compare across samples in MultiQC.

The reads that do not align are worth a moment too. HISAT2 can write the pairs that did not align concordantly to their own files, with --un-conc-gz followed by a file name. Taking a few and searching them against a sequence database, as in the project at the end of Module 4, often shows at once where they came from: ribosomal RNA, a bacterium, or adapter that trimming missed.

The next lesson needs a BAM file for every sample, so run the alignment for all four with the same loop pattern as Trimming and filtering:

Terminal
for s in ctrl_1 ctrl_2 treat_1 treat_2; do
  hisat2 -p 2 -x index/practice \
         -1 trimmed/${s}_R1.fastq.gz -2 trimmed/${s}_R2.fastq.gz 2> aligned/${s}.log \
    | samtools sort -o aligned/${s}.bam
  samtools index aligned/${s}.bam
done
grep "overall alignment rate" aligned/*.log

As in Trimming and filtering, grep pulls one line from each log, so the four alignment rates sit one under another and an outlier is easy to spot. The index is not rebuilt inside the loop: it belongs to the genome, not to any sample.

HISAT2 is described by Kim, Paggi, Park, Bennett and Salzberg (2019, Nature Biotechnology), building on the original HISAT paper by Kim, Langmead and Salzberg (2015, Nature Methods). STAR is Dobin et al. (2013, Bioinformatics). The SAM and BAM formats and the samtools program come from Li et al. (2009, Bioinformatics); the formal definition of every column and flag is the SAM specification, kept on the hts-specs site. Alignment rates depend on the aligner, its settings and the reference, so cite the aligner version and the exact genome files used, and compare rates only between samples processed the same way.

Inside a BAM file

Each line of a BAM file (shown as SAM text by samtools view) is one aligned read, with eleven fixed columns. The first nine are: 1 the read name; 2 the flag, a number that packs yes/no facts about the read; 3 the chromosome; 4 the leftmost position where it aligns; 5 the mapping quality; 6 the CIGAR string, which describes how the read lines up with the genome; 7 the chromosome of its mate (= means the same one); 8 the mate's position; and 9 the fragment (template) length. Columns 10 and 11 hold the read's bases and qualities.

The BAM file is sorted by position because almost every later step works along the genome. Counting reads per gene in Counting reads per gene visits genes in order, and a genome viewer such as IGV (the Integrative Genomics Viewer) shows the reads under a region you choose. Both need the sorted BAM and its .bai index; a common error message about a missing index usually means the samtools index step was skipped.

Terminal
samtools flagstat aligned/ctrl_1.bam | head -n 7
samtools view aligned/ctrl_1.bam | head -n 2 | cut -f 1-9
samtools view aligned/ctrl_1.bam | awk '$6 ~ /N/' | wc -l          # spliced reads
samtools view aligned/ctrl_1.bam chrP:10200-10300 | awk '$6 ~ /N/' | head -n 1 | cut -f 1-9
18000 + 0 in total (QC-passed reads + QC-failed reads) 18000 + 0 primary 0 + 0 secondary 0 + 0 supplementary 0 + 0 duplicates 0 + 0 primary duplicates 17964 + 0 mapped (99.80% : N/A) ctrl_1.4523 99 chrP 1508 60 100M = 1613 205 ctrl_1.7891 99 chrP 1511 60 100M = 1702 258 304 ctrl_1.2608 163 chrP 10219 60 90M351N10M = 10713 243

What this does

  • samtools flagstat counts the reads in each category; head -n 7 keeps the first seven lines.
  • samtools view turns the BAM file back into SAM text. Here it keeps two reads and the first nine columns, leaving out the long sequence and quality columns.
  • awk '$6 ~ /N/' keeps reads whose sixth column, the CIGAR string, contains an N, that is, spliced reads. ~ means "matches the pattern". wc -l counts them.
  • The last line asks only for reads in the region chrP:10200-10300, which needs the index, and shows the first spliced one.

Reading the output

  • flagstat: 18,000 reads in total, all primary (each read's main alignment). None are secondary (extra possible places for a read) or supplementary (pieces of a read split between distant places). The duplicate lines are 0 because no tool has marked duplicates yet. 17,964 reads are mapped, the same 99.80% as the HISAT2 log.
  • The two records: both reads start near position 1,500, at the start of gene prA, with mapping quality 60 and CIGAR 100M: all 100 bases aligned in one piece. Flag 99 is the sum 1 + 2 + 32 + 64: the read is paired (1), in a proper pair (2), its mate is on the reverse strand (32), and it is read 1 of the pair (64).
  • 304 reads in this sample are spliced.
  • The spliced read: flag 163 is 1 + 2 + 32 + 128, the same as 99 except that 128 means it is read 2 of its pair. Its CIGAR, 90M351N10M, is taken apart in the worked example and figure below.
  • The fragment length: column 9 is the length of the whole fragment the pair came from. For the first record, if the mate is a full 100 bases, it covers 1,613 to 1,712, and 1,712 − 1,508 + 1 = 205, the value shown. Fragments of a couple of hundred bases are what library preparation in From sample to reads aims for. A concordant pair is one whose fragment length and orientation fit that expectation.

The letters in a CIGAR string you will meet most are M (aligned bases, which may still include mismatches), N (skipped genome, an intron), I and D (bases inserted in or deleted from the read relative to the genome) and S (soft-clipped bases at the ends that were not aligned).

Worked example · reading a spliced alignment

CIGAR 90M351N10M → 90 bases match, skip 351 bases of genome, 10 bases match
starts at 10,219 → first block 10,219–10,308 · intron 10,309–10,659 · second block from 10,660
gene prC has exon 1 ending at 10,305 and exon 2 starting at 10,657 → the read spans the intron

Step by step. The read starts at 10,219. Ninety aligned bases cover 10,219 to 10,219 + 90 − 1 = 10,308. The 351 skipped bases are 10,309 to 10,309 + 351 − 1 = 10,659. The last 10 bases cover 10,660 to 10,669. Check: 90 + 10 = 100 bases of read, and 90 + 351 + 10 = 451 bases of genome. The annotated intron runs from 10,306 to 10,656, which is 10,656 − 10,306 + 1 = 351 bases: the same length as the skip, shifted by three bases.

The few bases' difference at each edge is normal: the same short sequence can sit on either side of a splice site. Column 5 (60) is the mapping quality: the aligner is confident the read belongs here and nowhere else.

Aligning reads to a genome · LSMA free course Gene prC on chrP with exon 1 ending at 10,305 and exon 2 starting at 10,657. Below, read ctrl_1.2608 aligns as 90 matched bases from 10,219 to 10,308, a skip of 351 bases from 10,309 to 10,659, and 10 matched bases from 10,660 to 10,669. 1 · GENE prC IN THE ANNOTATION exon 1exon 2 ends 10,305 intron 10,657 2 · ONE READ, CIGAR 90M351N10M 90M351N10M 10,219–10,308skipped: 10,309–10,65910,660–10,669
The read's two blocks sit on the two exons, and the dashed skip matches the intron, give or take three bases at the edges. Positions are drawn to scale; the read and exon heights are not.
Go deeper: mapping quality means different things in different aligners

In principle, column 5 is a Phred-scaled probability that the read is in the wrong place, by the same formula as base qualities: 60 would mean one chance in a million. In practice each aligner computes it in its own way. HISAT2 uses only a few values: 60 for a read with one good placement, and much lower values for reads with several candidate places. STAR gives 255 to reads it places uniquely, even though the SAM specification reserves 255 to mean that no quality is available.

This matters when you filter. A rule such as “keep reads with mapping quality of at least 30”, copied from a DNA pipeline built on BWA, happens to keep STAR's unique reads, but the same number can mean something quite different in another aligner. Check your aligner's manual before choosing a cut-off.

Mapping quality also explains what happens next to reads with a low score. A read from a gene that has a near-identical copy elsewhere, such as a recently duplicated gene, can fit two places equally well, so its mapping quality is low. By default the counting tool in Counting reads per gene leaves such multi-mapping reads uncounted, which is safe but means genes with close copies get fewer reads than they really have.

Key point

Always use the genome and the annotation from the same release (for example Ensembl GRCh38 release 112) and record it. Mixing releases misplaces genes.

GRCh38 is the current build of the human reference genome; the previous build, GRCh37 (also called hg19), places the same gene at different coordinates. An annotation made for one build applied to alignments made on the other puts gene boundaries in the wrong places, and many reads end up counted against the wrong gene or none. The release number matters too: each Ensembl release can add, remove or redraw genes, so record both the build and the release.

A related pitfall is chromosome names. Ensembl names human chromosome 1 1, while UCSC names it chr1. If the genome and the annotation use different styles, the counting step finds no genes on any chromosome and every count is zero, without an obvious error.

Further reading · Module 5Conesa et al. 2016: the whole RNA-seq workflow, from design to interpretation. · All reading for this course

Check your understanding

1. Why does RNA-seq need a splice-aware aligner?

2. What does N mean in a CIGAR string?

3. A human RNA-seq sample aligns at 40%. What do you check?

Up next

Lesson 5

Counting reads per gene

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.