Launch binder

Analyze Visium H&E data

This tutorial shows how to apply Squidpy for the analysis of Visium spatial transcriptomics data.

The dataset used here consists of a Visium slide of a coronal section of the mouse brain. The original dataset is publicly available at the 10x genomics dataset portal . Here, we provide a pre-processed dataset, with pre-annotated clusters, in AnnData format and the tissue image in squidpy.im.ImageContainer format.

A couple of notes on pre-processing:

See also

See Analyze Visium fluorescence data for a detailed analysis example of image features.

Import packages & data

To run the notebook locally, create a conda environment as conda env create -f environment.yml using this environment.yml

import scanpy as sc
import anndata as ad
import squidpy as sq

import numpy as np
import pandas as pd

sc.logging.print_header()
print(f"squidpy=={sq.__version__}")

# load the pre-processed dataset
img = sq.datasets.visium_hne_image()
adata = sq.datasets.visium_hne_adata()

Out:

scanpy==1.8.0.dev88+g525f08f6 anndata==0.7.6 umap==0.5.1 numpy==1.20.2 scipy==1.6.3 pandas==1.2.4 scikit-learn==0.24.2 statsmodels==0.12.2 python-igraph==0.9.1 pynndescent==0.5.2
squidpy==1.0.0

  0%|          | 0.00/380M [00:00<?, ?B/s]
  0%|          | 8.00k/380M [00:00<1:39:10, 67.0kB/s]
  0%|          | 56.0k/380M [00:00<25:16, 263kB/s]
  0%|          | 80.0k/380M [00:00<28:23, 234kB/s]
  0%|          | 200k/380M [00:00<12:23, 536kB/s]
  0%|          | 400k/380M [00:00<07:02, 942kB/s]
  0%|          | 808k/380M [00:00<03:44, 1.77MB/s]
  0%|          | 1.59M/380M [00:00<01:56, 3.41MB/s]
  1%|          | 3.20M/380M [00:00<00:59, 6.70MB/s]
  2%|1         | 5.98M/380M [00:01<00:32, 12.0MB/s]
  2%|2         | 8.59M/380M [00:01<00:25, 15.1MB/s]
  3%|3         | 11.6M/380M [00:01<00:21, 18.1MB/s]
  4%|3         | 14.5M/380M [00:01<00:18, 20.4MB/s]
  5%|4         | 17.4M/380M [00:01<00:17, 21.6MB/s]
  5%|5         | 20.4M/380M [00:01<00:16, 22.7MB/s]
  6%|6         | 23.1M/380M [00:01<00:16, 22.9MB/s]
  7%|6         | 25.9M/380M [00:01<00:15, 23.2MB/s]
  8%|7         | 28.8M/380M [00:02<00:15, 23.6MB/s]
  8%|8         | 31.7M/380M [00:02<00:15, 24.0MB/s]
  9%|9         | 34.6M/380M [00:02<00:15, 24.1MB/s]
 10%|9         | 37.5M/380M [00:02<00:14, 24.4MB/s]
 11%|#         | 40.5M/380M [00:02<00:14, 24.7MB/s]
 11%|#1        | 43.5M/380M [00:02<00:14, 24.9MB/s]
 12%|#2        | 46.4M/380M [00:02<00:13, 25.0MB/s]
 13%|#2        | 49.2M/380M [00:02<00:14, 24.5MB/s]
 14%|#3        | 51.9M/380M [00:03<00:14, 24.1MB/s]
 14%|#4        | 54.8M/380M [00:03<00:14, 24.2MB/s]
 15%|#5        | 57.6M/380M [00:03<00:13, 24.3MB/s]
 16%|#5        | 60.3M/380M [00:03<00:14, 23.8MB/s]
 17%|#6        | 63.2M/380M [00:03<00:13, 24.1MB/s]
 17%|#7        | 66.1M/380M [00:03<00:13, 24.2MB/s]
 18%|#8        | 68.9M/380M [00:03<00:13, 24.1MB/s]
 19%|#8        | 71.7M/380M [00:03<00:13, 24.2MB/s]
 20%|#9        | 74.6M/380M [00:04<00:13, 24.3MB/s]
 20%|##        | 77.6M/380M [00:04<00:12, 24.5MB/s]
 21%|##1       | 80.6M/380M [00:04<00:12, 24.5MB/s]
 22%|##1       | 83.6M/380M [00:04<00:12, 24.8MB/s]
 23%|##2       | 86.3M/380M [00:04<00:12, 24.7MB/s]
 23%|##3       | 89.2M/380M [00:04<00:12, 24.6MB/s]
 24%|##4       | 92.1M/380M [00:04<00:12, 24.7MB/s]
 25%|##5       | 95.1M/380M [00:04<00:12, 24.8MB/s]
 26%|##5       | 98.0M/380M [00:05<00:11, 24.9MB/s]
 27%|##6       | 101M/380M [00:05<00:11, 24.8MB/s]
 27%|##7       | 104M/380M [00:05<00:11, 24.7MB/s]
 28%|##8       | 107M/380M [00:05<00:11, 24.9MB/s]
 29%|##8       | 110M/380M [00:05<00:11, 24.6MB/s]
 30%|##9       | 112M/380M [00:05<00:11, 24.5MB/s]
 30%|###       | 115M/380M [00:05<00:11, 24.4MB/s]
 31%|###1      | 118M/380M [00:05<00:11, 24.7MB/s]
 32%|###1      | 121M/380M [00:06<00:10, 24.8MB/s]
 33%|###2      | 124M/380M [00:06<00:10, 24.6MB/s]
 33%|###3      | 127M/380M [00:06<00:10, 24.7MB/s]
 34%|###4      | 130M/380M [00:06<00:10, 24.8MB/s]
 35%|###4      | 133M/380M [00:06<00:10, 24.7MB/s]
 36%|###5      | 136M/380M [00:06<00:10, 24.6MB/s]
 36%|###6      | 139M/380M [00:06<00:10, 24.8MB/s]
 37%|###7      | 142M/380M [00:06<00:10, 24.9MB/s]
 38%|###7      | 144M/380M [00:07<00:10, 24.5MB/s]
 39%|###8      | 147M/380M [00:07<00:09, 24.4MB/s]
 39%|###9      | 150M/380M [00:07<00:09, 24.4MB/s]
 40%|####      | 153M/380M [00:07<00:09, 24.3MB/s]
 41%|####      | 156M/380M [00:07<00:09, 24.2MB/s]
 42%|####1     | 159M/380M [00:07<00:09, 24.3MB/s]
 43%|####2     | 161M/380M [00:07<00:09, 24.5MB/s]
 43%|####3     | 164M/380M [00:07<00:09, 24.6MB/s]
 44%|####4     | 167M/380M [00:08<00:09, 24.6MB/s]
 45%|####4     | 170M/380M [00:08<00:08, 24.7MB/s]
 46%|####5     | 173M/380M [00:08<00:08, 24.8MB/s]
 46%|####6     | 176M/380M [00:08<00:08, 24.5MB/s]
 47%|####7     | 179M/380M [00:08<00:08, 24.4MB/s]
 48%|####7     | 182M/380M [00:08<00:08, 24.5MB/s]
 49%|####8     | 185M/380M [00:08<00:08, 24.5MB/s]
 49%|####9     | 188M/380M [00:08<00:08, 24.6MB/s]
 50%|#####     | 190M/380M [00:08<00:08, 24.6MB/s]
 51%|#####     | 193M/380M [00:09<00:07, 24.8MB/s]
 52%|#####1    | 196M/380M [00:09<00:07, 24.5MB/s]
 52%|#####2    | 199M/380M [00:09<00:07, 24.1MB/s]
 53%|#####3    | 202M/380M [00:09<00:07, 23.9MB/s]
 54%|#####3    | 205M/380M [00:09<00:07, 24.1MB/s]
 55%|#####4    | 208M/380M [00:09<00:07, 24.5MB/s]
 55%|#####5    | 210M/380M [00:09<00:07, 24.1MB/s]
 56%|#####6    | 213M/380M [00:09<00:07, 23.9MB/s]
 57%|#####6    | 216M/380M [00:10<00:07, 24.0MB/s]
 58%|#####7    | 219M/380M [00:10<00:07, 24.1MB/s]
 58%|#####8    | 222M/380M [00:10<00:06, 24.4MB/s]
 59%|#####9    | 225M/380M [00:10<00:06, 24.6MB/s]
 60%|#####9    | 228M/380M [00:10<00:06, 24.6MB/s]
 61%|######    | 230M/380M [00:10<00:06, 24.6MB/s]
 61%|######1   | 233M/380M [00:10<00:06, 24.6MB/s]
 62%|######2   | 236M/380M [00:10<00:06, 24.6MB/s]
 63%|######2   | 239M/380M [00:11<00:05, 24.8MB/s]
 64%|######3   | 242M/380M [00:11<00:05, 24.7MB/s]
 64%|######4   | 245M/380M [00:11<00:05, 24.5MB/s]
 65%|######5   | 248M/380M [00:11<00:05, 24.4MB/s]
 66%|######5   | 251M/380M [00:11<00:05, 24.6MB/s]
 67%|######6   | 254M/380M [00:11<00:05, 24.8MB/s]
 68%|######7   | 257M/380M [00:11<00:05, 24.7MB/s]
 68%|######8   | 259M/380M [00:11<00:05, 24.6MB/s]
 69%|######9   | 262M/380M [00:12<00:05, 24.5MB/s]
 70%|######9   | 265M/380M [00:12<00:04, 24.5MB/s]
 71%|#######   | 268M/380M [00:12<00:04, 24.8MB/s]
 71%|#######1  | 271M/380M [00:12<00:04, 24.7MB/s]
 72%|#######2  | 274M/380M [00:12<00:04, 24.8MB/s]
 73%|#######2  | 277M/380M [00:12<00:04, 24.9MB/s]
 74%|#######3  | 280M/380M [00:12<00:04, 24.7MB/s]
 74%|#######4  | 283M/380M [00:12<00:04, 24.6MB/s]
 75%|#######5  | 286M/380M [00:13<00:03, 24.8MB/s]
 76%|#######5  | 289M/380M [00:13<00:03, 24.9MB/s]
 77%|#######6  | 291M/380M [00:13<00:03, 24.8MB/s]
 77%|#######7  | 294M/380M [00:13<00:03, 24.4MB/s]
 78%|#######8  | 297M/380M [00:13<00:03, 24.2MB/s]
 79%|#######8  | 300M/380M [00:13<00:03, 24.3MB/s]
 80%|#######9  | 303M/380M [00:13<00:03, 24.1MB/s]
 80%|########  | 306M/380M [00:13<00:03, 24.0MB/s]
 81%|########1 | 308M/380M [00:14<00:03, 24.1MB/s]
 82%|########1 | 311M/380M [00:14<00:02, 24.2MB/s]
 83%|########2 | 314M/380M [00:14<00:02, 24.3MB/s]
 83%|########3 | 317M/380M [00:14<00:02, 24.6MB/s]
 84%|########4 | 320M/380M [00:14<00:02, 24.7MB/s]
 85%|########4 | 323M/380M [00:14<00:02, 24.7MB/s]
 86%|########5 | 326M/380M [00:14<00:02, 24.6MB/s]
 87%|########6 | 329M/380M [00:14<00:02, 24.6MB/s]
 87%|########7 | 332M/380M [00:15<00:02, 24.6MB/s]
 88%|########8 | 334M/380M [00:15<00:01, 24.6MB/s]
 89%|########8 | 337M/380M [00:15<00:01, 24.5MB/s]
 90%|########9 | 340M/380M [00:15<00:01, 24.7MB/s]
 90%|######### | 343M/380M [00:15<00:01, 24.6MB/s]
 91%|#########1| 346M/380M [00:15<00:01, 24.8MB/s]
 92%|#########1| 349M/380M [00:15<00:01, 24.7MB/s]
 93%|#########2| 352M/380M [00:15<00:01, 24.8MB/s]
 93%|#########3| 355M/380M [00:16<00:01, 24.7MB/s]
 94%|#########4| 358M/380M [00:16<00:00, 24.6MB/s]
 95%|#########4| 361M/380M [00:16<00:00, 24.6MB/s]
 96%|#########5| 364M/380M [00:16<00:00, 24.8MB/s]
 96%|#########6| 366M/380M [00:16<00:00, 24.7MB/s]
 97%|#########7| 369M/380M [00:16<00:00, 24.8MB/s]
 98%|#########7| 372M/380M [00:16<00:00, 24.9MB/s]
 99%|#########8| 375M/380M [00:16<00:00, 24.8MB/s]
