You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](http://bioconda.github.io/recipes/spatialleiden/README.html)
# This is not recommended! Suppressing the warnings is only done because the code is run
36
+
# when building the docs and would clutter the webpage
37
+
import warnings
38
+
39
+
warnings.filterwarnings("ignore")
32
40
```
33
41
34
42
First of all we are going to load the relevant packages that we will be working with as well as setting a random seed that we will use throughout this example to make the results reproducible.
@@ -38,10 +46,10 @@ import scanpy as sc
38
46
import spatialleiden as sl
39
47
import squidpy as sq
40
48
41
-
seed = 42
49
+
random_state = 42
42
50
```
43
51
44
-
The data set consists of 155 genes and ~5,500 cells including their annotation for cell type as well as domains.
52
+
The data set consists of 155 genes and ~5,500 cells including their annotation for the cell type as well as domains.
45
53
46
54
+++
47
55
@@ -51,14 +59,36 @@ We will do some standard preprocessing by log-transforming the data and then usi
51
59
52
60
```{code-cell} ipython3
53
61
sc.pp.log1p(adata)
54
-
sc.pp.pca(adata, random_state=seed)
62
+
sc.pp.pca(adata, random_state=random_state)
55
63
56
-
sc.pp.neighbors(adata, random_state=seed)
64
+
sc.pp.neighbors(adata, random_state=random_state)
57
65
```
58
66
59
-
For SpatialLeiden we need an additional graph representing the connectivities in the topological space. Here we will use a kNN graph with 10 neighbors that we generate with {py:func}`squidpy.gr.spatial_neighbors`. Alternatives are Delaunay triangulation or regular grids in case of e.g. Visium data.
67
+
### Building spatial neighbor graphs
68
+
69
+
For SpatialLeiden we need an additional graph representing the neighbors in space i.e.
70
+
which cells are close/next to each other.
71
+
72
+
What kind of spatial neighbor graph is suitable for the analysis is dependent on the
73
+
technology used to generate the data. Most of the neighborhood structures interesting
74
+
for our use cases can be calculated using {py:func}`squidpy.gr.spatial_neighbors`.
60
75
61
-
We can use the calculated distances between neighboring points and transform them into connectivities using the {py:func}`spatialleiden.distance2connectivity` function.
76
+
Generally, if the data is generated from a method with a regular lattice it is advisible
77
+
to use this for the analysis;
78
+
* isometric grid (hexagonal): for Visium with `squidpy.gr.spatial_neighbors(adata, coord_type="grid", n_neighs=6)`
79
+
* square grid: for binned Stereo-seq and VisiumHD with `squidpy.gr.spatial_neighbors(adata, coord_type="grid", n_neighs=4)` (using 8 neighbors is also possible)
80
+
81
+
If your data does not originate from a regular lattice, there are various options to build your neighborhood graph.
82
+
This applies to all imaging-based methodologies that are usually analysed after segmenting cells, but also technolgoies with regular lattices if you use cell segmentation (such as Stereo-seq or VisiumHD).
83
+
* kNN: calculating the *k*-nearest neighbors per cell with `squidpy.gr.spatial_neighbors(adata, coord_type="generic", n_neighs=k)`
* radius-based: with a threshold of *r* units `squidpy.gr.spatial_neighbors(adata, coord_type="generic", radius=r)`
86
+
* other methods such as Gabriel graphs, ...
87
+
88
+
For the neighborhoods that are not based on regular grids we can, furthermore, scale the weight of each edge bsaed on the distance between the two cells (that's why it is not useful for the regular grid case as the neighbors will be equidistant).
89
+
This can be achieved by calculating connectivities based on the distances using the {py:func}`spatialleiden.distance2connectivity` function.
Now, we can already run {py:func}`spatialleiden.spatialleiden` (which we will also compare to normal Leiden clustering).
72
104
73
-
The `layer_ratio` determines the weighting between the gene expression and the topological layer and is influenced by the graph structures (i.e. how many connections exist, the edge weights, etc.); the lower the value is the closer SpatialLeiden will be to normal Leiden clustering, while higher values lead to more spatially homogeneous clusters.
105
+
The `layer_ratio` determines the weighting between the gene expression and the spatial layer and is influenced by the graph structures (i.e. how many connections exist, the edge weights, etc.); the lower the value is the closer SpatialLeiden will be to normal Leiden clustering, while higher values lead to more spatially homogeneous clusters.
74
106
75
107
The resolution has the same effect as in Leiden clustering (higher resolution will lead to more clusters) and can be defined for each of the layers (but for now is left at its default value).
In our case we can compare the resulting clusters to the annotated ground truth regions. If we are not satisfied with the results, we can go back and tweak other parameters such as the underlying neighborhood graphs or the `layer_ratio` to achieve the desired granularity of our results.
110
148
149
+
```{code-cell} ipython3
150
+
---
151
+
tags: [hide-cell]
152
+
---
153
+
154
+
# needed for scanpy v1.11 otherwise plotting fails because the number of clusters changed
0 commit comments