
Pathway activity functions
2026-11-29
Single-sample pathway activity
A common question in transcriptomics is whether the genes belonging to a given pathway are concordantly up- or down-regulated in each sample. Several algorithms tackle this by collapsing a gene-by-sample expression matrix into a pathway-by-sample score matrix, the most widely used being GSVA and ssGSEA. biverse provides fast versions of both with some tricks under the hood to make them faster than the original implementations. Moreover, if you have multiple contrasts (different drugs vs. DMSO for example). bixverse also provides Rust-accelerated implementations from mitch.
Data
Following the GSVA vignette convention, we generate some synthetic expression data (Gaussian-distributed to mimic normalised microarray or RNA-seq log counts, and Poisson-distributed to mimic raw counts) together with a collection of random gene sets.
p <- 10000L # genes
n <- 100L # samples
# Gaussian expression matrix
X <- matrix(
rnorm(p * n),
nrow = p,
dimnames = list(paste0("g", seq_len(p)), paste0("s", seq_len(n)))
)
# Poisson count matrix
X_counts <- matrix(
rpois(p * n, lambda = 10),
nrow = p,
dimnames = list(paste0("g", seq_len(p)), paste0("s", seq_len(n)))
)
storage.mode(X_counts) <- "numeric"
# Random gene sets of varying size
gs <- as.list(sample(10:100, size = 250, replace = TRUE))
gs <- lapply(
gs,
function(n, p) paste0("g", sample(seq_len(p), size = n, replace = FALSE)),
p
)
names(gs) <- paste0("gs", seq_along(gs))GSVA
Gaussian kernel
The Gaussian kernel version of GSVA is appropriate for continuous expression values (log-CPM, microarray intensities, etc.). Running it in bixverse is a single call:
bixverse_res_gaussian <- calc_gsva(
exp = X,
pathways = gs,
kernel = "gaussian"
)
bixverse_res_gaussian[1:5, 1:5]
#> s1 s2 s3 s4 s5
#> gs1 0.330549851 0.27120499 -0.14761545 0.03932514 -0.07835098
#> gs2 0.020948324 0.26843516 -0.17869448 0.10321580 -0.17206226
#> gs3 0.005625621 0.05727453 -0.31827781 0.18302032 -0.27567973
#> gs4 -0.057836122 -0.08068370 0.00624915 0.11715489 0.13417633
#> gs5 -0.101267472 0.08504406 0.16382133 0.04488198 -0.01764611If the original GSVA Bioconductor package is available we can verify that the results are essentially identical. Minor differences in numerical precision arise from optimisations on the Rust side, but per-pathway correlations between the two implementations are consistently above 0.99.
library(GSVA)
gsvaPar <- gsvaParam(X, gs)
gsva_res_gaussian <- as.matrix(gsva(gsvaPar, verbose = FALSE))
correlations <- diag(cor(bixverse_res_gaussian, gsva_res_gaussian))
print(sprintf(
"All pathway correlations >= 0.99: %s (min: %.4f)",
all(correlations >= 0.99),
min(correlations)
))
#> [1] "All pathway correlations >= 0.99: TRUE (min: 1.0000)"Let’s compare speed (differences will be more pronounced, the more computational fire power your system has.)
microbenchmark::microbenchmark(
gsva = {
gsvaPar <- gsvaParam(X, gs)
gsva(gsvaPar, verbose = FALSE)
},
bixverse = calc_gsva(exp = X, pathways = gs, kernel = "gaussian"),
times = 3L
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> gsva 3007.5257 3016.2270 3020.6159 3024.928 3027.1609 3029.3936 3
#> bixverse 225.3304 227.6152 230.5762 229.900 233.1991 236.4982 3Poisson kernel
For count data a Poisson kernel is more appropriate. The only change is setting kernel = "poisson (to note: prior to "0.4.1", this was controlled via a Boolean flag gaussian = FALSE - this still works, but will throw a deprecation warning now):
bixverse_res_poisson <- calc_gsva(
exp = X_counts,
pathways = gs,
kernel = "poisson"
)
bixverse_res_poisson[1:5, 1:5]
#> s1 s2 s3 s4 s5
#> gs1 -0.14279174 0.02752725 0.002713897 0.11335376 -0.22356984
#> gs2 0.20636750 0.23049903 0.037875759 0.05206118 0.08133925
#> gs3 -0.09542478 -0.25481678 -0.020042077 0.09803837 -0.45126811
#> gs4 -0.05354725 -0.05634640 -0.020257054 0.06671581 0.09396979
#> gs5 -0.19950141 -0.04172910 0.029074541 -0.02468590 0.02240333And vs. the original
gsvaParPoisson <- gsvaParam(X_counts, gs, kcdf = "Poisson")
gsva_res_poisson <- as.matrix(gsva(gsvaParPoisson, verbose = FALSE))
correlations <- diag(cor(bixverse_res_poisson, gsva_res_poisson))
print(sprintf(
"All pathway correlations >= 0.99: %s (min: %.4f)",
all(correlations >= 0.99),
min(correlations)
))
#> [1] "All pathway correlations >= 0.99: TRUE (min: 1.0000)"And speed:
microbenchmark::microbenchmark(
gsva = {
gsvaPar <- gsvaParam(X_counts, gs, kcdf = "Poisson")
gsva(gsvaPar, verbose = FALSE)
},
bixverse = calc_gsva(exp = X_counts, pathways = gs, kernel = "poisson"),
times = 3L
)
#> Unit: milliseconds
#> expr min lq mean median uq max
#> gsva 15529.8644 15549.8402 15561.2016 15569.8159 15576.8702 15583.9245
#> bixverse 345.4707 346.4898 347.2877 347.5089 348.1962 348.8835
#> neval
#> 3
#> 3ssGSEA
ssGSEA, first described in Barbie et al., takes a similar approach but does not apply a kernel-based normalisation across samples. bixverse provides a Rust-optimised version here as well:
bixverse_res_ssgsea <- calc_ssgsea(
exp = X,
pathways = gs
)
bixverse_res_ssgsea[1:5, 1:5]
#> s1 s2 s3 s4 s5
#> gs1 0.18366388 0.26814822 0.08519973 0.15895446 0.02027652
#> gs2 0.10267089 0.26177899 0.04636668 0.14167563 -0.03516097
#> gs3 0.21138318 0.14711378 -0.10512794 0.42809074 -0.04276905
#> gs4 0.06275931 0.04945732 0.16626347 0.18547244 0.16465230
#> gs5 0.04268310 0.14676158 0.15823311 0.09536812 0.11947279Let’s compare again against the GSVA version:
ssgseaPar <- ssgseaParam(X, gs)
ssgsea_res <- as.matrix(gsva(ssgseaPar, verbose = FALSE))
correlations <- diag(cor(bixverse_res_ssgsea, ssgsea_res))
print(sprintf(
"All pathway correlations >= 0.99: %s (min: %.4f)",
all(correlations >= 0.99),
min(correlations)
))
#> [1] "All pathway correlations >= 0.99: TRUE (min: 1.0000)"And check the underlying speed differences:
microbenchmark::microbenchmark(
gsva = {
ssgseaPar <- ssgseaParam(X, gs)
gsva(ssgseaPar, verbose = FALSE)
},
bixverse = calc_ssgsea(exp = X, pathways = gs),
times = 3L
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> gsva 568.31300 573.59514 579.71890 578.87728 585.42185 591.96643 3
#> bixverse 37.11827 38.24597 39.25805 39.37367 40.32794 41.28222 3singscore
singscore takes a different angle than GSVA or ssGSEA: rather than estimating a null distribution over samples or genes, it scores each sample independently using only the ranks of the genes in the signature. This makes it well-suited to scoring single samples in isolation and to working with directional signatures (separate up- and down-regulated gene sets). bixverse provides a Rust-accelerated implementation.
Ranking
singscore operates on a ranked expression matrix rather than the raw values, so the first step is to compute per-sample ranks:
ranks <- calc_singscore_rank(exp = X)
ranks[1:5, 1:5]
#> s1 s2 s3 s4 s5
#> g1 560 6369 6561 1050 7462
#> g2 7650 1512 3398 8156 8604
#> g3 5239 8526 5464 7369 7724
#> g4 5201 9122 8988 7691 5577
#> g5 9083 2990 5809 5259 828If your data comes from a small targeted panel (NanoString, RT-qPCR) rather than a transcriptome-wide assay, you can pass a set of stable genes to calc_singscore_rank to use the stable-gene ranking approach from Bhuva et al.:
ranks_stable <- calc_singscore_rank(
exp = X,
stable_genes = c("g1", "g2", "g3", "g4", "g5")
)The stable attribute on the rank matrix is read automatically by the downstream scoring functions to pick the appropriate bounds formula.
Scoring a single signature
For a single signature, calc_singscore returns one score and dispersion per sample. Below we use one of our random gene sets as the up-regulated set and another as the down-regulated set:
up_set <- gs[[1]]
down_set <- gs[[2]]
singscore_res <- calc_singscore(
ranks = ranks,
up_set = up_set,
down_set = down_set
)
head(singscore_res)
#> total_score total_dispersion up_score up_dispersion down_score
#> <num> <num> <num> <num> <num>
#> 1: 0.056769190 3523.775 0.06224930 3281.740 -0.005480108
#> 2: 0.007879840 3452.239 0.09380909 3531.558 -0.085929254
#> 3: 0.010082393 3916.294 -0.02951837 3945.205 0.039600759
#> 4: 0.006349438 3929.267 0.02575784 3828.079 -0.019408398
#> 5: -0.007290391 3317.693 -0.06551559 3651.649 0.058225195
#> 6: 0.058885693 3580.855 0.03825712 3186.853 0.020628573
#> down_dispersion sample_id
#> <num> <char>
#> 1: 3765.810 s1
#> 2: 3372.920 s2
#> 3: 3887.383 s3
#> 4: 4030.454 s4
#> 5: 2983.737 s5
#> 6: 3974.857 s6If the direction of the signature is unknown (a gene ontology term, for example), pass only up_set and set known_direction = FALSE. The scoring function then transforms ranks around their median so that genes at either extreme contribute symmetrically.
Permutation testing
To assess whether an observed score is larger than would be expected by chance for a random gene set of the same size, set n_permutations to a positive integer. The function draws random gene sets matching the size of the real signature, scores them, and returns empirical one-tailed p-values alongside the scores. The full null distribution is attached as an attribute for inspection or plotting.
singscore_perm <- calc_singscore(
ranks = ranks,
up_set = up_set,
down_set = down_set,
n_permutations = 1000L,
seed = 42L
)
head(singscore_perm)
#> total_score total_dispersion up_score up_dispersion down_score
#> <num> <num> <num> <num> <num>
#> 1: 0.056769190 3523.775 0.06224930 3281.740 -0.005480108
#> 2: 0.007879840 3452.239 0.09380909 3531.558 -0.085929254
#> 3: 0.010082393 3916.294 -0.02951837 3945.205 0.039600759
#> 4: 0.006349438 3929.267 0.02575784 3828.079 -0.019408398
#> 5: -0.007290391 3317.693 -0.06551559 3651.649 0.058225195
#> 6: 0.058885693 3580.855 0.03825712 3186.853 0.020628573
#> down_dispersion sample_id pval
#> <num> <char> <num>
#> 1: 3765.810 s1 0.008
#> 2: 3372.920 s2 0.020
#> 3: 3887.383 s3 0.022
#> 4: 4030.454 s4 0.141
#> 5: 2983.737 s5 0.940
#> 6: 3974.857 s6 0.038
dim(attr(singscore_perm, "null_distribution"))
#> [1] 1000 100The seed argument makes the permutations reproducible.
Scoring many signatures
When you have many signatures to score at once, calc_singscore_multi avoids the per-call overhead and parallelises across gene sets. It accepts a named list of up-regulated sets, optionally paired with a list of down-regulated sets keyed by the same names:
singscore_multi_res <- calc_singscore_multi(
ranks = ranks,
up_pathways = gs[1:10]
)
singscore_multi_res$scores[1:5, 1:5]
#> s1 s2 s3 s4 s5
#> gs1 0.062249298 0.09380909 -0.02951837 0.02575784 -0.06551559
#> gs2 0.005480108 0.08592925 -0.03960076 0.01940840 -0.05822520
#> gs3 0.070441574 0.02884904 -0.10914574 0.15035315 -0.08140968
#> gs4 -0.023666113 -0.03550555 0.01565447 0.03150942 0.03971898
#> gs5 -0.031368709 0.01039516 0.03387029 0.00372427 0.00692126The returned list contains a scores matrix and a matching dispersions matrix, both with shape gene sets × samples.
Comparison with singscore
When the original singscore package is available we can confirm that the two implementations agree closely. One detail worth noting: singscore uses ties.method = "min" when ranking, while bixverse uses average ranks. For continuous data the difference is negligible, but it can introduce small discrepancies on heavily tied data.
library(singscore)
sing_ranks <- rankGenes(X)
sing_res <- simpleScore(
rankData = sing_ranks,
upSet = up_set,
downSet = down_set
)
cor(singscore_res$total_score, sing_res$TotalScore)
#> [1] 1And the speed difference:
microbenchmark::microbenchmark(
singscore = {
sing_ranks <- rankGenes(X)
simpleScore(rankData = sing_ranks, upSet = up_set, downSet = down_set)
},
bixverse = {
ranks <- calc_singscore_rank(exp = X)
calc_singscore(ranks = ranks, up_set = up_set, down_set = down_set)
},
times = 5L
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> singscore 77.43444 77.93019 78.21651 77.93786 78.60168 79.17839 5
#> bixverse 14.08434 14.23234 15.02830 14.69540 14.89443 17.23498 5ULM and MLM
The rank-based scores above treat a gene set as a bag of genes. Transcription factor regulons (think CollecTRI or DoRothEA) carry a sign as well: some targets get activated, others repressed. decoupler handles that with a linear model per sample: regress the expression of all genes on the weights of a source’s targets, and take the t-value of the slope as the activity. The univariate version (ULM) fits one model per source; the multivariate one (MLM) puts all sources into one model, so overlapping regulons compete for the signal. calc_lm_activity() is the Rust version of both.
The network comes in decoupler’s long format: source, target and an optional weight. We turn the random gene sets into signed regulons and plant an activation of the first one in the first ten samples.
net <- data.table::rbindlist(
purrr::imap(gs[1:50], \(targets, source) {
data.table::data.table(
source = source,
target = targets,
weight = sample(c(-1, 1), length(targets), replace = TRUE)
)
})
)
X_lm <- X
gs1_net <- net[source == "gs1"]
X_lm[gs1_net$target, 1:10] <- X_lm[gs1_net$target, 1:10] + gs1_net$weight
ulm_res <- calc_lm_activity(
mat = X_lm,
net = net,
lm_params = params_lm_activity(method = "ulm", p_values = TRUE)
)
# planted samples first, the rest after
round(ulm_res$es[c(1:3, 11:13), 1:4], 2)
#> gs1 gs10 gs11 gs12
#> s1 7.41 -0.32 -0.19 -0.99
#> s2 5.33 -2.18 1.29 0.12
#> s3 3.39 -0.69 -0.93 0.53
#> s11 -0.20 -1.10 -0.37 -1.28
#> s12 0.43 0.76 0.37 -0.23
#> s13 0.76 -0.64 2.41 -0.05es holds the t-values (stat = "coef" gives you the slope instead), pvals the two-sided p-values and fdr the Benjamini-Hochberg adjustment per sample across sources, which is what decoupler does. Sources with fewer than tmin targets in the data (default 5) are dropped, same as decoupler.
mlm_res <- calc_lm_activity(
mat = X_lm,
net = net,
lm_params = params_lm_activity(method = "mlm", p_values = TRUE)
)
cor(ulm_res$es[, "gs1"], mlm_res$es[, "gs1"])
#> [1] 0.9991594A few things to be aware of:
- MLM throws an error on collinear sources, i.e., duplicated or proportional regulons. Merge or drop them first.
- Rows with
NAare dropped (with a warning) before scoring. - The p-values treat genes as independent. They are not, so read them as a ranking aid rather than gospel.
- A named vector (say the t statistics of a contrast) works as well and gives you a
data.tableback.
Comparison with decoupleR
dc_ulm <- decoupleR::run_ulm(
mat = X_lm,
network = net,
.source = "source",
.target = "target",
.mor = "weight",
minsize = 5L
)
dc_ulm_wide <- data.table::dcast(
data.table::as.data.table(dc_ulm),
condition ~ source,
value.var = "score"
)
dc_ulm_mat <- as.matrix(dc_ulm_wide[, -1])
rownames(dc_ulm_mat) <- dc_ulm_wide$condition
max(abs(dc_ulm_mat[rownames(ulm_res$es), colnames(ulm_res$es)] - ulm_res$es))And the speed difference:
microbenchmark::microbenchmark(
decoupleR_ulm = decoupleR::run_ulm(
mat = X_lm,
network = net,
.source = "source",
.target = "target",
.mor = "weight"
),
bixverse_ulm = calc_lm_activity(mat = X_lm, net = net),
decoupleR_mlm = decoupleR::run_mlm(
mat = X_lm,
network = net,
.source = "source",
.target = "target",
.mor = "weight"
),
bixverse_mlm = calc_lm_activity(
mat = X_lm,
net = net,
lm_params = params_lm_activity(method = "mlm")
),
times = 5L
)Multi-contrast enrichment (mitch)
When you have multiple contrasts - say, differential expression results from several comparisons or different omics layers - you often want to know which pathways show coordinated changes across contrasts. The mitch method does exactly this: it ranks genes within each contrast, computes mean ranks per pathway, and then uses a MANOVA test to identify pathways that are enriched in one or more contrasts simultaneously. bixverse re-implements the core algorithm so that it slots into the same workflow as the other pathway activity functions… Also, heavily multi-threaded and designed to go brrrrrr in terms of speed.
calc_mitch expects a numeric matrix of contrast statistics (genes in rows, contrasts in columns) and a named list of gene sets. It returns a data.table containing per-pathway MANOVA p-values, FDR-adjusted p-values, and the individual contrast-level enrichment scores and p-values.
set.seed(42L)
contrast_data <- matrix(rnorm(3 * 26), nrow = 26)
colnames(contrast_data) <- sprintf("contrast_%i", 1:3)
rownames(contrast_data) <- letters
gene_sets <- list(
pathway_A = sample(letters, 4),
pathway_B = sample(letters, 5),
pathway_C = sample(letters, 6),
pathway_D = sample(letters, 7)
)
res <- calc_mitch(
contrast_mat = contrast_data,
gene_set_list = gene_sets
)
resNote that pathways smaller than the internal minimum set size are dropped from the output, and the results are sorted by significance. The function also validates its input: passing a matrix containing NA values will throw an error rather than silently producing nonsense.
Comparison with the mitch package
When the original mitch package is available we can verify that the two implementations produce identical results. Here we use the example data bundled with mitch:
data(myImportedData, genesetsExample, package = "mitch")
mitch_res <- suppressMessages(mitch::mitch_calc(
myImportedData,
genesetsExample,
priority = "significance",
minsetsize = 5,
cores = 2
))
bixverse_res <- calc_mitch(
contrast_mat = as.matrix(myImportedData),
gene_set_list = genesetsExample
)
# Pathway ordering and FDR values should match exactly
all(bixverse_res$pathway_names == mitch_res$enrichment_result$set) &&
all.equal(bixverse_res$manova_fdr, mitch_res$enrichment_result$p.adjustMANOVA)
#> [1] TRUEAnd let’s check the speed differences:
microbenchmark::microbenchmark(
mitch = suppressMessages(mitch::mitch_calc(
myImportedData,
genesetsExample,
priority = "significance",
minsetsize = 5,
cores = 2
)),
bixverse = calc_mitch(
contrast_mat = as.matrix(myImportedData),
gene_set_list = genesetsExample
),
times = 5L
)
#> Unit: milliseconds
#> expr min lq mean median uq max
#> mitch 154.376993 156.528510 157.952303 157.928925 158.387083 162.540003
#> bixverse 2.994966 3.418073 4.336914 3.906616 4.479865 6.885048
#> neval
#> 5
#> 5As with the GSVA and ssGSEA implementations, you should observe meaningful speed improvements from Rust, particularly as the number of gene sets or contrasts grows and the more cores/oomph your system has.