100%|#########9| 378M/380M [00:17<00:00, 24.8MB/s]
100%|##########| 380M/380M [00:17<00:00, 23.4MB/s]

First, let’s visualize cluster annotation in spatial context with scanpy.pl.spatial().

sc.pl.spatial(adata, color="cluster")
cluster

Image features

Visium datasets contain high-resolution images of the tissue that was used for the gene extraction. Using the function squidpy.im.calculate_image_features() you can calculate image features for each Visium spot and create a obs x features matrix in adata that can then be analyzed together with the obs x gene gene expression matrix.

By extracting image features we are aiming to get both similar and complementary information to the gene expression values. Similar information is for example present in the case of a tissue with two different cell types whose morphology is different. Such cell type information is then contained in both the gene expression values and the tissue image features.

Squidpy contains several feature extractors and a flexible pipeline of calculating features of different scales and sizes. There are several detailed examples of how to use squidpy.im.calculate_image_features(). Extract image features provides a good starting point for learning more.

Here, we will extract summary features at different crop sizes and scales to allow the calculation of multi-scale features and segmentation features. For more information on the summary features, also refer to Extract summary features.

# calculate features for different scales (higher value means more context)
for scale in [1.0, 2.0]:
    feature_name = f"features_summary_scale{scale}"
    sq.im.calculate_image_features(
        adata,
        img,
        features="summary",
        key_added=feature_name,
        n_jobs=1,
        scale=scale,
    )


