{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SVI patterns (slide-seq mouse brain dataset)\n", "This notebook showcases the SVI patterns using the slide-seq mouse brain dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The utils.py file can be downloaded from https://github.com/deepomicslab/SPIDER-paper/blob/main/notebooks/utils.py" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using mouse pathway dataset.\n", " src dest\n", "Man1a_Mgat1 Man1a Mgat1\n", "Man1b1_Man1a Man1b1 Man1a\n", "Nrg2_Egfr Nrg2 Egfr\n", "Nrg2_Erbb2 Nrg2 Erbb2\n", "Nrg2_Erbb3 Nrg2 Erbb3\n", "Found 1037 SV pathway nodes.\n", "Found 59/69 receptors (86%) with SV target genes.\n", "Testing 114/163 LRIs (70%) with SV target genes.\n", "106/114 LRIs (93%) are predicted to be activated by downstream genes (threshold=0.3).\n", "Excluded 8/163 LRIs (5%) without SV support (threshold=0.3).\n", "using 149*149 SOM nodes for 111461 points\n", "Added key nnSVG in idata.uns\n", "Added key SOMDE in idata.uns\n", "Added key SpatialDE2 in idata.uns\n", "Added key SpatialDE in idata.uns\n", "Added key SPARKX in idata.uns\n", "Added key scGCO in idata.uns\n", "Added key moranI in idata.uns\n", "Added key gearyC in idata.uns\n", "finished running all SVI tests\n", "Using the results from SVI identification methods: ['SOMDE' 'SpatialDE' 'SpatialDE2' 'SPARKX' 'nnSVG' 'scGCO' 'gearyC'\n", " 'moranI']\n", "126/163 SVIs identified (threshold=0.01).\n", "Excluding 7 genes without TF support\n", "119/163 tf-supported SVIs identified.\n", "Added key pattern_score in idata.obsm and method results and running time in uns\n" ] } ], "source": [ "import sys\n", "import os\n", "import numpy as np\n", "import pandas as pd\n", "import anndata\n", "import scanpy as sc\n", "import matplotlib.pyplot as plt\n", "import time\n", "R_path = 'source /etc/profile;module load GCC/11.2.0 OpenMPI/4.1.1 R/4.2.0 Anaconda3/2022.05 R-bundle-Bioconductor/3.15-R-4.2.0;R'\n", "from spider import SPIDER\n", "op=SPIDER()\n", "%reload_ext autoreload\n", "%autoreload 2\n", "from utils import *\n", "\n", "ds = 'slide-seq-v2'\n", "sample_name = 'mousebrain'\n", "adata = anndata.read_h5ad(f'../example_datasets/{ds}/{sample_name}/adata.h5ad')\n", "\n", "if not os.path.exists(f'../results/{ds}'):\n", " os.makedirs(f'../results/{ds}/')\n", "out_f = f'../results/{ds}/{sample_name}/'\n", "if not os.path.exists(out_f):\n", " os.makedirs(out_f)\n", "if len(adata.obs_names) > 200:\n", " sc.pp.filter_genes(adata, min_cells=20)\n", " \n", "idata = anndata.read_h5ad(f'../results/{ds}/{sample_name}/idata.h5ad')\n", " \n", "# start = time.time()\n", "# idata = op.prep(adata, cluster_key=adata.uns['cluster_key'], is_human=adata.uns['is_human'], is_sc=adata.uns['is_sc'], itermax=1000, imputation=True, normalize_total=True)\n", "# idata.write(f'{out_f}idata.h5ad')\n", "# runtime = time.time()-start\n", "# with open(f'{out_f}ot_runtime.txt', 'w') as f:\n", "# f.write(str(runtime))\n", " \n", "start = time.time()\n", "op.svi.tf_corr(idata, adata, adata.uns['is_human'], out_f, threshold=0.3, n_jobs=30, overwrite=False, step=None, keep_top_score=5)\n", "runtime = time.time()-start\n", "with open(f'{out_f}tf_runtime.txt', 'w') as f:\n", " f.write(str(runtime))\n", " \n", "start = time.time()\n", "idata, meta_idata = op.find_svi(idata, out_f, R_path, alpha=0.3, overwrite=False, n_jobs=1, svi_number=0)\n", "runtime = time.time()-start\n", "with open(f'{out_f}svi_runtime.txt', 'w') as f:\n", " f.write(str(runtime))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using the results from SVI identification methods: ['SOMDE' 'SpatialDE' 'SpatialDE2' 'SPARKX' 'nnSVG' 'scGCO' 'gearyC'\n", " 'moranI']\n", "126/163 SVIs identified (threshold=0.01).\n", "Excluding 7 genes without TF support\n", "119/163 tf-supported SVIs identified.\n", "evaluating with ['moranI' 'gearyC' 'SOMDE' 'nnSVG']\n", "p-value annotation legend:\n", " ns: p <= 1.00e+00\n", " *: 1.00e-02 < p <= 5.00e-02\n", " **: 1.00e-03 < p <= 1.00e-02\n", " ***: 1.00e-04 < p <= 1.00e-03\n", " ****: p <= 1.00e-04\n", "\n", "Geary\n", "C (rev.)_SVI vs. Geary\n", "C (rev.)_Excluded: Mann-Whitney-Wilcoxon test greater with Benjamini-Hochberg correction, P_val:1.897e-06 U_stat=3.855e+03\n", "Moran\n", "I_SVI vs. Moran\n", "I_Excluded: Mann-Whitney-Wilcoxon test greater with Benjamini-Hochberg correction, P_val:1.612e-06 U_stat=3.864e+03\n", "FSV\n", "(SOMDE)_SVI vs. FSV\n", "(SOMDE)_Excluded: Mann-Whitney-Wilcoxon test greater with Benjamini-Hochberg correction, P_val:1.562e-07 U_stat=3.987e+03\n", "LR\n", "(SOMDE)_SVI vs. LR\n", "(SOMDE)_Excluded: Mann-Whitney-Wilcoxon test greater with Benjamini-Hochberg correction, P_val:2.981e-07 U_stat=3.954e+03\n", "LR\n", "(nnSVG)_SVI vs. LR\n", "(nnSVG)_Excluded: Mann-Whitney-Wilcoxon test greater with Benjamini-Hochberg correction, P_val:7.161e-07 U_stat=3.908e+03\n", "TF corr_SVI vs. TF corr_Excluded: Mann-Whitney-Wilcoxon test greater with Benjamini-Hochberg correction, P_val:4.321e-06 U_stat=2.049e+03\n" ] }, { "data": { "text/html": [ "
| \n", " | Category | \n", "Metric | \n", "value | \n", "
|---|---|---|---|
| 0 | \n", "Excluded | \n", "Moran\\nI | \n", "1.000000 | \n", "
| 1 | \n", "SVI | \n", "Moran\\nI | \n", "0.990115 | \n", "
| 2 | \n", "SVI | \n", "Moran\\nI | \n", "0.964646 | \n", "
| 3 | \n", "SVI | \n", "Moran\\nI | \n", "0.956778 | \n", "
| 4 | \n", "SVI | \n", "Moran\\nI | \n", "0.943627 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 972 | \n", "Excluded | \n", "TF corr | \n", "0.531543 | \n", "
| 973 | \n", "Excluded | \n", "TF corr | \n", "0.537164 | \n", "
| 974 | \n", "Excluded | \n", "TF corr | \n", "0.376015 | \n", "
| 975 | \n", "Excluded | \n", "TF corr | \n", "0.480949 | \n", "
| 976 | \n", "Excluded | \n", "TF corr | \n", "0.543410 | \n", "
929 rows × 3 columns
\n", "