|
1 | 1 | # S3 class definitions |
2 | 2 |
|
3 | | -# `geneSBML` class ------ |
| 3 | +# `reactDB` class -------- |
4 | 4 |
|
5 | | -#' Create a `geneSBML` object. |
| 5 | +#' Database of reaction annotation: `reactDB` class. |
6 | 6 | #' |
7 | 7 | #' @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. |
11 | 11 | #' |
12 | 12 | #' @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 |
15 | 24 | #' |
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. |
17 | 27 | #' |
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. |
27 | 30 | #' |
| 31 | +#' @md |
28 | 32 | #' @export |
29 | 33 |
|
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, ...) { |
40 | 35 |
|
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 -------- |
49 | 37 |
|
50 | | - if(!is.data.frame(reg)) stop(reg_error_txt, call. = FALSE) |
| 38 | + if(is_reactDB(x)) return(x) |
51 | 39 |
|
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) |
53 | 41 |
|
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" ) |
57 | 44 |
|
58 | | - if(!is.data.frame(gene_map)) stop(map_error_txt) |
| 45 | + missing_cols <- setdiff(fix_cols, names(x)) |
59 | 46 |
|
60 | | - if(any(!c("react_id", "entrez_id", "exprs") %in% names(gene_map))) { |
| 47 | + if(length(missing_cols) > 0) { |
61 | 48 |
|
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) |
63 | 52 |
|
64 | 53 | } |
65 | 54 |
|
66 | | - if(!is.null(mc)) { |
| 55 | + char_cols <- c("id", "name", "subsystem", "gene_association") |
67 | 56 |
|
68 | | - mc_error_txt <- "mc has to be a numeric matrix." |
| 57 | + class_check <- map_lgl(x[char_cols], is.character) |
69 | 58 |
|
70 | | - if(!is.numeric(mc)) stop(mc_error_txt, call. = FALSE) |
| 59 | + if(any(!class_check)) { |
71 | 60 |
|
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) |
73 | 64 |
|
74 | 65 | } |
75 | 66 |
|
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) |
82 | 68 |
|
83 | | - if(is.null(mc) & is.null(model)) { |
| 69 | + if(!is.list(x[["exprs"]])) stop("Column `exprs` must be a list.", call. = FALSE) |
84 | 70 |
|
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)) |
87 | 73 |
|
88 | | - } else { |
| 74 | + if(any(!class_check)) { |
89 | 75 |
|
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) |
92 | 79 |
|
93 | 80 | } |
94 | 81 |
|
95 | | - out_lst |
96 | | - |
97 | | - } |
| 82 | + ## the structure ------- |
98 | 83 |
|
99 | | -#' @rdname geneSBML |
100 | | -#' @export |
| 84 | + structure(x, class = c("reactDB", class(x))) |
101 | 85 |
|
102 | | - is_geneSBML <- function(x) inherits(x, "geneSBML") |
| 86 | + } |
103 | 87 |
|
104 | | -#' @rdname geneSBML |
| 88 | +#' @rdname reactDB |
105 | 89 | #' @export |
106 | 90 |
|
107 | | - is_memoSaver <- function(x) inherits(x, "geneSBML") & inherits(x, "memoSaver") |
| 91 | + is_reactDB <- function(x) inherits(x, "reactDB") |
108 | 92 |
|
109 | | -# `reactDB` class -------- |
| 93 | +# Storage of reaction activity data `actiData` class --------- |
110 | 94 |
|
111 | | -#' Database of reaction annotation: `reactDB` class. |
| 95 | +#' Storage of estimates of regulation of activity of metabolic reactions: `actiData` class. |
112 | 96 | #' |
113 | 97 | #' @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. |
117 | 100 | #' |
118 | 101 | #' @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: |
130 | 114 | #' |
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. |
132 | 120 | #' |
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. |
134 | 132 | #' |
135 | 133 | #' @md |
136 | 134 | #' @export |
137 | 135 |
|
138 | | - reactDB <- function(x) { |
| 136 | + actiData <- function(reg, mc = NULL, ...) { |
139 | 137 |
|
140 | | - ## input controls -------- |
| 138 | + ## input controls: mandatory columns in `reg` --------- |
141 | 139 |
|
142 | | - if(is_reactDB(x)) return(x) |
| 140 | + if(!is.data.frame(reg)) stop("`reg` has to be a data frame.", call. = FALSE) |
143 | 141 |
|
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") |
145 | 143 |
|
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)) |
150 | 145 |
|
151 | 146 | if(length(missing_cols) > 0) { |
152 | 147 |
|
|
156 | 151 |
|
157 | 152 | } |
158 | 153 |
|
159 | | - char_cols <- c("id", "name", "subsystem", "gene_association") |
| 154 | + char_cols <- c("id", "name", "subsystem") |
160 | 155 |
|
161 | | - class_check <- map_lgl(x[char_cols], is.character) |
| 156 | + char_check <- map_lgl(reg[, char_cols], is.character) |
162 | 157 |
|
163 | | - if(any(!class_check)) { |
| 158 | + if(any(!char_check)) { |
164 | 159 |
|
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 = ", ")), |
167 | 162 | call. = FALSE) |
168 | 163 |
|
169 | 164 | } |
170 | 165 |
|
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"]])) { |
172 | 167 |
|
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) |
174 | 169 |
|
175 | | - class_check <- |
176 | | - map_lgl(x[["exprs"]], function(x) is.call(x) | is.name(x) | is.null(x)) |
| 170 | + } |
177 | 171 |
|
178 | | - if(any(!class_check)) { |
| 172 | + ## input controls: optional columns in `reg` ------ |
179 | 173 |
|
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 | + } |
183 | 200 |
|
184 | 201 | } |
185 | 202 |
|
186 | | - ## the structure ------- |
| 203 | + ## the structure -------- |
187 | 204 |
|
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")) |
189 | 215 |
|
190 | 216 | } |
191 | 217 |
|
192 | | -#' @rdname reactDB |
| 218 | +#' @rdname actiData |
193 | 219 | #' @export |
194 | 220 |
|
195 | | - is_reactDB <- function(x) inherits(x, "reactDB") |
196 | | - |
| 221 | + is_actiData <- function(x) inherits(x, "actiData") |
197 | 222 |
|
198 | 223 | # END ------ |
0 commit comments