Many genes at once: limma

Week 3 — Many genes at once: limma

Quantitative biology and integrative omics

Problem: which of 20 000 genes respond to the treatment?

  • An omics experiment gives a table: one row per gene (or protein, metabolite), one column per sample, on a log2 scale. Plus a sample sheet: batch, sex, treatment.
  • For one gene you already know what to do: fit lm(expr ~ batch + sex + treatment) and read the treatment coefficient. On the log2 scale it is a log2 fold change (logFC): 1 = doubled, -1 = halved.
  • Now we need that answer for every gene, and a list of genes we can trust.

A loop of lm over the genes runs, but gives poor answers

Simulated RNA-seq: 2000 genes, 12 samples, and we know that 200 genes truly respond to treatment.

y <- log2(counts + 1)                       # genes x samples, log2 scale
coef(summary(lm(y["g5", ] ~ batch + sex + treatment, data = samples)))["treatmenttreat", ]
#   Estimate Std. Error    t value   Pr(>|t|)
#       2.27       0.20      11.27    3.5e-06       # one gene: clearly up

p <- apply(y, 1, function(yg)               # the same fit for every row
  coef(summary(lm(yg ~ batch + sex + treatment, data = samples)))["treatmenttreat", 4])
sum(p < 0.05)
# [1] 277

277 genes with p < 0.05, but only 200 genes respond. Two problems:

  1. Many false hits: testing many genes lets chance through.
  2. Each gene’s noise is badly estimated: 12 samples is too few to know how noisy one gene is.

Testing many genes at once gives many false hits

  • p < 0.05 means: a gene with no real change still passes 5% of the time.
  • 1800 genes do not respond, so about 1800 \times 0.05 = 90 pass by chance. We got 91. With 20 000 genes it would be about 1000.
  • Fix: adjust the p-values for the number of tests, p.adjust(p, "BH"). Then among the genes called at < 0.05, only about 5% are false. limma reports this as adj.P.Val.
cut-off genes called truly respond false
p < 0.05 277 186 91
adjusted < 0.05 131 124 7
  • The list is now trustworthy, but only 124 of the 200 responding genes are found. Can we find more?

With few samples, each gene’s noise is badly estimated

  • Each gene’s noise comes from only 12 samples. By chance some genes look far too quiet (false hits) or too noisy (real changes missed).
  • limma’s eBayes uses that most genes are about equally noisy: each gene’s noise is pulled towards the typical gene. Each gene borrows information from all the others.
  • With adjusted p-values: the lm loop finds 124 of the 200 genes (7 false), limma finds 181 (10 false).

model.matrix() builds the design limma needs

d <- data.frame(
  treatment = c("ctrl", "ctrl", "ctrl", "treat", "treat", "treat"),
  sex       = c("F", "M", "F", "M", "F", "M"),
  bmi       = c(22, 25, 28, 24, 27, 30))

X <- model.matrix(~ treatment + sex, data = d)

Input: the table d

treatment sex bmi
1 ctrl F 22
2 ctrl M 25
3 ctrl F 28
4 treat M 24
5 treat F 27
6 treat M 30

→

Output: X, one row per sample

(Intercept) treatmenttreat sexM
1 1 0 0
2 1 0 1
3 1 0 0
4 1 1 1
5 1 1 0
6 1 1 1
  • Column names are the variable name glued to the level: treatmenttreat, sexM. limma asks for a coefficient by this name.
  • The reference levels (ctrl, F) get no column. Check levels() first: real data often has trt and untrt, and trt sorts first. Fix with relevel(x, ref = "untrt").

Check the rank before you fit

Now suppose all controls were sequenced in batch A and all treated samples in batch B.

d$batch <- c("A", "A", "A", "B", "B", "B")
X <- model.matrix(~ batch + treatment, data = d)
X[, ]
#   (Intercept) batchB treatmenttreat
# 1           1      0              0
# 2           1      0              0
# 3           1      0              0
# 4           1      1              1
# 5           1      1              1
# 6           1      1              1

ncol(X)                 # 3 coefficients to estimate

# the rank: how many columns are independent? Three ways, same answer
qr(X)$rank              # 2   (base R, as in Linear systems in R)
Matrix::rankMatrix(X)   # 2   (Matrix package, comes with R)
limma::is.fullrank(X)   # FALSE  -> rank < ncol(X): no unique solution
  • batchB and treatmenttreat are identical columns: perfect confounding (Confounding & Simpson’s paradox).
  • limma then warns Coefficients not estimable: treatmenttreat and gives NA. Dropping batch silences it but does not fix it: only a new experiment with both conditions in every batch can.
  • Rank < ncol(X) means infinitely many \boldsymbol\beta fit equally well, as in Linear systems in R. Use whichever function you like: qr(X)$rank, Matrix::rankMatrix, or limma’s yes/no version is.fullrank.

