{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Matplotlib is building the font cache; this may take a moment.\n" ] } ], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Compute Moran\\'s I score\n", "========================\n", "\n", "This example shows how to compute the Moran\\'s I global spatial\n", "auto-correlation statistics.\n", "\n", "The Moran\\'s I global spatial auto-correlation statistics evaluates\n", "whether features (i.e. genes) shows a pattern that is clustered,\n", "dispersed or random in the tissue are under consideration.\n", "\n", "::: {seealso}\n", "- See {doc}`compute_co_occurrence` and\n", " {doc}`compute_ripley` for other scores to\n", " describe spatial patterns.\n", "- See {doc}`compute_spatial_neighbors` for\n", " general usage of {func}`squidpy.gr.spatial_neighbors`.\n", " \n", ":::\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/selman/projects/squidpy/docs/notebooks/.venv/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/page/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n", "/Users/selman/projects/squidpy/docs/notebooks/.venv/lib/python3.12/site-packages/docrep/decorators.py:43: SyntaxWarning: 'n_jobs' is not a valid key!\n", " doc = func(self, args[0].__doc__, *args[1:], **kwargs)\n", "/Users/selman/projects/squidpy/docs/notebooks/.venv/lib/python3.12/site-packages/docrep/decorators.py:43: SyntaxWarning: 'show_progress_bar' is not a valid key!\n", " doc = func(self, args[0].__doc__, *args[1:], **kwargs)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[34mINFO \u001b[0m Downloading visium_hne_adata.h5ad from \u001b[4;94mhttps://exampledata.scverse.org/squidpy/visium_hne_adata.h5ad\u001b[0m \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Downloading data from 'https://exampledata.scverse.org/squidpy/visium_hne_adata.h5ad' to file '/Users/selman/projects/squidpy/docs/notebooks/examples/graph/data/anndata/visium_hne_adata.h5ad'.\n", "100%|████████████████████████████████████████| 329M/329M [00:00<00:00, 343GB/s]\n" ] }, { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 2688 × 18078\n", " 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'\n", " 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'\n", " uns: 'cluster_colors', 'hvg', 'leiden', 'leiden_colors', 'neighbors', 'pca', 'rank_genes_groups', 'spatial', 'umap'\n", " obsm: 'X_pca', 'X_umap', 'spatial'\n", " varm: 'PCs'\n", " obsp: 'connectivities', 'distances'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import squidpy as sq\n", "\n", "adata = sq.datasets.visium_hne_adata()\n", "adata" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can compute the Moran\\'s I score with `squidpy.gr.spatial_autocorr`\n", "and `mode = 'moran'`. We first need to compute a spatial graph with\n", "`squidpy.gr.spatial_neighbors`. We will also subset the number of genes\n", "to evaluate.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[34mINFO \u001b[0m Creating graph using `\u001b[3;35mNone\u001b[0m` transform and `\u001b[1;36m1\u001b[0m` libraries. \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/xy/w8sj_5197yg42f3c2txh8gww0000gn/T/ipykernel_97027/3642335017.py:2: FutureWarning: Calling `spatial_neighbors` is deprecated and will be removed in squidpy v1.9.0. Use `spatial_neighbors_knn`, `spatial_neighbors_radius`, `spatial_neighbors_delaunay`, `spatial_neighbors_grid`, or `spatial_neighbors_from_builder` instead.\n", " sq.gr.spatial_neighbors(adata)\n", " 0%| | 0/100 [00:00, ?/s]/Users/selman/projects/squidpy/docs/notebooks/.venv/lib/python3.12/site-packages/docrep/decorators.py:43: SyntaxWarning: 'n_jobs' is not a valid key!\n", " doc = func(self, args[0].__doc__, *args[1:], **kwargs)\n", "/Users/selman/projects/squidpy/docs/notebooks/.venv/lib/python3.12/site-packages/docrep/decorators.py:43: SyntaxWarning: 'show_progress_bar' is not a valid key!\n", " doc = func(self, args[0].__doc__, *args[1:], **kwargs)\n", "100%|██████████| 100/100 [00:23<00:00, 4.34/s]\n" ] }, { "data": { "text/html": [ "
| \n", " | I | \n", "pval_norm | \n", "var_norm | \n", "pval_z_sim | \n", "pval_sim | \n", "var_sim | \n", "pval_norm_fdr_bh | \n", "pval_z_sim_fdr_bh | \n", "pval_sim_fdr_bh | \n", "
|---|---|---|---|---|---|---|---|---|---|
| 3110035E14Rik | \n", "0.665132 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000251 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Resp18 | \n", "0.649582 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000243 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| 1500015O10Rik | \n", "0.605940 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000234 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Ecel1 | \n", "0.570304 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000255 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| 2010300C02Rik | \n", "0.539469 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000240 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Scg2 | \n", "0.476060 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000164 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Ogfrl1 | \n", "0.457945 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000199 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Itm2c | \n", "0.451842 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000222 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Tuba4a | \n", "0.451810 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000182 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "
| Satb2 | \n", "0.429162 | \n", "0.0 | \n", "0.000131 | \n", "0.0 | \n", "0.009901 | \n", "0.000188 | \n", "0.0 | \n", "0.0 | \n", "0.011929 | \n", "