{
"cells": [
{
"cell_type": "markdown",
"id": "c382d399",
"metadata": {},
"source": [
"# SpatialDM tutorial with melanoma data"
]
},
{
"cell_type": "markdown",
"id": "58fcfc21-57d2-470b-b1b5-f1e8b2c52662",
"metadata": {},
"source": [
"You may also run it on Google Colab with this link: [melanoma.ipynb](https://colab.research.google.com/github/StatBiomed/SpatialDM/blob/main/tutorial/melanoma.ipynb)"
]
},
{
"cell_type": "markdown",
"id": "9bc97fc1-7736-4c93-8408-ef8be6b59030",
"metadata": {},
"source": [
"TO run it on Google Colab (or a new environment), please change this section into code (or copy into a new code block) for installing the following packages.\n",
"Note, please only install the package if you need (required on colab). For your local environment, you only need to install them for the first time.\n",
"\n",
"```bash\n",
"# optionally use an older version of scipy (also works till v1.15.3)\n",
"!pip install scipy==1.11.4\n",
"\n",
"# install anndata <= 0.10.8 to avoid complicated cupy package\n",
"!pip install anndata==0.10.8\n",
"\n",
"# install latest SpatialDM from github directly\n",
"!pip install -U git+https://github.com/StatBiomed/SpatialDM\n",
"\n",
"!pip install SparseAEH\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3de9fd5b-fa86-4dc5-b7a2-70edaa096cd4",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SpatailDM version: 0.3.0\n"
]
}
],
"source": [
"import os\n",
"import pandas as pd\n",
"import numpy as np\n",
"import anndata as ann\n",
"\n",
"import spatialdm as sdm\n",
"from spatialdm.datasets import dataset\n",
"import spatialdm.plottings as pl\n",
"\n",
"import matplotlib.pyplot as plt\n",
"print(\"SpatailDM version: %s\" %sdm.__version__)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03e579b2-a98c-4b2e-8304-59bbcd78e9ab",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "5b886171",
"metadata": {},
"source": [
"The melanoma dataset from [Thrane, et al. 2018 Caner Cell](https://aacrjournals.org/cancerres/article/78/20/5970/631815) was publicly available.\n",
"We obtained raw counts, pre-processed log counts, and spatial coordinates from Matt Stone ([Git repository](https://github.com/msto/spatial-datasets)). For easier reuse, we included them in an anndata object which can be loaded directly in SpatialDM Python package.\n",
"\n",
"We can see the histology image below from the orignal supplementary figure (the first sample):\n",
"\n",
"
\n",
"\n",
"For the cell type annotation, we can visualize it from SpatialDM's paper [Figure 2b](https://www.nature.com/articles/s41467-023-39608-w/figures/2)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "482d9a06",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"adata = dataset.melanoma()"
]
},
{
"cell_type": "markdown",
"id": "f96892ff",
"metadata": {},
"source": [
"The anndata object contains the following:\\\n",
" log-transformed expression in `adata.X`, \\\n",
" raw expression in `adata.raw`,\\\n",
" cell types computed by RCTD in `adata.obs`,\\\n",
" and spatial coordinates in `adata.obsm['spatial']`"
]
},
{
"cell_type": "markdown",
"id": "b428527a",
"metadata": {},
"source": [
"## Step 0: SpatialDM object and preprocessing"
]
},
{
"cell_type": "markdown",
"id": "341145f3",
"metadata": {},
"source": [
"We generate a suitable weight matrix for the SpatialDM object at first. Here considering the scale of the spatial coordinates and spot-spot distance (200 micrometers here), we set radial basis kernel parameter l = 1.2, and trimmed all weights < 0.2 (cutoff) to match the normal range of CCC (200 micrometers, 1 spot away from the sender cell here)"
]
},