Skip to content

Commit 6c2a408

Browse files
committed
Class actiData for storage of estmates of regulation of reaction activity replacing the geneSBML class
1 parent 1adffb0 commit 6c2a408

File tree

14 files changed

+308
-173
lines changed

14 files changed

+308
-173
lines changed

NAMESPACE

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
S3method(get_activity,data.frame)
44
S3method(get_activity,matrix)
5+
export(actiData)
56
export(as_reactDB)
67
export(extract_genes)
7-
export(geneSBML)
88
export(get_activity)
99
export(get_regulation)
10-
export(is_geneSBML)
11-
export(is_memoSaver)
10+
export(is_actiData)
1211
export(is_reactDB)
1312
export(reactDB)
1413
importFrom(Rcpp,sourceCpp)

R/classes.R

Lines changed: 136 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,147 @@
11
# S3 class definitions
22

3-
# `geneSBML` class ------
3+
# `reactDB` class --------
44

5-
#' Create a `geneSBML` object.
5+
#' Database of reaction annotation: `reactDB` class.
66
#'
77
#' @description
8-
#' Creates a `geneSBML` object given a SBML model object and a
9-
#' data frame with reaction regulation estimates and, optionally, regulation
10-
#' errors, and a data frame with gene - reaction mapping.
8+
#' Constructs an instance of a reaction annotation database data frame with
9+
#' processed gene - reaction association rules and gene - reaction rule evaluation
10+
#' expressions.
1111
#'
1212
#' @details
13-
#' In `model = NULL` and `mc = NULL`, a mamory saver object is returned
14-
#' (subclass `memoSaver`).#'
13+
#' The function conducts basic validation for conformity with reaction
14+
#' evaluation tools.
15+
#' The input and output data frames have the following columns:
16+
#' * __id__: reaction identifier beginning with `"R_"` string
17+
#' * __name__: character strings with reaction names
18+
#' * __subsystem__: character strings with names of Recon subsystems
19+
#' * __gene_association__: character strings with gene - reaction association rules
20+
#' * __entrez_id__: a column with lists of Entrez IDs of genes associated with
21+
#' the reactions
22+
#' * __exprs__: a column with `NULL`, R symbols or R language expressions used
23+
#' to evaluate the gene - reaction association rules
1524
#'
16-
#' @return an instance of `geneSBML` class.
25+
#' @return `reactDB()`: a data frame of class `reactDB` described in Details;
26+
#' `is_reactDB()`: a logical value indicating it an object is an instance of `reactDB` class.
1727
#'
18-
#' @param x an object.
19-
#' @param model `NULL` or a `SBML` model.
20-
#' @param reg a data frame with reaction IDs (`react_id`), regulation estimates
21-
#' (`fold_reg`) and, optionally, regulation errors (`error`).
22-
#' @param gene_map a data frame with reaction IDs (`react_id`),
23-
#' list of Entrez ID identifiers (`react_id`)
24-
#' and evaluation expressions (`react_id`).
25-
#' @param mc optional, a matrix with reaction regulation estimates in each step
26-
#' of Monte Carlo simulation. Columns are reactions, rows are subsequent runs.
28+
#' @param x a data frame with columns specified in Details or an R object.
29+
#' @param ... additional arguments, currently none.
2730
#'
31+
#' @md
2832
#' @export
2933

