-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatel_plots.R
236 lines (182 loc) · 8.24 KB
/
patel_plots.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
library(cowplot)
library(RColorBrewer)
library(magrittr)
library(ggplot2)
load("patel_covariates.rda")
counts <- read.table("Patel/glioblastoma_raw_rnaseq_SCandbulk_counts.txt", header=TRUE, stringsAsFactors = FALSE, row.names=NULL)
gene_symbols <- counts[,1]
ensembl_ids <- counts[,2]
sample_names <- colnames(counts)[-(1:2)]
all.counts <- counts[,-(1:2)]
rownames(all.counts) <- ensembl_ids
metadata <- read.table("patel/SraRunTable.txt", sep='\t', stringsAsFactors = FALSE, header=TRUE, row.names=5, na.strings = "<not provided>")
metadata <- metadata[sample_names,]
# select only single-cell samples from patients
keep <- which(grepl("^Single cell", metadata$source_name_s) &
!is.na(metadata$patient_id_s) &
!is.na(metadata$subtype_s))
metadata <- metadata[keep,]
all.counts <- all.counts[,keep]
stopifnot(all(rownames(metadata)==colnames(all.counts)))
level1 <- as.factor(metadata$patient_id_s[!is.na(metadata$subtype_s)])
level2 <- as.factor(metadata$subtype_s[!is.na(metadata$subtype_s)])
col1 <- brewer.pal(9, "Set1")
col2 <- c(brewer.pal(8, "Set2"), brewer.pal(8, "Set3"), brewer.pal(8, "Set1"))
detection_rate <- colSums(all.counts>0)
coverage <- colSums(all.counts)
library(scater)
sceset <- newSCESet(countData = all.counts)
keep_feature <- rowSums(exprs(sceset) > 0) > 0
sceset <- sceset[keep_feature,]
sceset <- calculateQCMetrics(sceset)
qc <- pData(sceset)[,c(1, 4, 7, 11:14)]
filter <- rowSums(all.counts>10)>=10
raw <- all.counts[filter,]
colMerged <- col1[level1]
colBatch <- col2[level2]
data.frame(Dim1=pc_tc[,1], Dim2=pc_tc[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> panel1_pca
data.frame(Dim1=zifa_tc[,1], Dim2=zifa_tc[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> panel1_zifa
data.frame(Dim1=zinb@W[,1], Dim2=zinb@W[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> panel1_zinb
p1 <- plot_grid(panel1_pca + theme(legend.position = "none"),
panel1_zifa + theme(legend.position = "none"),
panel1_zinb + theme(legend.position = "none"),
labels=c("a", "c", "e"), align = "h", ncol=3)
legend <- get_legend(panel1_pca)
upper <- plot_grid(p1, legend, rel_widths = c(3, .6))
data.frame(Dim1=pc_tc[,1], Dim2=pc_tc[,2]) %>%
ggplot(aes(Dim1, Dim2)) + geom_point(aes(color=detection_rate)) + scale_colour_gradient(low="blue", high="yellow") -> panel2_pca
data.frame(Dim1=zifa_tc[,1], Dim2=zifa_tc[,2]) %>%
ggplot(aes(Dim1, Dim2)) + geom_point(aes(color=detection_rate)) + scale_colour_gradient(low="blue", high="yellow") -> panel2_zifa
data.frame(Dim1=zinb@W[,1], Dim2=zinb@W[,2]) %>%
ggplot(aes(Dim1, Dim2)) + geom_point(aes(color=detection_rate)) + scale_colour_gradient(low="blue", high="yellow") -> panel2_zinb
p2 <- plot_grid(panel2_pca + theme(legend.position = "none"),
panel2_zifa + theme(legend.position = "none"),
panel2_zinb + theme(legend.position = "none"),
labels=c("b", "d", "f"), align = "h", ncol=3)
legend2 <- get_legend(panel2_pca)
lower <- plot_grid(p2, legend2, rel_widths = c(3, .6))
fig1 <- plot_grid(upper, lower, ncol=1, nrow=2)
save_plot("patel_fig1.pdf", fig1,
ncol = 3,
nrow = 3,
base_aspect_ratio = 1.3
)
cors <- lapply(1:2, function(i) abs(cor(pc_tc[,i], qc, method="spearman")))
cors <- unlist(cors)
bars <- data.frame(AbsoluteCorrelation=cors,
QC=rep(stringr::str_to_lower(colnames(qc)), 2),
Dimension=as.factor(rep(1:2, each=ncol(qc))))
bars %>%
ggplot(aes(Dimension, AbsoluteCorrelation, group=QC, fill=QC)) +
geom_bar(stat="identity", position='dodge') +
scale_fill_manual(values=col2) + ylim(0, .5) -> panel2_pca
cors <- lapply(1:2, function(i) abs(cor(zifa_tc[,i], qc)))
cors <- unlist(cors)
bars <- data.frame(AbsoluteCorrelation=cors,
QC=rep(stringr::str_to_lower(colnames(qc)), 2),
Dimension=as.factor(rep(1:2, each=ncol(qc))))
bars %>%
ggplot(aes(Dimension, AbsoluteCorrelation, group=QC, fill=QC)) +
geom_bar(stat="identity", position='dodge') +
scale_fill_manual(values=col2) + ylim(0, .5) -> panel2_zifa
cors <- lapply(1:2, function(i) abs(cor(zinb@W[,i], qc)))
cors <- unlist(cors)
bars <- data.frame(AbsoluteCorrelation=cors,
QC=rep(stringr::str_to_lower(colnames(qc)), 2),
Dimension=as.factor(rep(1:2, each=ncol(qc))))
bars %>%
ggplot(aes(Dimension, AbsoluteCorrelation, group=QC, fill=QC)) +
geom_bar(stat="identity", position='dodge') +
scale_fill_manual(values=col2) + ylim(0, .5) -> panel2_zinb
p2 <- plot_grid(panel2_pca + theme(legend.position = "none"),
panel2_zifa + theme(legend.position = "none"),
panel2_zinb + theme(legend.position = "none"),
labels=c("b", "d", "f"), align = "h", ncol=3)
legend2 <- get_legend(panel2_pca)
lower <- plot_grid(p2, legend2, rel_widths = c(3, 1))
fig1bis <- plot_grid(upper, lower, ncol=1, nrow=2)
save_plot("patel_fig1bis.pdf", fig1bis,
ncol = 3,
nrow = 3,
base_aspect_ratio = 1.3
)
library(cluster)
methods <- list(pc_raw[,1:2], pc_tc[,1:2], pc_tmm[,1:2], pc_fq[,1:2],
zifa_raw, zifa_tc, zifa_tmm, zifa_fq,
zinb@W)
names(methods) <- c(paste0("PCA_", c("RAW", "TC", "TMM", "FQ")),
paste0("ZIFA_", c("RAW", "TC", "TMM", "FQ")),
"ZINB")
met_type <- as.factor(c(rep(c("PCA", "ZIFA"), each=4), "ZINB"))
sil_cl <- sapply(seq_along(methods), function(i) {
d <- dist(methods[[i]])
ss <- silhouette(as.numeric(level1), d)
mean(ss[,3])
})
bars <- data.frame(AverageSilhouette=sil_cl, Method=names(methods), Type=met_type)
bars %>%
ggplot(aes(Method, AverageSilhouette, group=Type, fill=Type)) +
geom_bar(stat="identity", position='dodge') +
scale_fill_manual(values=col1) + coord_flip() +
theme(legend.position = "none") -> sil
sil2 <- plot_grid(sil, NULL, NULL, ncol=3, nrow=1, labels="G")
fig1_tris <- plot_grid(upper, lower, sil2, ncol=1, nrow=3)
fig1_tris
save_plot("patel_fig1tris.pdf", fig1_tris,
ncol = 3,
nrow = 3,
base_aspect_ratio = 1.3
)
methods_sub <- methods[c(2, 6, 9)]
sil_pc <- lapply(seq_along(methods_sub), function(i) {
d <- dist(methods_sub[[i]])
ss <- silhouette(as.numeric(level1), d)
sss <- summary(ss)
sss$clus.avg.widths
})
bars <- data.frame(AverageSilhouette=unlist(sil_pc),
Method=rep(c("PCA", "ZIFA", "ZINB"), each=nlevels(level1)),
Cluster=rep(levels(level1), length(methods_sub)))
library(dplyr)
bars %>%
dplyr::mutate(ClusterByMethod = paste0(Cluster, " ", Method)) %>%
ggplot(aes(ClusterByMethod, AverageSilhouette, fill=Cluster)) +
geom_bar(stat="identity", position='dodge') +
scale_fill_manual(values=col1) + coord_flip() +
theme(legend.position = "none", axis.text = element_text(size=8)) -> sil
p1 <- plot_grid(panel1_pca + theme(legend.position = "none"),
panel1_zifa + theme(legend.position = "none"),
panel1_zinb + theme(legend.position = "none"),
labels=c("a", "c", "e"), align = "h", ncol=3)
upper <- plot_grid(p1, sil, labels=c("", "g"), rel_widths = c(3, 1))
fig1_4 <- plot_grid(upper, lower, ncol=1, nrow=2)
fig1_4
save_plot("patel_fig1_v4.pdf", fig1_4,
ncol = 3,
nrow = 3,
base_aspect_ratio = 1.3
)
save_plot("patel_supp_sil.pdf", sil)
data.frame(Dim1=pc_raw[,1], Dim2=pc_raw[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> pca_raw
data.frame(Dim1=pc_tc[,1], Dim2=pc_tc[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> pca_tc
data.frame(Dim1=pc_tmm[,1], Dim2=pc_tmm[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> pca_tmm
data.frame(Dim1=pc_fq[,1], Dim2=pc_fq[,2]) %>%
ggplot(aes(Dim1, Dim2, colour=level1)) + geom_point() +
scale_color_brewer(palette="Set1") -> pca_fq
fig_pca <- plot_grid(pca_raw, pca_tc, pca_tmm, pca_fq, labels=c("a", "b", "c", "d"))
save_plot("patel_supp_pca.pdf", fig_pca,
ncol = 2,
nrow = 2,
base_aspect_ratio = 1.3)