/
spatial_features.Rmd
316 lines (240 loc) · 10.2 KB
/
spatial_features.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
---
title: "Spatial Features"
author:
date: ''
#output:
# html_document:
# theme: darkly
# css: style.css
# number_sections: false
# toc: yes
# toc_depth: 3
# toc_float:
# collapsed: false
# smooth_scroll: true
# pdf_document:
# number_sections: false
# toc: yes
# toc_depth: 3
---
<style type="text/css">
div.main-container {
background-color: #000000 !important;
max-width: 1400px;
margin-left: auto;
margin-right: auto;
}
</style>
<style>
#TOC {
background: url("https://www.spatialresearch.org/wp-content/uploads/2019/09/str-logo-spatial_research_3@2x.png");
background-size: contain;
padding-top: 100px !important;
background-repeat: no-repeat;
op: 5%;
opacity: 0.8;
width: 500px;
color: white;
border-color: #000000 !important;
}
</style>
<style> code, pre{
background-color: #000000 !important;
color: white !important;
}
</style>
<style>
body {
color: white
}
</style>
<style>
.list-group-item.active, .list-group-item.active:focus, .list-group-item.active:hover {
background-color: #375a7f;
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, autodep = TRUE)
```
```{r load_lib, warning=FALSE, message=FALSE, results="hide", autodep=TRUE, include=FALSE}
library(STutility)
load("~/STUtility/saved/preSaved_10xHippo_norm_reductions.RData")
```
## Finding spatial expression patterns
The strength of untargeted whole transcriptome capture is the ability to perform unsupervised analysis and the ability to find spatial gene expression patterns. We've found good use of the dimensionality reduction method "ICA" (Independent Component Analysis) to find spatial expression patterns. An example is demonstrated below:
<br>
```{r ICA, fig.width=16, fig.height=10, eval = FALSE}
se <- RunICA(se)
```
<br>
Different dimensionality reductions methods are supported via Seurat (`RunPCA()`, `RunTSNE`, `RunICA()`, `runUMAP()` ) and the output are stored in the Seurat object.
We can then plot a variable number of dimensions across the samples.
<br>
```{r dimplot, fig.height=28, fig.width=16, out.width = "100%"}
ST.DimPlot(se,
dims = c(-1, 2, -3, -4, 5, -6, 7, -8, 9, 10, 11, 12, -13, 14, 15, -16), # If we flip the sign, the ICA vector will be inverted
ncol = 8, # Sets the number of columns at dimensions level
grid.ncol = 2, # Sets the number of columns at sample level
reduction = "ica",
dark.theme = T,
pt.size = 0.5,
center.zero = T,
palette = "MaYl")
```
<br>
To extract the genes that drives the separation according to the dimensionality reduction, we can use the `ProjectDim()` function. Where we specify the dimensions of interest (here IC_1 and IC_3, which are regions that seems to confer a clear spatial expression histology.
<br>
```{r project_dim}
ProjectDim(se, reduction = "ica", dims = c(1, 2, 4, 5))
```
## Clustering
Clustering is a standard procedure in genomic analysis, and the methods for doing so are numerous. Here we demonstrate an example where we use the result of ICA to perform clustering. In the previous section, we demonstrated how to plot a subset of dimensions from the ICA output. Going through this list, we can notice the dimensions that are "spatially active", i.e. that seems to confer a spatial pattern along their axis. We can extract these dimensions:
<br>
```{r keep_dims, fig.width=16, fig.height=10}
keep.dims <- c(1:12, 16:18, 21:28, 30:34, 40:41, 47, 49:50)
```
<br>
And then use them to construct a Shared Nearest Neighbor (SSN) Graph.
<br>
```{r findneighbours, eval=FALSE}
se <- FindNeighbors(object = se, dims = keep.dims, verbose = FALSE, reduction = "ica")
```
<br>
Followed by clustering using a modularity optimizer
<br>
```{r findclusters, eval=FALSE}
se <- FindClusters(object = se, verbose = FALSE)
```
<br>
And plotting of the clusters spatially
<br>
```{r plot_clusters, fig.height = 5, fig.width = 10, out.width='100%'}
library(RColorBrewer)
n <- 21
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
se[["clusters_ICA"]] <- se[["seurat_clusters"]]
ST.FeaturePlot(object = se, features = "clusters_ICA", dark.theme = T, cols = col_vector, pt.size = 0.5)
```
<br>
```{r clusters_UMAP, fig.height=6, fig.width=8}
se <- SetIdent(se, value = "clusters_ICA")
DimPlot(se, reduction = "umap", cols = col_vector) + DarkTheme()
```
<br>
If you think that the distribution of clusters gets too cluttered, you can also split the view so that only one cluster at the time gets colored.
<br>
```{r plot_clusters_split, fig.width=8, out.width='100%'}
ST.FeaturePlot(object = se, features = "clusters_ICA", dark.theme = T, cols = col_vector, pt.size = 0.1, split.labels = T, indices = 1)
```
<br>
We can take a look a specific look at some of the most variable features.
<br>
```{r variable_features, fig.height=12, fig.width=6, out.width='100%'}
head(se@assays$SCT@var.features, 20)
top <- se@assays$SCT@var.features
fts <- c("Slc6a3", "Mbp", "Nrgn")
for (ftr in fts) {
MultiFeatureOverlay(se,
features = ftr,
sampleids = 1:2,
cols = c("black", "dark blue", "cyan", "yellow", "red", "dark red"),
pt.size = 0.5,
pt.alpha = 0.5,
dark.theme = T)
}
```
<br>
Another useful feature is that you can now compare the spatial distribution of a gene with the typical "graph embeddings" s.a. UMAP and t-SNE.
<br>
```{r run_UMAP, eval=F}
# Run UMAP
se <- RunUMAP(se, reduction = "ica", dims = keep.dims, n.neighbors = 10)
````
```{r embedding_vs_ST, fig.width = 16, fig.height = 20}
# Define colors for heatmap
heatmap.colors <- c(rgb(40, 40, 40, maxColorValue = 255), "dark blue", "cyan", "white")
fts <- c("Slc18a2", "Prkcd", "Opalin", "Lamp5")
# plot transformed features expression on UMAP embedding
p.fts <- lapply(fts, function(ftr) {
FeaturePlot(se, features = ftr, reduction = "umap", order = TRUE, cols = heatmap.colors) + DarkTheme()
})
# plot transformed features expression on Visium coordinates
p3 <- ST.FeaturePlot(se, features = fts, ncol = 2, grid.ncol = 1, cols = heatmap.colors, pt.size = 0.5, dark.theme = T)
# Construct final plot
cowplot::plot_grid(cowplot::plot_grid(plotlist = p.fts, ncol = 1), p3, ncol = 2, rel_widths = c(1, 2))
```
## RGB dimensionality reduction plots
One approach to visualize the result of dimensionality reduction is to use the first three dimensions and transform the values into RGB color space. This 3 dimensional space can then be utilized for spatial visualization.
We demonstrate the technique with UMAP, using our ICA dimensions as input:
<br>
```{r UMAP, val=FALSE, eval=FALSE}
se <- RunUMAP(object = se, dims = keep.dims, verbose = FALSE, n.components = 3, reduction = "ica", reduction.name = "umap.3d")
```
<br>
We use the first three dimensions for plotting:
<br>
```{r UMAP_blend, fig.height = 6, fig.width = 12}
ST.DimPlot(object = se, dims = 1:3, reduction = "umap.3d", blend = T, dark.theme = T, pt.size = 0.5)
```
<br>
Once again the `ProjectDim()` function can be used to display the genes that are most strongly correlated with the coordinate system. Note in the function call above that we defined `reduction.name`, which are subsequently stored in the Seurat object in the reduction slot:
<br>
```{r projectdim_UMAP}
ProjectDim(se, reduction = "umap.3d")
```
<br>
## DEA and validation of results
As the white knights of science, we promote the use of orthonogal data to validate results :-)
Since we're using mouse brain here, we can utilize the [Allen brain atlas](https://portal.brain-map.org/) as external data source.
Lets try this out by an example. Looking at \ref{plot_clusters_split}, lets say we are interested in cluster 19, and want to answer - "Which genes are significantly upregulated in this region compared to the overall expression across the tisse?"
```{r}
markers <- FindMarkers(se, ident.1 = "19")
head(markers)
```
Note that the clusters were already set as the Seurat objects levels. Type `levels(se)` to see the current levels of your object. If other clusters, annotations etc are of interest, set this before by specifing `Idents(se) <- `
Note also, if we are interested in comparing two levels against each other, and not just "one against the rest", we simply add a `ident.2 = ` parameter to the above.
```{r, out.width='100%'}
FeatureOverlay(se, features = "Dsp",
sampleids = 1:2,
cols = c("black", "dark blue", "cyan", "yellow", "red", "dark red"),
pt.size = 0.5,
pt.alpha = 0.5,
dark.theme = T)
```
<br>
How does it look in the Allen Brain Atlas?
<br>
<center>
![ISH data - Allen brain atlas](assets/Allen_ISH_data_dsp.PNG)
<br>
![Expression data - Allen brain atlas](assets/Allen_expression_data_dsp.PNG)
</center>
<br>
```{r allen_example, fig.height=12, fig.width=6, out.width='100%'}
# CHANGE
MultiFeatureOverlay(se, features = "Cck",
sampleids = 1:2,
cols = c("black", "dark blue", "cyan", "yellow", "red", "dark red"),
pt.size = 0.5,
pt.alpha = 0.5,
dark.theme = T)
```
```{r, FIt-SNE, eval=F, echo=F}
se <- RunPCA(se)
se <- RunTSNE(se, reduction="pca",
dims=1:50,
tsne.method = "FIt-SNE",
reduction.name = "fitSNE",
perplexity_list = list(30,250))
```
<hr />
<p style="text-align: center;">A work by <a href="j.bergenstrahle@scilifelab.se">Joseph Bergenstråhle</a> and <a href="ludvig.larsson@scilifelab.se">Ludvig Larsson</a></p>
<!-- Add icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Add font awesome icons -->
<p style="text-align: center;">
<a href="https://www.spatialresearch.org" class="fa fa-beer"></a>
</p>