Skip to main content

25  Bayesian term selection for enrichment results

Aspect Bayesian term selection
Question Which candidate enriched terms jointly explain the observed genes while avoiding a redundant list of near-duplicate terms?
Input A checked enrichResult-like term/gene table, candidate term-to-gene mappings, and a declared selection criterion.
Engine Bayesian term selection on top of an existing enrichment result; it does not replace ORA, GSEA, or the original multiple-testing correction.
Output A selected set of representative terms with posterior or scoring information that can be compared with simplify() and semantic-similarity results.
Limitations The selected theme depends on the candidate space, gene-set overlap, priors, and annotation release. Selection is an evidence-organization step, not causal validation.

Use this chapter after result-object inspection and before visualization; keep the original enrichment result for auditability.

Traditional over-representation analysis (ORA) often returns a long list of significant terms. Because each term is tested independently, groups of overlapping GO terms or pathways may all be reported as significant even when they describe the same underlying biological program.

bayes_enrich() adds a Bayesian term-selection layer on top of ORA results. Instead of asking only which terms are individually enriched, it asks which terms are most likely to represent the active biological programs that jointly explain the observed gene list. This procedure does not replace the ORA p-value and does not convert p.adjust into a posterior probability. It reuses the information already stored in an enrichResult object and builds a model based on term-gene coverage.

25.1 Input requirements

bayes_enrich() accepts an enrichResult object and extracts the information it needs directly from that object. In particular, it reuses the observed genes, the background universe, the term-to-gene mapping, and the enrichment table. Users therefore do not need to provide gene or universe again.

Internally, this means the Bayesian layer starts from the same enrichment result that users already trust, but asks a different question: among the candidate enriched terms, which ones provide the best joint explanation for the observed genes?

25.2 Candidate term space

The candidate argument defines the term space used by Bayesian inference. It is not a posterior filter. Instead, it determines which terms are allowed to compete for active-term selection.

  • candidate = "top" sorts x@result by by and takes the first n_terms rows. By default, by = "p.adjust", so the Bayesian model starts from the most significant ORA terms.
  • candidate = "significant" uses as.data.frame(x), which respects the cutoffs stored in the enrichment object and therefore works on the significant terms that users usually inspect.
  • candidate = "all" uses the full x@result slot.
  • Users can also provide a character vector of term IDs to define a custom candidate set.
ego2 <- bayes_enrich(ego, candidate = "top", n_terms = 200)
ego3 <- bayes_enrich(ego, candidate = "significant")
ego4 <- bayes_enrich(ego, candidate = "all")
ego5 <- bayes_enrich(ego, candidate = c("GO:0006955", "GO:0002376"))

Using a restricted candidate space is often helpful in practice because it keeps the model focused, runs faster, and reduces interference from weakly related terms.

25.3 The model

Let \(G\) be the background gene universe and \(T\) the candidate term set. For each gene \(g \in G\), let \(y_g\) denote whether the gene appears in the observed gene list. For each candidate term \(t \in T\), let \(z_t\) denote whether the term is active, and let \(A_t\) be the set of genes covered by that term.

Each candidate term has a latent binary state:

\[ z_t = \begin{cases} 1, & \text{term } t \text{ is active} \\ 0, & \text{term } t \text{ is inactive} \end{cases} \]

The prior is

\[ P(z_t = 1) = \text{prior}, \qquad P(z_t = 0) = 1 - \text{prior}. \]

A gene is considered covered if it belongs to at least one active term:

\[ \mathrm{covered}_g = \mathbb{I}\left(\exists t \in T \text{ such that } z_t = 1 \text{ and } g \in A_t \right). \]

If a gene is covered, the model assumes that it is observed with probability \(1 - \text{false\_negative}\). If it is not covered, it may still appear in the observed gene list with probability false_positive:

\[ P(y_g = 1 \mid \mathrm{covered}_g = 1) = 1 - \text{false\_negative} \]

\[ P(y_g = 1 \mid \mathrm{covered}_g = 0) = \text{false\_positive}. \]

This leads to the posterior quantity of interest:

\[ P(z_t = 1 \mid \text{observed genes, candidate terms, term-gene mapping}), \]

which represents the posterior support that term \(t\) is an active explanatory term under the current candidate space and model parameters.

25.4 MCMC inference

