-
Notifications
You must be signed in to change notification settings - Fork 3
/
cidr_rare.R
90 lines (71 loc) · 2.58 KB
/
cidr_rare.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
suppressMessages(library(cidr))
suppressMessages(library(Matrix))
suppressMessages(library(ggplot2))
suppressMessages(library(plyr))
suppressMessages(library(cowplot))
suppressMessages(library(Rtsne))
source("~/code/single_cell/R/seurat.R", chdir = TRUE)
set.seed(0) # Set seed for entire stochastic process.
sink(file="/dev/null") # No other messages
dir.create("./cidr_img/", showWarnings = FALSE)
args = commandArgs(TRUE)
output = args[1]
inputs = args[2:length(args)]
loadCells = function(x) {
df = read.table(file = paste0(x, "/barcodes.tsv"), col.names = c("item"),
header = FALSE)
df$label = tail(strsplit(x, "_")[[1]], n = 1)
return(df)
}
tsne = function(isLabel, df) {
p = ggplot(df, aes(x = TSNE.1, y = TSNE.2, color = factor(group))) +
geom_point() +
xlab("TNSE 1") +
ylab("TNSE 2") +
theme_cowplot(font_size = 10) +
theme(aspect.ratio = 1)
if(isLabel) {
p = p + scale_color_manual(guide = guide_legend(title = ""), values=c("#e41a1c", "#377eb8", "#999999"))
} else {
p = p + scale_color_discrete(guide = guide_legend(title = ""))
}
return(p)
}
cells = do.call("rbind", lapply(inputs, loadCells))
rawNamedMat = do.call("cbind", lapply(inputs, loadNamedMatrix))
rawMat = do.call("cbind", lapply(inputs, loadMatrix))
mat = scDataConstructor(as.matrix(rawNamedMat))
mat = determineDropoutCandidates(mat)
mat = wThreshold(mat)
mat = scDissim(mat)
mat = scPCA(mat)
mat = nPC(mat)
mat = try(scCluster(mat), TRUE)
if(typeof(mat) == "character") {
sink() # No other messages
cat("label,cluster,item",file=stdout(),sep="\n")
} else {
# Dimensionality reduction where applicable
tsneMat = Rtsne(as.matrix(t(rawNamedMat)))
tsne1 = tsneMat$Y[,1]
tsne2 = tsneMat$Y[,2]
df = data.frame(item = colnames(rawNamedMat), cluster = mat@clusters, TSNE.1 =
tsne1,
TSNE.2 = tsne2)
df = merge(cells, df, by = "item")
df$group = df$cluster
clusterP = tsne(FALSE, df)
df$group = df$label
labelP = tsne(TRUE, df)
suppressMessages(ggsave(clusterP,
file = paste0(c("./cidr_img/", output,
"_clusters.pdf"), collapse = ""),
useDingbats = FALSE))
suppressMessages(ggsave(labelP,
file = paste0(c("./cidr_img/", output,
"_labels.pdf"), collapse = ""),
useDingbats = FALSE))
sink() # No other messages
outDf = df[,c("label", "cluster", "item")]
write.csv(outDf, stdout(), row.names = FALSE, quote = FALSE)
}