-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublet_score.R
61 lines (41 loc) · 2.04 KB
/
doublet_score.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
load("data/seurat_33k_main")
TSNEPlot(pbmc33k.merged, do.label = T)
pbmc33k.subset <- SubsetData(pbmc33k.merged, ident.use = c("B", "NK", "TH", "TC", "Monocytes"))
TSNEPlot(pbmc33k.subset, do.label = T)
markers <- FindAllMarkers(pbmc33k.subset, do.print = T)
FeaturePlot(pbmc33k.subset, "ENSG00000161570")
markers[markers$cluster=="TH",][1:10,]
top.markers <- NULL
for (cluster in levels(pbmc33k.subset@ident)) {
# select the first 10 markers where the percentage in the other cluster of that marker is < 10%
cluster.markers <- markers[markers$cluster==cluster & markers$pct.2<0.10,]
top.markers <- rbind(top.markers, cluster.markers[1:10,])
}
top.markers
doublet.scores <- matrix(nrow = length([email protected]),
ncol = length(levels(pbmc33k.subset@ident)) + 1,
dimnames = list([email protected], c("highest.frac", levels(pbmc33k.subset@ident))))
idents <- levels(pbmc33k.subset@ident)
pb <- txtProgressBar(min = 0, max = nrow(pbmc33k.subset@data), style = 3)
for (i in 1:nrow(pbmc33k.subset@data)) {
cell <- pbmc33k.subset@data[,i]
for (ident in idents) {
top.markers.ident <- top.markers[top.markers$cluster == ident, "gene"]
cell.top.markers.ident <- cell[top.markers.ident]
doublet.scores[i, ident] <- sum(cell.top.markers.ident)
}
doublet.scores[i,1] <- max(doublet.scores[i,], na.rm = T) / sum(doublet.scores[i,], na.rm = T)
setTxtProgressBar(pb, i)
}
close(pb)
hist(doublet.scores[,1])
doublet.scores[1:25,]
head(doublet.scores[is.na(doublet.scores[,1]),])
doublet.scores[is.nan(doublet.scores)] <- 1
doublet.scores[is.na(doublet.scores)] <- 1
hist(doublet.scores[,1])
length(doublet.scores[,1])
pbmc33k.subset <- AddMetaData(pbmc33k.subset, doublet.scores[,1], "doublet.score")
FeaturePlot(pbmc33k.subset, "percent.mito", cols.use = c("blue", "lightgrey"))
all.cells <- FetchData(pbmc33k.subset, c("nUMI", "nGene", "percent.mito", "doublet.score"))
plot(all.cells$nGene, all.cells$doublet.score, pch=20, col=rgb(0,0,0,alpha=0.3), cex=0.2, cex.lab=1.4)