bayes_enrich() uses a lightweight Metropolis-Hastings sampler. In each iteration, one candidate term is selected at random, its active state is flipped, and only the genes covered by that term are updated. The acceptance probability is

\[ \min\left(1, \exp\left[\log p(\text{proposal}) - \log p(\text{current})\right]\right). \]

After burn-in, the sampler records active states every thin iterations. The posterior probability of a term is then estimated as the proportion of saved samples in which that term is active. Because the implementation updates only the genes affected by the flipped term, the computational cost of one iteration is driven mainly by the size of that term rather than the size of the whole universe.

25.5 Why this helps

ORA tests each term independently. In ontologies such as GO, many significant terms overlap heavily. For example, terms such as “immune response”, “immune system process”, “lymphocyte activation”, and “T cell activation” may all be enriched because they share many of the same genes.

bayes_enrich() performs joint explanation instead of independent scoring. If one specific term already explains most of the observed genes, another highly overlapping term may no longer be necessary. Because every active term pays a prior cost, redundant terms compete with one another during MCMC sampling. Terms with high posterior support therefore tend to be the ones that explain a large portion of the observed genes while remaining worth keeping after sparsity is taken into account.

25.6 A worked example

The following example uses a smaller number of iterations than the default settings so that the book can render quickly. For interactive analysis, users can increase n_iter and burnin for more stable posterior estimates.

library(clusterProfiler)
library(org.Hs.eg.db)
library(enrichit)

data(geneList, package = "DOSE")
de <- names(geneList)[abs(geneList) > 2]

ego <- enrichGO(
    gene = de,
    universe = names(geneList),
    OrgDb = org.Hs.eg.db,
    ont = "BP",
    pvalueCutoff = 0.05,
    qvalueCutoff = 0.2
)

ego2 <- bayes_enrich(
    ego,
    candidate = "top",
    n_terms = 100,
    n_iter = 1000,
    burnin = 200,
    thin = 2,
    seed = 1
)

bayes_summary(ego2, n = 10)
           ID                                                Description
1  GO:0007059                                     chromosome segregation
2  GO:0141061                     disruption of cell in another organism
3  GO:0002544                              chronic inflammatory response
4  GO:1901970 positive regulation of mitotic sister chromatid separation
5  GO:1901978               positive regulation of cell cycle checkpoint
6  GO:0051783                             regulation of nuclear division
7  GO:2000816 negative regulation of mitotic sister chromatid separation
8  GO:1901989         positive regulation of cell cycle phase transition
9  GO:1905818                        regulation of chromosome separation
10 GO:0051438       regulation of ubiquitin-protein transferase activity
   GeneRatio   BgRatio RichFactor FoldEnrichment oddsRatio    zScore
1     33/194 310/11525  0.1064516       6.323994  8.179519 12.433321
2     10/194  84/11525  0.1190476       7.072288  8.267479  7.308502
3      4/194  13/11525  0.3076923      18.279144 26.484211  8.156181
4      7/194  17/11525  0.4117647      24.461795 42.378075 12.666427
5      5/194  23/11525  0.2173913      12.914612 16.626984  7.483858
6     18/194 106/11525  0.1698113      10.088018 13.066503 12.299188
7     10/194  48/11525  0.2083333      12.376503 16.151316 10.334360
8     10/194  76/11525  0.1315789       7.816739  9.276186  7.801319
9     14/194  67/11525  0.2089552      12.413448 16.550524 12.259372
10     4/194  11/11525  0.3636364      21.602624 34.057143  8.944872
         pvalue     p.adjust       qvalue
1  1.187757e-17 4.391308e-15 1.291227e-15
2  1.356918e-06 4.620664e-05 1.358668e-05
3  4.941435e-05 1.253768e-03 3.686600e-04
4  5.796310e-09 2.884779e-07 8.482451e-08
5  3.375046e-05 9.004761e-04 2.647775e-04
6  1.409953e-13 4.054399e-11 1.192162e-11
7  5.435313e-09 2.758155e-07 8.110124e-08
8  5.271320e-07 2.006203e-05 5.899071e-06
9  4.103539e-12 7.585686e-10 2.230507e-10
10 2.341823e-05 6.660042e-04 1.958330e-04
                                                                                                                                                                               geneID
