LSMA

LONDON SCHOOL OF MULTIOMICS & AI

LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 79 lessons

Counting without aligning: Salmon and kallisto

Counting without aligning: Salmon and kallisto

Counting without aligning: Salmon and kallisto

Salmon and kallisto skip exact alignment and ask which transcripts each read could have come from. Learn how equivalence classes and the EM step turn that into counts and TPM, and how to get back to genes.

In Counting reads per gene you placed every read on the genome first and then counted which gene's exons it landed in. This lesson shows a faster route that skips the exact placement altogether, and asks a slightly different question: not where each read sits, but which transcripts it could have come from.

Why skip alignment

To count reads per gene, you do not really need to know that a read starts at base 10,219 with a CIGAR string of 90M351N10M. You only need to know which gene, or which transcript, it belongs to. Salmon and kallisto are built on that observation. Instead of the genome, they use the transcriptome: a FASTA file with the sequence of every annotated transcript, already spliced, so a read from an exon–exon junction matches the transcript in one piece. They then estimate how many reads came from each transcript.

The gains are large. A sample that takes an hour to align with a splice-aware aligner can be quantified in a few minutes on a laptop, with a few gigabytes of memory. And because the tools work with transcripts rather than genes, they can share out reads between the different isoforms of a gene: the alternative transcripts made from one gene by splicing in different exons, which counting per gene, as in Counting reads per gene, adds together.

There are costs too. You get no BAM file, so there is nothing to look at in a genome browser and nothing to call variants from. The tools can only find transcripts that are already in the annotation: reads from an unannotated gene or a new splice form have nowhere correct to go. Everything depends on the annotation, which is why its release must match the genome, as Aligning reads to a genome stressed.

Building a transcriptome and an index

For human or mouse, Ensembl and GENCODE publish ready-made transcriptome FASTA files for every release. For the practice data you can make one from the genome and the annotation with gffread, which cuts out each transcript's exons and joins them. The quantifier then builds an index from the transcript sequences, much as HISAT2 indexed the genome.

Terminal
conda create -n quant -c conda-forge -c bioconda salmon kallisto gffread
conda activate quant
gffread -w index/transcripts.fa -g data/practice_genome.fa data/practice_genes.gtf
grep -c ">" index/transcripts.fa

salmon index -t index/transcripts.fa -i index/salmon_practice -k 31
salmon quant -i index/salmon_practice -l A -p 2 \
      -1 trimmed/ctrl_1_R1.fastq.gz -2 trimmed/ctrl_1_R2.fastq.gz -o quant/ctrl_1

kallisto index -i index/kallisto_practice.idx index/transcripts.fa
kallisto quant -i index/kallisto_practice.idx -o quant_k/ctrl_1 -t 2 \
      trimmed/ctrl_1_R1.fastq.gz trimmed/ctrl_1_R2.fastq.gz

What this does

  • A separate environment, quant, keeps these tools apart from the hts environment, so updating one cannot break the other.
  • gffread -w writes the spliced sequence of each transcript in the GTF, taken from the genome given with -g. Each FASTA header is the transcript ID. grep -c ">" counts the headers, which is the number of transcripts.
  • salmon index builds Salmon's index from the transcripts. -k 31 sets the length of the short words, the k-mers, used to find candidate matches; 31 is the default and suits reads of 75 bases or more.
  • salmon quant quantifies one sample from its trimmed read pairs. -l A asks Salmon to work out the library type, including the strandedness from Counting reads per gene, from the reads themselves. The results go into the folder named with -o.
  • kallisto index and kallisto quant do the same with kallisto. Given two read files, kallisto treats them as a pair.

Reading the output

Salmon writes a table called quant.sf with one row per transcript and five columns: Name, Length, EffectiveLength, TPM and NumReads. kallisto writes abundance.tsv with the same information under different names: target_id, length, eff_length, est_counts and tpm. Check the mapping rate before anything else: Salmon records it in logs/salmon_quant.log, and kallisto prints how many reads it pseudoaligned. Like the alignment rate in Aligning reads to a genome, a rate far below the other samples' points to contamination, the wrong transcriptome or a library problem. Salmon's lib_format_counts.json also says which library type it detected; check that it matches the kit.

Counting without aligning: Salmon and kallisto · LSMA free course Two isoforms of one gene share exons 1 and 3; isoform A also has exon 2. A read inside exon 2, or across the junction of exons 1 and 2, fits only isoform A. A read across the junction of exons 1 and 3 fits only isoform B. A read inside exon 1 fits both, so it belongs to the equivalence class A and B. WHICH ISOFORMS COULD EACH READ COME FROM? isoform A isoform B exon 1exon 2exon 3 exon 1exon 3 reads peach: fits A and B · vermilion: fits A only · navy: fits B only
The read in exon 1 is shared; the read in exon 2 and the one across the exon 1–2 junction fit only isoform A; the navy read crosses the exon 1–3 junction, which exists only in isoform B. Shared reads are divided between isoforms by the EM step described below. Schematic, not to scale.