# combine features in one dataframe
adata.obsm["features"] = pd.concat(
    [adata.obsm[f] for f in adata.obsm.keys() if "features_summary" in f], axis="columns"
)
# make sure that we have no duplicated feature names in the combined table
adata.obsm["features"].columns = ad.utils.make_index_unique(adata.obsm["features"].columns)

Out:

0%|          | 0/2688 [00:00<?, ?/s]
0%|          | 0/2688 [00:00<?, ?/s]

We can use the extracted image features to compute a new cluster annotation. This could be useful to gain insights in similarities across spots based on image morphology.

# helper function returning a clustering
def cluster_features(features: pd.DataFrame, like=None):
    """Calculate leiden clustering of features.

    Specify filter of features using `like`.
    """
    # filter features
    if like is not None:
        features = features.filter(like=like)
    # create temporary adata to calculate the clustering
    adata = ad.AnnData(features)
    # important - feature values are not scaled, so need to scale them before PCA
    sc.pp.scale(adata)
    # calculate leiden clustering
    sc.pp.pca(adata, n_comps=min(10, features.shape[1] - 1))
    sc.pp.neighbors(adata)
    sc.tl.leiden(adata)

    return adata.obs["leiden"]


# calculate feature clusters
adata.obs["features_cluster"] = cluster_features(adata.obsm["features"], like="summary")

