{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SPACEc: Distance Permutation Analysis" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_csv from `anndata` is deprecated. Import anndata.io.read_csv instead.\n", " warnings.warn(msg, FutureWarning)\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_text from `anndata` is deprecated. Import anndata.io.read_text instead.\n", " warnings.warn(msg, FutureWarning)\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_excel from `anndata` is deprecated. Import anndata.io.read_excel instead.\n", " warnings.warn(msg, FutureWarning)\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_mtx from `anndata` is deprecated. Import anndata.io.read_mtx instead.\n", " warnings.warn(msg, FutureWarning)\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_loom from `anndata` is deprecated. Import anndata.io.read_loom instead.\n", " warnings.warn(msg, FutureWarning)\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_hdf from `anndata` is deprecated. Import anndata.io.read_hdf instead.\n", " warnings.warn(msg, FutureWarning)\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/anndata/utils.py:429: FutureWarning: Importing read_umi_tools from `anndata` is deprecated. Import anndata.io.read_umi_tools instead.\n", " warnings.warn(msg, FutureWarning)\n", "2024-11-24 21:33:36.026147: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n", "2024-11-24 21:33:36.026168: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n", "INFO:root: * TissUUmaps version: 3.1.1.6\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_geometric/typing.py:68: UserWarning: An issue occurred while importing 'pyg-lib'. Disabling its usage. Stacktrace: /home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/libpyg.so: undefined symbol: _ZN3c1010Dispatcher17runRecordFunctionERN2at14RecordFunctionESt17reference_wrapperIKNS_14FunctionSchemaEENS_11DispatchKeyE\n", " warnings.warn(f\"An issue occurred while importing 'pyg-lib'. \"\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_geometric/typing.py:86: UserWarning: An issue occurred while importing 'torch-scatter'. Disabling its usage. Stacktrace: /home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_scatter/_version_cuda.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs\n", " warnings.warn(f\"An issue occurred while importing 'torch-scatter'. \"\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_geometric/typing.py:97: UserWarning: An issue occurred while importing 'torch-cluster'. Disabling its usage. Stacktrace: /home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_cluster/_version_cuda.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs\n", " warnings.warn(f\"An issue occurred while importing 'torch-cluster'. \"\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_geometric/typing.py:113: UserWarning: An issue occurred while importing 'torch-spline-conv'. Disabling its usage. Stacktrace: /home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_spline_conv/_version_cuda.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs\n", " warnings.warn(\n", "/home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_geometric/typing.py:124: UserWarning: An issue occurred while importing 'torch-sparse'. Disabling its usage. Stacktrace: /home/tim/miniforge3/envs/spacec/lib/python3.10/site-packages/torch_sparse/_version_cuda.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs\n", " warnings.warn(f\"An issue occurred while importing 'torch-sparse'. \"\n" ] } ], "source": [ "import spacec as sp\n", "\n", "#import standard packages\n", "import os\n", "import pandas as pd\n", "import scanpy as sc\n", "\n", "# silencing warnings\n", "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# set plotting parameters\n", "sc.settings.set_figure_params(dpi=80, facecolor='white')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Specify the path to the data\n", "root_path = \"/home/user/path/SPACEc/\" # inset your own path\n", "\n", "data_path = root_path + 'example_data/raw/' # where the data is stored\n", "\n", "# where you want to store the output\n", "output_dir = root_path + 'example_data/output/'\n", "os.makedirs(output_dir, exist_ok=True)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 46789 × 59\n", " obs: 'DAPI', 'x', 'y', 'area', 'region_num', 'unique_region', 'condition', 'leiden_1', 'leiden_1_subcluster', 'cell_type_coarse', 'cell_type_coarse_subcluster', 'cell_type_coarse_f', 'cell_type_coarse_f_subcluster', 'cell_type_coarse_f_f', 'cell_type', 'CN_k20_n40', 'CN_k20_n30', 'CN_k20_n20', 'CN_k20_n25', 'CN_k20_n6', 'CN_k20_n6_annot'\n", " uns: 'CN_k20_n6_colors', 'Centroid_k20_n20', 'Centroid_k20_n25', 'Centroid_k20_n30', 'Centroid_k20_n40', 'Centroid_k20_n6', 'cell_type_coarse_f_colors', 'cell_type_colors', 'dendrogram_cell_type_coarse_f_subcluster', 'leiden', 'leiden_1_colors', 'neighbors', 'umap', 'unique_region_colors'\n", " obsm: 'X_pca', 'X_umap'\n", " layers: 'scaled'\n", " obsp: 'connectivities', 'distances'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load data\n", "adata = sc.read(output_dir + \"adata_nn_demo_annotated_cn.h5ad\")\n", "adata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Identify potential interactions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# compute the potential interactions\n", "distance_pvals, results_dict = sp.tl.identify_interactions(\n", " adata = adata, # AnnData object\n", " cellid = \"index\", # column that contains the cell id (set index if the cell id is the index of the dataframe)\n", " x_pos = \"x\", # x coordinate column\n", " y_pos = \"y\", # y coordinate column\n", " cell_type = \"cell_type\", # column that contains the cell type information\n", " region = \"unique_region\", # column that contains the region information\n", " num_iterations=1000, # number of iterations for the permutation test\n", " num_cores=12, # number of CPU threads to use\n", " min_observed = 10, # minimum number of observed interactions to consider a cell type pair\n", " comparison = 'condition', # column that contains the condition information we want to compare\n", " distance_threshold=20/0.5085) # distance threshold in px (20 µm)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['distance_pvals', 'triangulation_distances_observed', 'triangulation_distances_iterated'])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the results_dict contains the results of the permutation test as well as the observed and shuffled distances\n", "results_dict.keys()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | unique_region | \n", "celltype1_index | \n", "celltype1 | \n", "celltype1_X | \n", "celltype1_Y | \n", "celltype2_index | \n", "celltype2 | \n", "celltype2_X | \n", "celltype2_Y | \n", "distance | \n", "condition | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "reg002 | \n", "3675 | \n", "CD4+ T cell | \n", "612 | \n", "1209 | \n", "3752 | \n", "Plasma cell | \n", "620 | \n", "1220 | \n", "13.601471 | \n", "tonsillitis | \n", "
| 1 | \n", "reg002 | \n", "15372 | \n", "CD8+ T cell | \n", "1691 | \n", "310 | \n", "15416 | \n", "Plasma cell | \n", "1697 | \n", "296 | \n", "15.231546 | \n", "tonsillitis | \n", "
| 2 | \n", "reg002 | \n", "6507 | \n", "CD8+ T cell | \n", "897 | \n", "651 | \n", "6508 | \n", "CD4+ T cell | \n", "897 | \n", "672 | \n", "21.000000 | \n", "tonsillitis | \n", "
| 3 | \n", "reg002 | \n", "7976 | \n", "GCB | \n", "1033 | \n", "1410 | \n", "8070 | \n", "GCB | \n", "1043 | \n", "1425 | \n", "18.027756 | \n", "tonsillitis | \n", "
| 4 | \n", "reg002 | \n", "22014 | \n", "CD4+ T cell | \n", "2435 | \n", "1409 | \n", "22050 | \n", "B cell | \n", "2444 | \n", "1419 | \n", "13.453624 | \n", "tonsillitis | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 79446 | \n", "reg001 | \n", "49351 | \n", "B cell | \n", "2835 | \n", "847 | \n", "49307 | \n", "Epithelial cell | \n", "2841 | \n", "871 | \n", "24.738634 | \n", "tonsil | \n", "
| 79447 | \n", "reg001 | \n", "46044 | \n", "Epithelial cell | \n", "2457 | \n", "425 | \n", "46027 | \n", "Epithelial cell | \n", "2459 | \n", "448 | \n", "23.086793 | \n", "tonsil | \n", "
| 79448 | \n", "reg001 | \n", "47417 | \n", "M2 Macrophage | \n", "2598 | \n", "2274 | \n", "47298 | \n", "Mast cell | \n", "2609 | \n", "2290 | \n", "19.416488 | \n", "tonsil | \n", "
| 79449 | \n", "reg001 | \n", "43846 | \n", "CD4+ T cell | \n", "2234 | \n", "1393 | \n", "43625 | \n", "cDC1 | \n", "2252 | \n", "1390 | \n", "18.248288 | \n", "tonsil | \n", "
| 79450 | \n", "reg001 | \n", "30272 | \n", "CD8+ T cell | \n", "1124 | \n", "230 | \n", "30103 | \n", "Vessel | \n", "1140 | \n", "231 | \n", "16.031220 | \n", "tonsil | \n", "
280510 rows × 11 columns
\n", "| \n", " | celltype1 | \n", "celltype2 | \n", "condition | \n", "expected | \n", "expected_mean | \n", "keep_x | \n", "observed | \n", "observed_mean | \n", "keep_y | \n", "pvalue | \n", "logfold_group | \n", "interaction | \n", "logfold_group_abs | \n", "pairs | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | \n", "B cell | \n", "CD4+ T cell | \n", "tonsil | \n", "[17.44611626424993, 17.240214223537937, 17.384... | \n", "17.508022 | \n", "True | \n", "[21.0, 14.7648230602334, 23.706539182259394, 2... | \n", "16.568973 | \n", "True | \n", "2.649916e-54 | \n", "-0.079532 | \n", "B cell --> CD4+ T cell | \n", "0.079532 | \n", "B cell_CD4+ T cell | \n", "
| 3 | \n", "B cell | \n", "CD4+ T cell | \n", "tonsillitis | \n", "[17.991364990576724, 17.796405842536796, 17.37... | \n", "17.596348 | \n", "True | \n", "[22.255004511542957, 14.317821063276353, 27.29... | \n", "16.143926 | \n", "True | \n", "1.783677e-80 | \n", "-0.124285 | \n", "B cell --> CD4+ T cell | \n", "0.124285 | \n", "B cell_CD4+ T cell | \n", "
| 4 | \n", "B cell | \n", "CD8+ T cell | \n", "tonsil | \n", "[17.578718052603264, 17.373822784572745, 17.52... | \n", "17.668738 | \n", "True | \n", "[14.142135623730951, 15.908466048591219, 13.60... | \n", "16.833803 | \n", "True | \n", "1.343202e-32 | \n", "-0.069838 | \n", "B cell --> CD8+ T cell | \n", "0.069838 | \n", "B cell_CD8+ T cell | \n", "
| 5 | \n", "B cell | \n", "CD8+ T cell | \n", "tonsillitis | \n", "[17.220703294913235, 17.586321595513557, 17.69... | \n", "17.679001 | \n", "True | \n", "[13.92838827718412, 12.0, 22.47220505424423, 1... | \n", "16.493192 | \n", "True | \n", "4.567463e-59 | \n", "-0.100166 | \n", "B cell --> CD8+ T cell | \n", "0.100166 | \n", "B cell_CD8+ T cell | \n", "
| 8 | \n", "B cell | \n", "Epithelial cell | \n", "tonsil | \n", "[17.112417871305603, 17.32405435992097, 17.480... | \n", "17.645577 | \n", "True | \n", "[25.495097567963924, 11.180339887498949, 32.0,... | \n", "17.582085 | \n", "True | \n", "8.317085e-06 | \n", "-0.005200 | \n", "B cell --> Epithelial cell | \n", "0.005200 | \n", "B cell_Epithelial cell | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 399 | \n", "Vessel | \n", "Epithelial cell | \n", "tonsillitis | \n", "[18.656136722237417, 17.23539386972536, 17.452... | \n", "17.697952 | \n", "True | \n", "[20.27019348662979, 25.553864678361276, 20.538... | \n", "19.468968 | \n", "True | \n", "4.654472e-02 | \n", "0.137594 | \n", "Vessel --> Epithelial cell | \n", "0.137594 | \n", "Vessel_Epithelial cell | \n", "
| 400 | \n", "Vessel | \n", "GCB | \n", "tonsil | \n", "[17.123726173924652, 17.86949875671633, 17.259... | \n", "17.727516 | \n", "True | \n", "[23.219544457292887, 22.561028345356956, 31.57... | \n", "22.337618 | \n", "True | \n", "1.868711e-05 | \n", "0.333485 | \n", "Vessel --> GCB | \n", "0.333485 | \n", "Vessel_GCB | \n", "
| 401 | \n", "Vessel | \n", "GCB | \n", "tonsillitis | \n", "[17.73771013605273, 18.023692483603742, 18.473... | \n", "17.674742 | \n", "True | \n", "[27.730849247724095, 18.679380239274902, 13.34... | \n", "19.167979 | \n", "True | \n", "3.128661e-01 | \n", "0.117009 | \n", "Vessel --> GCB | \n", "0.117009 | \n", "Vessel_GCB | \n", "
| 428 | \n", "cDC1 | \n", "Epithelial cell | \n", "tonsil | \n", "[18.463345515951513, 18.99715862104245, 17.564... | \n", "17.649416 | \n", "True | \n", "[19.235384061671343, 20.518284528683193, 21.93... | \n", "19.038398 | \n", "True | \n", "6.141596e-03 | \n", "0.109292 | \n", "cDC1 --> Epithelial cell | \n", "0.109292 | \n", "cDC1_Epithelial cell | \n", "
| 429 | \n", "cDC1 | \n", "Epithelial cell | \n", "tonsillitis | \n", "[17.422664720988926, 17.507966079507376, 17.26... | \n", "17.610267 | \n", "True | \n", "[15.639410298049853, 15.71062198086001, 23.600... | \n", "18.774279 | \n", "True | \n", "5.173701e-02 | \n", "0.092341 | \n", "cDC1 --> Epithelial cell | \n", "0.092341 | \n", "cDC1_Epithelial cell | \n", "
64 rows × 14 columns
\n", "