-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10X_Visium_secondary_analysis_plot.R
251 lines (202 loc) · 9.09 KB
/
10X_Visium_secondary_analysis_plot.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
#!/usr/bin/env Rscript
library(png)
library(rjson)
library(tidyverse)
library(ggrepel)
options(bitmapType='cairo')
args = commandArgs(trailingOnly = TRUE)
samplename = args[1]
#samplename = 'CytAssist_11mm_FFPE_Human_Glioblastoma'
#lowres.image = '/SAN/colcc/Kordasti_RNA-Seq_Roberto/Z_spatial_trial/0_spatial/results_spaceranger/CytAssist_11mm_FFPE_Human_Glioblastoma/CytAssist_11mm_FFPE_Human_Glioblastoma/outs/spatial/tissue_lowres_image.png'
louvain.res = 0.8
lowres.xdim = 600
lowres.ydim = 600
png.dim = 500
spng.xdim = 750
spng.ydim = 700
spng.dot = 1
target.dir <- paste0(getwd(), '/results_secondary_analysis/', samplename)
lowres.image <- paste0(getwd(), '/results_spaceranger/', samplename, '/', samplename, '/outs/spatial/tissue_lowres_image.png')
positions <- read.csv(paste0(getwd(), '/results_spaceranger/', samplename, '/', samplename, '/outs/spatial/tissue_positions.csv'), header = TRUE, sep = ',', row.names = 1)
positions <- positions[positions[,'in_tissue'] == 1,]
scale.factors <- fromJSON(file = paste0(getwd(), '/results_spaceranger/', samplename, '/', samplename, '/outs/spatial/scalefactors_json.json'))
final.data <- read.csv(paste0(target.dir, '/tables/', samplename, '_remaining_final.csv'), header = TRUE, sep = ',', row.names = 1)
# RNA Clustering
RNA.column <- paste0('SNN_SCT_res.', louvain.res)
final.data[,RNA.column] <- factor(final.data[,RNA.column], levels = levels(factor(final.data[,RNA.column])))
png(paste0(target.dir, '/plots/', samplename, '_expression_count_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='nCount_Spatial', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('Gene Expression Count') +
ggtitle(paste0(samplename, ': Gene Expression Count')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
png(paste0(target.dir, '/plots/', samplename, '_feature_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='nFeature_Spatial', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('Feature Count') +
ggtitle(paste0(samplename, ': Feature Count')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
png(paste0(target.dir, '/plots/', samplename, '_mito_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='percent.mt', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('% Mitochondrial Content') +
ggtitle(paste0(samplename, ': % Mitochondrial Content')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
png(paste0(target.dir, '/plots/', samplename, '_ribo_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='percent.ribo', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('% Ribosomal Genes') +
ggtitle(paste0(samplename, ': % Ribosomal Genes')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
png(paste0(target.dir, '/plots/', samplename, '_POLR_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='percent.POLR', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('% RNA Polymerase Genes') +
ggtitle(paste0(samplename, ': % RNA Polymerase Genes')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
png(paste0(target.dir, '/plots/', samplename, '_HSP_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='percent.HSP', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('% HSP Genes') +
ggtitle(paste0(samplename, ': % HSP Genes')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
png(paste0(target.dir, '/plots/', samplename, '_HIST_RNA_clusters.png'), width = png.dim, height = png.dim)
final.data %>% ggplot(aes_string(x=RNA.column, y='percent.HIST', color=RNA.column)) +
geom_jitter(size = 0.1, width = 0.2) +
geom_violin(size = 0.5, alpha = 0) +
theme_minimal() +
xlab('RNA Cluster') +
ylab('% HIST Genes') +
ggtitle(paste0(samplename, ': % HIST Genes')) +
theme(legend.position = 'none',
plot.title = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
TSNE_RNA <- read.csv(paste0(target.dir, '/tables/', samplename, '_TSNE_SCT.csv'), header = TRUE, sep = ',', row.names = 1)
TSNE_RNA[,'Cluster'] <- final.data[rownames(TSNE_RNA),paste0('SNN_SCT_res.', louvain.res)]
png(paste0(target.dir, '/plots/', samplename, '_TSNE_RNA_RNA_clusters.png'), width = png.dim, height = png.dim)
TSNE_RNA %>% ggplot(aes(x=TSNESCT_1, y=TSNESCT_2, color=as.factor(Cluster))) +
geom_point(size = 1) +
theme_minimal() +
xlab('t-SNE 1') +
ylab('t-SNE 2') +
ggtitle(paste0(samplename, ': t-SNE Plot')) +
scale_color_discrete(name = 'Cluster') +
theme(legend.position = 'right',
plot.title = element_text(hjust=0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
UMAP_RNA <- read.csv(paste0(target.dir, '/tables/', samplename, '_UMAP_SCT.csv'), header = TRUE, sep = ',', row.names = 1)
UMAP_RNA[,'Cluster'] <- final.data[rownames(UMAP_RNA),paste0('SNN_SCT_res.', louvain.res)]
png(paste0(target.dir, '/plots/', samplename, '_UMAP_RNA_RNA_clusters.png'), width = png.dim, height = png.dim)
UMAP_RNA %>% ggplot(aes(x=UMAPSCT_1, y=UMAPSCT_2, color=as.factor(Cluster))) +
geom_point(size = 1) +
theme_minimal() +
xlab('UMAP 1') +
ylab('UMAP 2') +
ggtitle(paste0(samplename, ': UMAP Plot')) +
scale_color_discrete(name = 'Cluster') +
theme(legend.position = 'right',
plot.title = element_text(hjust=0.5),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = 'black'))
dev.off()
# Spatial Results
grd <- expand.grid(1:lowres.xdim, 1:lowres.ydim)
img <- readPNG(lowres.image)
dim(img) <- c(600 * 600, 3)
img <- cbind(grd, img)
colnames(img) <- c("X", "Y", "R", "G", "B")
img[,'RGB'] <- rgb(img$R, img$G, img$B)
base.image <- ggplot() +
geom_raster(data=img, aes(x = Y, y = -X, fill = RGB)) +
scale_fill_identity() +
theme_void()
# Spot Results
positions[,'x_axis'] <- positions[,'pxl_row_in_fullres'] * scale.factors$tissue_lowres_scalef
positions[,'y_axis'] <- positions[,'pxl_col_in_fullres'] * scale.factors$tissue_lowres_scalef
to.plot <- merge(positions, final.data, by = 0)
colnames(to.plot)[1] <- 'Cell'
png(paste0(target.dir, '/plots/', samplename, '_all_clusters.png'), width = spng.xdim, height = spng.ydim)
base.image + geom_point(data = to.plot, aes(x = y_axis, y = -x_axis, color = .data[[RNA.column]]), size = spng.dot) +
ggtitle(paste0(samplename, ': Clustering Results')) +
labs(color = 'Cluster') +
theme(legend.position = 'right',
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
legend.title=element_text(size = 15),
legend.text=element_text(size = 15))
dev.off()
for ( i in levels(to.plot[,RNA.column]) ) {
temp.plot <- to.plot[to.plot[,RNA.column] == i,]
to.print <- base.image + geom_point(data = temp.plot, aes(x = y_axis, y = -x_axis), colour = 'red', size = spng.dot) +
theme(legend.position = 'None',
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
legend.title=element_text(size = 15),
legend.text=element_text(size = 15))
png(paste0(target.dir, '/plots/', samplename, '_cluster_', i, '.png'), width = lowres.xdim, height = lowres.xdim)
print(to.print)
dev.off()
}
unlink(paste0(getwd(), '/Rplots.pdf'))