{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Colab setup - run this first, then switch the runtime to R\n", "# (Runtime > Change runtime type > R). Precompiled Linux binaries via Posit P3M\n", "# keep the install to a couple of minutes instead of compiling from source.\n", "local({\n", " cn <- tryCatch(system(\"lsb_release -cs\", intern = TRUE), error = function(e) \"jammy\")\n", " options(repos = c(CRAN = sprintf(\"https://packagemanager.posit.co/cran/__linux__/%s/latest\", cn)),\n", " HTTPUserAgent = sprintf(\"R/%s R (%s)\", getRversion(),\n", " paste(getRversion(), R.version$platform, R.version$arch, R.version$os)))\n", "})\n", "if (!requireNamespace(\"BiocManager\", quietly = TRUE)) install.packages(\"BiocManager\")\n", "BiocManager::install(\n", " c(\"SingleCellExperiment\", \"SummarizedExperiment\", \"MultiAssayExperiment\",\n", " \"S4Vectors\", \"basilisk\"),\n", " update = FALSE, ask = FALSE)\n", "install.packages(c(\"remotes\", \"reticulate\", \"Matrix\", \"ggplot2\"))\n", "remotes::install_github(\"PYangLab/M3\", subdir = \"m3-r\")\n", "# The first m3_train() builds the bundled Python engine via basilisk (a few\n", "# minutes on first run); afterwards it is cached for the session." ], "id": "cell-00" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Feature attribution\n", "\n", "Once m3 can predict disease, attribution asks which genes, cells and cell types drove that prediction. We train on the full reference, then trace each prediction back to the genes, cells, and cell types behind it.\n", "\n", "## 1. Load the demo dataset" ], "id": "cell-01" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "library(m3)\n", "library(ggplot2)\n", "set.seed(0)\n", "data <- m3_demo()\n", "data" ], "id": "cell-02" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Build and train the model\n", "\n", "Same setup as the patient-prediction tutorial, minus the held-out batch (we attribute on the full reference). `m3_train(...)` fits both the integration model and the disease predictor; attribution explains that predictor." ], "id": "cell-03" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#To save time, users can set max_epochs to 100 for test.\n", "model <- m3_train(\n", " data,\n", " condition_keys = c(\"cond_group\", \"Age_interval\"),\n", " target_condition = \"cond_group\",\n", " celltype_key = \"mergedcelltype\",\n", " batch_key = \"batch\",\n", " donor_key = \"sample_id\",\n", " embedding_dim = 30L,\n", " max_epochs = 500L,\n", " seed = 0L\n", ")\n", "m3_capabilities(model)" ], "id": "cell-04" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Attribute and rank the cell types\n", "\n", "`m3_attribute(model, reference_labels = \"HC\")` scores how much each gene, cell, and cell type pushed the prediction away from the reference label (HC, i.e. healthy). Here we look at the **top cell types**: `m3_top_celltypes(...)` ranks them, keeping only cell types with enough cells in each condition so a small group can't mislead the ranking." ], "id": "cell-05" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "attr <- m3_attribute(model, reference_labels = \"HC\") # n_steps defaults to 50\n", "cat(\"\\ntop cell types (>= 200 cells per condition):\\n\")\n", "\n", "print(m3_top_celltypes(attr, min_cells_per_condition = 200L))" ], "id": "cell-06" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Top genes\n", "\n", "`m3_top_genes(...)` ranks the genes by how strongly they drove the prediction. `min_cells_per_condition` keeps only cell types with enough cells in each condition (so a tiny group can't dominate the ranking), and housekeeping / ribosomal genes are dropped." ], "id": "cell-07" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top100_rna <- m3_top_genes(attr, n = 100L, min_cells_per_condition = 200L, modality = \"rna\")\n", "cat(sprintf(\"top-100 RNA genes (computed over %d balanced cell types):\\n\",\n", " top100_rna$n_celltypes_used[1]))\n", "\n", "print(utils::head(top100_rna, 15))" ], "id": "cell-08" }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Done.** The genes, cells, and cell types behind m3's Severe-vs-HC prediction." ], "id": "cell-09" } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "name": "R" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 5 }