forked from microbiome/mia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetAbundant.R
380 lines (355 loc) · 12.2 KB
/
getAbundant.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#' @name
#' getAbundant
#'
#' @title
#' Determine abundant and rare taxa. Rare taxa can be further classified
#' to conditionally rare and permanently rare.
#'
#' @description
#' These functions determine abundant and rare taxa based on the abundances
#' of taxa. Compared to \code{\link[=getPrevalence]{getPrevalent}} and
#' \code{\link[=getPrevalence]{getRare}}, these functions determine abundant
#' and rare taxa based on abundance while the first mentioned are based on
#' prevalence.
#'
#' @details
#' These functions identify abundant and rare taxa in a dataset.
#' Abundant taxa are characterized by high average abundance across the dataset,
#' while rare taxa are characterized by consistently low abundance.
#'
#' Conditionally rare taxa exhibit variable abundance, being abundant in some
#' samples and rare in others. In contrast, permanently rare taxa consistently
#' maintain low abundance across all samples.
#'
#' \itemize{
#' \item Abundant taxa: Taxa with an average abundance exceeding
#' \code{abundant.th}.
#'
#' \item Low abundant / rare taxa: Taxa with an average abundance not
#' exceeding \code{abundant.th}. Optionally, if specified, they must also
#' satisfy the condition \eqn{crt.th >=
#' \frac{abundance_{max}}{abundance_{min}} > prt.th}.
#'
#' \item Conditionally rare or low abundant taxa (CRT): Taxa with an average
#' abundance not exceeding \code{abundant.th} and with a maximum-to-minimum
#' abundance ratio (\eqn{\frac{abundance_{max}}{abundance_{min}}}) greater
#' than \code{crt.th}.
#'
#' \item Permanently rare or low abundant taxa (PRT): Taxa with an average
#' abundance not exceeding \code{abundant.th} and with a maximum-to-minimum
#' abundance ratio (\eqn{\frac{abundance_{max}}{abundance_{min}}}) less than
#' or equal to \code{prt.th}.
#' }
#'
#' @return
#' For \code{getAbundant}, \code{getLowAbundant},
#' \code{getConditionallyLowAbundant}, and \code{getPermanentlyLowAbundant} a
#' \code{vector} of taxa. For \code{getAbudanceClass} a vector of abundance
#' classes for each feature. For \code{addAbudanceClass}, a
#' \code{SummarizedExperiment} object.
#'
#' @inheritParams addAlpha
#'
#' @param assay.type \code{Character scalar}. Specifies the name of assay
#' used in calculation. (Default: \code{"relabundance"})
#'
#' @param abundant.th \code{Numeric scalar}. Specifies threshold that is used
#' to separate abundant features from rare. (Default: \code{1/100})
#'
#' @param crt.th \code{Numeric scalar}. Specifies threshold that is used
#' to separate conditionally rare features from other rare features.
#' (Default: \code{100})
#'
#' @param prt.th \code{Numeric scalar}. Specifies threshold that is used
#' to separate permanently rare features from other rare features.
#' (Default: \code{5})
#'
#' @param name \code{Character scalar}. Specifies name of column in
#' \code{rowData} where the results will be stored.
#' (Default: \code{"abundance_class"})
#'
#' @param ... additional arguments.
#'
#' @examples
#'
#' data(GlobalPatterns)
#' tse <- GlobalPatterns
#'
#' # Agglomerate to family level
#' tse <- agglomerateByRank(tse, rank = "Family")
#' # Transform to relative abundances. Note that we add pseudocount. This is
#' # because otherwise we cannot calculate CRT and PRT due to zeroes and
#' # zero division in calculating abundance ratio.
#' tse <- transformAssay(tse, method = "relabundance", pseudocount = TRUE)
#'
#' # Get abundant taxa
#' abundant <- getAbundant(tse, assay.type = "relabundance")
#' abundant |> head()
#'
#' # Get all rare taxa that have average relative abundance below 10%
#' rare <- getLowAbundant(
#' tse, assay.type = "relabundance", abundant.th = 10/100)
#' rare |> head()
#'
#' # Get rare taxa that are not permanently or conditionally rare
#' rare <- getLowAbundant(
#' tse, assay.type = "relabundance", prt.th = 5, crt.th = 100)
#' rare |> head()
#'
#' # Get permanently rare taxa
#' prt <- getPermanentlyLowAbundant(
#' tse, assay.type = "relabundance", prt.th = 5)
#' prt |> head()
#'
#' # Get conditionally rare taxa
#' prt <- getConditionallyLowAbundant(
#' tse, assay.type = "relabundance", crt.th = 100)
#' prt |> head()
#'
#' # To classify all features, one can use *AbundantClass function
#' tse <- addAbundanceClass(tse)
#' # When one uses add* function, the results are stored to rowData
#' rowData(tse)
#'
#' @seealso
#' \code{\link[=getPrevalence]{getPrevalent}} and
#' \code{\link[=getPrevalence]{getRare}}
#'
#' @references
#'
#' Sizhong Y. et al. (2017) Community structure of rare methanogenic archaea:
#' insight from a single functional group- _FEMS Microbiol. Ecol._ 93(11).
#' \url{https://doi.org/10.1093/femsec/fix126}
#'
NULL
################################# getAbundant ##################################
#' @rdname getAbundant
#' @export
setMethod("getAbundant", signature = c(x = "SingleCellExperiment"),
function(x, ...){
x <- .check_and_get_altExp(x, ...)
res <- callNextMethod(x, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getAbundant", signature = c(x = "SummarizedExperiment"),
function(x, assay.type = "relabundance", ...){
.check_assay_present(assay.type, x)
mat <- assay(x, assay.type)
res <- getAbundant(mat, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getAbundant", signature = c(x = "ANY"),
function(x, abundant.th = 1/100, ...){
if( is.null(rownames(x)) ){
rownames(x) <- seq_len(nrow(x))
}
#
res <- .classify_by_abundance(x, abundant.th, ...)
res <- rownames(x)[ which( res == "abundant" ) ]
return(res)
}
)
################################ getLowAbundant ################################
#' @rdname getAbundant
#' @export
setMethod("getLowAbundant", signature = c(x = "SingleCellExperiment"),
function(x, ...){
x <- .check_and_get_altExp(x, ...)
res <- callNextMethod(x, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getLowAbundant", signature = c(x = "SummarizedExperiment"),
function(x, assay.type = "relabundance", abundant.th = 1/100, ...){
.check_assay_present(assay.type, x)
mat <- assay(x, assay.type)
res <- getLowAbundant(mat, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getLowAbundant", signature = c(x = "ANY"),
function(x, abundant.th = 1/100, ...){
if( is.null(rownames(x)) ){
rownames(x) <- seq_len(nrow(x))
}
#
res <- .classify_by_abundance(x, abundant.th, ...)
res <- rownames(x)[ which( res == "rare" ) ]
return(res)
}
)
######################### getConditionallyLowAbundant ##########################
#' @rdname getAbundant
#' @export
setMethod("getConditionallyLowAbundant",
signature = c(x = "SingleCellExperiment"),
function(x, ...){
x <- .check_and_get_altExp(x, ...)
res <- callNextMethod(x, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getConditionallyLowAbundant",
signature = c(x = "SummarizedExperiment"),
function(x, assay.type = "relabundance", ...){
.check_assay_present(assay.type, x)
mat <- assay(x, assay.type)
res <- getConditionallyLowAbundant(mat, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getConditionallyLowAbundant", signature = c(x = "ANY"),
function(x, abundant.th = 1/100, crt.th = 100, ...){
if( !.is_a_numeric(crt.th) && crt.th > 0 ){
stop("'crt.th' must be positive numeric value.", call. = FALSE)
}
if( is.null(rownames(x)) ){
rownames(x) <- seq_len(nrow(x))
}
#
res <- .classify_by_abundance(x, abundant.th, crt.th = crt.th, ...)
res <- rownames(x)[ which( res == "crt" ) ]
return(res)
}
)
########################## getPermanentlyLowAbundant ###########################
#' @rdname getAbundant
#' @export
setMethod("getPermanentlyLowAbundant",
signature = c(x = "SingleCellExperiment"),
function(x, ...){
x <- .check_and_get_altExp(x, ...)
res <- callNextMethod(x, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getPermanentlyLowAbundant",
signature = c(x = "SummarizedExperiment"),
function(x, assay.type = "relabundance", ...){
.check_assay_present(assay.type, x)
mat <- assay(x, assay.type)
res <- getPermanentlyLowAbundant(mat, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getPermanentlyLowAbundant", signature = c(x = "ANY"),
function(x, abundant.th = 1/100, prt.th = 5, ...){
if( !.is_a_numeric(prt.th) && prt.th > 0 ){
stop("'prt.th' must be positive numeric value.", call. = FALSE)
}
if( is.null(rownames(x)) ){
rownames(x) <- seq_len(nrow(x))
}
#
res <- .classify_by_abundance(x, abundant.th, prt.th = prt.th, ...)
res <- rownames(x)[ which( res == "prt" ) ]
return(res)
}
)
############################### getAbundanceClass ##############################
#' @rdname getAbundant
#' @export
setMethod("getAbundanceClass", signature = c(x = "SingleCellExperiment"),
function(x, ...){
x <- .check_and_get_altExp(x, ...)
res <- callNextMethod(x, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getAbundanceClass", signature = c(x = "SummarizedExperiment"),
function(x, assay.type = "relabundance", ...){
.check_assay_present(assay.type, x)
mat <- assay(x, assay.type)
res <- getAbundanceClass(mat, ...)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("getAbundanceClass", signature = c(x = "ANY"),
function(x, abundant.th = 1/100, crt.th = 100, prt.th = 5, ...){
if( is.null(rownames(x)) ){
rownames(x) <- seq_len(nrow(x))
}
#
res <- .classify_by_abundance(x, abundant.th, crt.th, prt.th, ...)
return(res)
}
)
############################### addAbundanceClass ##############################
#' @rdname getAbundant
#' @export
setMethod("addAbundanceClass", signature = c(x = "SingleCellExperiment"),
function(x, ...){
x <- .check_and_get_altExp(x, ...)
args <- c(list(x = x), list(...))
args <- args[ !names(args) %in% "altexp" ]
res <- do.call(callNextMethod, args)
return(res)
}
)
#' @rdname getAbundant
#' @export
setMethod("addAbundanceClass", signature = c(x = "SummarizedExperiment"),
function(x, name = "abundance_class", ...){
if( !.is_a_string(name) ){
stop("'name' must be a single character value.", call. = FALSE)
}
res <- getAbundanceClass(x, ...) |> list()
x <- .add_values_to_colData(x, res, name, MARGIN = 1L)
return(x)
}
)
################################ HELP FUNCTIONS ################################
# This function classifies the features based on their abundance. The result
# is a vector, an abundance class for each feature.
#' @importFrom DelayedMatrixStats rowMeans2 rowMaxs rowMins
.classify_by_abundance <- function(
mat, abundant.th, crt.th = NULL, prt.th = NULL, ...){
if( !(.is_a_numeric(abundant.th) && abundant.th > 0) ){
stop("'abundant.th' must be positive numeric value.", call. = FALSE)
}
if( !( is.null(crt.th) || (.is_a_numeric(crt.th) && crt.th > 0)) ){
stop("'crt.th' must be positive numeric value or NULL.", call. = FALSE)
}
if( !( is.null(prt.th) || (.is_a_numeric(prt.th) && prt.th > 0) ) ){
stop("'prt.th' must be positive numeric value or NULL.", call. = FALSE)
}
if( (!is.null(crt.th) || !is.null(prt.th)) && any(mat==0) ){
stop("CRT or PRT cannot be calculated when abundance contains zeroes. ",
"Please consider adding pseudocount.", call. = FALSE)
}
# Classify to abundant and rare based on average abundance
means <- rowMeans2(mat, ...)
res <- rep("abundant", length(means))
res[ means <= abundant.th ] <- "rare"
# Rare features are further assigned based on abundance MAX/MIN ratio
ratio <- rowMaxs(mat, ...)/rowMins(mat, ...)
if( !is.null(crt.th) ){
res[ res == "rare" & ratio > crt.th ] <- "crt"
}
if( !is.null(prt.th) ){
res[ res == "rare" & ratio <= prt.th ] <- "prt"
}
return(res)
}