Lesson 4 of 9
Module 7 · From genes to function
A list of changed genes becomes meaningful when you ask what they share. Learn how GO, KEGG and Reactome group genes, how to test an overlap with the hypergeometric test, and why the background and multiple testing decide the answer.
From a gene list to a biological story
Many omics experiments end with a list. An RNA-seq study, starting from a count table like the one built in Module 5, might find 500 genes that change between treated and untreated cells. A list of 500 gene symbols is hard to read. The natural next question is: do these genes have something in common? Are many of them involved in the immune response, or in DNA repair, more than you would expect by chance?
This lesson also closes a loop opened earlier in the module. In Finding genes and annotating a genome, an annotation pipeline gave each predicted gene a name and, where possible, Gene Ontology terms; Protein families, domains and motifs added domains, which are one of the main sources of those terms. Enrichment analysis is where that annotation is put to work: it is only as good as the labels the earlier steps attached to each gene.
Answering that needs two things: a catalogue that says which genes belong to which biological process or pathway, and a statistical test that says when an overlap is surprising. This lesson covers both. The code uses SciPy and pandas, which Colab already has installed.
The catalogues: GO, KEGG and Reactome
A gene set is simply a named group of genes that share something, such as a role, a location in the cell or a pathway. Three resources supply most of the gene sets used in enrichment analysis.
| Resource | What it describes | How it is organised |
|---|---|---|
| Gene Ontology (GO) | What gene products do, in three aspects: biological process, molecular function and cellular component | Tens of thousands of terms, from very broad to very specific, linked in a hierarchy |
| KEGG | Pathways such as metabolism, signalling and disease, drawn as maps | Pathway maps with reference versions for many organisms |
| Reactome | Human biological processes as series of reactions, with links to other species | Expert-curated, peer-reviewed pathways nested inside larger ones |
How to read this table
Read across each row: the name, what kind of biology it captures, and how its contents are arranged.
- The Gene Ontology is a controlled vocabulary: an agreed list of terms with fixed definitions, so that "DNA repair" means the same thing in every database. An ontology also records how terms relate. A specific term such as "double-strand break repair" is a kind of "DNA repair", which is a kind of "DNA metabolic process". A term can have more than one parent, so the structure is a network rather than a simple tree. A gene annotated to a specific term is counted as belonging to all its parent terms too. Each annotation, the link between a gene and a term, carries an evidence code saying whether it came from an experiment or was inferred, much like the evidence you saw in UniProt in Reading a gene record and a protein record.
- KEGG (Kyoto Encyclopedia of Genes and Genomes) is best known for its hand-drawn pathway maps, in which each box is a gene product and arrows show reactions or signals. Its website is free to browse for academic use; bulk downloads need a licence.
- Reactome is free and open. Every pathway is built from individual reactions, and small pathways sit inside larger ones, so "DNA repair" contains several more specific repair pathways.
The three overlap but do not agree perfectly, and each changes with every release. Always record which resource and which version you used.
One bias runs through all three. Well-studied genes carry many annotations, and little-studied genes carry few or none. A gene like TP53 is annotated to a long list of processes because thousands of papers have examined it; a gene first described last year may have a single inferred term. So enrichment results lean towards biology that is already known, and a list full of poorly annotated genes can come back with nothing significant even when something real is happening. An empty enrichment result is therefore weak evidence that the list has no theme.
The founding papers are Ashburner et al. 2000, Nature Genetics, for the Gene Ontology, and Kanehisa & Goto 2000, Nucleic Acids Research, for KEGG. Reactome publishes regular updates in the same journal, for example Gillespie et al. 2022. Cite the founding paper and also state the release or download date you used, since terms and annotations change every few weeks. One caution on interpretation: many GO annotations are inferred electronically, without curator review. And a gene with no annotation to a term is not known to lack that function; often nobody has looked.
Over-representation analysis
Over-representation analysis (ORA) asks a simple question for each gene set: does my list contain more of its genes than I would expect if the list were a random draw? Four numbers are needed:
- N, the background or universe: every gene that could have ended up on your list.
- K, how many background genes belong to the gene set.
- n, how many genes are on your list.
- k, how many genes on your list belong to the gene set: the overlap.
If the list were random, the expected overlap would be n × K ÷ N. The hypergeometric distribution gives the exact probability of each possible overlap when n genes are drawn at random, without replacement, from N genes of which K are "marked". The p-value is the probability of seeing an overlap at least as large as k by chance alone. This test is the same as a one-sided Fisher's exact test, a name you will often see in tool output.
Why "without replacement"? Because each gene can be on your list only once. Once a gene has been drawn, it cannot be drawn again, so the chance of drawing a set member changes slightly with every draw. The hypergeometric distribution accounts for this exactly, which is why it, rather than a simpler formula, is the standard for this question.
How to read this figure
The large box is everything that could have been picked. The shaded circle is one gene set, the outlined circle is your list, and the overlap is the genes that are both. The panel on the right turns these into an expected overlap, the observed one, and their ratio, the fold enrichment. The p-value then says how surprising an overlap of 15 would be if the list had been drawn at random.
from scipy.stats import hypergeom
N = 20000 # background: every gene that could have been on the list
K = 200 # background genes annotated to the term
n = 500 # genes on your list (for example, significant genes)
k = 15 # list genes annotated to the term
expected = n * K / N
p = hypergeom.sf(k - 1, N, K, n) # P(overlap of k or more by chance)
print("expected overlap:", expected)
print("fold enrichment:", k / expected)
print(f"p = {p:.2e}")
# The background trap: only 10,000 genes were actually measured,
# and 150 of the term's genes are among them
p_fair = hypergeom.sf(k - 1, 10000, 150, n)
print("expected with the right background:", n * 150 / 10000)
print(f"p with the right background = {p_fair:.3f}")What this does
- The four numbers are made up for teaching, but they are of a realistic size for a human RNA-seq experiment.
expectedis the overlap you would get on average from a random list of 500.hypergeom.sf(k - 1, N, K, n)gives the probability of an overlap greater than k − 1, which is the same as k or more.sfstands for "survival function", the upper tail. The- 1is needed so that k itself is included; leaving it out is a common mistake.f"{p:.2e}"prints the p-value in scientific notation with two decimals.- The last lines repeat the test with a smaller background, as explained below.
Reading the output
A random list of 500 would contain about 5 genes from this set. Ours contains 15, three times as many. The p-value, 1.61e-04, means 1.61 × 10−4, or 0.000161: an overlap this large would happen by chance about 16 times in 100,000 random lists. With the right background, discussed next, the expected overlap rises to 7.5, the fold enrichment falls to 2, and p rises to 0.008, about fifty times larger.
Fold enrichment and p-value answer different questions, and it helps to read both. Fold enrichment says how strong the overlap is; the p-value says how sure you can be that it is not chance. A tiny gene set can give a large fold enrichment from very few genes: a set of 6 genes, 2 of them on your list, is more than ten times the expectation against the 20,000-gene background used first, yet two genes is a thin basis for a story. A very large set can give a small p-value from a modest fold enrichment, simply because there are many genes to count. Many tools therefore let you limit the analysis to sets within a size range, for example between about 10 and 500 genes.
The background-set trap
The background must be the genes that could have made your list. In an RNA-seq study, that is usually the genes that were actually measured and passed filtering, often around 10,000 to 15,000 in a given tissue, not every gene in the genome. Genes that are switched off in your tissue could never have appeared on the list, so including them makes every set of genes active in that tissue look enriched.
That is exactly what the second test shows. Using all 20,000 genes, the result looks very strong. Using the 10,000 genes that were really measured, of which 150 belong to the set, the same 15 overlapping genes are only twice the expectation. The result may still be interesting, but it is far weaker than it first looked. Many web tools use the whole genome by default, so check the setting and, where you can, upload your own background.
Go deeper: gene length bias in RNA-seq
The background is not the only hidden bias. In RNA-seq, a longer gene collects more reads than a short gene expressed at the same level. More reads mean more statistical power, so a long gene is more likely to be called significant for the same fold change.
This matters for enrichment because gene sets differ in the typical length of their members. Sets full of long genes can look enriched simply because their members were easier to detect, not because the biology changed. A plain hypergeometric test assumes every background gene had the same chance of making the list, and here they did not. Methods such as GOseq, by Young and colleagues in 2010, correct for this by weighting each gene by its chance of being selected given its length. If your enriched terms are dominated by long genes, it is worth checking.
Many terms, many tests
An enrichment analysis does not test one gene set; it tests hundreds or thousands. At a cut-off of p < 0.05, about 1 in 20 sets with no real link to your list would still pass by chance. This is the multiple-testing problem, the same one that arises when testing thousands of genes for differential expression.
The usual remedy is the Benjamini–Hochberg procedure. It controls the false discovery rate (FDR), the expected share of false positives among the results you call significant. Each p-value is turned into an adjusted value, often called a q-value; keeping all terms with q < 0.05 means that, on average, no more than 5% of the kept terms are false.
import pandas as pd
from scipy.stats import false_discovery_control
terms = pd.DataFrame({ # made-up results for six terms
"term": ["immune response", "cell cycle", "DNA repair",
"lipid metabolism", "cell adhesion", "translation"],
"K": [400, 300, 150, 250, 350, 200], # term size in the background
"k": [25, 16, 8, 9, 11, 4]}) # overlap with the list of 500
terms["p"] = hypergeom.sf(terms["k"] - 1, N, terms["K"], n)
terms["q"] = false_discovery_control(terms["p"], method="bh")
print(terms.sort_values("p").to_string(index=False, float_format="{:.2g}".format))What this does
- The table holds six made-up gene sets, each with its size in the background (
K) and its overlap with the same list of 500 (k).Nandnare reused from the first block. hypergeom.sf(...)works on a whole column at once, giving one p-value per set.false_discovery_control(..., method="bh")applies Benjamini–Hochberg and returns the adjusted values, stored as columnq.sort_values("p")orders the rows from smallest p-value;float_formatprints each decimal number to two significant figures.
Reading the output
- "immune response" is strongly enriched: 25 genes where 10 were expected, p = 2.8 × 10−5, and q = 0.00017 after adjustment.
- "cell cycle" (16 where 7.5 were expected) passes too, with q = 0.011.
- "DNA repair" has p = 0.035, which looks significant on its own, but q = 0.07 after adjustment. At an FDR of 5% it does not make the cut.
- The other three are unremarkable. "translation" has fewer genes than expected (4 against 5) and p = 0.74.
Worked example · Benjamini–Hochberg by hand
Check the q-values for the top three terms. There are m = 6 tests. Sort the p-values from smallest to largest and give each a rank, i.
rank 2 cell cycle 0.0038 × 6 ÷ 2 = 0.011
rank 3 DNA repair 0.035 × 6 ÷ 3 = 0.070
rank 6 translation 0.74 × 6 ÷ 6 = 0.74
Step by step. For each term, multiply its p-value by the number of tests, m, and divide by its rank, i. The top term is multiplied by 6, the second by 3, the third by 2, and the last by 1. One more rule keeps the order sensible: if a term's value is larger than that of a term ranked below it, it takes the lower value instead. Here that never happens, so the numbers above are the q-values the code printed.
What the answer means. The adjustment is harshest on the smallest p-values, which are multiplied by the most, and gentlest on the largest. DNA repair's p of 0.035 becomes 0.070, which is above 0.05, so on its own evidence it is not a discovery. With thousands of terms instead of six, the multipliers are far larger, and only strong signals survive.
Two more cautions apply to real results. First, GO terms overlap heavily: a list rich in immune genes will light up dozens of related terms, from "immune response" down to its many children. Read them as one theme, not as many separate findings. Tools can group or prune related terms. Second, a significant term is a description of your list, not proof of a mechanism. It tells you where to look next.
Grouping and pruning work because of the hierarchy described earlier. If a specific term and its parent are both enriched, but nearly all the parent's overlapping genes come from the specific term, the parent adds no new information. Grouping tools keep the most informative term in each cluster and set the rest aside, which can turn a page of results into a handful of themes.
GSEA: no cut-off needed
ORA needs a list, and a list needs a cut-off, such as "adjusted p < 0.05". Genes just below the line count fully; genes just above it count not at all. Gene set enrichment analysis (GSEA) avoids the cut-off. It starts from all measured genes, ranked from most increased to most decreased, and asks whether the members of a gene set tend to bunch near the top or the bottom of the ranking rather than being scattered through it.
Walking down the ranked list, a running score goes up at each member of the set and down at each non-member. The furthest the score strays from zero, up or down, is the enrichment score. Its significance is judged by repeating the walk many times after shuffling, which shows how high the score climbs by chance. GSEA can detect a set in which many genes each change a little, which ORA would miss. The Molecular Signatures Database (MSigDB), made by the same group that developed GSEA, is a widely used collection of gene sets for it.
The ranking is a choice you make. A common ranking uses the statistic from differential expression for each gene, so that genes strongly increased sit at the top, strongly decreased at the bottom, and unchanged in the middle. Ranking by p-value alone would lose the direction of change, and a set whose genes mostly go up could not be told from one whose genes mostly go down. GSEA also reports the leading edge: the members of the set that appear before the running score reaches its peak. These are the genes driving the result, and they are the ones to look at first.
Key point
An enrichment result depends on three choices: the gene sets, the background and the multiple-testing correction. Get the background wrong and almost anything can look enriched.
A second example: a proteomics study detects 3,000 proteins in liver and finds 200 that change. Tested against the whole genome, "lipid metabolism" comes out enriched, but liver is full of lipid enzymes, so many of them were bound to be detected. Tested against the 3,000 detected proteins, the enrichment may vanish. Only the second test asks whether the changed proteins are special.
Further reading · Module 7Gene Ontology: ontology documentation and GO enrichment analysis. · KEGG pathway maps. · Reactome user guide. · GSEA and MSigDB. · SciPy: the hypergeometric distribution. · All reading for this course
Check your understanding
1. An RNA-seq study measured 11,000 genes in muscle. The enrichment tool used all 20,000 human genes as the background. What is the likely effect?
2. A term has p = 0.03 but q = 0.09 after Benjamini–Hochberg. At a false discovery rate of 5%, what should you report?
3. Many genes in a pathway each rise slightly, but none reaches significance alone. Which approach is most likely to detect it?
Up next
Lesson 5
Biological networks and systems biology