/
spatial_features.Rmd
320 lines (242 loc) · 11 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
317
318
319
320
---
title: "Spatial Features"
author:
date: ''
---
<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, include=FALSE}
library(STutility)
library(magrittr)
```
```{r, pre-load, echo=FALSE, eval=TRUE, include=FALSE}
load("pre_data/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 using non-negative matrix factorization (NNMF or NMF) to find underlying patterns of transcriptomic profiles. This factor analysis, along with various dimensionality reduction techniques, can all be ran via "RunXXX()", where X = the method of choice, e.g.:
<br>
```{r NMF, fig.width=16, fig.height=10, eval = FALSE}
se <- RunNMF(se, nfactors = 40) # Specificy nfactors to choose the number of factors, default=20.
```
<br>
While `RunNMF()` is an STUtility add-on, others are supported via Seurat (`RunPCA()`, `RunTSNE`, `RunICA()`, `runUMAP()` ) and for all of them, the output are stored in the Seurat object.
We can then plot a variable number of dimensions across the samples using `ST.DimPlot` or as an overlay using `DimOverlay`. These two functions are similar to the `ST.FeaturePlot` and `FeatureOverlay` but have been adapted to specifically draw dimensionality reduction vectors instead of features.
NOTE: by default, the colorscale of dimensionality reduction vectors will be centered at 0. If we have a dimensionality reduction vector x this means that the range of colors will go from -max(abs(x)) to max(abs(x)). This behaviour is typically desired when plotting e.g. PCA vectors, but for NMF all values are strictly positive so you can disable this centering by setting `center.zero = FALSE`.
<br>
```{r dimplot, fig.height=18, fig.width=12, out.width = "100%"}
cscale <- c("darkblue", "cyan", "yellow", "red", "darkred")
ST.DimPlot(se,
dims = 1:10,
ncol = 2, # Sets the number of columns at dimensions level
grid.ncol = 2, # Sets the number of columns at sample level
reduction = "NMF",
dark.theme = T,
pt.size = 1,
center.zero = F,
cols = cscale)
ST.DimPlot(se,
dims = 11:20,
ncol = 2,
grid.ncol = 2,
reduction = "NMF",
dark.theme = T,
pt.size = 1,
center.zero = F,
cols = cscale)
ST.DimPlot(se,
dims = 21:30,
ncol = 2,
grid.ncol = 2,
reduction = "NMF",
dark.theme = T,
pt.size = 1,
center.zero = F,
cols = cscale)
ST.DimPlot(se,
dims = 31:40,
ncol = 2,
grid.ncol = 2,
reduction = "NMF",
dark.theme = T,
pt.size = 1,
center.zero = F,
cols = cscale)
```
<br>
We can also print a summary of the genes that contribute most to the dimensionality reduction vectors.
For NMF output which is not centered at 0 looking at the "negative" side of the distribution doesn't really add any valuable information, instead you can aget a barplot summarizing the top most contributing genes using `FactorGeneLoadingPlot`.
<br>
```{r project_dim}
print(se[["NMF"]])
FactorGeneLoadingPlot(se, factor = 1, dark.theme = TRUE)
```
## 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 the factor analysis the previous section. Going through the list of factors (e.g. via `ST:DimPlot(se, dims = [dims you want to look at])`), we can notice dimensions that are "spatially active", i.e. that seems to confer a spatial pattern along their axis. We can extract these dimensions and use as input to e.g. clustering functions. Here, we use all dimensions from the NMF and construct a Shared Nearest Neighbor (SSN) Graph.
<br>
```{r findneighbours, eval=FALSE}
se <- FindNeighbors(object = se, verbose = FALSE, reduction = "NMF", dims = 1:40)
```
<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 = 6, fig.width = 12, out.width='100%'}
library(RColorBrewer)
n <- 19
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)))
ST.FeaturePlot(object = se, features = "seurat_clusters", dark.theme = T, cols = col_vector, pt.size = 1)
```
<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, just note that you can only do this for one section at the time (set `ìndex`).
<br>
```{r plot_clusters_split, fig.height=22, fig.width=22, out.width="100%"}
ST.FeaturePlot(object = se, features = "seurat_clusters", dark.theme = T, pt.size = 1, split.labels = T, indices = 1, show.sb = FALSE)
ST.FeaturePlot(object = se, features = "seurat_clusters", dark.theme = T, pt.size = 1, split.labels = T, indices = 2, show.sb = FALSE)
```
<br>
## Most variable features
***
We can take a specific look at some of the most variable features defined during the normalization step.
<br>
```{r variable_features, fig.height=6, fig.width=12, out.width='100%'}
head(se@assays$SCT@var.features, 20)
top <- se@assays$SCT@var.features
fts <- c("Th", "Mbp", "Nrgn")
for (ftr in fts) {
p <- FeatureOverlay(se,
features = ftr,
sampleids = 1:2,
cols = c("darkblue", "cyan", "yellow", "red", "darkred"),
pt.size = 1.5,
pt.alpha = 0.5,
dark.theme = T,
ncols.samples = 2)
print(p)
}
```
<br>
## Compare graph embeddings
***
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=FALSE}
# Run UMAP
se <- RunUMAP(se, reduction = "NMF", dims = 1:40, n.neighbors = 10)
````
```{r embedding_vs_ST, fig.width = 14, fig.height = 14}
# Define colors for heatmap
heatmap.colors <- viridis::viridis(n = 9)
fts <- c("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 = 1, dark.theme = T, show.sb = FALSE)
# Construct final plot
cowplot::plot_grid(cowplot::plot_grid(plotlist = p.fts, ncol = 1), p3, ncol = 2, rel_widths = c(1, 1.3))
```
## 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 factors as input:
<br>
```{r UMAP, val=FALSE, eval=FALSE, warning=FALSE}
se <- RunUMAP(object = se, dims = 1:40, verbose = FALSE, n.components = 3, reduction = "NMF", reduction.name = "umap.3d")
```
<br>
We use the first three dimensions for plotting:
<br>
```{r UMAP_blend, fig.height = 7, fig.width = 12, out.width="100%"}
ST.DimPlot(object = se, dims = 1:3, reduction = "umap.3d", blend = T, dark.theme = T, pt.size = 1.5)
```
<br>
## DEA
***
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 up-regulated in this region compared to the overall expression across the tissue?"
```{r de_analysis}
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 specifying `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, fig.height=6, fig.width=12, out.width="100%"}
FeatureOverlay(se, features = "Dsp",
sampleids = 1:2,
cols = c("darkblue", "cyan", "yellow", "red", "darkred"),
pt.size = 1.5,
pt.alpha = 0.5,
ncols.samples = 2,
dark.theme = T)
```
<br>
## Spatial Auto-correlation
***
STUtility also includes an additional method for finding genes with spatial patterns across the tissue. The ranking method makes use neighborhood networks to compute the spatial lag for each gene, here defined as the summed expression of that gene in neighboring spots. Each gene is then ranked by the correlation between the lag vector and the original expression vector. The output is data.frame genes ranked by correlation between the two vectors.
This method is partly inspired by work from the [Giotto team](http://spatial.rc.fas.harvard.edu/spatialgiotto/giotto.html) and we reccomend you to check out their R package Giotto and their preprint; ["Giotto, a pipeline for integrative analysis and visualization of single-cell spatial transcriptomic data"](https://www.biorxiv.org/content/10.1101/701680v1) that is available on bioRxiv.
<br>
```{r autocorrelation, eval=FALSE}
library(spdep)
spatgenes <- CorSpatialGenes(se)
```
```{r load_spatgenes, include=FALSE}
load("pre_data/spatgenes")
```
```{r print_spatgenes}
head(spatgenes)
```
<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>