
{
"cell_type": "markdown",
"id": "de39ed50",
"metadata": {},
"source": [
"
| \n", " | Ligand0 | \n", "Ligand1 | \n", "Ligand2 | \n", "Receptor0 | \n", "Receptor1 | \n", "Receptor2 | \n", "z_pval | \n", "z | \n", "fdr | \n", "selected | \n", "
|---|---|---|---|---|---|---|---|---|---|---|
| CCL21_CCR7 | \n", "CCL21 | \n", "None | \n", "None | \n", "CCR7 | \n", "None | \n", "None | \n", "2.351186e-63 | \n", "16.761051 | \n", "2.118419e-60 | \n", "True | \n", "
| CD99_CD99 | \n", "CD99 | \n", "None | \n", "None | \n", "CD99 | \n", "None | \n", "None | \n", "1.786933e-60 | \n", "16.361968 | \n", "8.050133e-58 | \n", "True | \n", "
| PECAM1_PECAM1 | \n", "PECAM1 | \n", "None | \n", "None | \n", "PECAM1 | \n", "None | \n", "None | \n", "6.034266e-50 | \n", "14.813025 | \n", "1.812291e-47 | \n", "True | \n", "
| ESAM_ESAM | \n", "ESAM | \n", "None | \n", "None | \n", "ESAM | \n", "None | \n", "None | \n", "6.911664e-48 | \n", "14.490942 | \n", "1.556852e-45 | \n", "True | \n", "
| SPP1_ITGAV_ITGB5 | \n", "SPP1 | \n", "None | \n", "None | \n", "ITGAV | \n", "ITGB5 | \n", "None | \n", "2.987723e-46 | \n", "14.229915 | \n", "5.383876e-44 | \n", "True | \n", "
| \n", " | total_genes | \n", "query_genes | \n", "overlapped_genes | \n", "background_mapped_genes | \n", "background_unmapped_genes | \n", "selected | \n", "fisher_p | \n", "pattern | \n", "
|---|---|---|---|---|---|---|---|---|
| pathway | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
| ACTIVIN | \n", "INHBA_ACVR1B_ACVR2A INHBA_ACVR1B_ACVR2A... | \n", "{HLA-DMB_CD4, HLA-DRB5_CD4, CCL5_CCR1, HLA-C_C... | \n", "{} | \n", "{} | \n", "{ANGPTL4_SDC4, TNC_ITGAV_ITGB3, C3_ITGAM_ITGB2... | \n", "0 | \n", "1.0 | \n", "Pattern_0 | \n", "
| ADIPONECTIN | \n", "ADIPOQ_ADIPOR1 ADIPOQ_ADIPOR1\n", "ADIPOQ_ADIPOR... | \n", "{HLA-DMB_CD4, HLA-DRB5_CD4, CCL5_CCR1, HLA-C_C... | \n", "{} | \n", "{} | \n", "{ANGPTL4_SDC4, TNC_ITGAV_ITGB3, C3_ITGAM_ITGB2... | \n", "0 | \n", "1.0 | \n", "Pattern_0 | \n", "
| AGRN | \n", "AGRN_DAG1 AGRN_DAG1\n", "Name: interaction_name,... | \n", "{HLA-DMB_CD4, HLA-DRB5_CD4, CCL5_CCR1, HLA-C_C... | \n", "{} | \n", "{} | \n", "{ANGPTL4_SDC4, TNC_ITGAV_ITGB3, C3_ITGAM_ITGB2... | \n", "0 | \n", "1.0 | \n", "Pattern_0 | \n", "
| ALCAM | \n", "ALCAM_CD6 ALCAM_CD6\n", "Name: interaction_name,... | \n", "{HLA-DMB_CD4, HLA-DRB5_CD4, CCL5_CCR1, HLA-C_C... | \n", "{} | \n", "{} | \n", "{ANGPTL4_SDC4, TNC_ITGAV_ITGB3, C3_ITGAM_ITGB2... | \n", "0 | \n", "1.0 | \n", "Pattern_0 | \n", "
| AMH | \n", "AMH_AMHR2_BMPR1A AMH_AMHR2_BMPR1A\n", "AMH_AMHR2... | \n", "{HLA-DMB_CD4, HLA-DRB5_CD4, CCL5_CCR1, HLA-C_C... | \n", "{} | \n", "{} | \n", "{ANGPTL4_SDC4, TNC_ITGAV_ITGB3, C3_ITGAM_ITGB2... | \n", "0 | \n", "1.0 | \n", "Pattern_0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| VISFATIN | \n", "NAMPT_INSR NAMPT_INSR\n", "NAMPT_I... | \n", "{COL9A3_ITGA2_ITGB1, OCLN_OCLN, ANGPTL4_SDC4, ... | \n", "{} | \n", "{} | \n", "{HLA-DRB5_CD4, C3_ITGAM_ITGB2, CCL21_CCR7, SEL... | \n", "0 | \n", "1.0 | \n", "Pattern_2 | \n", "
| VWF | \n", "VWF_ITGAV_ITGB3 VWF_ITGAV_I... | \n", "{COL9A3_ITGA2_ITGB1, OCLN_OCLN, ANGPTL4_SDC4, ... | \n", "{} | \n", "{VWF_GP1BA_GP1BB_GP5_GP9} | \n", "{HLA-DRB5_CD4, C3_ITGAM_ITGB2, CCL21_CCR7, SEL... | \n", "0 | \n", "1.0 | \n", "Pattern_2 | \n", "
| WNT | \n", "WNT10A_FZD9_LRP6 WNT10A_FZD9_LRP6\n", "WNT10A_... | \n", "{COL9A3_ITGA2_ITGB1, OCLN_OCLN, ANGPTL4_SDC4, ... | \n", "{} | \n", "{} | \n", "{HLA-DRB5_CD4, C3_ITGAM_ITGB2, CCL21_CCR7, SEL... | \n", "0 | \n", "1.0 | \n", "Pattern_2 | \n", "
| XCR | \n", "XCL1_XCR1 XCL1_XCR1\n", "Name: interaction_name,... | \n", "{COL9A3_ITGA2_ITGB1, OCLN_OCLN, ANGPTL4_SDC4, ... | \n", "{} | \n", "{} | \n", "{HLA-DRB5_CD4, C3_ITGAM_ITGB2, CCL21_CCR7, SEL... | \n", "0 | \n", "1.0 | \n", "Pattern_2 | \n", "
| ncWNT | \n", "WNT5B_FZD9 WNT5B_FZD9\n", "WNT5B_FZD8 WNT5B_F... | \n", "{COL9A3_ITGA2_ITGB1, OCLN_OCLN, ANGPTL4_SDC4, ... | \n", "{} | \n", "{} | \n", "{HLA-DRB5_CD4, C3_ITGAM_ITGB2, CCL21_CCR7, SEL... | \n", "0 | \n", "1.0 | \n", "Pattern_2 | \n", "
408 rows × 8 columns
\n", "