Which processes does dexamethasone change? This lesson tests GO terms two ways: over-representation in a list of significant genes, and GSEA across every gene, ranked.
This lesson follows Volcano plot and interpretation, which ended with a first Gene Ontology test of the genes higher after dexamethasone. Here you look harder at that test, see where it falls short, and meet the method that answers its main weakness: gene set enrichment analysis.
Over-representation, revisited
The test in Volcano plot and interpretation is called over-representation analysis (ORA). It takes a list of significant genes and asks, for each GO term, whether the list holds more of the term's genes than a random draw would. Everything starts from the airway results, built exactly as in RNA-seq counts and exploration and Differential expression with DESeq2.
library(airway); library(DESeq2)
library(org.Hs.eg.db); library(clusterProfiler); library(enrichplot)
data("airway")
counts <- assay(airway)
coldata <- as.data.frame(colData(airway))
coldata$dex <- relevel(coldata$dex, ref = "untrt")
dds <- DESeqDataSetFromMatrix(counts, coldata, design = ~ cell + dex)
dds <- dds[rowSums(counts(dds) >= 10) >= 4, ]
dds <- DESeq(dds)
res <- results(dds, contrast = c("dex", "trt", "untrt"), alpha = 0.05)
res_shrunk <- lfcShrink(dds, coef = "dex_trt_vs_untrt", type = "apeglm", res = res)
df <- as.data.frame(res_shrunk)
df <- df[!is.na(df$padj), ]
df$status <- "Not significant"
df$status[df$log2FoldChange > 1 & df$padj < 0.05] <- "Up"
df$status[df$log2FoldChange < -1 & df$padj < 0.05] <- "Down"
up <- rownames(df)[df$status == "Up"]
ego <- enrichGO(gene = up, universe = rownames(df), OrgDb = org.Hs.eg.db,
keyType = "ENSEMBL", ont = "BP")
ego_all <- enrichGO(gene = up, OrgDb = org.Hs.eg.db,
keyType = "ENSEMBL", ont = "BP") # no universe
nrow(as.data.frame(ego)); nrow(as.data.frame(ego_all))
head(as.data.frame(ego)[, c("Description", "GeneRatio", "BgRatio", "p.adjust")], 3)
head(as.data.frame(ego_all)[, c("Description", "GeneRatio", "BgRatio", "p.adjust")], 3)The pipeline gives 412 "Up" genes, as before. With the tested genes as the universe, 73 terms reach an adjusted p-value below 0.05. Leaving the universe out, so that clusterProfiler uses every human gene with a GO annotation, gives 136 terms and smaller p-values for the same top terms. The extra terms are not extra biology.
Worked example · why the universe changes the answer
Take the top term, extracellular matrix organization. GeneRatio is term genes in your list over your list's annotated genes; BgRatio is term genes in the background over annotated background genes.
tested universe: 241 ÷ 11,690 = 2.1% → 6.6 ÷ 2.1 = 3.2-fold
whole genome: 394 ÷ 21,273 = 1.9% → 6.6 ÷ 1.9 = 3.6-fold
Step by step. Only 346 of the 412 up genes have a biological-process annotation, so 346 is the list size the test uses; likewise 11,690 of the 15,826 tested genes. The term's share of the list is the same both times. What changes is the comparison: the whole genome includes genes that are not expressed in airway muscle cells and so could never have been in the list.
What the answer means. The wrong background makes the expected share too small and the enrichment look larger and more certain than it is. The universe should be every gene that could have been called significant: the genes you tested.
The limits of a cut-off
ORA needs a list, and a list needs thresholds. Change them and the answer changes. Using a 1.5-fold cut-off (log2FoldChange > log2(1.5)) with the same padj of 0.05 gives 924 up genes instead of 412, and enrichGO() then returns 305 terms instead of 73. Of the original 73, 61 are still there. The core finding is stable, but the size of the answer depends on a choice that is a convention, not a fact.
A threshold also throws information away. A gene at padj 0.049 counts fully; one at 0.051 counts not at all. And a process can change through many genes that each move a little, none of them passing the cut-off. ORA cannot see that pattern. Both problems come from turning a ranked table of 16,000 genes into a yes-or-no list.
GSEA: rank every gene
Gene set enrichment analysis (GSEA) drops the list. It ranks all tested genes from most increased to most decreased and asks, for each gene set, whether its genes sit near the top or the bottom of the ranking more than chance would place them. No cut-off is needed.
The ranking needs a number per gene that combines direction and strength. The Wald statistic, stat in res, is a good choice: it is the fold change divided by its standard error, so a gene ranks high only if it changed a lot and was measured well. The shrunken log2 fold change from res_shrunk is the other common choice, discussed below.
ranks <- res$stat
names(ranks) <- rownames(res)
ranks <- sort(ranks[!is.na(ranks)], decreasing = TRUE)
length(ranks)
set.seed(42)
gse <- gseGO(geneList = ranks, OrgDb = org.Hs.eg.db, keyType = "ENSEMBL",
ont = "BP", minGSSize = 15, maxGSSize = 500,
pvalueCutoff = 0.05, eps = 0, seed = TRUE)
gt <- as.data.frame(gse)
gt <- gt[order(gt$p.adjust), ]
nrow(gt); sum(gt$NES > 0); sum(gt$NES < 0)
head(gt[gt$NES > 0, c("Description", "setSize", "NES", "p.adjust")], 4)
head(gt[gt$NES < 0, c("Description", "setSize", "NES", "p.adjust")], 4)What this does
res$statholds the Wald statistic for each gene, named by Ensembl ID.!is.na()drops any gene without a statistic, leaving 16,139. That is more than the 15,826 genes indf, because genes given an NA padj by independent filtering in Differential expression with DESeq2 still have a statistic, and GSEA can use them.sort(..., decreasing = TRUE)puts the most increased genes first, whichgseGO()requires.gseGO()runs GSEA on GO biological-process terms, using the fgsea package underneath. LikeenrichGO(), it reads the annotation from org.Hs.eg.db on your computer, so nothing is downloaded.minGSSize = 15andmaxGSSize = 500skip terms with fewer than 15 or more than 500 of your genes: very small sets are noisy and very large ones too general to interpret.- GSEA estimates p-values by random permutation, so
set.seed(42)andseed = TRUEmake the result repeatable.eps = 0asks fgsea to estimate very small p-values exactly rather than stopping at a floor.
R also warns that a few ranks are tied (6 of 16,139); their order is arbitrary, which barely matters at this size. 258 terms pass, 162 with genes concentrated at the top of the ranking and 96 at the bottom. GSEA tests both directions at once, which ORA on the up genes alone could not.
Reading GSEA results
Three columns carry most of the meaning.
- NES, the normalised enrichment score. Positive means the set's genes gather among the increased genes, negative among the decreased. The score is scaled to allow for set size, so sets can be compared; in this table its size runs from about 1.5 to 2.2. It measures how concentrated the set is at one end, not how large the fold changes are.
- p.adjust, adjusted with Benjamini–Hochberg across all the terms tested, just as
padjwas adjusted across genes. Thousands of terms are tested, so raw p-values would give many false hits. - The leading edge: the genes that drive the score, those ranked before the point where the set's running score peaks. They are listed in
core_enrichment.
The glucocorticoid term is a good check that the method sees the expected biology:
gc_term <- gt[grepl("glucocorticoid", gt$Description),
c("ID", "Description", "setSize", "NES", "p.adjust", "leading_edge")]
gc_term
le <- strsplit(gt[gc_term$ID[1], "core_enrichment"], "/")[[1]]
mapIds(org.Hs.eg.db, keys = le, keytype = "ENSEMBL", column = "SYMBOL")
gseaplot2(gse, geneSetID = gc_term$ID[1], title = gc_term$Description[1])"Cellular response to glucocorticoid stimulus" has 43 genes in the ranking, an NES of 1.81 and an adjusted p-value of 0.025. In the leading_edge summary, tags=35% means 35% of the set's genes are in the leading edge, and list=9% that the peak is reached within the top 9% of the ranking. Those 15 genes include KLF9, DDIT4 and ZFP36, all known to respond to glucocorticoids. gseaplot2() draws the running score climbing to its peak, with a tick for each set gene along the ranking. Always read the leading-edge genes: they tell you whether a term rests on genes you believe.
The top of the table also shows redundancy. "D-glucose import", "regulation of D-glucose import" and "D-glucose transmembrane transport" largely share genes, because GO terms are nested, broad terms containing narrower ones. Three rows are one finding. clusterProfiler's simplify() removes terms that overlap strongly in meaning with a better-scoring one:
gse_s <- simplify(gse, cutoff = 0.7, by = "p.adjust", select_fun = min) nrow(as.data.frame(gse_s))
The result is [1] 142: 258 terms become 142. Of each group of similar terms (a semantic similarity above 0.7), the one with the smallest adjusted p-value is kept, so "D-glucose import" stays and "D-glucose transmembrane transport" goes. Simplify for reading and reporting; the adjusted p-values themselves were computed over the full set of terms and do not change.
Which ranking?
Ranking by the shrunken log2 fold change from res_shrunk (for the 15,826 genes in df) gives 82 significant terms, 73 of them also found with the Wald statistic. The shrunken fold change ranks by size of effect, the statistic by strength of evidence; both are defensible, and the core terms agree. Choose one before you look, report it, and never rank by unshrunken fold changes, which put noisy low-count genes at both ends.
Go deeper: fgsea directly, and other gene set collections
gseGO() builds GO gene sets and passes them to fgsea. You can call fgsea::fgsea(pathways, stats = ranks) yourself with any named list of gene sets, where each element is a vector of IDs matching the names of ranks. That is how you would test your own sets, or curated collections such as MSigDB's hallmark sets, which the msigdbr package provides as tables.
Whatever the collection, the same rules apply: IDs in the sets and in the ranking must be the same type, set sizes should be limited, and the adjustment is across all the sets you tested. Testing GO, then hallmarks, then pathways, and reporting whichever looked best is the multiple-testing problem again, one level up.
Common mistakes
- No universe in ORA. The default background is the whole annotated genome, which inflates enrichment. Pass the tested genes.
- An unsorted or unnamed ranking.
gseGO()needs a named vector sorted from high to low. - Ranking by padj or p-value. These lose the direction, so increased and decreased genes are mixed together. Use a signed value such as
stat. - Reading ten nested terms as ten discoveries. Group them, or use
simplify(). - Trusting a term without its genes. Check the leading edge or
geneIDfor the genes behind each claim.
GSEA was introduced in Subramanian et al. 2005, "Gene set enrichment analysis: a knowledge-based approach for interpreting genome-wide expression profiles" (PNAS). The fast algorithm used here is from Korotkevich et al. 2019, "Fast gene set enrichment analysis" (bioRxiv preprint). clusterProfiler 4.0 is described in Wu et al. 2021 (The Innovation). Timmons, Szkop & Gallagher 2015 (Genome Biology) show how the wrong background distorts enrichment results. Report the ranking metric, the gene set collection and the versions of org.Hs.eg.db and GO.db, because annotations change with every release.
Key point
ORA asks whether a term is over-represented in a thresholded list, and depends on the threshold and the universe. GSEA asks whether a term's genes cluster at one end of the full ranking, and needs no threshold. Use the tested genes as the universe, rank by a signed statistic, and read the leading edge.
That completes the tools of this module. The project, Counts to results, asks you to run them yourself, from a count file to a written summary.
Further reading · Module 4Love, Huber & Anders 2014: the method, and why it shrinks fold changes. · All reading for this course
Check your understanding
1. Why should the universe in enrichGO() be the genes you tested?
2. What does GSEA use instead of a list of significant genes?
3. A GSEA term has NES = -1.9 and p.adjust = 0.002. What does that mean?