Limma fits all genes with one design in three calls

library(limma)
# counts: 2000 genes x 12 samples; samples: one row per sample (batch, sex, treatment)
design <- model.matrix(~ batch + sex + treatment, data = samples)
v   <- voom(counts, design)       # RNA-seq counts -> log2 scale, with weights
fit <- lmFit(v, design)           # one linear model per gene
fit <- eBayes(fit)                # share noise information across genes
topTable(fit, coef = "treatmenttreat", number = 3)
#      logFC AveExpr     t  P.Value adj.P.Val
# g5    2.20   10.83  8.05  1.0e-12   2.0e-09
# g188 -1.97   10.02 -6.88  3.8e-10   2.2e-07
# g145 -1.87   11.57 -6.86  4.2e-10   2.2e-07
  • lmFit is the loop, eBayes the shared noise, topTable the adjusted list. coef = is a column name of X: treatmenttreat.
  • voom is only needed for RNA-seq counts: it takes the log2 and records how noisy each value is. Data that are already log2 intensities (proteomics, metabolomics) go straight into lmFit.

Reading the results table

tt <- topTable(fit, coef = "treatmenttreat", number = Inf)   # all genes, best first
head(tt, 5)
#      logFC AveExpr     t  P.Value adj.P.Val
# g5    2.20   10.83  8.05 1.01e-12  2.03e-09
# g188 -1.97   10.02 -6.88 3.84e-10  2.19e-07
# g145 -1.87   11.57 -6.86 4.19e-10  2.19e-07
# g144  1.94    9.82  6.83 4.90e-10  2.19e-07
# g17   1.96    9.64  6.80 5.51e-10  2.19e-07
sum(tt$adj.P.Val < 0.05)
# [1] 188
column meaning
logFC the treatment coefficient: treat vs ctrl, holding batch and sex fixed
AveExpr the gene’s average log2 expression
t, P.Value the test of “coefficient = 0” for this gene
adj.P.Val adjusted for testing 2000 genes: about 5% of the genes called at < 0.05 are false
  • g188: logFC -1.97, so about four times lower in treated samples (2^{-2} = 1/4).
  • This is simulated data with a known truth: 200 genes respond. The 188 calls are 177 true and 11 false.

Do not add more covariates than you need to the model

One gene in 8 mice. The sample sheet has sex, BMI and sequencing batch. Should batch go in the model?

treatment sex bmi batch expr
ctrl F 26 A 8.9
ctrl M 23 B 9.5
ctrl F 28 C 8.5
ctrl M 27 A 9.2
treat F 30 B 10.2
treat M 27 C 10.7
treat F 21 A 9.1
treat M 24 B 10.5
fit1 <- lm(expr ~ treatment + sex + bmi + batch, data = d)
fit2 <- lm(expr ~ treatment + sex + bmi, data = d)
confint(fit1); confint(fit2)   # 95% intervals

  • batchB and batchC are close to 0, with intervals on both sides of 0: batch explains nothing here.
  • Yet its two columns use up 2 of the 8 samples. The confidence interval (the range of values that fit the data) for treatment gets wider and now covers 0. Without batch, the treatment effect is clear. If batch explains nothing, leave it out.

Limma also works for metabolomics, and beyond

Mass spectrometry gives log2 intensities directly, so there is no voom. In the exercise, 150 metabolites in 8 healthy and 8 disease samples; the patients have a higher BMI, and 20 metabolites truly change with disease.

design <- model.matrix(~ group + sex + bmi + day, data = meta)
fit <- eBayes(lmFit(logint, design))
topTable(fit, coef = "groupdisease")
design called true false
~ group 28 15 13
~ group + sex + bmi + day 12 12 0
  • bmi is a continuous covariate: one column of numbers. day is the MS run day, a batch.
  • Without bmi, metabolites that only follow BMI look like disease markers.

Photo: Nick Birse, CC BY-SA 4.0, via Wikimedia Commons.

Summary

  • An omics table is many genes with one design: the same X for every row, built with model.matrix(~ ...).
  • A plain loop of lm has two problems: many false hits and badly estimated noise per gene.
  • limma: log2 data (voom for counts) + design → lmFit() (the loop) → eBayes() (shared noise) → topTable(coef = ...) (adjusted list).
  • Read logFC (1 = doubled) and adj.P.Val (about 5% of the calls at < 0.05 are false).
  • Before fitting: check levels() and the rank of X. “Coefficients not estimable” can only be fixed by the experiment.
  • Include the covariates that explain the data; each extra one costs precision.

Next: Bulk RNA-seq with DESeq2: the same design, but starting from the raw counts.