20
20
# ' Can be a list for multiple, independent comparisons.
21
21
# ' @param min_cells a numeric. Specifies the minimum number of cells in a given
22
22
# ' cluster-sample required to consider the sample for differential testing.
23
- # ' @param filter characterstring specifying whether
23
+ # ' @param filter character string specifying whether
24
24
# ' to filter on genes, samples, both or neither.
25
25
# ' @param treat logical specifying whether empirical Bayes moderated-t
26
26
# ' p-values should be computed relative to a minimum fold-change threshold.
82
82
# ' @export
83
83
84
84
pbDS <- function (pb ,
85
- method = c(" edgeR" , " DESeq2" , " limma-trend" , " limma-voom" ),
86
- design = NULL , coef = NULL , contrast = NULL , min_cells = 10 ,
87
- filter = c(" both" , " genes" , " samples" , " none" ), treat = FALSE ,
88
- verbose = TRUE , BPPARAM = SerialParam(progressbar = verbose )) {
85
+ method = c(" edgeR" , " DESeq2" , " limma-trend" , " limma-voom" , " DD " ),
86
+ design = NULL , coef = NULL , contrast = NULL , min_cells = 10 ,
87
+ filter = c(" both" , " genes" , " samples" , " none" ), treat = FALSE ,
88
+ verbose = TRUE , BPPARAM = SerialParam(progressbar = verbose )) {
89
89
90
90
# check validity of input arguments
91
91
args <- as.list(environment())
92
92
method <- match.arg(method )
93
93
filter <- match.arg(filter )
94
- .check_pbs(pb , check_by = TRUE )
94
+ .check_pbs(pb , check_by = TRUE )
95
95
.check_args_pbDS(args )
96
96
stopifnot(is(BPPARAM , " BiocParallelParam" ))
97
97
@@ -104,7 +104,7 @@ pbDS <- function(pb,
104
104
}
105
105
if (is.null(coef ) & is.null(contrast )) {
106
106
c <- colnames(design )[ncol(design )]
107
- contrast <- makeContrasts(contrasts = c , levels = design )
107
+ contrast <- makeContrasts(contrasts = c , levels = design )
108
108
args $ contrast <- contrast
109
109
}
110
110
@@ -117,18 +117,19 @@ pbDS <- function(pb,
117
117
if (! is.list(coef ))
118
118
coef <- list (coef )
119
119
cs <- vapply(coef , function (i )
120
- paste(colnames(design )[i ], collapse = " -" ),
120
+ paste(colnames(design )[i ], collapse = " -" ),
121
121
character (1 ))
122
122
names(cs ) <- names(coef ) <- cs
123
123
}
124
124
ct <- ifelse(is.null(coef ), " contrast" , " coef" )
125
125
126
126
if (! is.function(method )) {
127
127
fun <- switch (method ,
128
- " DESeq2" = .DESeq2 ,
129
- " edgeR" = .edgeR ,
130
- " limma-trend" = .limma_trend ,
131
- " limma-voom" = .limma_voom )
128
+ " DD" = .edgeR_NB ,
129
+ " edgeR" = .edgeR ,
130
+ " DESeq2" = .DESeq2 ,
131
+ " limma-voom" = .limma_voom ,
132
+ " limma-trend" = .limma_trend )
132
133
} else {
133
134
fun_call <- 1
134
135
}
@@ -139,28 +140,30 @@ pbDS <- function(pb,
139
140
n_cells <- .n_cells(pb )
140
141
names(kids ) <- kids <- assayNames(pb )
141
142
res <- bplapply(
142
- BPPARAM = BPPARAM ,
143
+ BPPARAM = BPPARAM ,
143
144
kids , function (k ) {
144
145
rmv <- n_cells [k , ] < min_cells
145
- d <- design [colnames(y <- pb [ , ! rmv ]), , drop = FALSE ]
146
+ d <- design [colnames(y <- pb [ , ! rmv ]), , drop = FALSE ]
146
147
if (filter %in% c(" samples" , " both" )) {
147
148
ls <- colSums(assay(y , k ))
148
- ol <- isOutlier(ls , log = TRUE , type = " lower" , nmads = 3 )
149
- d <- d [colnames(y <- y [, ! ol ]), , drop = FALSE ]
149
+ ol <- isOutlier(ls , log = TRUE , type = " lower" , nmads = 3 )
150
+ d <- d [colnames(y <- y [, ! ol ]), , drop = FALSE ]
150
151
}
151
152
if (any(tabulate(y $ group_id ) < 2 )
152
- || qr(d )$ rank == nrow(d )
153
+ || qr(d )$ rank == nrow(d )
153
154
|| qr(d )$ rank < ncol(d ))
154
155
return (NULL )
155
- y <- y [rowSums(assay(y , k )) != 0 , , drop = FALSE ]
156
+ y <- y [rowSums(assay(y , k )) != 0 , , drop = FALSE ]
156
157
if (filter %in% c(" genes" , " both" ) & max(assay(y , k )) > 100 )
157
- y <- y [filterByExpr(assay(y , k ), d ), , drop = FALSE ]
158
+ y <- y [filterByExpr(assay(y , k ), d ), , drop = FALSE ]
158
159
# drop samples without any detected features
159
160
keep <- colAnys(assay(y , k ) > 0 )
160
- y <- y [, keep , drop = FALSE ]
161
- d <- d [keep , , drop = FALSE ]
162
- args <- list (x = y , k = k , design = d , coef = coef ,
163
- contrast = contrast , ct = ct , cs = cs , treat = treat )
161
+ y <- y [, keep , drop = FALSE ]
162
+ d <- d [keep , , drop = FALSE ]
163
+ args <- list (
164
+ x = y , k = k , design = d , coef = coef ,
165
+ contrast = contrast , ct = ct , cs = cs ,
166
+ treat = treat , nc = n_cells [k , ! rmv ])
164
167
args <- args [intersect(names(args ), fun_args )]
165
168
suppressWarnings(do.call(fun , args ))
166
169
})
@@ -169,14 +172,24 @@ pbDS <- function(pb,
169
172
rmv <- vapply(res , is.null , logical (1 ))
170
173
res <- res [! rmv ]
171
174
172
- if (length(res ) == 0 ) stop(
175
+ if (length(res )== 0 ) stop(
173
176
" Specified filtering options result in no genes in any clusters " ,
174
177
" being tested. To force testing, consider modifying arguments " ,
175
178
" 'min_cells' and/or 'filter'. See '?pbDS' for details." )
176
179
177
180
# reorganize & do global p-value adjustment
178
181
names(i ) <- i <- c(" table" , " data" , " fit" )
179
- res <- lapply(i , map , .x = res )
182
+ res <- lapply(i , map , .x = res )
180
183
res $ table <- .p_adj_global(res $ table )
181
- return (c(res , list (args = args )))
184
+ return (c(res , list (args = args )))
185
+ }
186
+
187
+ # ' @rdname pbDS
188
+ # ' @export
189
+ pbDD <- function (pb , design = NULL , coef = NULL , contrast = NULL ,
190
+ min_cells = 10 , filter = c(" both" , " genes" , " samples" , " none" ),
191
+ verbose = TRUE , BPPARAM = SerialParam(progressbar = verbose ))
192
+ {
193
+ args <- as.list(environment())
194
+ do.call(pbDS , c(args , list (method = " DD" )))
182
195
}
0 commit comments