# compare feature and gene clusters
sc.set_figure_params(facecolor="white", figsize=(8, 8))
sc.pl.spatial(adata, color=["features_cluster", "cluster"])
features_cluster, cluster

Comparing gene and feature clusters, we notice that in some regions, they look very similar, like the cluster Fiber_tract, or clusters around the Hippocampus seems to be roughly recapitulated by the clusters in image feature space. In others, the feature clusters look different, like in the cortex, where the gene clusters show the layered structure of the cortex, and the features clusters rather seem to show different regions of the cortex.

This is only a simple, comparative analysis of the image features, note that you could also use the image features to e.g. compute a common image and gene clustering by computing a shared neighbors graph (for instance on concatenated PCAs on both feature spaces).

Spatial statistics and graph analysis

Similar to other spatial data, we can investigate spatial organization by leveraging spatial and graph statistics in Visium data.

Neighborhood enrichment

Computing a neighborhood enrichment can help us identify spots clusters that share a common neighborhood structure across the tissue. We can compute such score with the following function: squidpy.gr.nhood_enrichment(). In short, it’s an enrichment score on spatial proximity of clusters: if spots belonging to two different clusters are often close to each other, then they will have a high score and can be defined as being enriched. On the other hand, if they are far apart, and therefore are seldom a neighborhood, the score will be low and they can be defined as depleted. This score is based on a permutation-based test, and you can set the number of permutations with the n_perms argument (default is 1000).

Since the function works on a connectivity matrix, we need to compute that as well. This can be done with squidpy.gr.spatial_neighbors(). Please see Building spatial neighbors graph for more details of how this function works.

Finally, we’ll directly visualize the results with squidpy.pl.nhood_enrichment().

sq.gr.spatial_neighbors(adata)
sq.gr.nhood_enrichment(adata, cluster_key="cluster")
sq.pl.nhood_enrichment(adata, cluster_key="cluster")
Neighborhood enrichment

Out:

0%|          | 0/1000 [00:00<?, ?/s]

Given the spatial organization of the mouse brain coronal section, not surprisingly we find high neighborhood enrichment the Hippocampus region: Pyramidal_layer_dentate_gyrus and Pyramidal_layer clusters seems to be often neighbors with the larger Hippocampus cluster.

Co-occurrence across spatial dimensions

