Nothing
#' Render altered gene sets by over-representation analysis
#'
#' @param ags Named list of altered gene sets.
#' @param fgs Named list of functional gene sets.
#' @param universe Character vector of eligible gene or protein identifiers, or
#' a positive integer giving only the universe size. An explicit vector is
#' preferred.
#' @param case_sensitive Use case-sensitive identifier matching. By default,
#' identifiers in AGS, FGS, and the universe are matched case-insensitively.
#' @param n_cores Positive integer number of worker processes. Independent FGS
#' rows are distributed across workers when greater than one.
#' @param verbose Report progress with [message()].
#' @details Each AGS--FGS pair is tested with a two-sided Fisher exact test.
#' BH adjustment is applied across all cells, after which depleted pairs
#' (odds ratio below 1) receive q-value 1 to retain the enrichment-only
#' convention used by NEArender.
#' @return A `nearender_result`. `scores$odds_ratio` contains Fisher-test
#' odds ratios. The identical master matrix is also available as
#' `statistics$odds_ratio`; other statistics are literal overlap counts
#' (`n.actual`), two-sided p-values, and enrichment q-values. No statistical
#' matrices occur at the result's top level. `inputs` records the analyzed
#' set names, sizes, and universe size.
#' @seealso [nea.render()], [ear()], [gsva.render()]
#' @examples
#' data("NEArender.test")
#' ags <- NEArender.test$AGS$CCLE[1:2]
#' fgs <- NEArender.test$FGS$Hallmarks[1:3]
#' universe <- unique(c(unlist(ags), unlist(fgs)))
#' ora <- ora.render(ags, fgs, universe, verbose = FALSE)
#' ora$scores$odds_ratio
#' ora$statistics$p
#' @export
ora.render <- function(
ags,
fgs,
universe,
case_sensitive = FALSE,
n_cores = 1L,
verbose = TRUE) {
.assert_flag(case_sensitive, "case_sensitive")
.assert_flag(verbose, "verbose")
n_cores <- .validate_n_cores(n_cores)
ags <- .normalize_gene_sets(ags, "ags", case_sensitive)
fgs <- .normalize_gene_sets(fgs, "fgs", case_sensitive)
.assert_compatible_identifiers(ags = ags, fgs = fgs)
explicit_universe <- !(is.numeric(universe) && length(universe) == 1L &&
!is.na(universe))
if (explicit_universe) {
universe <- unique(.canonical_ids(universe, case_sensitive))
universe <- universe[!is.na(universe) & nzchar(universe)]
if (!length(universe)) {
stop("universe must contain at least one identifier.", call. = FALSE)
}
ags <- lapply(ags, intersect, y = universe)
fgs <- lapply(fgs, intersect, y = universe)
universe_size <- length(universe)
} else {
if (!is.finite(universe) || universe < 1 ||
universe != as.integer(universe)) {
stop("A numeric universe must be one positive integer.", call. = FALSE)
}
universe_size <- as.integer(universe)
}
if (any(lengths(ags) > universe_size) ||
any(lengths(fgs) > universe_size)) {
stop("Gene-set size cannot exceed the universe size.", call. = FALSE)
}
if (verbose) message("Calculating over-representation scores.")
overlap <- estimate <- p <- matrix(
NA_real_, length(fgs), length(ags),
dimnames = list(names(fgs), names(ags))
)
rows <- .parallel_lapply(seq_along(fgs), function(i) {
row <- matrix(NA_real_, 3L, length(ags))
for (j in seq_along(ags)) {
n_overlap <- length(intersect(fgs[[i]], ags[[j]]))
fgs_only <- length(fgs[[i]]) - n_overlap
ags_only <- length(ags[[j]]) - n_overlap
neither <- universe_size - n_overlap - fgs_only - ags_only
if (neither < 0) {
stop("AGS and FGS union exceeds the supplied universe size.",
call. = FALSE)
}
test <- stats::fisher.test(
matrix(c(n_overlap, fgs_only, ags_only, neither), nrow = 2L),
alternative = "two.sided"
)
row[, j] <- c(n_overlap, unname(test$estimate), test$p.value)
}
row
}, n_cores = n_cores)
for (i in seq_along(rows)) {
overlap[i, ] <- rows[[i]][1L, ]
estimate[i, ] <- rows[[i]][2L, ]
p[i, ] <- rows[[i]][3L, ]
}
finite_estimate <- is.finite(estimate)
if (any(finite_estimate) && any(is.infinite(estimate))) {
estimate[is.infinite(estimate)] <-
max(estimate[finite_estimate], na.rm = TRUE) + 1
}
q <- matrix(stats::p.adjust(p, method = "BH"), nrow(p), ncol(p),
dimnames = dimnames(p))
q[estimate < 1] <- 1
statistics <- list(
n.actual = overlap,
p = p,
q = q
)
diagnostics <- list(
universe_size = universe_size,
explicit_universe = explicit_universe,
ags_sizes = lengths(ags),
fgs_sizes = lengths(fgs)
)
.new_nearender_result(
scores = list(odds_ratio = estimate),
score_info = .new_score_info(
score_id = "odds_ratio",
renderer = "ora",
metric = "odds_ratio"
),
statistics = statistics,
inputs = list(
ags_names = names(ags),
fgs_names = names(fgs),
ags_sizes = lengths(ags),
fgs_sizes = lengths(fgs),
universe_size = universe_size
),
diagnostics = diagnostics,
parameters = list(
case_sensitive = case_sensitive,
n_cores = n_cores,
explicit_universe = explicit_universe
)
)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.