30-
geneSBML <- function(model,
31-
reg,
32-
gene_map,
33-
mc = NULL) {
34-
35-
## entry control -------
36-
37-
if(!is.null(model)) {
38-
39-
if(!inherits(model, "Model")) {
34+
reactDB <- function(x, ...) {
4035

41-
stop("model has to be a valid SBML model.", call. = FALSE)
42-
43-
}
44-
45-
}
46-
47-
reg_error_txt <-
48-
"`reg` has to be a data frame with `react_id` and `fold_reg` columns."
36+
## input controls --------
4937

50-
if(!is.data.frame(reg)) stop(reg_error_txt, call. = FALSE)
38+
if(is_reactDB(x)) return(x)
5139

52-
if(any(!c("react_id", "fold_reg") %in% names(reg))) stop(reg_error_txt, call. = FALSE)
40+
if(!is.data.frame(x)) stop("`x` has to be a data frame.", call. = FALSE)
5341

54-
map_error_txt <-
55-
paste("`gene_map` has to be a data frame with `react_id`,",
56-
"`entrez_id` and `exprs` columns.")
42+
fix_cols <-
43+
c("id", "name", "subsystem", "gene_association", "entrez_id", "exprs" )
5744

58-
if(!is.data.frame(gene_map)) stop(map_error_txt)
45+
missing_cols <- setdiff(fix_cols, names(x))
5946

60-
if(any(!c("react_id", "entrez_id", "exprs") %in% names(gene_map))) {
47+
if(length(missing_cols) > 0) {
6148

62-
stop(map_error_txt, call. = FALSE)
49+
stop(paste("The following obligatory columns are missing from `x`:",
50+
paste(missing_cols, collapse = ", ")),
51+
call. = FALSE)
6352

6453
}
6554

66-
if(!is.null(mc)) {
55+
char_cols <- c("id", "name", "subsystem", "gene_association")
6756

68-
mc_error_txt <- "mc has to be a numeric matrix."
57+
class_check <- map_lgl(x[char_cols], is.character)
6958

70-
if(!is.numeric(mc)) stop(mc_error_txt, call. = FALSE)
59+
if(any(!class_check)) {
7160

72-
if(!is.matrix(mc)) stop(mc_error_txt, call. = FALSE)
61+
stop(paste("The following obligatory column in `x` must be of character type:",
62+
paste(char_cols[!class_check]), collapse = ", "),
63+
call. = FALSE)
7364

7465
}
7566

76-
## the output object ---------
77-
78-
out_lst <- list(model = model,
79-
reg = reg,
80-
gene_map = gene_map,
81-
mc = mc)
67+
if(!is.list(x[["entrez_id"]])) stop("Column `entrez_id` must be a list of Entrez IDs.", call. = FALSE)
8268

83-
if(is.null(mc) & is.null(model)) {
69+
if(!is.list(x[["exprs"]])) stop("Column `exprs` must be a list.", call. = FALSE)
8470

85-
out_lst <-
86-
structure(out_lst, class = c("memoSaver", "geneSBML"))
71+
class_check <-
72+
map_lgl(x[["exprs"]], function(x) is.call(x) | is.name(x) | is.null(x))
8773

88-
} else {
74+
if(any(!class_check)) {
8975

90-
out_lst <-
91-
structure(out_lst, class = "geneSBML")
76+
stop(paste("Unrecognized objects in `exprs` columns.",
77+
"The allowed formats are NULL, R calls, or names."),
78+
call. = FALSE)
9279

9380
}
9481

95-
out_lst
96-
97-
}
82+
## the structure -------
9883

99-
#' @rdname geneSBML
100-
#' @export
84+
structure(x, class = c("reactDB", class(x)))
10185

102-
is_geneSBML <- function(x) inherits(x, "geneSBML")
86+
}
10387

104-
#' @rdname geneSBML
88+
#' @rdname reactDB
10589
#' @export
10690

107-
is_memoSaver <- function(x) inherits(x, "geneSBML") & inherits(x, "memoSaver")
91+
is_reactDB <- function(x) inherits(x, "reactDB")
10892

109-
# `reactDB` class --------
93+
# Storage of reaction activity data `actiData` class ---------
11094

111-
#' Database of reaction annotation: `reactDB` class.
95+
#' Storage of estimates of regulation of activity of metabolic reactions: `actiData` class.
11296
#'
11397
#' @description
114-
#' Constructs an instance of a reaction annotation database data frame with
115-
#' processed gene - reaction association rules and gene - reaction rule evaluation
116-
#' expressions.
98+
#' A list for storage of estimates of regulation of activity of metabolic reactions
99+
#' and, optionally, results of Monte Carlo simulations of reaction activity.
117100
#'
118101
#' @details
119-
#' The function conducts basic validation for conformity with reaction
120-
#' evaluation tools.
121-
#' The input and output data frames have the following columns:
122-
#' * __id__: reaction identifier beginning with `"R_"` string.
123-
#' * __name__: character strings with reaction names.
124-
#' * __subsystem__: character strings with names of Recon subsystems.
125-
#' * __gene_association__: character strings with gene - reaction association rules.
126-
#' * __entrez_id__: a column with lists of Entrez IDs of genes associated with
127-
#' the reactions.
128-
#' * __exprs__: a column with `NULL`, R symbols or R language expressions used
129-
#' to evaluate the gene - reaction association rules.
102+
#' `actiData` class objects are lists with the obligatory element `reg`,
103+
#' a data frame which stores the reaction regulation estimates, and `mc`,
104+
#' a numeric matrix of regulation estimates in single iterations of Monte Carlo
105+
#' simulations.
106+
#' The `reg` data frame is expected to have the following obligatory columns:
107+
#'
108+
#' * __id__ and __name__: character variables storing identifiers of metabolic reactions
109+
#' * __subsystem__: a character variable with names of Recon subsystems
110+
#' * __fold_reg__: a numeric variable with estimates of fold-regulation of reaction
111+
#' activity.
112+
#'
113+
#' The optional columns in `reg` are:
130114
#'
131-
#' @return a data frame of class `reactDB`.
115+
#' * __error__, __lower_ci__, and __upper_ci__: numeric variables with errors,
116+
#' lower and upper bounds of confidence intervals of fold-regulation of reaction activity.
117+
#' * __z__: a numeric variable with values of the test statistic Z
118+
#' * __p_value__ and __p_adjusted__: numeric variables with raw p values and
119+
#' p-values adjusted for multiple testing.
132120
#'
133-
#' @param x a data frame with columns specified in Details.
121+
#'
122+
#' @return `actiData()`: a list of class `actiData` as described in Details,
123+
#' `is_actiData()`: a logical value indicating if an object is an instance of
124+
#' `actiData` class.
125+
#'
126+
#' @param x an object
127+
#' @param reg a data frame with estimates of regulation of metabolic reactions
128+
#' as described in Details.
129+
#' @param mc `NULL` or a numeric matrix with estimates of activity of metabolic
130+
#' reactions obtained in Monte-Carlo simulations.
131+
#' @param ... additional arguments, currently none.
134132
#'
135133
#' @md
136134
#' @export
137135

138-
reactDB <- function(x) {
136+
actiData <- function(reg, mc = NULL, ...) {
139137

140-
## input controls --------
138+
## input controls: mandatory columns in `reg` ---------
141139

142-
if(is_reactDB(x)) return(x)
140+
if(!is.data.frame(reg)) stop("`reg` has to be a data frame.", call. = FALSE)
143141

144-
if(!is.data.frame(x)) stop("`x` has to be a data frame.", call. = FALSE)
142+
fix_cols <- c("id", "name", "subsystem", "fold_reg")
145143

146-
fix_cols <-
147-
c("id", "name", "subsystem", "gene_association", "entrez_id", "exprs" )
148-
149-
missing_cols <- setdiff(fix_cols, names(x))
144+
missing_cols <- setdiff(fix_cols, names(reg))
150145

151146
if(length(missing_cols) > 0) {
152147

@@ -156,43 +151,73 @@
156151

157152
}
158153

159-
char_cols <- c("id", "name", "subsystem", "gene_association")
154+
char_cols <- c("id", "name", "subsystem")
160155

161-
class_check <- map_lgl(x[char_cols], is.character)
156+
char_check <- map_lgl(reg[, char_cols], is.character)
162157

163-
if(any(!class_check)) {
158+
if(any(!char_check)) {
164159

165-
stop(paste("The following obligatory column in `x` must be of character type:",
166-
paste(char_cols[!class_check]), collapse = ", "),
160+
stop(paste("The following columns in `x` must be character variables:",
161+
paste(char_cols[!char_check], collapse = ", ")),
167162
call. = FALSE)
168163

169164
}
170165

171-
if(!is.list(x[["entrez_id"]])) stop("Column `entrez_id` must be a list of Entrez IDs.", call. = FALSE)
166+
if(!is.numeric(reg[["fold_reg"]])) {
172167

173-
if(!is.list(x[["exprs"]])) stop("Column `exprs` must be a list.", call. = FALSE)
168+
stop("`fold_reg` column in `x` has to be numeric.", call = FALSE)
174169

175-
class_check <-
176-
map_lgl(x[["exprs"]], function(x) is.call(x) | is.name(x) | is.null(x))
170+
}
177171

178-
if(any(!class_check)) {
172+
## input controls: optional columns in `reg` ------
179173

180-
stop(paste("Unrecognized objects in `exprs` columns.",
181-
"The allowed formats are NULL, R calls, or names."),
182-
call. = FALSE)
174+
if("z" %in% names(reg)) stopifnot(is.numeric(reg[["z"]]))
175+
if("error" %in% names(reg)) stopifnot(is.numeric(reg[["error"]]))
176+
if("lower_ci" %in% names(reg)) stopifnot(is.numeric(reg[["lower_ci"]]))
177+
if("upper_ci" %in% names(reg)) stopifnot(is.numeric(reg[["upper_ci"]]))
178+
if("p_value" %in% names(reg)) stopifnot(is.numeric(reg[["p_value"]]))
179+
if("p_adjusted" %in% names(reg)) stopifnot(is.numeric(reg[["p_adjusted"]]))
180+
181+
## input controls: `mc` matrix --------
182+
183+
if(!is.null(mc)) {
184+
185+
mc_err <- "`mc` has to be a numeric matrix."
186+
187+
if(!is.matrix(mc)) stop(mc_err, call. = FALSE)
188+
if(!is.numeric(mc)) stop(mc_err, call. = FALSE)
189+
190+
reaction_ids <- reg[["id"]]
191+
mc_ids <- colnames(mc)
192+
193+
stopifnot(!is.null(mc_ids))
194+
195+
if(any(!reaction_ids %in% mc_ids) | any(!mc_ids %in% reaction_ids)) {
196+
197+
stop("Reactions in `reg` and `mc` do not match.", call. = FALSE)
198+
199+
}
183200

184201
}
185202

186-
## the structure -------
203+
## the structure --------
187204

188-
structure(x, class = c("reactDB", class(x)))
205+
if(is.null(mc)) {
206+
207+
return(structure(list(reg = reg),
208+
class = "actiData"))
209+
210+
}
211+
212+
return(structure(list(reg = reg,
213+
mc = mc),
214+
class = "actiData"))
189215

190216
}
191217

192-
#' @rdname reactDB
218+
#' @rdname actiData
193219
#' @export
194220

195-
is_reactDB <- function(x) inherits(x, "reactDB")
196-
221+
is_actiData <- function(x) inherits(x, "actiData")
197222

198223
# END ------

0 commit comments

Comments
 (0)