Skip to content

Commit 4caa6bd

Browse files
committed
Function suba() for enrichment analyses of metabolic subsystems
1 parent 3dbdc32 commit 4caa6bd

File tree

11 files changed

+200
-10
lines changed

11 files changed

+200
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ Authors@R: c(
1414
comment = c(ORCID = "0000-0002-0398-6034"))
1515
)
1616
Imports:
17+
fastTest,
1718
furrr,
1819
future,
1920
ggplot2,
20-
microViz,
2121
purrr,
2222
Rcpp,
2323
rlang,

NAMESPACE

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export(plot_mc)
2727
export(plot_numbers)
2828
export(reactDB)
2929
export(select.actiData)
30+
export(suba)
3031
export(transform_estimates)
3132
importFrom(Rcpp,sourceCpp)
3233
importFrom(dplyr,all_of)
@@ -41,7 +42,9 @@ importFrom(dplyr,relocate)
4142
importFrom(dplyr,select)
4243
importFrom(dplyr,summarise)
4344
importFrom(dplyr,tibble)
45+
importFrom(dplyr,transmute)
4446
importFrom(dplyr,ungroup)
47+
importFrom(fastTest,f_enrichment)
4548
importFrom(furrr,furrr_options)
4649
importFrom(furrr,future_map)
4750
importFrom(future,plan)
@@ -60,7 +63,7 @@ importFrom(ggplot2,labs)
6063
importFrom(ggplot2,position_jitter)
6164
importFrom(ggplot2,scale_fill_manual)
6265
importFrom(ggplot2,scale_x_continuous)
63-
importFrom(microViz,theme_micro)
66+
importFrom(ggplot2,theme_classic)
6467
importFrom(purrr,compact)
6568
importFrom(purrr,map)
6669
importFrom(purrr,map2_chr)

R/enrichment_funs.R

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# functions for subsystem enrichment analyses
2+
3+
#' Subsystem enrichment analysis for differentially regulated reactions.
4+
#'
5+
#' @description
6+
#' Function `suba()` performs enrichment analyses for member reactions of
7+
#' metabolic subsystems among significantly activated and inhibited reactions in
8+
#' an \code{\link{actiData}} object.
9+
#' Please note, that prior to the analyses, significantly activated and
10+
#' inhibited reactions need to by determined by calling
11+
#' \code{\link{identify_regulated}}.
12+
#'
13+
#' @details
14+
#' The analyses are done with \code{\link[fastTest]{f_enrichment}} with two methods:
15+
#' Fisher's exact test (`type = "fisher`) or by random drawing from the entire
16+
#' reaction pool (`type = "random"`).
17+
#'
18+
#' @return a data frame with enrichment analysis results:
19+
#' numbers of reactions in the activated and inhibited reaction sets and in
20+
#' the subsystems, enrichment magnitude measured by odds ratio (OR) statistic,
21+
#' raw p values, and p values adjusted for multiple testing.
22+
#'
23+
#' @param x a \code{\link{actiData}} object.
24+
#' @param type type of the hypothesis test: Fisher's exact test or random drawing
25+
#' from the whole reaction pool.
26+
#' @param ... additional arguments passed to \code{\link[fastTest]{f_enrichment}}
27+
#' which specify e.g. type of confidence intervals for the OR statistic and
28+
#' number of random draws, and serial/parallel execution of the testing algorithm.
29+
#'
30+
#' @seealso [get_regulation()]
31+
#'
32+
#' @export
33+
34+
suba <- function(x,
35+
type = c("fisher", "random"), ...) {
36+
37+
## the input control -------
38+
39+
if(!is_actiData(x)) {
40+
41+
stop("`x` has to be an `actiData` object created e.g. with `get_regulation()`",
42+
call. = FALSE)
43+
44+
}
45+
46+
type <- match.arg(type[[1]], c("fisher", "random"))
47+
48+
if(!"regulation" %in% names(x[["reg"]])) {
49+
50+
stop(paste("No regulation status in the `x` object.",
51+
"Please call `identify_regulated()`",
52+
"prior to launching `suba()`."),
53+
call. = FALSE)
54+
55+
}
56+
57+
## the testing data ---------
58+
59+
### vectors of significantly regulated reactions
60+
61+
signif_reactions <-
62+
filter(x$reg,
63+
.data[["regulation"]] %in% c("activated", "inhibited"))
64+
65+
if(nrow(signif_reactions) == 0) {
66+
67+
stop("No significantly regulated reactions.", call. = FALSE)
68+
69+
}
70+
71+
signif_reactions <-
72+
split(signif_reactions[["id"]],
73+
f = signif_reactions[["regulation"]])
74+
75+
signif_reactions <-
76+
compact(map(signif_reactions,
77+
function(x) if(length(x) == 0) NULL else x))
78+
79+
### dictionary of reactions in the subsystems
80+
### vector with identifiers of all reactions
81+
82+
reaction_dict <- split(x[["reg"]][["id"]],
83+
x[["reg"]][["subsystem"]])
84+
85+
all_reactions <- unique(x[["reg"]][["id"]])
86+
87+
## testing and customizing the results ---------
88+
89+
result <- list()
90+
91+
for(i in names(signif_reactions)) {
92+
93+
result[[i]] <- f_enrichment(x = signif_reactions[[i]],
94+
type = type,
95+
dict = reaction_dict,
96+
all = all_reactions,
97+
as_data_frame = TRUE,
98+
adj_method = "BH", ...)
99+
100+
}
101+
102+
reaction_set <- NULL
103+
104+
result <-
105+
map2_dfr(result, names(result),
106+
~mutate(.x,
107+
reaction_set = factor(.y,
108+
c("activated", "inhibited"))))
109+
110+
n_total_reaction_set <- NULL
111+
subsystem <- NULL
112+
n_total_subsystem <- NULL
113+
n_intersect <- NULL
114+
or <- NULL
115+
lower_ci <- NULL
116+
upper_ci <- NULL
117+
p_value <- NULL
118+
p_adjusted <- NULL
119+
120+
transmute(as_tibble(result),
121+
reaction_set = .data[["reaction_set"]],
122+
n_total_reaction_set = .data[["n_x_total"]],
123+
subsytem = .data[["entry_name"]],
124+
n_total_subsystem = .data[["n_entry"]],
125+
n_intersect = .data[["n_intersect"]],
126+
or = .data[["or"]],
127+
lower_ci = if(type == "fisher") NULL else .data[["lower_ci"]],
128+
upper_ci = if(type == "fisher") NULL else .data[["upper_ci"]],
129+
p_value = .data[["p_value"]],
130+
p_adjusted = .data[["p_adjusted"]])
131+
132+
}
133+
134+
# END ----------