1  55143/991/9493/1062/10403/7153/23397/9787/11065/55355/220134/51203/22974/10460/4751/79019/55839/983/4085/81930/81620/332/3832/7272/64151/9212/9319/9055/3833/146909/891/24137/9232
2                                                                                                                                  3627/10563/6373/4283/6362/3002/6355/10578/730/9547
3                                                                                                                                                                 6280/6279/10563/820
4                                                                                                                                                55143/991/9787/11065/220134/332/9212
5                                                                                                                                                           55143/10403/4085/332/9212
6                                                                                        55143/991/10403/9787/11065/220134/51203/4751/10635/4085/332/7272/9212/1111/9319/6790/891/652
7                                                                                                                                  55143/991/10403/220134/4085/332/7272/9212/9319/891
8                                                                                                                                   991/6241/9787/11065/220134/983/332/2146/51514/891
9                                                                                                           55143/991/10403/23397/9787/11065/220134/4085/332/7272/64151/9212/9319/891
10                                                                                                                                                               991/11065/27338/4085
   Count SetSize DESize UniverseSize posterior posterior_odds bayes_rank
1     33     310    194        11525    1.0000   4.503600e+15          1
2     10      84    194        11525    1.0000   4.503600e+15          2
3      4      13    194        11525    1.0000   4.503600e+15          3
4      7      17    194        11525    0.5425   1.185792e+00          4
5      5      23    194        11525    0.5400   1.173913e+00          5
6     18     106    194        11525    0.5050   1.020202e+00          6
7     10      48    194        11525    0.4825   9.323671e-01          7
8     10      76    194        11525    0.4825   9.323671e-01          8
9     14      67    194        11525    0.4650   8.691589e-01          9
10     4      11    194        11525    0.4475   8.099548e-01         10
   bayes_active
1          TRUE
2          TRUE
3          TRUE
4          TRUE
5          TRUE
6          TRUE
7         FALSE
8         FALSE
9         FALSE
10        FALSE
                                                                                                                                                                   bayes_covered_gene
1  332/891/983/991/1062/3832/3833/4085/4751/7153/7272/9055/9212/9232/9319/9493/9787/10403/10460/11065/22974/23397/24137/51203/55143/55355/55839/64151/79019/81620/81930/146909/220134
2                                                                                                                                  730/3002/3627/4283/6355/6362/6373/9547/10563/10578
3                                                                                                                                                                 820/6279/6280/10563
4                                                                                                                                                332/991/9212/9787/11065/55143/220134
5                                                                                                                                                           332/4085/9212/10403/55143
6                                                                                        332/652/891/991/1111/4085/4751/6790/7272/9212/9319/9787/10403/10635/11065/51203/55143/220134
7                                                                                                                                  332/891/991/4085/7272/9212/9319/10403/55143/220134
8                                                                                                                                   332/891/983/991/2146/6241/9787/11065/51514/220134
9                                                                                                           332/891/991/4085/7272/9212/9319/9787/10403/11065/23397/55143/64151/220134
10                                                                                                                                                               991/4085/11065/27338
   bayes_covered_count
1                   33
2                   10
3                    4
4                    7
5                    5
6                   18
7                   10
8                   10
9                   14
10                   4

We can also focus on terms that pass a posterior cutoff:

bayes_summary(ego2, active = TRUE)
          ID                                                Description
1 GO:0007059                                     chromosome segregation
2 GO:0141061                     disruption of cell in another organism
3 GO:0002544                              chronic inflammatory response
4 GO:1901970 positive regulation of mitotic sister chromatid separation
5 GO:1901978               positive regulation of cell cycle checkpoint
6 GO:0051783                             regulation of nuclear division
  GeneRatio   BgRatio RichFactor FoldEnrichment oddsRatio    zScore
1    33/194 310/11525  0.1064516       6.323994  8.179519 12.433321
2    10/194  84/11525  0.1190476       7.072288  8.267479  7.308502
3     4/194  13/11525  0.3076923      18.279144 26.484211  8.156181
4     7/194  17/11525  0.4117647      24.461795 42.378075 12.666427
5     5/194  23/11525  0.2173913      12.914612 16.626984  7.483858
6    18/194 106/11525  0.1698113      10.088018 13.066503 12.299188
        pvalue     p.adjust       qvalue
