forked from GreenleafLab/ArchR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidationUtils.R
369 lines (261 loc) · 9.81 KB
/
ValidationUtils.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
##########################################################################################
# Validation Methods
##########################################################################################
.validInput <- function(input = NULL, name = NULL, valid = NULL){
valid <- unique(valid)
if(is.character(valid)){
valid <- tolower(valid)
}else{
stop("Validator must be a character!")
}
if(!is.character(name)){
stop("name must be a character!")
}
if("null" %in% tolower(valid)){
valid <- c("null", valid[which(tolower(valid) != "null")])
}
av <- FALSE
for(i in seq_along(valid)){
vi <- valid[i]
if(vi == "integer" | vi == "wholenumber"){
if(all(is.numeric(input))){
#https://stackoverflow.com/questions/3476782/check-if-the-number-is-integer
cv <- min(abs(c(input%%1, input%%1-1)), na.rm = TRUE) < .Machine$double.eps^0.5
}else{
cv <- FALSE
}
}else if(vi == "null"){
cv <- is.null(input)
}else if(vi == "bool" | vi == "boolean" | vi == "logical"){
cv <- is.logical(input)
}else if(vi == "numeric"){
cv <- is.numeric(input)
}else if(vi == "vector"){
cv <- is.vector(input)
}else if(vi == "matrix"){
cv <- is.matrix(input)
}else if(vi == "sparsematrix"){
cv <- is(input, "dgCMatrix")
}else if(vi == "character"){
cv <- is.character(input)
}else if(vi == "factor"){
cv <- is.factor(input)
}else if(vi == "rlecharacter"){
cv1 <- is(input, "Rle")
if(cv1){
cv <- is(input@values, "factor") || is(input@values, "character")
}else{
cv <- FALSE
}
}else if(vi == "palette"){
cv <- all(.isColor(input))
}else if(vi == "timestamp"){
cv <- is(input, "POSIXct")
}else if(vi == "dataframe" | vi == "data.frame" | vi == "df"){
cv1 <- is.data.frame(input)
cv2 <- is(input, "DataFrame")
cv <- any(cv1, cv2)
}else if(vi == "fileexists"){
cv <- all(file.exists(input))
}else if(vi == "direxists"){
cv <- all(dir.exists(input))
}else if(vi == "granges" | vi == "gr"){
cv <- is(input, "GRanges")
}else if(vi == "grangeslist" | vi == "grlist"){
cv <- .isGRList(input)
}else if(vi == "list" | vi == "simplelist"){
cv1 <- is.list(input)
cv2 <- is(input, "SimpleList")
cv <- any(cv1, cv2)
}else if(vi == "bsgenome"){
cv1 <- is(input, "BSgenome")
cv2 <- tryCatch({
library(input)
eval(parse(text=input))
}, error = function(e){
FALSE
})
cv <- any(cv1, cv2)
}else if(vi == "se" | vi == "summarizedexperiment"){
cv <- is(input, "SummarizedExperiment")
}else if(vi == "seurat" | vi == "seuratobject"){
cv <- is(input, "Seurat")
}else if(vi == "txdb"){
cv <- is(input, "TxDb")
}else if(vi == "orgdb"){
cv <- is(input, "OrgDb")
}else if(vi == "bsgenome"){
cv <- is(input, "BSgenome")
}else if(vi == "parallelparam"){
cv <- is(input, "BatchtoolsParam")
}else if(vi == "archrproj" | vi == "archrproject"){
cv <- is(input, "ArchRProject")
###validObject(input) check this doesnt break anything if we
###add it. Useful to make sure all ArrowFiles exist! QQQ
}else{
stop("Validator is not currently supported by ArchR!")
}
if(cv){
av <- TRUE
break
}
}
if(av){
return(invisible(TRUE))
}else{
stop("Input value for '", name,"' is not a ", paste(valid, collapse="," ), ", (",name," = ",class(input),") please supply valid input!")
}
}
#https://stackoverflow.com/questions/3476782/check-if-the-number-is-integer
.isWholenumber <- function(x, tol = .Machine$double.eps^0.5){
abs(x - round(x)) < tol
}
#https://stackoverflow.com/questions/13289009/check-if-character-string-is-a-valid-color-representation
.isColor <- function(x = NULL){
unlist(lapply(x, function(y) tryCatch(is.matrix(col2rgb(y)), error = function(e) FALSE)))
}
.isDiscrete <- function(x = NULL){
is.factor(x) || is.character(x) || is.logical(x)
}
.isGRList <- function(x){
isList <- grepl("list", class(x), ignore.case=TRUE)
if(!isList){
FALSE
}else{
allGR <- all(unlist(lapply(x, function(x) is(x, "GRanges") )))
if(allGR){
TRUE
}else{
FALSE
}
}
}
#' Get/Validate BSgenome
#'
#' This function will attempt to get or validate an input as a BSgenome.
#'
#' @param genome This option must be one of the following: (i) the name of a valid ArchR-supported genome ("hg38", "hg19", or "mm10"),
#' (ii) the name of a `BSgenome` package (for ex. "BSgenome.Hsapiens.UCSC.hg19"), or (iii) a `BSgenome` object.
#' @param masked A boolean describing whether or not to access the masked version of the selected genome. See `BSgenome::getBSgenome()`.
#' @export
validBSgenome <- function(genome = NULL, masked = FALSE){
.validInput(input = genome, name = "genome", valid = c("character", "bsgenome"))
.validInput(input = masked, name = "masked", valid = c("boolean"))
stopifnot(!is.null(genome))
if(inherits(genome, "BSgenome")){
return(genome)
}else if(is.character(genome)){
genome <- tryCatch({
.requirePackage(genome)
bsg <- eval(parse(text = genome))
if(inherits(bsg, "BSgenome")){
return(bsg)
}else{
stop("genome is not a BSgenome valid class!")
}
}, error = function(x){
BSgenome::getBSgenome(genome, masked = masked)
})
return(genome)
}else{
stop("Cannot validate BSgenome options are a valid BSgenome or character for getBSgenome")
}
}
.validTxDb <- function(TxDb = NULL){
stopifnot(!is.null(TxDb))
if(inherits(TxDb, "TxDb")){
return(TxDb)
}else if(is.character(TxDb)){
return(getTxDb(TxDb)) #change
}else{
stop("Cannot validate TxDb options are a valid TxDb or character for getTxDb")
}
}
.validOrgDb <- function(OrgDb = NULL){
stopifnot(!is.null(OrgDb))
if(inherits(OrgDb, "OrgDb")){
return(OrgDb)
}else if(is.character(OrgDb)){
return(getOrgDb(OrgDb)) #change
}else{
stop("Cannot validate OrgDb options are a valid OrgDb or character for getOrgDb")
}
}
.validGRanges <- function(gr = NULL){
stopifnot(!is.null(gr))
if(inherits(gr, "GRanges")){
return(gr)
}else{
stop("Error cannot validate genomic range!")
}
}
.validGeneAnnotation <- function(geneAnnotation = NULL){
if(!inherits(geneAnnotation, "SimpleList")){
if(inherits(geneAnnotation, "list")){
geneAnnotation <- as(geneAnnotation, "SimpleList")
}else{
stop("geneAnnotation must be a list/SimpleList of 3 GRanges for : Genes GRanges, Exons GRanges and TSS GRanges!")
}
}
if(identical(sort(tolower(names(geneAnnotation))), c("exons", "genes", "tss"))){
gA <- SimpleList()
gA$genes <- .validGRanges(geneAnnotation[[grep("genes", names(geneAnnotation), ignore.case = TRUE)]])
gA$exons <- .validGRanges(geneAnnotation[[grep("exons", names(geneAnnotation), ignore.case = TRUE)]])
gA$TSS <- .validGRanges(geneAnnotation[[grep("TSS", names(geneAnnotation), ignore.case = TRUE)]])
}else{
stop("geneAnnotation must be a list/SimpleList of 3 GRanges for : Genes GRanges, Exons GRanges and TSS GRanges!")
}
gA
}
.validGenomeAnnotation <- function(genomeAnnotation = NULL){
if(!inherits(genomeAnnotation, "SimpleList")){
if(inherits(genomeAnnotation, "list")){
genomeAnnotation <- as(genomeAnnotation, "SimpleList")
}else{
stop("genomeAnnotation must be a list/SimpleList of 3 GRanges for : blacklist GRanges, chromSizes GRanges and genome BSgenome package string (ie hg38 or BSgenome.Hsapiens.UCSC.hg38)!")
}
}
if(identical(sort(tolower(names(genomeAnnotation))), c("blacklist", "chromsizes", "genome"))){
gA <- SimpleList()
gA$blacklist <- .validGRanges(genomeAnnotation[[grep("blacklist", names(genomeAnnotation), ignore.case = TRUE)]])
if(genomeAnnotation[[grep("genome", names(genomeAnnotation), ignore.case = TRUE)]]=="nullGenome"){
gA$genome <- "nullGenome"
}else{
bsg <- validBSgenome(genomeAnnotation[[grep("genome", names(genomeAnnotation), ignore.case = TRUE)]])
gA$genome <- bsg@pkgname
}
gA$chromSizes <- .validGRanges(genomeAnnotation[[grep("chromsizes", names(genomeAnnotation), ignore.case = TRUE)]])
}else{
stop("genomeAnnotation must be a list/SimpleList of 3 GRanges for : blacklist GRanges, chromSizes GRanges and genome BSgenome package string (ie hg38 or BSgenome.Hsapiens.UCSC.hg38)!")
}
gA
}
.validGeneAnnoByGenomeAnno <- function(geneAnnotation, genomeAnnotation){
allSeqs <- unique(paste0(seqnames(genomeAnnotation$chromSizes)))
geneSeqs <- unique(paste0(seqnames(geneAnnotation$genes)))
if(!all(geneSeqs %in% allSeqs)){
geneNotIn <- geneSeqs[which(geneSeqs %ni% allSeqs)]
message("Found Gene Seqnames not in GenomeAnnotation chromSizes, Removing : ", paste0(geneNotIn, collapse=","))
geneAnnotation$genes <- .subsetSeqnamesGR(geneAnnotation$genes, names = allSeqs)
}
exonSeqs <- unique(paste0(seqnames(geneAnnotation$exons)))
if(!all(exonSeqs %in% allSeqs)){
exonNotIn <- exonSeqs[which(exonSeqs %ni% allSeqs)]
message("Found Exon Seqnames not in GenomeAnnotation chromSizes, Removing : ", paste0(exonNotIn, collapse=","))
geneAnnotation$exons <- .subsetSeqnamesGR(geneAnnotation$exons, names = allSeqs)
}
TSSSeqs <- unique(paste0(seqnames(geneAnnotation$TSS)))
if(!all(TSSSeqs %in% allSeqs)){
TSSNotIn <- TSSSeqs[which(TSSSeqs %ni% allSeqs)]
message("Found TSS Seqnames not in GenomeAnnotation chromSizes, Removing : ", paste0(TSSNotIn, collapse=","))
geneAnnotation$TSS <- .subsetSeqnamesGR(geneAnnotation$TSS, names = allSeqs)
}
geneAnnotation
}
.validArchRProject <- function(ArchRProj = NULL){
if(!inherits(ArchRProj, "ArchRProject")){
stop("Not a valid ArchRProject as input!")
}else{
ArchRProj
}
}