{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# scSVC reconstructs whole-genome profiles of Xenium datasets and defines fine-grained subtypes of T cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook Guide\n", "\n", "**Purpose.** Analyze reconstructed T-cell states and compare sc-SVC signals against raw Xenium measurements.\n", "\n", "**Inputs.** Executed T-cell reconstruction outputs under `../../output/sc_SVC_case/P5CRC_Xenium/T/` plus raw Xenium data.\n", "\n", "**Outputs.** Subtype spatial maps, marker dot plots, volcano plots, and enrichment summaries displayed inline and saved to disk.\n", "\n", "**Reading order.**\n", "1. Load reconstructed SVCs\n", "2. Visualize T-cell subtypes\n", "3. Quantify differential genes and pathways\n", "4. Compare raw and reconstructed signals\n", "\n", "**Reproducibility note.** `revise` imports are standard package imports from the installed `revise-svc` distribution; this notebook does not modify `sys.path` to import the repository source tree.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-05-13T10:41:16.509649Z", "iopub.status.busy": "2026-05-13T10:41:16.509435Z", "iopub.status.idle": "2026-05-13T10:41:17.352486Z", "shell.execute_reply": "2026-05-13T10:41:17.352007Z" } }, "outputs": [], "source": [ "import os\n", "os.environ.setdefault(\"TQDM_DISABLE\", \"1\")\n", "os.environ.setdefault(\"TQDM_MININTERVAL\", \"60\")\n", "\n", "try:\n", " from IPython import get_ipython\n", " _ipython = get_ipython()\n", " if _ipython is not None:\n", " _ipython.run_line_magic(\"matplotlib\", \"inline\")\n", "except Exception:\n", " pass\n", "\n", "output_dir = \"../../output/sc_SVC_case/P2CRC_Xenium\"\n", "select_ct = \"T\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Reconstructed SVCs\n", "\n", "Load the executed reconstruction outputs that drive the downstream figures.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-05-13T10:41:17.354778Z", "iopub.status.busy": "2026-05-13T10:41:17.354509Z", "iopub.status.idle": "2026-05-13T10:42:12.621347Z", "shell.execute_reply": "2026-05-13T10:42:12.620611Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/cpfs01/projects-HDD/cfff-c7cd658afc74_HDD/jiaoyifeng/miniconda3/envs/brainbeacon/lib/python3.9/site-packages/numba/core/decorators.py:246: RuntimeWarning: nopython is set for njit and is ignored\n", " warnings.warn('nopython is set for njit and is ignored', RuntimeWarning)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Conducting differential expression analysis...\n" ] } ], "source": [ "import os\n", "import scanpy as sc\n", "\n", "from revise.backend.runners.sc_svc_application import ScSVCAnalysis\n", "import pandas as pd\n", "import gseapy as gp\n", "import revise.analysis.bio as _revise_bio\n", "\n", "_EMPTY_ENRICHMENT_COLUMNS = [\n", " \"Gene_set\",\n", " \"Term\",\n", " \"Overlap\",\n", " \"P-value\",\n", " \"Adjusted P-value\",\n", " \"Old P-value\",\n", " \"Old Adjusted P-value\",\n", " \"Odds Ratio\",\n", " \"Combined Score\",\n", " \"Genes\",\n", "]\n", "\n", "def _get_enrichment_human_compatible(deg_genes, geneset_file, cutoff=0.05):\n", " if not deg_genes:\n", " return pd.DataFrame(columns=_EMPTY_ENRICHMENT_COLUMNS)\n", " try:\n", " enr = gp.enrichr(\n", " gene_list=deg_genes,\n", " gene_sets=geneset_file,\n", " organism=\"human\",\n", " cutoff=cutoff,\n", " )\n", " return enr.results\n", " except Exception as exc:\n", " print(f\"Skipping enrichment analysis: {type(exc).__name__}: {exc}\")\n", " return pd.DataFrame(columns=_EMPTY_ENRICHMENT_COLUMNS)\n", "\n", "_revise_bio.get_enrichment = _get_enrichment_human_compatible\n", "\n", "\n", "svc_save_dir = f\"{output_dir}/{select_ct}\"\n", "sc_svc_expr = sc.read_h5ad(f\"{svc_save_dir}/sc_SVC_expr.h5ad\")\n", "sc_svc_spatial = sc.read_h5ad(f\"{svc_save_dir}/sc_SVC_spatial.h5ad\")\n", "\n", "sc_svc_analysis = ScSVCAnalysis(sc_svc_spatial, sc_svc_expr, \n", " \"SVC_cluster\")\n", "# Legacy notebooks use these aliases in downstream result and plotting cells.\n", "sc_SVC_adata = sc_svc_analysis.sc_SVC_adata_spatial.copy()\n", "adata_sp = sc_svc_analysis.sc_SVC_adata_spatial.copy()\n", "adata_sc = sc_svc_analysis.sc_SVC_adata_expr.copy()\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2026-05-13T10:42:12.624275Z", "iopub.status.busy": "2026-05-13T10:42:12.623650Z", "iopub.status.idle": "2026-05-13T10:42:12.642347Z", "shell.execute_reply": "2026-05-13T10:42:12.641755Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/cpfs01/projects-HDD/cfff-c7cd658afc74_HDD/jiaoyifeng/miniconda3/envs/brainbeacon/lib/python3.9/site-packages/revise/backend/runners/sc_svc_application.py:67: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n", " grouped = self.sc_SVC_adata_expr.obs.groupby([self.cluster_col, sub_cell_type_col]).size()\n" ] }, { "data": { "text/html": [ "
| Level2 | \n", "CD4T | \n", "CD8T | \n", "NK | \n", "Tprolif | \n", "
|---|---|---|---|---|
| SVC_cluster | \n", "\n", " | \n", " | \n", " | \n", " |
| 0 | \n", "553 | \n", "6 | \n", "0 | \n", "57 | \n", "
| 1 | \n", "22 | \n", "3 | \n", "0 | \n", "262 | \n", "
| 2 | \n", "164 | \n", "1098 | \n", "99 | \n", "73 | \n", "
| 3 | \n", "498 | \n", "85 | \n", "1 | \n", "7 | \n", "
| 4 | \n", "712 | \n", "50 | \n", "0 | \n", "32 | \n", "
| 5 | \n", "819 | \n", "7 | \n", "0 | \n", "31 | \n", "
| 6 | \n", "197 | \n", "99 | \n", "53 | \n", "36 | \n", "
| \n", " | Gene_set | \n", "Term | \n", "Overlap | \n", "P-value | \n", "Adjusted P-value | \n", "Old P-value | \n", "Old Adjusted P-value | \n", "Odds Ratio | \n", "Combined Score | \n", "Genes | \n", "group | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "MSigDB_Hallmark_2020 | \n", "E2F Targets | \n", "22/200 | \n", "3.112157e-29 | \n", "6.224314e-28 | \n", "0 | \n", "0 | \n", "64.276168 | \n", "4219.065275 | \n", "TOP2A;TCF19;HELLS;RRM2;MCM7;TUBB;PLK1;HMGB2;KI... | \n", "1 | \n", "
| 1 | \n", "MSigDB_Hallmark_2020 | \n", "G2-M Checkpoint | \n", "19/200 | \n", "5.976739e-24 | \n", "5.976739e-23 | \n", "0 | \n", "0 | \n", "50.589004 | \n", "2705.204861 | \n", "TOP2A;PLK1;KIF11;KIF22;HMMR;MKI67;SMC4;NDC80;L... | \n", "1 | \n", "
| 2 | \n", "MSigDB_Hallmark_2020 | \n", "Mitotic Spindle | \n", "12/199 | \n", "6.214391e-13 | \n", "4.142927e-12 | \n", "0 | \n", "0 | \n", "26.407754 | \n", "742.235837 | \n", "TOP2A;TPX2;CENPF;PLK1;NUSAP1;CDK1;KIF11;KIF22;... | \n", "1 | \n", "
| 3 | \n", "MSigDB_Hallmark_2020 | \n", "mTORC1 Signaling | \n", "6/200 | \n", "2.963485e-05 | \n", "1.185394e-04 | \n", "0 | \n", "0 | \n", "11.309278 | \n", "117.916863 | \n", "RRM2;PLK1;MCM4;CORO1A;BUB1;MCM2 | \n", "1 | \n", "
| 4 | \n", "MSigDB_Hallmark_2020 | \n", "Myc Targets V1 | \n", "6/200 | \n", "2.963485e-05 | \n", "1.185394e-04 | \n", "0 | \n", "0 | \n", "11.309278 | \n", "117.916863 | \n", "RRM1;MCM7;HNRNPA2B1;MCM4;TYMS;MCM2 | \n", "1 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 19 | \n", "MSigDB_Hallmark_2020 | \n", "Apical Junction | \n", "1/200 | \n", "4.533326e-01 | \n", "4.533326e-01 | \n", "0 | \n", "0 | \n", "1.681373 | \n", "1.330183 | \n", "LAYN | \n", "5 | \n", "
| 16 | \n", "MSigDB_Hallmark_2020 | \n", "Apoptosis | \n", "1/161 | \n", "3.847172e-01 | \n", "4.533326e-01 | \n", "0 | \n", "0 | \n", "2.095339 | \n", "2.001566 | \n", "BCL2L11 | \n", "5 | \n", "
| 17 | \n", "MSigDB_Hallmark_2020 | \n", "Mitotic Spindle | \n", "1/199 | \n", "4.516711e-01 | \n", "4.533326e-01 | \n", "0 | \n", "0 | \n", "1.689950 | \n", "1.343174 | \n", "BCL2L11 | \n", "5 | \n", "
| 18 | \n", "MSigDB_Hallmark_2020 | \n", "Adipogenesis | \n", "1/200 | \n", "4.533326e-01 | \n", "4.533326e-01 | \n", "0 | \n", "0 | \n", "1.681373 | \n", "1.330183 | \n", "NABP1 | \n", "5 | \n", "
| 20 | \n", "MSigDB_Hallmark_2020 | \n", "Complement | \n", "1/200 | \n", "4.533326e-01 | \n", "4.533326e-01 | \n", "0 | \n", "0 | \n", "1.681373 | \n", "1.330183 | \n", "F5 | \n", "5 | \n", "
61 rows × 11 columns
\n", "