-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.Rmd
189 lines (153 loc) · 7.9 KB
/
example.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
---
title: "Vignette SuperSpot"
author: "Matei Teleman"
date: "2024-01-15"
output: html_document
---
## 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")
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$imagerow <- as.numeric(spotPosition$imagerow)
spotPosition$imagecol <- as.numeric(spotPosition$imagecol)
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 = 16 # 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_DLS <- SCimplify_SpatialDLS(X = well7.mtx ,
spotPositions = spotPosition ,
method_similarity = "1",
split_not_connected = T,
genes.use = rownames(well7.mtx),
gamma = g,
n.pc = 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_DLS$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_DLS$purity <- supercell_purity(
clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7_DLS$membership,
method = method_purity
)
print(str_c("mean purity is ",mean(MC.well7_DLS$purity)))
#We assign each metaspot with its corresponding annotation
MC.well7_DLS$Main_molecular_cell_type <- supercell_assign(clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7_DLS$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}
print("Creating polygons for visualization")
MC.well7_DLS$polygons <- supercell_metaspots_shape(MC = MC.well7_DLS,
spotpositions = spotPosition,
annotation = "Main_molecular_cell_type",
concavity = 2,
membership_name = "membership")
print("Done")
SpatialDimPlotSC(original_coord = spotPosition,
MC = MC.well7_DLS,
sc.col = "Main_molecular_cell_type",
sc.col2 = str_c("MC_membership_",g),
polygons_col = "polygons",
meta_data = well7.md)+
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_DLS.spl <- split_unconnected(MC.well7_DLS)
well7.md[,str_c("MC_membership_spl_",g)] <- MC.well7_DLS.spl$membership %>%
as.character()
MC.well7_DLS.spl$Main_molecular_cell_type <- supercell_assign(clusters = well7.md$Main_molecular_cell_type,
supercell_membership = MC.well7_DLS.spl$membership,
method = "absolute")
MC.well7_DLS.spl$polygons <- supercell_metaspots_shape(MC = MC.well7_DLS.spl,
spotpositions = spotPosition,
annotation = "Main_molecular_cell_type",
concavity = 2,membership_name = "membership")
SpatialDimPlotSC(original_coord = spotPosition,
MC = MC.well7_DLS.spl,
sc.col = "Main_molecular_cell_type",
sc.col2 = str_c("MC_membership_spl_",g),
polygons_col = "polygons",
meta_data = well7.md)+
NoLegend()+
theme(plot.background = element_rect(fill = 'black'),
panel.background = element_rect(fill = 'black'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
```
## 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_DLS.spl,spotPositions = spotPosition)
MC.ge <- superspot_GE(MC = MC.well7_DLS.spl,
ge = well7.mtx %>% as.matrix(),
groups = as.numeric(MC.well7_DLS.spl$membership),
mode = "sum"
)
MC.seurat <- supercell_2_Seuratv5(
SC.GE = MC.ge,
SC = MC.well7_DLS.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()
```