-
Notifications
You must be signed in to change notification settings - Fork 0
/
filterDoublets_modified.R
68 lines (50 loc) · 2.13 KB
/
filterDoublets_modified.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
# Make new filterDoublets function from ArchR
filterDoublets <- function(ArchRProj = NULL, cutEnrich = 1, cutScore = -Inf, filterRatio = 1){
fn <- unclass(lsf.str(envir = asNamespace("ArchR"), all = TRUE))
for (i in seq_along(fn)) {
tryCatch({
eval(parse(text = paste0(fn[i], "<-ArchR:::", fn[i])))
}, error = function(x) {
})
}
.validInput(input = ArchRProj, name = "ArchRProj", valid = c("ArchRProj"))
.validInput(input = cutEnrich, name = "cutEnrich", valid = c("numeric"))
.validInput(input = cutScore, name = "cutScore", valid = c("numeric"))
.validInput(input = filterRatio, name = "filterRatio", valid = c("numeric"))
if(any(grepl("filterDoublets", names(ArchRProj@projectSummary)))){
stop("Already ran filterDoublets on ArchRProject! Cannot be re-ran on an ArchRProject!")
}
df <- getCellColData(ArchRProj, c("Sample", "DoubletEnrichment", "DoubletScore"))
splitDF <- split(seq_len(nrow(df)), as.character(df$Sample))
cellsFilter <- lapply(splitDF, function(y){
x <- df[y, ,drop = FALSE]
n <- nrow(x)
x <- x[order(x$DoubletEnrichment, decreasing = TRUE), ]
if(!is.null(cutEnrich)){
x <- x[which(x$DoubletEnrichment >= cutEnrich), ]
}
if(!is.null(cutScore)){
x <- x[which(x$DoubletScore >= cutScore), ]
}
if(nrow(x) > 0){
head(rownames(x), filterRatio * n * (n / 100000))
}else{
NULL
}
}) %>% unlist(use.names=FALSE)
message("Filtering ", length(cellsFilter), " cells from ArchRProject!")
tabRemove <- table(df[cellsFilter,]$Sample)
tabAll <- table(df$Sample)
samples <- unique(df$Sample)
for(i in seq_along(samples)){
if(!is.na(tabRemove[samples[i]])){
message("\t", samples[i], " : ", tabRemove[samples[i]], " of ", tabAll[samples[i]], " (", round(100 * tabRemove[samples[i]] / tabAll[samples[i]], 1),"%)")
}else{
message("\t", samples[i], " : ", 0, " of ", tabAll[samples[i]], " (0%)")
}
}
if(length(cellsFilter) > 0){
ArchRProj@cellColData <- ArchRProj@cellColData[rownames(ArchRProj@cellColData) %ni% cellsFilter,,drop=FALSE]
}
ArchRProj
}