1 1.187757e-17 4.391308e-15 1.291227e-15
2 1.356918e-06 4.620664e-05 1.358668e-05
3 4.941435e-05 1.253768e-03 3.686600e-04
4 5.796310e-09 2.884779e-07 8.482451e-08
5 3.375046e-05 9.004761e-04 2.647775e-04
6 1.409953e-13 4.054399e-11 1.192162e-11
                                                                                                                                                                              geneID
1 55143/991/9493/1062/10403/7153/23397/9787/11065/55355/220134/51203/22974/10460/4751/79019/55839/983/4085/81930/81620/332/3832/7272/64151/9212/9319/9055/3833/146909/891/24137/9232
2                                                                                                                                 3627/10563/6373/4283/6362/3002/6355/10578/730/9547
3                                                                                                                                                                6280/6279/10563/820
4                                                                                                                                               55143/991/9787/11065/220134/332/9212
5                                                                                                                                                          55143/10403/4085/332/9212
6                                                                                       55143/991/10403/9787/11065/220134/51203/4751/10635/4085/332/7272/9212/1111/9319/6790/891/652
  Count SetSize DESize UniverseSize posterior posterior_odds bayes_rank
1    33     310    194        11525    1.0000   4.503600e+15          1
2    10      84    194        11525    1.0000   4.503600e+15          2
3     4      13    194        11525    1.0000   4.503600e+15          3
4     7      17    194        11525    0.5425   1.185792e+00          4
5     5      23    194        11525    0.5400   1.173913e+00          5
6    18     106    194        11525    0.5050   1.020202e+00          6
  bayes_active
1         TRUE
2         TRUE
3         TRUE
4         TRUE
5         TRUE
6         TRUE
                                                                                                                                                                  bayes_covered_gene
1 332/891/983/991/1062/3832/3833/4085/4751/7153/7272/9055/9212/9232/9319/9493/9787/10403/10460/11065/22974/23397/24137/51203/55143/55355/55839/64151/79019/81620/81930/146909/220134
2                                                                                                                                 730/3002/3627/4283/6355/6362/6373/9547/10563/10578
3                                                                                                                                                                820/6279/6280/10563
4                                                                                                                                               332/991/9212/9787/11065/55143/220134
5                                                                                                                                                          332/4085/9212/10403/55143
6                                                                                       332/652/891/991/1111/4085/4751/6790/7272/9212/9319/9787/10403/10635/11065/51203/55143/220134
  bayes_covered_count
1                  33
2                  10
3                   4
4                   7
5                   5
6                  18

25.7 Output columns

bayes_enrich() returns the original enrichment object and adds several columns to the result table:

  • posterior, the posterior probability that a term is active.
  • posterior_odds, calculated as posterior / (1 - posterior).
  • bayes_rank, the rank induced by ordering terms by posterior support.
  • bayes_active, whether posterior >= posterior_cutoff.
  • bayes_covered_gene, the observed genes covered by the term.
  • bayes_covered_count, the number of covered observed genes.

These columns make it easy to keep working with the same enrichment object while gaining a second ranking criterion that emphasizes explanation rather than marginal significance.

25.8 Relationship to simplify()

simplify() and bayes_enrich() address different kinds of redundancy.

  • simplify() uses semantic similarity among GO terms and keeps one representative term from a redundant semantic cluster.
  • bayes_enrich() uses term-gene coverage and asks which terms are jointly needed to explain the observed genes.

These approaches can be used together. For example, one can first compute posterior support and then use it as the selection criterion in simplify():

ego <- enrichGO(gene = de, universe = names(geneList), OrgDb = org.Hs.eg.db, ont = "BP")
ego <- bayes_enrich(ego)
ego <- simplify(ego, by = "posterior", select_fun = max)

25.9 Scope and interpretation

The current implementation is designed primarily for ORA and other analyses that operate on a binary observed gene list. Ranked-list methods such as GSEA require an additional step to define which genes count as “observed”, for example by using leading-edge genes or another thresholding rule. For this reason, bayes_enrich() should be viewed as a posterior term-selection layer for ORA-style enrichment rather than a direct replacement for GSEA statistics.

posterior should be interpreted as posterior support for a term being an active explanatory program under the chosen candidate space and model parameters. It is not a Bayesian-adjusted p-value, not direct experimental proof of pathway activation, and not an absolute probability over the entire ontology.

In practice, the main value of bayes_enrich() is that it compresses a long list of overlapping enriched terms into a smaller set of high-posterior programs, which is often much easier to interpret and report.

25.10 Next steps