{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# hv-anndata" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import holoviews as hv\n", "import panel as pn\n", "import scanpy as sc\n", "\n", "import hv_anndata\n", "from hv_anndata.interface import ACCESSOR as A" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hv_anndata.register()\n", "hv.extension(\"bokeh\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example workflow 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata = sc.datasets.paul15()\n", "sc.pp.pca(adata)\n", "sc.pp.neighbors(adata)\n", "sc.tl.umap(adata)\n", "adata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple HoloViews/Bokeh scatter of UMAP results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(\n", " hv.Scatter(\n", " adata, A.obsm[\"X_umap\"][0], [A.obsm[\"X_umap\"][1], A.obs[\"paul15_clusters\"]]\n", " )\n", " .opts(\n", " color=A.obs[\"paul15_clusters\"],\n", " cmap=\"Category20\",\n", " legend_position=\"left\",\n", " frame_width=500,\n", " height=500,\n", " alpha=0.5,\n", " tools=[\"hover\"],\n", " )\n", " .hist()\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `ManifoldMap` App to explore all dimensionality reduction results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hv_anndata.ManifoldMap(\n", " adata=adata, datashade=False, reduction=\"X_umap\", show_labels=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Holoviews/Bokeh Dotmap plot " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `ManifoldMap` App to explore all dimensionality reduction results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hv_anndata.ManifoldMap(\n", " adata=adata, datashade=False, reduction=\"X_umap\", show_labels=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Holoviews/Bokeh Dotmap plot " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "marker_genes = {\n", " \"Erythroids\": [\"Gata1\", \"Klf1\", \"Epor\", \"Gypa\", \"Hba-a2\"],\n", " # \"Neutrophils\": [\"Elane\", \"Cebpe\", \"Ctsg\", \"Mpo\", \"Gfi1\"],\n", " # \"Neutrophils\": [\"Elane\", \"Cebpe\", \"Ctsg\", \"Mpo\", \"Gfi1\"],\n", " \"Monocytes\": [\"Irf8\", \"Csf1r\", \"Ctsg\", \"Mpo\"],\n", " \"Megakaryocytes\": [\"Itga2b\", \"Pbx1\", \"Sdpr\", \"Vwf\"],\n", " \"Basophils\": [\"Mcpt8\", \"Prss34\"],\n", " \"Mast cells\": [\"Cma1\", \"Gzmb\"],\n", "}\n", "hv_anndata.Dotmap(\n", " adata=adata, marker_genes=marker_genes, groupby=\"paul15_clusters\"\n", ").opts(width=1000, height=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example workflow 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dotmap plot with dendrogram" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata2 = sc.datasets.pbmc68k_reduced()\n", "sel_marker_genes = {\n", " \"CD14+ Mono\": [\"FCN1\"],\n", " \"CD16+ Mono\": [\"FCGR3A\"],\n", " \"ID2-hi myeloid prog\": [\"ID2\", \"S100A9\"],\n", " \"cDC2\": [\"CST3\", \"LYZ\", \"CLEC10A\", \"FCER1A\"],\n", " \"Lymph prog\": [\"CD79B\", \"IGLL1\"],\n", " \"B1 B\": [\"MS4A1\", \"BLK\"],\n", " \"Plasma cells\": [\"MZB1\"],\n", " \"CD4+ T activated\": [\"CD4\", \"IL7R\"],\n", " \"pDC\": [\"GZMB\"],\n", "}\n", "dm2 = hv_anndata.Dotmap(\n", " adata=adata2, marker_genes=sel_marker_genes, groupby=\"bulk_labels\"\n", ").opts(height=500, width=1200)\n", "hv.operation.dendrogram(\n", " dm2, adjoint_dims=[\"cluster\"], main_dim=\"mean_expression\", invert=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example workflow 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dotmap plot configurable with the AutoCompleteMultiChoice widget" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Required for the AutoCompleteMultiChoice to render properly\n", "pn.extension(\"jsoneditor\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "marker_genes = {\n", " \"Erythroids\": [\"Gata1\", \"Klf1\", \"Epor\", \"Gypa\", \"Hba-a2\"],\n", " \"Neutrophils\": [\"Elane\", \"Cebpe\", \"Ctsg\", \"Mpo\", \"Gfi1\"],\n", " \"Monocytes\": [\"Irf8\", \"Csf1r\", \"Ctsg\", \"Mpo\"],\n", " \"Megakaryocytes\": [\"Itga2b\", \"Pbx1\", \"Sdpr\", \"Vwf\"],\n", " \"Basophils\": [\"Mcpt8\", \"Prss34\"],\n", " \"Mast cells\": [\"Cma1\", \"Gzmb\"],\n", "}\n", "options = sorted(gene for genes in marker_genes.values() for gene in genes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w_autocompletemc = hv_anndata.AutoCompleteMultiChoice(options=options)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def dotmap_plot(adata, marker_genes, groupby):\n", " return hv_anndata.Dotmap(\n", " adata=adata, marker_genes=marker_genes, groupby=groupby\n", " ).opts(height=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.Row(\n", " w_autocompletemc,\n", " pn.bind(dotmap_plot, adata, w_autocompletemc.param.value, \"paul15_clusters\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{toctree}\n", ":hidden:\n", ":maxdepth: 2\n", "\n", "examples/AnnData\n", "examples/Violin" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" } }, "nbformat": 4, "nbformat_minor": 4 }