In addition to the neighbor enrichment score, we can visualize cluster co-occurrence in spatial dimensions. This is a similar analysis of the one presented above, yet it does not operate on the connectivity matrix, but on the original spatial coordinates. The co-occurrence score is defined as:

\[\frac{p(exp|cond)}{p(exp)}\]

where \(p(exp|cond)\) is the conditional probability of observing a cluster \(exp\) conditioned on the presence of a cluster \(cond\), whereas \(p(exp)\) is the probability of observing \(exp\) in the radius size of interest. The score is computed across increasing radii size around each observation (i.e. spots here) in the tissue.

We are gonna compute such score with squidpy.gr.co_occurrence() and set the cluster annotation for the conditional probability with the argument clusters. Then, we visualize the results with squidpy.pl.co_occurrence().

sq.gr.co_occurrence(adata, cluster_key="cluster")
sq.pl.co_occurrence(
    adata,
    cluster_key="cluster",
    clusters="Hippocampus",
    figsize=(8, 4),
)
$\frac{p(exp|Hippocampus)}{p(exp)}$

Out:

  0%|          | 0/1 [00:00<?, ?/s]
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'rocket' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'rocket_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'mako' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'mako_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'icefire' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'icefire_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'vlag' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'vlag_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'flare' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'flare_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'crest' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'crest_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)

The result largely recapitulates the previous analysis: the Pyramidal_layer cluster seem to co-occur at short distances with the larger Hippocampus cluster. It should be noted that the distance units are given in pixels of the Visium source_image, and corresponds to the same unit of the spatial coordinates saved in adata.obsm["spatial"].

Ligand-receptor interaction analysis

We are continuing the analysis showing couple of feature-level methods that are very relevant for the analysis of spatial molecular data. For instance, after quantification of cluster co-occurrence, we might be interested in finding molecular instances that could potentially drive cellular communication. This naturally translates in a ligand-receptor interaction analysis. In Squidpy, we provide a fast re-implementation the popular method CellPhoneDB [Efremova et al., 2020] (code ) and extended its database of annotated ligand-receptor interaction pairs with the popular database Omnipath [Türei et al., 2016]. You can run the analysis for all clusters pairs, and all genes (in seconds, without leaving this notebook), with squidpy.gr.ligrec(). Furthermore, we’ll directly visualize the results, filtering out lowly-expressed genes (with the means_range argument) and increasing the threshold for the adjusted p-value (with the alpha argument). We’ll also subset the visualization for only one source group, the Hippocampus cluster, and two target groups, Pyramidal_layer_dentate_gyrus and Pyramidal_layer cluster.

sq.gr.ligrec(
    adata,
    n_perms=100,
    cluster_key="cluster",
)
sq.pl.ligrec(
    adata,
    cluster_key="cluster",
    source_groups="Hippocampus",
    target_groups=["Pyramidal_layer", "Pyramidal_layer_dentate_gyrus"],
    means_range=(3, np.inf),
    alpha=1e-4,
    swap_axes=True,
)
Receptor-ligand test, $-\log_{10} ~ P$, $log_2(\frac{molecule_1 + molecule_2}{2} + 1)$

Out:

  0%|          | 0.00/8.94M [00:00<?, ?B/s]
  0%|          | 32.0k/8.94M [00:00<00:37, 249kB/s]
  1%|          | 80.0k/8.94M [00:00<00:28, 322kB/s]
  2%|2         | 192k/8.94M [00:00<00:16, 571kB/s]
  5%|4         | 416k/8.94M [00:00<00:08, 1.03MB/s]
  9%|9         | 864k/8.94M [00:00<00:04, 1.91MB/s]
 19%|#9        | 1.72M/8.94M [00:00<00:02, 3.61MB/s]
 39%|###8      | 3.45M/8.94M [00:00<00:00, 6.90MB/s]
 67%|######6   | 5.96M/8.94M [00:01<00:00, 11.0MB/s]
 93%|#########3| 8.33M/8.94M [00:01<00:00, 13.4MB/s]
100%|##########| 8.94M/8.94M [00:01<00:00, 7.83MB/s]
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/omnipath/_core/requests/interactions/_interactions.py:377: DtypeWarning: Columns (8) have mixed types.Specify dtype option on import or set low_memory=False.
  return cls(include, exclude=exclude)._get(**kwargs)
