Over-Representation Analysis
Over-Representation Analysis asks a simple question. You have a list of significant genes. Are any biological pathways or GO terms overrepresented in that list compared to what you would expect by chance?
ORA is the oldest and most widely used pathway analysis method. It works by comparing your gene list against a background set using a statistical test. If a pathway has more of your significant genes than expected, that pathway is enriched.
How ORA works
Section titled “How ORA works”The logic is straightforward. You start with two sets of genes:
- Foreground: your significant differentially expressed genes
- Background: all genes you tested
For each pathway or GO term, ORA builds a 2x2 contingency table. It counts how many foreground genes are in the pathway versus how many are not. Then it compares this to the same ratio in the background. A hypergeometric test or Fisher’s exact test determines whether the overlap is greater than chance.
ORA vs GSEA
Section titled “ORA vs GSEA”ORA requires a hard cutoff to define your significant genes. This is its main limitation. Genes that just miss the cutoff are discarded entirely. A gene with padj = 0.051 contributes nothing, while a gene with padj = 0.049 contributes equally to one with padj = 1e-100.
The GSEA page compares the two methods in one table, and says when ORA is still useful.
clusterProfiler runs the enrichment in R and gseapy does the same job in Python. Both
tabs below work from one DESeq2 result and one GO annotation, the one in org.Hs.eg.db.
The end of the GO section explains why their answers still differ.
The page recomputes the differential expression rather than reading a file, so it stands on its own.
The count object comes from Michael Love’s airway 1.32.0 data package, which declares LGPL. It represents the Himes and colleagues experiment, deposited as GSE52778 and SRP033351. The source and license notice identifies the original object. This lesson derives its summaries and analysis results from that object.
library(tidyverse)library(clusterProfiler)library(org.Hs.eg.db)library(GO.db)library(DESeq2)library(airway)
data("airway")dds <- DESeqDataSet(airway, design = ~ cell + dex)dds$dex <- relevel(dds$dex, ref = "untrt")dds <- dds[rowSums(counts(dds)) >= 10, ]dds <- DESeq(dds)
res <- as.data.frame(results(dds, contrast = c("dex", "trt", "untrt")))res$gene <- rownames(res)nrow(res)[1] 22369Define significant genes
Section titled “Define significant genes”We apply a standard cutoff: adjusted p-value < 0.05 and absolute log2 fold change > 1. This keeps genes that pass the test and changed more than two-fold.
sig_genes <- res |> filter(padj < 0.05, abs(log2FoldChange) > 1) |> pull(gene)
bg_genes <- res |> filter(!is.na(padj)) |> pull(gene)
length(sig_genes)length(bg_genes)
# Hand both lists, and the GO definitions, to the Python tab. Restricting the# terms to a usable size range is the same filter enrichGO applies internally.dir.create("outputs", showWarnings = FALSE, recursive = TRUE)writeLines(sig_genes, "outputs/ora_significant.txt")writeLines(bg_genes, "outputs/ora_background.txt")
go_map <- AnnotationDbi::select( org.Hs.eg.db, keys = bg_genes, columns = c("GO", "ONTOLOGY"), keytype = "ENSEMBL")# Bioconductor attaches a dplyr::count() of its own, so name dplyr's explicitly.go_map <- go_map |> dplyr::filter(!is.na(GO), ONTOLOGY == "BP") |> dplyr::distinct(GO, ENSEMBL)
go_sizes <- dplyr::count(go_map, GO)keep_terms <- go_sizes |> dplyr::filter(n >= 10, n <= 500) |> dplyr::pull(GO)go_map <- go_map |> dplyr::filter(GO %in% keep_terms)# Carry the term names too, so the Python tab can print something readable.go_names <- AnnotationDbi::select( GO.db, keys = keep_terms, columns = "TERM", keytype = "GOID")go_map <- go_map |> dplyr::left_join(go_names, by = c("GO" = "GOID"))
write_csv(go_map, "outputs/ora_go_sets.csv")length(keep_terms)[1] 1018[1] 18032[1] 2065import pandas as pdimport gseapy as gp
significant = pd.read_csv( "outputs/ora_significant.txt", header=None)[0].tolist()background = pd.read_csv( "outputs/ora_background.txt", header=None)[0].tolist()
go_sets = pd.read_csv("outputs/ora_go_sets.csv")names = dict(zip(go_sets["GO"], go_sets["TERM"]))gene_sets = go_sets.groupby("GO")["ENSEMBL"].apply(list).to_dict()
print(len(significant))print(len(background))print(len(gene_sets))1018180322065We have 1,018 significant DEGs out of 18,032 tested genes.
Convert gene IDs
Section titled “Convert gene IDs”enrichGO reads Entrez gene IDs by default, and the Hallmark example below selects Entrez IDs from its membership table. Our DESeq2 results use Ensembl IDs. The bitr() function handles the conversion.
sig_entrez <- bitr( sig_genes, fromType = "ENSEMBL", toType = "ENTREZID", OrgDb = org.Hs.eg.db)
bg_entrez <- bitr( bg_genes, fromType = "ENSEMBL", toType = "ENTREZID", OrgDb = org.Hs.eg.db)
# bitr returns one row per Ensembl and Entrez pair. Count the Ensembl IDs# that mapped, then the Entrez IDs they gave, for each list.length(unique(sig_entrez$ENSEMBL))length(unique(sig_entrez$ENTREZID))length(unique(bg_entrez$ENSEMBL))length(unique(bg_entrez$ENTREZID))[1] 936[1] 949[1] 15049[1] 15218The conversion mapped 936 of the 1,018 significant genes, and those 936 gave 949 Entrez IDs, because a few Ensembl IDs map to more than one. In the background, 15,049 of 18,032 genes mapped and gave 15,218 Entrez IDs. The other Ensembl IDs have no Entrez ID in org.Hs.eg.db, so they drop out of the R analysis.
GO enrichment
Section titled “GO enrichment”Gene Ontology groups genes into three categories: Biological Process, Molecular Function, and Cellular Component. Biological Process is the most commonly used for pathway-level interpretation.
ego <- enrichGO( gene = sig_entrez$ENTREZID, universe = bg_entrez$ENTREZID, OrgDb = org.Hs.eg.db, ont = "BP", pAdjustMethod = "BH", pvalueCutoff = 0.05, readable = TRUE)
ego |> as_tibble() |> dplyr::select(Description, GeneRatio, p.adjust, Count) |> head(10)# A tibble: 10 × 4 Description GeneRatio p.adjust Count <chr> <chr> <dbl> <int> 1 circulatory system process 58/814 0.00000124 58 2 regulation of system process 54/814 0.00000198 54 3 vasculature development 68/814 0.00000198 68 4 blood circulation 49/814 0.00000200 49 5 regulation of blood circulation 32/814 0.00000583 32 6 blood vessel development 64/814 0.00000699 64 7 axon guidance 29/814 0.00000866 29 8 neuron projection guidance 29/814 0.00000901 29 9 animal organ morphogenesis 58/814 0.0000174 5810 angiogenesis 56/814 0.0000181 56ora_res = gp.enrich( gene_list=significant, gene_sets=gene_sets, background=background, outdir=None,)
results = ora_res.results.sort_values("Adjusted P-value")results["Description"] = results["Term"].map(names)columns = ["Description", "Overlap", "Adjusted P-value"]print(results[columns].head(10).to_string(index=False)) Description Overlap Adjusted P-value cell adhesion 57/432 0.000003 axon guidance 24/113 0.000010 angiogenesis 33/200 0.000012 nervous system development 55/449 0.000018 cell-cell signaling 21/104 0.000082 positive regulation of cell migration 32/213 0.000092cell surface receptor protein tyrosine kinase signaling pathway 18/84 0.000163 intracellular signal transduction 47/393 0.000167 G protein-coupled receptor signaling pathway 35/257 0.000193 immune response 27/177 0.000296The GeneRatio column divides the significant genes in each term by 814, the number of significant Entrez IDs that carry a Biological Process annotation. That is fewer than the 949 Entrez IDs the conversion returned.
The two tabs do not return the same ranking, and the test is not the reason. The first
cause is the gene sets. enrichGO walks the GO graph, so a gene annotated to a specific
child term also counts toward every parent above it. The sets exported for the Python tab
hold direct annotations only, which makes each set smaller and shifts which ones clear the
correction. The second cause is the input. The R tab passes the 949 Entrez IDs and a
universe of 15,218, after the conversion above. The Python tab passes the 1,018 Ensembl IDs
and the 18,032 background genes with no conversion, so it keeps the genes that have no
Entrez ID. Axon guidance and angiogenesis are in both top tens, and neither ordering is the
right one to quote without saying which gene sets and which gene lists produced it.
GO dot plot
Section titled “GO dot plot”The dot plot shows the top enriched GO terms. Dot size represents gene count. Color represents adjusted p-value. The x axis is the GeneRatio, and the terms are sorted by it.
dot_plot <- dotplot(ego, showCategory = 15) + theme_minimal()ggsave("outputs/ora-go-dotplot.png", dot_plot, width = 8, height = 7, dpi = 110, bg = "white")
Larger, redder dots indicate terms with many genes and strong statistical significance. Vasculature development has the highest gene count at 68. Circulatory system process has the lowest adjusted p-value.
GO bar plot
Section titled “GO bar plot”A bar plot provides an alternative view. Bar length is the gene count, and the terms run in order of adjusted p-value with the smallest at the top. Vasculature development, the longest bar at 68 genes, sits third.
bar_plot <- barplot(ego, showCategory = 15) + theme_minimal()ggsave("outputs/ora-go-barplot.png", bar_plot, width = 8, height = 7, dpi = 110, bg = "white")
Hallmark gene-set enrichment
Section titled “Hallmark gene-set enrichment”The MSigDB Hallmark collection groups genes into biological programs, such as inflammatory response and cell division. These sets describe coordinated expression patterns rather than individual pathway maps. The image stores the human v2026.1 membership table and checks its SHA-256 hash during the build.
enricher() uses the significant Entrez IDs and background from the GO example.
Selecting unique set and gene pairs prevents duplicate membership rows from counting
the same gene twice. The size limits apply after each set is restricted to the background.
Outside the image, download the pinned Hallmark membership table
and save it as hallmark_2026.1.Hs.tsv. Point hallmark_dir at its folder.
The download notice records the source, license, and exact file hash.
The memberships come from the MSigDB Hallmark collection, release 2026.1, described by Liberzon and colleagues. MSigDB credits the Broad Institute, Massachusetts Institute of Technology, and Regents of the University of California. The MSigDB terms provide this collection under CC BY 4.0. This example selects unique Hallmark and Entrez ID pairs, removes missing IDs, and restricts memberships to the tested background during enrichment.
# Read Entrez IDs as text so they match the identifiers from bitr().hallmark_dir <- "/opt/data/msigdb"hallmark_members <- read_tsv( file.path(hallmark_dir, "hallmark_2026.1.Hs.tsv"), col_types = cols(.default = col_character())) |> dplyr::filter(!is.na(ncbi_gene), ncbi_gene != "")hallmark_sets <- hallmark_members |> dplyr::distinct(gs_name, ncbi_gene)hallmark_names <- hallmark_members |> dplyr::distinct(gs_name, gs_description)
ehallmark <- enricher( gene = sig_entrez$ENTREZID, universe = bg_entrez$ENTREZID, TERM2GENE = hallmark_sets, TERM2NAME = hallmark_names, minGSSize = 10, maxGSSize = 500, pAdjustMethod = "BH", pvalueCutoff = 0.05)
length(unique(hallmark_sets$gs_name))# Count the eligible sets before retaining those with a foreground hit.set_sizes <- hallmark_sets |> dplyr::filter(ncbi_gene %in% bg_entrez$ENTREZID) |> dplyr::count(gs_name)sum(set_sizes$n >= 10 & set_sizes$n <= 500)nrow(ehallmark@result)hallmark_results <- as_tibble(ehallmark)nrow(hallmark_results)hallmark_results |> dplyr::select(Description, GeneRatio, p.adjust, Count) |> head(10)[1] 50[1] 50[1] 49[1] 8# A tibble: 8 × 4 Description GeneRatio p.adjust Count <chr> <chr> <dbl> <int>1 Genes regulated by NF-kB in response to TNF [GeneID=… 42/321 2.52e-8 422 Genes defining epithelial-mesenchymal transition, as… 38/321 4.32e-5 383 Genes involved in p53 pathways and networks. 34/321 2.80e-4 344 Genes up-regulated by KRAS activation. 26/321 5.58e-3 265 Genes down-regulated by KRAS activation. 18/321 1.51e-2 186 Genes defining inflammatory response. 23/321 1.51e-2 237 Genes up-regulated in response to low oxygen levels … 27/321 1.88e-2 278 Genes involved in development of skeletal muscle (my… 23/321 3.97e-2 23All 50 Hallmark sets contain 10 to 500 genes in this background. Of those, 49 contain at least one significant gene and enter the Benjamini-Hochberg correction. Eight sets pass the result filters. The TNF response through NF-kB has the smallest adjusted p-value, with 42 significant genes among its members.
The GeneRatio denominator is 321, the significant Entrez IDs with at least one Hallmark membership. GO uses 814 because it covers a different portion of the same input list. The foreground combines increased and decreased expression, so enrichment alone does not establish whether a biological program is activated or suppressed.
Export results
Section titled “Export results”enrichGO does not fix the order of the genes inside a term, so the block sorts them
before it writes the table, and two runs write the same file.
go_table <- as_tibble(ego)gene_lists <- lapply(strsplit(go_table$geneID, "/"), sort, method = "radix")go_table$geneID <- vapply(gene_lists, paste, character(1), collapse = "/")write_csv(go_table, "outputs/ora_go_results.csv")Next: Gene Set Enrichment Analysis (GSEA)
Scientific references
Section titled “Scientific references”Love, Huber, and Anders, 2014 describe the DESeq2 count model and its shrinkage of dispersion and fold change estimates.
Himes and colleagues, 2014 report the airway smooth muscle RNA-seq experiment that the airway data package holds.
Yu, 2024 describes thirteen years of clusterProfiler development for enrichment analysis.
Fang, Liu, and Peltz, 2022 describe GSEApy, the Python package for gene set enrichment analysis.
Further reading
Section titled “Further reading”The clusterProfiler enrichGO reference defines the gene universe, identifier type, and multiple-testing controls. Those choices determine which genes and terms enter an over-representation test.
The Hallmark collection paper explains how the collection combines overlapping signatures into biological programs. The MSigDB collection terms identify its license and the restrictions that apply to other collections.