Pseudoalignment, selective alignment and the EM step

Both tools start the same way. They break each read into k-mers and look them up in the index to find which transcripts contain them. A read whose k-mers all occur in isoforms A and B, but in no other transcript, is compatible with A and B. The set of transcripts a read is compatible with is its equivalence class. Millions of reads fall into far fewer classes, so the tools only need to remember how many reads each class has, which is one reason they are so fast.

The two tools differ in how strictly they check a match. kallisto uses pseudoalignment: if the read's k-mers are consistent with a set of transcripts, that set is the answer, and no base-by-base alignment is done. Salmon, in its default selective alignment mode, finds candidate transcripts the same fast way, then scores a real alignment of the read against each candidate and keeps only those that score well. The extra step costs a little time and removes some false matches, for example a read that shares many k-mers with a transcript but has too many mismatches to really come from it.

Sharing out the shared reads

Reads that fit one transcript only are easy. The work is in the reads that fit several. Both tools use the expectation–maximisation (EM) algorithm: start with a guess of each transcript's abundance, give each shared read to its compatible transcripts in proportion to that guess, add up the result to make a better guess, and repeat until the numbers stop changing. Transcripts with plenty of unique evidence end up taking most of the shared reads, which is what you would expect if the shared reads were drawn from the same mixture.

Worked example · the EM step for two isoforms

reads unique to A: 300 · unique to B: 100 · shared by A and B: 400 (equal effective lengths)
guess 1: A 50%, B 50% → A = 300 + 200 = 500 · B = 100 + 200 = 300
guess 2: A 62.5% → A = 300 + 250 = 550 · B = 100 + 150 = 250
repeat until stable → A = 600, B = 200

Step by step. Start by assuming the two isoforms are equally abundant, so each takes half of the 400 shared reads: A has 300 + 200 = 500 and B has 100 + 200 = 300. A now looks like 500 ÷ 800 = 62.5% of the gene, so the next round gives A 62.5% of the shared reads, 250, and B the other 150. Repeating moves A's share towards 75%. At 75% the answer no longer changes: A gets 300 + 0.75 × 400 = 600 and B gets 100 + 0.25 × 400 = 200, and 600 ÷ 800 is again 75%.

What the answer means. The shared reads were divided in the same 3 : 1 ratio as the unique reads, because the unique reads were the only evidence about the mixture. If the two isoforms differed in length, each share would also be weighted by length, as the next section explains. When the unique evidence is thin, many different splits fit the data almost equally well, and the estimate for each isoform is uncertain even though the gene total is not.

Effective length and decoys

A longer transcript produces more fragments, so its reads must be weighed against its length. The length that matters is the effective length: the number of positions where a fragment could start and still fit inside the transcript. With fragments of about 250 bases, a 300-base transcript has only around 50 such positions, while a 3,000-base transcript has nearly 2,750. Both tools estimate the fragment length distribution from the read pairs and use it to compute effective lengths, which is why short transcripts can have an effective length far below their real length.

Because the tools only know the transcriptome, a read from somewhere else in the genome, such as an intron, an unannotated gene or a pseudogene, may be forced onto the most similar transcript. Salmon can guard against this with decoys: you add the genome sequence to the index and list its chromosomes as decoys, and a read that matches the genome better than any transcript is then set aside instead of being counted. The Salmon documentation describes how to build this combined index. kallisto does not use decoys in the same way.

Go deeper: bias corrections and bootstraps

Library preparation does not sample every part of a transcript evenly. Fragments rich in G and C, or starting with certain sequences, are over- or under-represented. Salmon can learn and correct both patterns with --gcBias and --seqBias, which changes the effective lengths rather than the counts. These corrections help most when samples were prepared in different batches with different biases.

Both tools can also repeat the quantification on resampled data: --numBootstraps in Salmon and -b in kallisto. The spread of the bootstrap estimates shows how uncertain each transcript's number is. Tools for testing differential transcript use can take that uncertainty into account, so that an isoform whose estimate swings widely is not reported as changed.

TPM, counts and getting back to genes

Each tool reports two numbers per transcript, and they are for different jobs. The estimated count (NumReads or est_counts) is the number of reads given to the transcript after the EM step. It need not be a whole number, because shared reads are split. TPM, transcripts per million, adjusts for length: divide each transcript's count by its effective length to get a rate, then scale the rates so they add up to one million in every sample.

