-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D Plots
312 lines (232 loc) · 14.4 KB
/
3D Plots
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# This is the adptation of code developed by @Dragonmasterx87 (Dept. of Cell Biology, UM) and @msaadsadiq (Dept. of Electrical and Computer Engineering, UM)
# 3D UMAP plots of Seurat_4.1.1 objects utilizing the visualization
# package plot_ly
# R version 4.1.3(x64 bit) and RStudio 2022.07.1+554 "Spotted Wakerobin" Release (7872775ebddc40635780ca1ed238934c3345c5de, 2022-07-22) for Windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36 were used for running this code :)
# Seurat is a multimodal single Cell RNA seq analysis algorithm created by
# The Satija Lab. For more information please see: https://satijalab.org/seurat/
# Contributors (by their Github handles):
# @Dragonmasterx87 (Dept. of Cell Biology, UM)
# @msaadsadiq (Dept. of Electrical and Computer Engineering, UM)
# Install plot_ly
install.packages('plotly')
# Load plot_ly
library(plotly)
# Construct a dataframe using data from your pre-clustered Seurat_4.1.1 object
# Here 'seurat_clusters' is list of numeric cluster identities, you can find it here: DiabetesST2UMAP3d.combined[["seurat_cluster"]],
# or DiabetesST2UMAP3d.combined$seurat_clusters, where 'DiabetesST2UMAP3d.combined' is a Seurat object created with Seurat_4.1.1
DiabetesST2UMAP.combined <- "integrated"
# Re-run UMAPs that you have accurate calculations for all UMAP(s)
DiabetesST2UMAP3d.combined <- RunUMAP(DiabetesST2UMAP.combined_AD,
dims = 1:10,
n.components = 3L)
# Extract UMAP information from Seurat Object
umap_1 <- DiabetesST2UMAP3d.combined[["umap"]]@cell.embeddings[,1]
umap_2 <- DiabetesST2UMAP3d.combined[["umap"]]@cell.embeddings[,2]
umap_3 <- DiabetesST2UMAP3d.combined[["umap"]]@cell.embeddings[,3]
# Visualize what headings are called so that you can extract them to form a dataframe
Embeddings(object = DiabetesST2UMAP3d.combined, reduction = "umap")
# Prepare a dataframe for cell plotting
plotting.data <- FetchData(object = DiabetesST2UMAP3d.combined, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "CellType", "Islets_Island", "groups", "integrated_snn_res.0.2"))
plotting.data1 <- FetchData(object = DiabetesST2UMAP3d.combined, vars = c("UMAP_1", "UMAP_2", "UMAP_3"))
# Make a column of row name identities (these will be your cell/barcode names)
plotting.data$label <- paste(rownames(plotting.data))
plotting.data1$label <- paste(rownames(plotting.data1))
write.csv(plotting.data1, 'C:/Users/mad1188/Box/Mayur_shared_JDB/JUAN UPDATED/New UMAP identities/ST2 VELOCYTO NEW/umap_coords.csv')
plotting.data$CellType <- Idents(DiabetesST2UMAP.combined)
head(x = plotting.data[[]])
# Plot your data, in this example my Seurat object had 17 clusters (0-20)
plot_ly(data = plotting.data,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~integrated_snn_res.0.2,
colors = c("turquoise1","mediumseagreen",
"royalblue1","lightslateblue",
"darkturquoise","navyblue", "darkcyan",
"lightgoldenrod2","sienna3",
"sienna","greenyellow",
"yellow","gold3",
"grey",
"pink",
"red",
"palegreen2"),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use for cell ID
hoverinfo="text") #When you visualize your plotly object, hovering your mouse pointer over a point shows cell names
plot_ly(data = plotting.data,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~Islets_Island,
colors = c("green","red", "blue"
),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use for cell ID
hoverinfo="text")
# On running this code the HTML output should appear in RStudio. You can save the output as a
# HTML file. Once you have saved, just open the HTML file in any web browser (double click on the html- file
# and if asked select to open with any web browser like google chrome/safari/mozilla/explorer etc).
# It should be have all of the integrated features you saw in the RStudio output file.
# Thank you for reading and using this code to further your scRNAseq analysis!
# If you liked it, dont forget to acknowledge, fork and star!
# Citation information is within the Readme, please dont forget to cite!
# Have a wonderful day!!
# Run plot3d and while the rgl widget is open run the code below to generate a html file in the plots panel of RStudio
# You can save as a html file via export. These files work with optimal resolution and are most user friendly in Google Chrome
rgl::rglwidget() #save as html
DiabetesST2UMAP3d.combined$barcode <- colnames(DiabetesST2UMAP3d.combined)
DiabetesST2UMAP3d.combined$UMAP_1 <- DiabetesST2UMAP3d.combined[["umap"]]@cell.embeddings[,1]
DiabetesST2UMAP3d.combined$umap_2 <- DiabetesST2UMAP3d.combined[["umap"]]@cell.embeddings[,2]
DiabetesST2UMAP3d.combined$umap_3 <- DiabetesST2UMAP3d.combined[["umap"]]@cell.embeddings[,3]
write.csv(DiabetesST2UMAP3d.combined@meta.data, file='metadata.csv', quote=F, row.names=F)
DefaultAssay(object = DiabetesST2UMAP3d.combined) <- "RNA"
DiabetesST2UMAP3d.combined <- NormalizeData(DiabetesST2UMAP3d.combined)
DiabetesST2UMAP3d.combined <- SCTransform(DiabetesST2UMAP3d.combined, assay = "RNA", new.assay.name = "SCT", verbose = TRUE, return.only.var.genes = TRUE)
out_data_dir="C:/Users/mad1188/Box/Mayur_shared_JDB/JUAN UPDATED/New UMAP identities/ST2 VELOCYTO NEW"
# write expression counts matrix
library(Matrix)
counts_matrix <- GetAssayData(DiabetesST2UMAP3d.combined, assay='RNA', slot='counts')
writeMM(counts_matrix, file=paste0(out_data_dir, 'counts.mtx'))
# write dimesnionality reduction matrix, in this example case pca matrix
write.csv(DiabetesST2UMAP3d.combined@reductions$pca@cell.embeddings, file='pca.csv', quote=F,row.names=F)
# write gene names
write.table(
data.frame('gene'=rownames(counts_matrix)),file='gene_names.csv', quote=F,row.names=F,col.names=F)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
# BMP7- TimePoint_1+TimePoint-2
# Interacive multimodal 3D UMAP plotting of scRNA sequencing datasets
# The following is a length of code generated to create nice
# 3D UMAP plots of Seurat_4.1.1 objects utilizing the visualization
# package plot_ly
# R version 4.1.3(x64 bit) and RStudio 2022.07.1+554 "Spotted Wakerobin" Release (7872775ebddc40635780ca1ed238934c3345c5de, 2022-07-22) for Windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36 were used for running this code :)
# Seurat is a multimodal single Cell RNA seq analysis algorithm created by
# The Satija Lab. For more information please see: https://satijalab.org/seurat/
# Contributors (by their Github handles):
# @Dragonmasterx87 (Dept. of Cell Biology, UM)
# @msaadsadiq (Dept. of Electrical and Computer Engineering, UM)
# Install plot_ly
install.packages('plotly')
# Load plot_ly
library(plotly)
# Construct a dataframe using data from your pre-clustered Seurat_4.1.1 object
# Here 'seurat_clusters' is list of numeric cluster identities, you can find it here: BMPcomparison3d.combined[["seurat_cluster"]],
# or BMPcomparison3d.combined$seurat_clusters, where 'BMPcomparison3d.combined' is a Seurat object created with Seurat_4.1.1
BMPcomparison.combinedUMAP2<- "integrated"
# Re-run UMAPs that you have accurate calculations for all UMAP(s)
BMPcomparison3d.combined <- RunUMAP(BMPcomparison.combinedUMAP2,
dims = 1:10,
n.components = 3L)
# Extract UMAP information from Seurat Object
umap_1 <- BMPcomparison3d.combined[["umap"]]@cell.embeddings[,1]
umap_2 <- BMPcomparison3d.combined[["umap"]]@cell.embeddings[,2]
umap_3 <- BMPcomparison3d.combined[["umap"]]@cell.embeddings[,3]
# Visualize what headings are called so that you can extract them to form a dataframe
Embeddings(object = BMPcomparison3d.combined, reduction = "umap")
# Prepare a dataframe for cell plotting
plotting.data2 <- FetchData(object = BMPcomparison3d.combined, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "seurat_clusters"))
plotting.data3 <- FetchData(object = BMPcomparison3d.combined, vars = c("UMAP_1", "UMAP_2", "UMAP_3"))
# Make a column of row name identities (these will be your cell/barcode names)
plotting.data2$label <- paste(rownames(plotting.data2))
plotting.data3$label <- paste(rownames(plotting.data3))
write.csv(plotting.data2, 'C:/Users/mad1188/Box/Mayur_shared_JDB/JUAN UPDATED/New UMAP identities/ST2 VELOCYTO NEW/umap_coords1.csv')
plotting.data2$CellType <- Idents(BMPcomparison.combinedUMAP2)
head(x = plotting.data2[[]])
# Plot your data, in this example my Seurat object had 14 clusters (0-20)
plot_ly(data = plotting.data2,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~CellType,
colors = c("turquoise1",
"mediumseagreen",
"royalblue1",
"lightslateblue",
"lightgoldenrod2",
"sienna3",
"yellow3",
"CORAL2",
"YELLOW",
"GOLD3",
"pink", "red","palegreen2","grey"),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use for cell ID
hoverinfo="text") #When you visualize your plotly object, hovering your mouse pointer over a point shows cell names
-----------------------------------------------------------------------------------------------------------
# For Islets of control+BMP7- TimePoint_1
ISLETST2RE3d <- RunUMAP(BMP_ISLETSRE,
dims = 1:10,
n.components = 3L)
# Extract UMAP information from Seurat Object
umap_1 <- ISLETST2RE3d[["umap"]]@cell.embeddings[,1]
umap_2 <- ISLETST2RE3d[["umap"]]@cell.embeddings[,2]
umap_3 <- ISLETST2RE3d[["umap"]]@cell.embeddings[,3]
# Visualize what headings are called so that you can extract them to form a dataframe
Embeddings(object = ISLETST2RE3d, reduction = "umap")
# Prepare a dataframe for cell plotting
plotting.data5 <- FetchData(object = ISLETST2RE3d, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "seurat_clusters"))
plotting.data6 <- FetchData(object = ISLETST2RE3d, vars = c("UMAP_1", "UMAP_2", "UMAP_3"))
# Make a column of row name identities (these will be your cell/barcode names)
plotting.data5$label <- paste(rownames(plotting.data5))
plotting.data1$label <- paste(rownames(plotting.data1))
write.csv(plotting.data1, 'C:/Users/mad1188/Box/Mayur_shared_JDB/JUAN UPDATED/New UMAP identities/ST2 VELOCYTO NEW/umap_coords.csv')
plotting.data$CellType <- Idents(DiabetesST2UMAP.combined)
head(x = plotting.data5[[]])
# Plot your data, in this example my Seurat object had 3 clusters
plot_ly(data = plotting.data5,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~seurat_clusters,
colors = c("#f8766d", "#0cb702", "#1e90ff"),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use for cell ID
hoverinfo="text")
# create a dataframe
plotting.data8 <- FetchData(object = ISLETST2RE3d, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "INS"), slot = 'data')
# Say you want change the scale, so that every cell having an expression >1 will be one color
# Basically, you are re-adjusting the scale here, so that any cell having a certain expression will light up on your 3D plot
# First make another column in your dataframe, where all values above 1 are re-assigned a value of 1
# This information is stored in the 'changed' column of your dataframe
plotting.data8$changed <- ifelse(test = plotting.data8$INS <3, yes = plotting.data8$INS, no = 1)
# Add the label column, so that now the column has 'cellname-its expression value'
plotting.data8$label <- paste(rownames(plotting.data8)," - ", plotting.data8$INS, sep="")
# Plot your data, in this example my Seurat object had 21 clusters (0-20), and cells express a gene called INS
plot_ly(data = plotting.data8,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~changed, # you can just run this against the column for the gene as well using ~INS, the algorith will automatically scale in that case based on maximal and minimal values
opacity = .5,
colors = c('darkgreen', 'red'),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2),
text=~label,
hoverinfo="text"
)
-----------------------------------------------------------------------------------------------------------
# For Islets of BMP7- TimePoint_1+TimePoint_2
BMP_ISLETSRE3d <- RunUMAP(BMP_ISLETSRE,
dims = 1:10,
n.components = 3L)
# Extract UMAP information from Seurat Object
umap_1 <- BMP_ISLETSRE3d[["umap"]]@cell.embeddings[,1]
umap_2 <- BMP_ISLETSRE3d[["umap"]]@cell.embeddings[,2]
umap_3 <- BMP_ISLETSRE3d[["umap"]]@cell.embeddings[,3]
# Visualize what headings are called so that you can extract them to form a dataframe
Embeddings(object = BMP_ISLETSRE3d, reduction = "umap")
# Prepare a dataframe for cell plotting
plotting.data10 <- FetchData(object = BMP_ISLETSRE3d, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "seurat_clusters"))
# Make a column of row name identities (these will be your cell/barcode names)
plotting.data10$label <- paste(rownames(plotting.data10))
head(x = plotting.data10[[]])
# Plot your data, in this example my Seurat object had 4 clusters
plot_ly(data = plotting.data10,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~seurat_clusters,
colors = c("greenyellow","#f8766d","#1e90ff","#0cb702"),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use for cell ID
hoverinfo="text")