-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_v2.Rmd
325 lines (256 loc) · 13.5 KB
/
example_v2.Rmd
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
313
314
315
316
317
318
319
320
321
322
323
324
325
---
title: "Vignette SuperSpot"
author: "Matei Teleman"
date: "2024-01-15"
output:
md_document:
variant: markdown_github
html_document: default
pdf_document: default
---
## Import libraries
```{r}
library(Seurat)
library(SuperSpot)
library(SuperCell)
library(tidyr)
library(tidyverse)
library(igraph)
```
Download files from <https://zenodo.org/records/8327576>. You need "well7_5raw_expression_pd.csv", "metadata.csv" and "well7_5_spatial.csv".
```{bash, eval = FALSE}
wget https://zenodo.org/records/8327576/files/well7_5raw_expression_pd.csv
wget https://zenodo.org/records/8327576/files/metadata.csv
wget https://zenodo.org/records/8327576/files/well7_5_spatial.csv
```
## Import dataset
```{r}
well7.mtx <- read_csv("./well7_5raw_expression_pd.csv") %>% column_to_rownames("GENE") %>% as.sparse()
well7.mtx[1:5,1:5]
md <- read_csv("./metadata.csv")
md[1:5,1:5]
well7.md <- md[md$NAME %in% colnames(well7.mtx) == T,]
well7.md[1:5,1:5]
well7.spatial <- read_csv("./well7_5_spatial.csv")
well7.spatial <- well7.spatial[-1,]
spotPosition <- dplyr::select(well7.spatial,c("NAME","X","Y")) %>% column_to_rownames("NAME")
#colnames(spotPosition) <- c("imagerow","imagecol")
spotPosition$X <- as.numeric(spotPosition$X)
spotPosition$Y <- as.numeric(spotPosition$Y)
#spotPosition <- select(spotPosition,c("imagerow","imagecol"))
```
## Create metaspot object
The function "SCimplify_SpatialDLS" uses the raw count matrix and the spatial coordinates of the spots to build the metaspots. You can choose to split or not the the connections between the spots that have a higher distance compared to the other ones with "split_not_connected". You can also split the metaspots based on a provided annotation with "cell.annotation" parameter (i.e.,metaspots containing spots from different cell types/regions will be split resulting in one metaspot for each cell type/region). The main output here is the membership given to each spot to know in which metaspot it is assigned.
```{r}
g = 10 # gamma
n.pc = 1:5 # number of first PC to use
k.knn = 15 # number of neighbors to connect to each spot
print("Creating metaspots")
# By default, SCimplify_SpatialDLS computes distances in a parallalized way. By default, all the available cpus are used. If your computer doesn't support, you can change the number of cpus with the paramater "n.cpu"
MC.well7 <- SCimplify_SpatialDLS_v2(X = well7.mtx ,
spotPositions = spotPosition ,
col.x = "X",
col.y = "Y",
method_similarity = "1",
split_not_connected = TRUE,
method_reduction = "PCA",
gamma = g,
n.dim = n.pc,
method_knn = "1",
k.knn = k.knn,
method_normalization = "log_normalize",
cell.annotation = well7.md$Main_molecular_cell_type)
print("Done")
well7.md[,str_c("MC_membership_",g)] <- MC.well7$membership %>% as.character()
```
The major quality control for metaspots is purity (proportion of the most abundant cell type/region within each metaspot). In the case where we decided to split the metaspots with the paramater "cell.annotation", the purity should be equal to 1.
```{r}
#We compute the purity for each metaspot
method_purity <- c("max_proportion", "entropy")[1]
MC.well7$purity <- supercell_purity(
clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7$membership,
method = method_purity
)
print(str_c("mean purity is ",mean(MC.well7$purity)))
#We assign each metaspot with its corresponding annotation
MC.well7$Main_molecular_cell_type <- supercell_assign(clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7$membership,
method = "absolute")
```
SuperSpot come with its own way to visualize the metapots. The function "supercell_metaspots_shape" first builds the polygons representing the metaspots covering the original spots.
```{r,fig.height=20, fig.width=20}
MC.well7$polygons <- supercell_metaspots_shape_v2(MC = MC.well7,
spotpositions = spotPosition,
annotation = "Main_molecular_cell_type",
concavity = 2,
membership_name = "membership")
SpatialDimPlotSC(original_coord = spotPosition,
col.x = "Y",
col.y = "X",
MC = MC.well7,
sc.col = "Main_molecular_cell_type",
sc.col2 = str_c("MC_membership_",g),
polygons_col = "polygons",
meta_data = well7.md)+
coord_fixed()+
theme_minimal()+
NoLegend()
```
Because we wanted that every metaspot contains only one cell type and we split them, it created metaspots with gaps. To overcome this, we split them again based on if they are still connected or not in the KNN.
```{r,fig.height=20, fig.width=20}
MC.well7.spl <- split_unconnected(MC.well7)
well7.md[,str_c("MC_membership_spl_",g)] <- MC.well7.spl$membership %>%
as.character()
MC.well7.spl$Main_molecular_cell_type <- supercell_assign(clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7.spl$membership,
method = "absolute")
MC.well7.spl$polygons <- supercell_metaspots_shape_v2(MC = MC.well7.spl,
spotpositions = spotPosition,
annotation = "Main_molecular_cell_type",
concavity = 2,membership_name = "membership")
SpatialDimPlotSC(original_coord = spotPosition,
col.x = "Y",
col.y = "X",
MC = MC.well7.spl,
sc.col = "Main_molecular_cell_type",
sc.col2 = str_c("MC_membership_spl_",g),
polygons_col = "polygons",
meta_data = well7.md)+
coord_fixed()+
theme_minimal()+
NoLegend()
```
## Computing Metaspots without threshold on the KNN
We set the percentage to keep at 1:
```{r,fig.height=20, fig.width=20}
g = 10 # gamma
n.pc = 1:5 # number of first PC to use
k.knn = 15 # number of neighbors to connect to each spot
pct = 1 # percentage of connections to keep
print("Creating metaspots")
# By default, SCimplify_SpatialDLS computes distances in a parallalized way. By default, all the available cpus are used. If your computer doesn't support, you can change the number of cpus with the paramater "n.cpu"
MC.well7_no_tresh <- SCimplify_SpatialDLS_v2(X = well7.mtx ,
spotPositions = spotPosition ,
col.x = "X",
col.y = "Y",
method_similarity = "1",
split_not_connected = TRUE,
method_reduction = "PCA",
gamma = g,
n.dim = n.pc,
method_knn = "1",
k.knn = k.knn,
pct = pct,
method_normalization = "log_normalize",
cell.annotation = well7.md$Main_molecular_cell_type)
print("Done")
well7.md[,str_c("MC_membership_no_tresh_",g)] <- MC.well7_no_tresh$membership %>% as.character()
MC.well7_no_tresh.spl <- split_unconnected(MC.well7_no_tresh)
well7.md[,str_c("MC_membership_no_tresh_spl_",g)] <- MC.well7_no_tresh.spl$membership %>%
as.character()
MC.well7_no_tresh.spl$Main_molecular_cell_type <- supercell_assign(clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7_no_tresh.spl$membership,
method = "absolute")
MC.well7_no_tresh.spl$polygons <- supercell_metaspots_shape_v2(MC = MC.well7_no_tresh.spl,
spotpositions = spotPosition,
annotation = "Main_molecular_cell_type",
concavity = 2,membership_name = "membership")
SpatialDimPlotSC(original_coord = spotPosition,
col.x = "Y",
col.y = "X",
MC = MC.well7_no_tresh.spl,
sc.col = "Main_molecular_cell_type",
sc.col2 = str_c("MC_membership_no_tresh_spl_",g),
polygons_col = "polygons",
meta_data = well7.md)+
coord_fixed()+
theme_minimal()+
NoLegend()
```
With the exact same parameters as for cell types, we can clearly observe the differences with the sparse regions of the tissue.
## Computing Metaspots with fixed distance instead of percentage
We set the distance at 100:
```{r,fig.height=20, fig.width=20}
g = 10 # gamma
n.pc = 1:5 # number of first PC to use
k.knn = 15 # number of neighbors to connect to each spot
dist.trhesh = 125 # percentage of connections to keep
print("Creating metaspots")
# By default, SCimplify_SpatialDLS computes distances in a parallalized way. By default, all the available cpus are used. If your computer doesn't support, you can change the number of cpus with the paramater "n.cpu"
MC.well7_dist <- SCimplify_SpatialDLS_v2(X = well7.mtx ,
spotPositions = spotPosition ,
col.x = "X",
col.y = "Y",
method_similarity = "1",
split_not_connected = TRUE,
method_reduction = "PCA",
gamma = g,
n.dim = n.pc,
method_knn = "1",
k.knn = k.knn,
dist.thresh = dist.trhesh,
method_normalization = "log_normalize",
cell.annotation = well7.md$Main_molecular_cell_type,)
print("Done")
well7.md[,str_c("MC_membership_dist_",g)] <- MC.well7_dist$membership %>% as.character()
MC.well7_dist.spl <- split_unconnected(MC.well7_dist)
well7.md[,str_c("MC_membership_dist_spl_",g)] <- MC.well7_dist.spl$membership %>%
as.character()
MC.well7_dist.spl$Main_molecular_cell_type <- supercell_assign(clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7_dist.spl$membership,
method = "absolute")
MC.well7_dist.spl$polygons <- supercell_metaspots_shape_v2(MC = MC.well7_dist.spl,
spotpositions = spotPosition,
annotation = "Main_molecular_cell_type",
concavity = 2,membership_name = "membership")
SpatialDimPlotSC(original_coord = spotPosition,
col.x = "Y",
col.y = "X",
MC = MC.well7_dist.spl,
sc.col = "Main_molecular_cell_type",
sc.col2 = str_c("MC_membership_dist_spl_",g),
polygons_col = "polygons",
meta_data = well7.md)+
coord_fixed()+
theme_minimal()+
NoLegend()
```
With the exact same parameters as for cell types, we can clearly observe the differences with the sparse regions of the tissue.
## Create Seurat object from metaspot object
As normalization is a matter of debate for spatial transcriptomics data, we use here Log Normalization. But SuperSpot offers also using SCT and using raw counts.
```{r,fig.height=20, fig.width=20}
MC_centroids <- supercell_spatial_centroids(MC.well7.spl,spotPositions = spotPosition)
MC.ge <- superspot_GE(MC = MC.well7.spl,
ge = well7.mtx %>% as.sparse(),
groups = as.numeric(MC.well7.spl$membership),
mode = "sum"
)
MC.seurat <- supercell_2_Seuratv5(
SC.GE = MC.ge,
SC = MC.well7.spl,
fields = c("Main_molecular_cell_type")
)
MC.seurat <- NormalizeData(MC.seurat)
MC.seurat <- ScaleData(MC.seurat)
MC.seurat <- FindVariableFeatures(MC.seurat)
MC.seurat <- RunPCA(MC.seurat)
MC.seurat <- RunUMAP(MC.seurat, dims = 1:30)
DimPlot(MC.seurat, reduction = "umap", group.by = "Main_molecular_cell_type")
```
## Perform downstream analyses
```{r,fig.height=20, fig.width=20}
Idents(MC.seurat) <- "Main_molecular_cell_type"
levels(MC.seurat) <- sort(levels(MC.seurat))
VlnPlot(MC.seurat,"nFeature_RNA")+NoLegend()
#Compute marker genes
well7.mc.markers <- FindAllMarkers(MC.seurat,
only.pos = TRUE,
min.pct = 0.25,
logfc.threshold = 0.25 ) %>%
filter(p_val_adj < 0.05)
well7.mc.top.markers <- well7.mc.markers %>%
group_by(cluster) %>%
slice_max(n = 5, order_by = avg_log2FC)
DotPlot(MC.seurat,features = well7.mc.top.markers$gene %>% unique()) + RotatedAxis()
```