Main Approaches
- Over-representation analysis (ORA)
- Gene Set Enrichment Analysis (GSEA) / Functional Class Scoring (FCS)
- Pathway Topology-based methods
Advanced R-course 2026
Bioinformatics Core Facility CECAD
2026-11-27
git clone https://github.com/CECADBioinformaticsCoreFacility/Advanced_R_course_2026.git
https://cecadbioinformaticscorefacility.github.io/Advanced_R_course_2026/
Session 5 :: Enrichment Analysis
What to do with the DE genes?
Are these genes associated with particular biological functions, pathways, or processes more than we’d expect by chance?
Advantages:
Limitations:
Note
A category is over-represented if it makes up a bigger share of your gene list than it does in the background set — and the p-value says how likely that is by chance.
Regrouping genes together in meaningful sets based on:
| Symbol | Description |
|---|---|
| \(N\) | Total number of genes (universe) |
| \(M\) | Number of DE genes (your list) |
| \(k\) | Number of genes in a category(GO term) |
| \(n\) | Number of DE genes found in that category |
Human curated:
Domains / Patterns:
Experimental:
Molecular Signatures Database (MSigDB) is a popular resource that compiles many gene sets from various sources.
GO provides a controlled vocabulary to describe gene functions.
Three main domains:
| Gene set (category) | Size \(k/N\) | DE genes \(n/M\) | Overrepresented ? |
|---|---|---|---|
| Category 1 | 59/20000 | 45/2000 | Yes/No |
| Category 2 | 150/20000 | 30/2000 | Yes/No |
| Category 3 | 300/20000 | 10/2000 | Yes/No |
| … | … | … | … |
| Symbol | Description |
|---|---|
| \(N\) | Total number of genes (universe) |
| \(M\) | Number of DE genes (your list) |
| \(k\) | Number of genes in a category(GO term) |
| \(n\) | Number of DE genes found in that category |
I have 1,086 “significant” genes — how was 1,086 decided?
Note
Choose thresholds before looking at enrichment results — filtering on what “looks interesting” after the fact biases everything downstream.
102 of my 1,086 DE genes are in “regulation of immune effector process”. Is that a lot?
Not yet — 102 means nothing until you know how big the set is. For every gene set, you count how many of your DE genes fall in it:
| Gene set (category) | Size \(k/N\) | DE genes \(n/M\) |
|---|---|---|
| regulation of immune effector process | 401/15,893 | 102/987 |
| regulation of lymphocyte activation | 491/15,893 | 113/987 |
| leukocyte cell-cell adhesion | 412/15,893 | 96/987 |
Important
Where did 987 come from? My list has 1,086 genes. enrichGO silently drops genes it can’t map to the OrgDb and genes with no BP annotation: 1,086 → 987 (DE list), 20,729 → 15,893 (universe). The test runs on the filtered lists. Always check: ego_result@result$DESize and $UniverseSize — if they differ from your input lengths, the filter did its work.
Warning
Gene IDs must match the database key type. enrichGO(..., keyType = "SYMBOL") means your list has to contain gene symbols. A list of Entrez IDs “succeeds” with 0 hits — the most common silent failure in ORA.
If DE genes fell into gene sets at random, how many would I expect to see in “regulation of immune effector process”?
The background (universe) of \(N = 15893\) genes contains \(k = 401\) regulation of immune effector process genes. If my \(M = 987\) DE genes are a random draw from the universe, the expected overlap is:
\[ E = \frac{M \times k}{N} = \frac{987 \times 401}{15893} = 24.9 \]
I observed 102 — about 4 times the expectation. Now I need to know whether that gap is statistically significant.
Note
The choice of background changes the answer: genome-wide (\(N = 20000\)) vs. genes detected in your experiment (e.g., \(N = 15893\)). The detected-genes background removes detection bias; pick one and be consistent.
102 observed vs 24.9 expected — but is that gap luck or signal?
The comparison is laid out as a 2×2 contingency table:
| In gene set | Not in gene set | |
|---|---|---|
| DE | 102 | 885 |
| Not DE | 299 | 15594 |
Fisher’s exact test asks: given the margins are fixed, what is the probability of an overlap at least as extreme as 102 by chance?
Mathematically, that is the hypergeometric test — “drawing \(M = 987\) balls from an urn of \(N = 15893\) that holds \(k = 401\) marked balls”:
\[ P(X \geq n) = \sum_{i=n}^{M} \frac{\binom{k}{i} \, \binom{N-k}{M-i}}{\binom{N}{M}} \]
Tip
Same test, different names: Fisher’s exact test on the contingency table ≡ hypergeometric test on the urn problem. The next two slides work through it in R.
# Define parameters
N <- 15893 # Total number of genes
k <- 401 # Genes in the GO term
M <- 987 # Total number of DE genes
n <- 102 # DE genes in the GO term
# = UniverseSize / DESize from the enrichGO result, after OrgDb + BP filtering
# Calculate p-value using hypergeometric test
p_value <- phyper(q = n - 1, # q = x-1 for upper tail
m = k, # number of white balls in population
n = N - k, # number of black balls in population
k = M, # number of draws
lower.tail = FALSE)
print(paste("P-value:", p_value))[1] "P-value: 4.25016412569541e-36"
Interpreting the P-Value
This p-value indicates the probability of observing 102 or more DE genes in the GO term by chance. A low p-value (e.g., < 0.05) suggests that the GO term is significantly over-represented among the DE genes.
Important
Remember to adjust for multiple testing when evaluating p-values across many GO terms.
More on Multiple testing
I just tested 4,949 gene sets at \(p < 0.05\). How many “significant” sets should I expect — even if the experiment was perfect?
\[ 4949 \times 0.05 = 247.45 \approx 247 \text{ false positives by chance alone} \]
So a raw p-value threshold guarantees a pile of false discoveries. Instead, control the False Discovery Rate with the Benjamini–Hochberg (BH) procedure:
Interpretation: at FDR 0.05, roughly 5% of the declared significant sets are expected to be false positives.
| Rank (\(i\)) | GO term | Raw pvalue | Raw BH: \(p \cdot \frac{m}{i}\), \(m=8\) | Cumulated p.adjust (running min)[BH] | FDR < 0.05? |
|---|---|---|---|---|---|
| 1 | Cell Cycle | 0.002 | 0.002 × 8/1 = 0.016 | 0.016 | Yes |
| 2 | DNA Replication | 0.008 | 0.008 × 8/2 = 0.032 | 0.032 | Yes |
| 3 | Mitosis | 0.012 | 0.012 × 8/3 = 0.032 | 0.032 | Yes |
| 4 | Apoptosis | 0.025 | 0.025 × 8/4 = 0.050 | 0.050 | Yes |
| 5 | Metabolism | 0.250 | 0.250 × 8/5 = 0.400 | 0.320 | No |
| 6 | Lipid transport | 0.240 | 0.240 × 8/6 = 0.320 | 0.320 | No |
| 7 | Protein folding | 0.600 | 0.600 × 8/7 = 0.686 | 0.686 | No |
| 8 | Circadian rhythm | 0.900 | 0.900 × 8/8 = 0.900 | 0.900 | No |
The table says “regulation of immune effector process, padj = 2.1e-32”. Now what?
ID / Description — which termpvalue → p.adjust — use the adjusted valueGeneRatio — your DE genes in the term / all DE genesgeneID — which of your DE genes drove the termBgRatio — genes in the term / all background genesTip
Significant ≠ important. A large, well-powered but tiny-effect process can out-test a small, central one. Let the biology, not just the p-value, drive your conclusions.
GO terms form a hierarchical structure.
Each GO term may have:
Relationships include:
Hierarchy is not strictly a tree; terms can have multiple parents.
Image source: https://www.nature.com/articles/nrc2036
Because children inherit from parents:
Use tools that:
Focus on most specific significant terms for clearer insights.
packages like topGO, ClueGO, GO-Bayes, GO-Elit etc. help address hierarchy issues.
clusterProfilertopGOGOstatsgprofiler2enrichRReactomePAfgsea (for GSEA)DOSE (for Disease Ontology)pathfindRWebGestaltRclusterProfiler# Load necessary libraries
library(clusterProfiler)
library(org.Mm.eg.db)
library(ggplot2)
library(dplyr)
library(DESeq2)
library(qs2)
set.seed(420)
dds <- qs_read("dds_all_genotype.qs")
uni_genes <- results(dds) |> as.data.frame() |>
filter(!is.na(padj)) |>
rownames()
de_genes_up <- results(dds) |> as.data.frame() |>
filter(!is.na(padj),padj <= 0.05, log2FoldChange > 0)|>
rownames()
de_genes_dn <- results(dds) |> as.data.frame() |>
filter(!is.na(padj),padj <= 0.05, log2FoldChange < 0)|>
rownames()
ego_result_up <- enrichGO(gene = de_genes_up,
universe = uni_genes,
OrgDb = org.Mm.eg.db,
keyType = "SYMBOL",
ont = "BP",
pAdjustMethod = "BH",
pvalueCutoff = 0.05,
qvalueCutoff = 0.2,
readable = TRUE)
# View results
#DT::datatable(head(ego_result_up))
# Visualize results
barplot(ego_result_up, font.size=14) +
ggtitle("GO Enrichment Analysis - Upregulated Genes : Barplot")# Add similarity matrix to the termsim slot of enrichment result
ego_result_up <- enrichplot::pairwise_termsim(ego_result_up)
# Enrichmap clusters the 30 most significant (by padj) GO terms
# to visualize relationships between terms
emapplot(ego_result_up, showCategory = 30) +
ggtitle("GO Enrichment Analysis - Upregulated Genes : Enrichment Map")Directional Gene Lists
One search :
Two searches:
Note
Quiz Time!
Quiz Time!