Launch binder

Compute Moran’s I score

This example shows how to compute the Moran’s I global spatial auto-correlation statistics.

The Moran’s I global spatial auto-correlation statistics evaluates whether features (i.e. genes) shows a pattern that is clustered, dispersed or random in the tissue are under consideration.

See also

import scanpy as sc
import squidpy as sq

adata = sq.datasets.visium_hne_adata()
adata

Out:

AnnData object with n_obs × n_vars = 2688 × 18078
    obs: 'in_tissue', 'array_row', 'array_col', 'n_genes_by_counts', 'log1p_n_genes_by_counts', 'total_counts', 'log1p_total_counts', 'pct_counts_in_top_50_genes', 'pct_counts_in_top_100_genes', 'pct_counts_in_top_200_genes', 'pct_counts_in_top_500_genes', 'total_counts_mt', 'log1p_total_counts_mt', 'pct_counts_mt', 'n_counts', 'leiden', 'cluster'
    var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells_by_counts', 'mean_counts', 'log1p_mean_counts', 'pct_dropout_by_counts', 'total_counts', 'log1p_total_counts', 'n_cells', 'highly_variable', 'highly_variable_rank', 'means', 'variances', 'variances_norm'
    uns: 'cluster_colors', 'hvg', 'leiden', 'leiden_colors', 'neighbors', 'pca', 'rank_genes_groups', 'spatial', 'umap'
    obsm: 'X_pca', 'X_umap', 'spatial'
    varm: 'PCs'
    obsp: 'connectivities', 'distances'

We can compute the Moran’s I score with squidpy.gr.spatial_autocorr() and mode = 'moran'. We first need to compute a spatial graph with squidpy.gr.spatial_neighbors(). We will also subset the number of genes to evaluate.

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

Out:

0%|          | 0/100 [00:00<?, ?/s]
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
3110035E14Rik 0.665132 0.0 0.000131 0.0 0.009901 0.000222 0.0 0.0 0.011929
Resp18 0.649582 0.0 0.000131 0.0 0.009901 0.000190 0.0 0.0 0.011929
1500015O10Rik 0.605940 0.0 0.000131 0.0 0.009901 0.000234 0.0 0.0 0.011929
Ecel1 0.570304 0.0 0.000131 0.0 0.009901 0.000179 0.0 0.0 0.011929
2010300C02Rik 0.539469 0.0 0.000131 0.0 0.009901 0.000233 0.0 0.0 0.011929
Scg2 0.476060 0.0 0.000131 0.0 0.009901 0.000203 0.0 0.0 0.011929
Ogfrl1 0.457945 0.0 0.000131 0.0 0.009901 0.000218 0.0 0.0 0.011929
Itm2c 0.451842 0.0 0.000131 0.0 0.009901 0.000193 0.0 0.0 0.011929
Tuba4a 0.451810 0.0 0.000131 0.0 0.009901 0.000189 0.0 0.0 0.011929
Satb2 0.429162 0.0 0.000131 0.0 0.009901 0.000249 0.0 0.0 0.011929


We can visualize some of those genes with scanpy.pl.spatial().

sc.pl.spatial(adata, color=["Resp18", "Tuba4a"])
Resp18, Tuba4a

We could’ve also passed mode = 'geary' to compute a closely related auto-correlation statistic, Geary’s C. See squidpy.gr.spatial_autocorr() for more information.

Total running time of the script: ( 0 minutes 19.393 seconds)

Estimated memory usage: 363 MB