R/imports.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#' @importFrom dplyr filter
44
#' @importFrom dplyr mutate
5+
#' @importFrom dplyr transmute
56
#' @importFrom dplyr count
67
#' @importFrom dplyr tibble
78
#' @importFrom dplyr select
@@ -45,6 +46,7 @@
4546
#' @importFrom ggplot2 position_jitter
4647
#' @importFrom ggplot2 is.theme
4748
#' @importFrom ggplot2 labs
49+
#' @importFrom ggplot2 theme_classic
4850
#'
4951
#' @importFrom rlang set_names
5052
#' @importFrom rlang `.data`
@@ -77,7 +79,7 @@
7779
#' @importFrom stats pnorm
7880
#' @importFrom stats na.omit
7981
#'
80-
#' @importFrom microViz theme_micro
82+
#' @importFrom fastTest f_enrichment
8183
#'
8284
#' @useDynLib biggrExtra
8385

R/plotting_funs.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
hline_color = "black",
7171
hline_type = "dashed",
7272
hline_alpha = 1,
73-
cust_theme = microViz::theme_micro(),
73+
cust_theme = theme_classic(),
7474
plot_title = NULL,
7575
plot_subtitle = NULL,
7676
x_lab = "regulation estimate",
@@ -241,7 +241,7 @@
241241
zero_line_color = "black",
242242
zero_line_type = "dashed",
243243
zero_line_alpha = 1,
244-
cust_theme = microViz::theme_micro(),
244+
cust_theme = theme_classic(),
245245
plot_title = NULL,
246246
plot_subtitle = NULL,
247247
x_lab = NULL,
@@ -435,7 +435,7 @@
435435
show_all = FALSE,
436436
labeller_fun = identity,
437437
show_n_total = TRUE,
438-
cust_theme = microViz::theme_micro(),
438+
cust_theme = theme_classic(),
439439
plot_title = NULL,
440440
plot_subtitle = NULL,
441441
x_lab = NULL,

inst/examples/development.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@
183183
point_alpha = 0.15,
184184
plot_title = "Oxidative phosphorylation")
185185

186+
tst_mc_estimates <- tst_mc_estimates %>%
187+
identify_regulated
188+
186189
tst_mc_estimates %>%
187-
identify_regulated %>%
188190
plot(plot_type = "numbers",
189191
scale = "percent",
190192
type = "plus_minus",
@@ -197,5 +199,14 @@
197199

198200
# Subsystem enrichment ---------
199201

202+
tst_enrichment <- tst_mc_estimates %>%
203+
suba(type = "random",
204+
n_iter = 1000,
205+
.parallel = TRUE)
206+
207+
tst_enrichment %>%
208+
filter(p_adjusted < 0.05,
209+
or >= 1.44) %>%
210+
blast(reaction_set)
200211

201212
# END --------

man/plot_errors.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_mc.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_numbers.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/suba.Rd

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)