/
AllClasses.R
346 lines (320 loc) · 10.6 KB
/
AllClasses.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# marker_table class ------------------------------------------------------
#' The S4 class for storing microbiome marker information
#'
#' This Class is inherit from `data.frame`. Rows represent the microbiome
#' markers and variables represents feature of the marker.
#'
#' @name marker_table-class
#' @aliases marker_table-class
#' @field names,row.names a character vector, inherited from the input
#' data.frame
#' @field .data a list, each element corresponding the each column of the
#' input data.frame
#' @field .S3Class character, the S3 class `marker_table` inherited from:
#' "`data.frame`"
#' @author Yang Cao
#' @exportClass marker_table
setClass("marker_table", contains = "data.frame")
# validator of marker_table
validity_marker_table <- function(object) {
msg <- NULL
if (!"feature" %in% names(object)) {
msg <- c(
msg,
"marker table must contain variable `feature`: the name of marker"
)
}
if (any(dim(object) == 0)) {
msg <- c(msg, "marker table must have non-zero dimensions")
}
if (length(msg)) {
return(msg)
} else {
return(TRUE)
}
}
setValidity("marker_table", validity_marker_table)
################################################################################
# A class may be defined as the union of other classes; that is, as a virtual
# class defined as a superclass of several other classes. This is a way of
# dealing with the expected scenarios in which one or more of the slot is not
# available, in which case NULL will be used instead.
################################################################################
#' @importClassesFrom phyloseq taxonomyTable
#' @keywords internal
setClassUnion("marker_tableOrNULL", c("marker_table", "NULL"))
#' @keywords internal
setClassUnion("taxonomyTableOrNULL", c("taxonomyTable", "NULL"))
#' @keywords internal
setClassUnion("characterOrNULL", c("character", "NULL"))
#' @keywords internal
setClassUnion("numericOrNULL", c("numeric", "NULL"))
# microbiomeMarker class --------------------------------------------------
#' The main class for microbiomeMarker data
#'
#' `microbiomeMarker-class` is inherited from the [`phyloseq::phyloseq-class`]
#' by adding a custom slot `microbiome_marker` to save the differential analysis
#' results. And it provides a seamless interface with **phyloseq**, which makes
#' **microbiomeMarker** simple and easy to use. For more details on see the
#' document of [`phyloseq::phyloseq-class`].
#' @name microbiomeMarker-class
#' @aliases microbiomeMarker-class
#' @importClassesFrom phyloseq phyloseq
#' @slot marker_table a data.frame, a [`marker_table-class`] object.
#' @slot norm_method character, method used to normalize the input `phyloseq`
#' object.
#' @slot diff_method character, method used for marker identification.
#' @seealso [`phyloseq::phyloseq-class`], [`marker_table-class`],
#' [summarize_taxa()]
#' @exportClass microbiomeMarker
#' @return a [`microbiomeMarker-class`] object.
`microbiomeMarker-class` <- setClass("microbiomeMarker",
slots = c(
marker_table = "marker_tableOrNULL",
norm_method = "characterOrNULL",
diff_method = "characterOrNULL"
),
contains = "phyloseq",
prototype = list(
marker_table = NULL,
norm_method = NULL,
diff_method = NULL
)
)
#' Build microbiomeMarker-class objects
#'
#' This the constructor to build the [`microbiomeMarker-class`] object, don't
#' use the `new()` constructor.
#' @param marker_table a [`marker_table-class`] object differtial analysis.
#' @param norm_method character, method used to normalize the input `phyloseq`
#' object.
#' @param diff_method character, method used for microbiome marker
#' identification.
#' @param ... arguments passed to [phyloseq::phyloseq()]
#' @seealso [phyloseq::phyloseq()]
#' @name microbiomeMarker
#' @export
#' @return a [`microbiomeMarker-class`] object.
#' @examples
#' microbiomeMarker(
#' marker_table = marker_table(data.frame(
#' feature = c("speciesA", "speciesB"),
#' enrich_group = c("groupA", "groupB"),
#' ef_logFC = c(-2, 2),
#' pvalue = c(0.01, 0.01),
#' padj = c(0.01, 0.01),
#' row.names = c("marker1", "marker2")
#' )),
#' norm_method = "TSS",
#' diff_method = "DESeq2",
#' otu_table = otu_table(matrix(
#' c(4, 1, 1, 4),
#' nrow = 2, byrow = TRUE,
#' dimnames = list(c("speciesA", "speciesB"), c("sample1", "sample2"))
#' ),
#' taxa_are_rows = TRUE
#' ),
#' tax_table = tax_table(matrix(
#' c("speciesA", "speciesB"),
#' nrow = 2,
#' dimnames = list(c("speciesA", "speciesB"), "Species")
#' )),
#' sam_data = sample_data(data.frame(
#' group = c("groupA", "groupB"),
#' row.names = c("sample1", "sample2")
#' ))
#' )
microbiomeMarker <- function(marker_table = NULL,
norm_method = NULL,
diff_method = NULL,
...) {
ps_slots <- list(...)
ps_component_cls <- vapply(ps_slots, class, character(1))
if (!"otu_table" %in% ps_component_cls) {
stop("otu_table is required")
}
if (!"taxonomyTable" %in% ps_component_cls) {
stop("tax_table is required")
}
# set the rownmaes of marker_table as "markern"
if (!is.null(marker_table)) {
rownames(marker_table) <- paste0("marker", seq_len(nrow(marker_table)))
}
new(
"microbiomeMarker",
marker_table = marker_table,
norm_method = norm_method,
diff_method = diff_method,
...
)
}
# validity for microbiomeMarker, at least contains two slots: otu_table,
# tax_table
#' @importMethodsFrom phyloseq taxa_names
validity_microbiomeMarker <- function(object) {
msg <- NULL
otu <- object@otu_table
tax <- object@tax_table
marker <- object@marker_table
norm_method <- object@norm_method
diff_method <- object@diff_method
# summarized taxa
if (is.null(tax)) {
msg <- c(msg, "tax_table is required")
}
if (is.null(otu)) {
msg <- c(msg, "otu_table is required")
}
# marker in marker_table must be contained in tax_table
if (!is.null(marker) && !is.null(tax) &&
!all(marker$feature %in% tax@.Data[, 1])) {
msg <- c(msg, "marker in marker_table must be contained in tax")
}
if (!is.null(otu) && !is.null(tax) && nrow(otu) != nrow(tax)) {
msg <- c(
msg,
"nrow of `otu_table` must be equal to the length of `tax_table()`"
)
}
if (!is.null(tax) && !is.null(marker) && nrow(marker) > nrow(tax)) {
msg <- c(
msg,
paste0(
"The number of different feature must be smaller than the",
" total number of feature"
)
)
}
if (length(msg)) {
return(msg)
} else {
return(TRUE)
}
}
setValidity("microbiomeMarker", validity_microbiomeMarker)
# postHocTest class ------------------------------------------------------
#' The postHocTest Class, represents the result of post-hoc test result among
#' multiple groups
#'
#' @slot result a [`IRanges::DataFrameList-class`], each `DataFrame` consists
#' of five variables:
#' * `comparisons`: character, specify which two groups to test (the group names
#' are separated by "_)
#' * `diff_mean`: numeric, difference in mean abundances
#' * `pvalue`: numeric, p values
#' * `ci_lower` and `ci_upper`: numeric, lower and upper confidence interval of
#' difference in mean abundances
#' @slot abundance abundance of each feature in each group
#' @slot conf_level confidence level
#' @slot method method used for post-hoc test
#' @slot method_str method illustration
#' @name postHocTest-class
#' @aliases postHocTest-class
#' @author Yang Cao
#' @exportClass postHocTest
#' @importClassesFrom IRanges DataFrameList
#' @return a [`postHocTest-class`] object.
setClass("postHocTest",
slots = c(
result = "DataFrameList",
abundance = "data.frame",
conf_level = "numeric",
method = "character",
method_str = "character"
),
prototype = list(
result = NULL,
conf_level = NULL,
method = NULL,
method_str = NULL
)
)
# validity for postHocTest
validity_postHocTest <- function(object) {
msg <- NULL
conf_level <- object@conf_level
if (!is.numeric(conf_level) || conf_level < 0 || conf_level > 1) {
msg <- c(
msg,
"conf_level must in the range of (0,1)"
)
}
method <- object@method
if (!method %in%
c("tukey", "games_howell", "scheffe", "welch_uncorrected")) {
msg <- c(
msg,
paste(
"method must be one of tukey, games_howell, scheffe or",
"welch_uncorrected"
)
)
}
if (length(msg)) {
return(msg)
} else {
return(TRUE)
}
}
setValidity("postHocTest", validity_postHocTest)
#' Build postHocTest object
#'
#' This function is used for create `postHocTest` object, and is only used for
#' developers.
#'
#' @param result a [`IRanges::SimpleDFrameList-class`] object.
#' @param abundance data.frame.
#' @param conf_level numeric, confidence level.
#' @param method character, method for posthoc test.
#' @param method_str character, illustrates which method is used for posthoc
#' test.
#' @return a [`postHocTest-class`] object.
#' @export
#' @examples
#' require(IRanges)
#' pht <- postHocTest(
#' result = DataFrameList(
#' featureA = DataFrame(
#' comparisons = c("group2-group1",
#' "group3-group1",
#' "group3-group2"),
#' diff_mean = runif(3),
#' pvalue = rep(0.01, 3),
#' ci_lower = rep(0.01, 3),
#' ci_upper = rep(0.011, 3)
#' ),
#' featureB = DataFrame(
#' comparisons = c("group2-group1",
#' "group3-group1",
#' "group3-group2"),
#' diff_mean = runif(3),
#' pvalue = rep(0.01, 3),
#' ci_lower = rep(0.01, 3),
#' ci_upper = rep(0.011, 3)
#' )
#' ),
#' abundance = data.frame(
#' featureA = runif(3),
#' featureB = runif(3),
#' group = c("group1", "group2", "grou3")
#' )
#' )
#' pht
postHocTest <- function(result,
abundance,
conf_level = 0.95,
method = "tukey",
method_str =
paste(
"Posthoc multiple comparisons of means: ",
method
)) {
new(
"postHocTest",
result = result,
abundance = abundance,
conf_level = conf_level,
method = method,
method_str = method_str
)
}