{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Neighbors enrichment analysis\n", "\n", "This example shows how to run the neighbors enrichment analysis routine.\n", "\n", "It calculates an enrichment score based on proximity on the connectivity graph of cell clusters.\n", "The number of observed events is compared against $N$ permutations and a *z-score* is computed.\n", "\n", ":::{seealso}\n", "\n", " See {doc}`compute_spatial_neighbors` for general usage of\n", " {func}`squidpy.gr.spatial_neighbors`.\n", "\n", ":::\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 2800 × 16562\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', 'spatial', 'umap'\n", " obsm: 'X_pca', 'X_umap', 'spatial'\n", " varm: 'PCs'\n", " obsp: 'connectivities', 'distances'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import squidpy as sq\n", "\n", "adata = sq.datasets.visium_fluo_adata()\n", "adata" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "This dataset contains cell type annotations in {attr}`anndata.AnnData.obs` which are used for calculation of the\n", "neighborhood enrichment. First, we need to compute a connectivity matrix from spatial coordinates.\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sq.gr.spatial_neighbors(adata)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Then we can calculate the neighborhood enrichment score with {func}`squidpy.gr.nhood_enrichment`.\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "44a84f9535d74bc8b21262781016e9aa", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sq.pl.nhood_enrichment(adata, cluster_key=\"cluster\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 0 }