For example, take a sample with just two transcripts. Transcript A has 500 reads and an effective length of 1,000 bases; B has 500 reads and an effective length of 2,000. The rates are 0.5 and 0.25 reads per base, adding up to 0.75. A's TPM is 0.5 ÷ 0.75 × 1,000,000 ≈ 666,667, and B's is about 333,333. The counts are equal, but A is twice as abundant as a molecule, because it produced as many reads from half the length.

TPM is good for comparing transcripts within one sample, and for figures. It is not what a differential expression test should get. Tests such as DESeq2 or edgeR model counts, and their own normalisation deals with depth and composition, the problem shown in Counting reads per gene. So the estimated counts go into the test, and TPM stays out of it.

From transcripts to genes

Most analyses still ask about genes. To get gene-level numbers, add up the estimated counts of each gene's transcripts. That needs a table linking every transcript ID to its gene ID, which the GTF already contains:

Terminal
awk -F'\t' '$3=="exon" {
       match($9, /transcript_id "[^"]+"/); t = substr($9, RSTART+15, RLENGTH-16)
       match($9, /gene_id "[^"]+"/);       g = substr($9, RSTART+9,  RLENGTH-10)
       print t "\t" g }' data/practice_genes.gtf | sort -u > index/tx2gene.tsv
Python
import pandas as pd

quant = pd.read_csv("quant/ctrl_1/quant.sf", sep="\t", index_col="Name")
tx2gene = pd.read_csv("index/tx2gene.tsv", sep="\t", header=None,
                      names=["transcript", "gene"], index_col="transcript")

gene_counts = quant["NumReads"].groupby(tx2gene["gene"]).sum()
print(gene_counts.round(1))

What this does

  • The awk program reads column 9 of each exon line and pulls out the transcript ID and the gene ID, using the same match and substr pattern as Working with genomic intervals: BED files, overlaps and coverage; transcript_id " is 15 characters long. sort -u keeps one line per transcript.
  • In Python, pd.read_csv reads Salmon's table with the transcript names as the row index, and the link table with transcript IDs as its index.
  • groupby(tx2gene["gene"]) lines each transcript up with its gene by matching the two indexes, and .sum() adds the counts within each gene.

For the practice data, the gene totals should be close to the featureCounts numbers from Counting reads per gene, but not identical: the two methods treat reads at gene edges, ambiguous reads and multi-mapping reads differently. Comparing the two for a few genes is a good check that the transcriptome and the annotation match. Simply adding counts ignores one subtlety: if a gene switches to a longer isoform between conditions, it produces more reads without more molecules. The R package tximport, and the Python package pytximport, can pass a length correction on to the test for that reason.

RouteGives youBest when
HISAT2 or STAR, then featureCountsBAM files and gene countsYou also need to view reads, call variants or find new splice sites
Salmon (selective alignment, with decoys)Transcript counts and TPMExpression is the goal, and you want speed with a careful match
kallisto (pseudoalignment)Transcript counts and TPMExpression is the goal, and speed and simplicity matter most

How to read this table

Each row is one route from FASTQ files to numbers per gene or transcript. For gene-level differential expression the three routes usually agree well; the choice depends on what else you need. The nf-core RNA-seq pipeline from Beyond RNA-seq: other assays and pipelines runs alignment and Salmon side by side for that reason. The same ideas, k-mer matching and equivalence classes, also power the fast single-cell tools in Single-cell data: from counts to cell types.

kallisto is described by Bray, Pimentel, Melsted and Pachter (2016, Nature Biotechnology), and Salmon by Patro, Duggal, Love, Irizarry and Kingsford (2017, Nature Methods). Srivastava et al. (2020, Genome Biology) compared alignment and mapping approaches and showed how reads from outside the annotated transcriptome can distort estimates, the problem decoys address. Soneson, Love and Robinson (2015, F1000Research) set out why gene-level counts from transcript estimates need care. Report the tool version, the transcriptome release and whether decoys or bias corrections were used.

Common mistakes

  • Putting TPM values into a differential expression test instead of estimated counts.
  • Building the index from one annotation release and summarising to genes with another.
  • Trusting isoform-level numbers for genes with little unique evidence, without looking at bootstraps.
  • Not checking the mapping rate and the detected library type before moving on.

Key point

Salmon and kallisto answer "how much of each transcript?", not "where exactly is each read?". If expression is all you need, they are fast and accurate; if you also need the reads in place, align.

For example, a study of which genes respond to a drug in 200 samples can be quantified with Salmon in an afternoon. A study looking for a new fusion gene in a tumour cannot: a fusion is not in the annotated transcriptome, and only an alignment to the genome can show reads that join two genes.

Further reading · Module 4The Salmon documentation: library types, decoys and output files explained. · The kallisto manual: commands and options. · All reading for this course

Check your understanding

1. Which numbers from Salmon or kallisto should go into a differential expression test?

2. What is an equivalence class?

3. What do decoys do in a Salmon index?

Ask LSMA