/home/runner/work/squidpy_notebooks/squidpy_notebooks/.tox/docs/lib/python3.8/site-packages/omnipath/_core/requests/_utils.py:155: FutureWarning: The default value of regex will change from True to False in a future version.
  _split_unique_join(data.str.replace(r"[-\w]*:?(\d+)", r"\1")), func=func

  0%|          | 0.00/1.39M [00:00<?, ?B/s]
  2%|2         | 32.0k/1.39M [00:00<00:05, 250kB/s]
  6%|5         | 80.0k/1.39M [00:00<00:04, 324kB/s]
 13%|#3        | 192k/1.39M [00:00<00:02, 575kB/s]
 26%|##5       | 368k/1.39M [00:00<00:01, 890kB/s]
 53%|#####2    | 752k/1.39M [00:00<00:00, 1.65MB/s]
100%|##########| 1.39M/1.39M [00:00<00:00, 2.21MB/s]

  0%|          | 0.00/2.60M [00:00<?, ?B/s]
  1%|1         | 32.0k/2.60M [00:00<00:11, 244kB/s]
  3%|3         | 80.0k/2.60M [00:00<00:08, 313kB/s]
  7%|7         | 192k/2.60M [00:00<00:04, 557kB/s]
 14%|#3        | 368k/2.60M [00:00<00:02, 861kB/s]
 28%|##8       | 752k/2.60M [00:00<00:01, 1.59MB/s]
 55%|#####5    | 1.44M/2.60M [00:00<00:00, 2.90MB/s]
100%|##########| 2.60M/2.60M [00:00<00:00, 3.32MB/s]
  0%|          | 0/100 [00:00<?, ?permutation/s]

The dotplot visualization provides an interesting set of candidate ligand-receptor annotation that could be involved in cellular interactions in the Hippocampus. A more refined analysis would be for instance to integrate these results with the results of a deconvolution method, to understand what’s the proportion of single-cell cell types present in this region of the tissue.

Spatially variable genes with Moran’s I

Finally, we might be interested in finding genes that show spatial patterns. There are several methods that aimed at address this explicitly, based on point processes or Gaussian process regression framework:

Here, we provide a simple approach based on the well-known Moran’s I statistics which is in fact used also as a baseline method in the spatially variable gene papers listed above. The function in Squidpy is called squidpy.gr.spatial_autocorr(), and returns both test statistics and adjusted p-values in anndata.AnnData.var slot. For time reasons, we will evaluate a subset of the highly variable genes only.

genes = adata[:, adata.var.highly_variable].var_names.values[:1000]
sq.gr.spatial_autocorr(
    adata,
    mode="moran",
    genes=genes,
    n_perms=100,
    n_jobs=1,
)

Out:

0%|          | 0/100 [00:00<?, ?/s]

The results are saved in adata.uns['moranI'] slot. Genes have already been sorted by Moran’s I statistic.

adata.uns["moranI"].head(10)
I pval_norm var_norm pval_z_sim pval_sim var_sim pval_norm_fdr_bh pval_z_sim_fdr_bh pval_sim_fdr_bh
Olfm1 0.763291 0.0 0.000131 0.0 0.009901 0.000294 0.0 0.0 0.011648
Plp1 0.747660 0.0 0.000131 0.0 0.009901 0.000266 0.0 0.0 0.011648
Itpka 0.727076 0.0 0.000131 0.0 0.009901 0.000291 0.0 0.0 0.011648
Snap25 0.720987 0.0 0.000131 0.0 0.009901 0.000265 0.0 0.0 0.011648
Nnat 0.708637 0.0 0.000131 0.0 0.009901 0.000244 0.0 0.0 0.011648
Ppp3ca 0.693320 0.0 0.000131 0.0 0.009901 0.000273 0.0 0.0 0.011648
Chn1 0.684957 0.0 0.000131 0.0 0.009901 0.000270 0.0 0.0 0.011648
Mal 0.679775 0.0 0.000131 0.0 0.009901 0.000265 0.0 0.0 0.011648
Tmsb4x 0.676719 0.0 0.000131 0.0 0.009901 0.000192 0.0 0.0 0.011648
Cldn11 0.674110 0.0 0.000131 0.0 0.009901 0.000237 0.0 0.0 0.011648


We can select few genes and visualize their expression levels in the tissue with scanpy.pl.spatial()

sc.pl.spatial(adata, color=["Olfm1", "Plp1", "Itpka", "cluster"])
Olfm1, Plp1, Itpka, cluster

Interestingly, some of these genes seems to be related to the pyramidal layers and the fiber tract.

Total running time of the script: ( 50 minutes 16.950 seconds)

Estimated memory usage: 1686 MB