{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "# Vitessce Widget Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization of 3k PBMC reference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Import dependencies\n", "\n", "We need to import the classes and functions that we will be using from the corresponding packages." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from os.path import join, isfile, isdir\n", "from urllib.request import urlretrieve\n", "from anndata import read_h5ad\n", "import scanpy as sc\n", "\n", "from vitessce import (\n", " VitessceConfig,\n", " Component as cm,\n", " CoordinationType as ct,\n", " AnnDataWrapper,\n", ")\n", "from vitessce.data_utils import (\n", " optimize_adata,\n", " VAR_CHUNK_SIZE,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Download the dataset\n", "\n", "Download `pbmc3k_final.h5ad` from https://seurat.nygenome.org/pbmc3k_final.h5ad" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata_filepath = join(\"data\", \"pbmc3k_final.h5ad\")\n", "if not isfile(adata_filepath):\n", " os.makedirs(\"data\", exist_ok=True)\n", " urlretrieve('https://seurat.nygenome.org/pbmc3k_final.h5ad', adata_filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Load the dataset\n", "\n", "Load the dataset using AnnData's `read_h5ad` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata = read_h5ad(adata_filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.1 Save the AnnData object to Zarr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "zarr_filepath = join(\"data\", \"pbmc3k_final.zarr\")\n", "if not isdir(zarr_filepath):\n", " adata = optimize_adata(\n", " adata,\n", " obs_cols=[\"leiden\"],\n", " obsm_keys=[\"X_umap\", \"X_pca\"],\n", " optimize_X=True,\n", " )\n", " adata.write_zarr(zarr_filepath, chunks=[adata.shape[0], VAR_CHUNK_SIZE])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Create a Vitessce view config\n", "\n", "Define the data and views you would like to include in the widget." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vc = VitessceConfig(schema_version=\"1.0.15\", name='PBMC Reference')\n", "dataset = vc.add_dataset(name='PBMC 3k').add_object(AnnDataWrapper(\n", " adata_path=zarr_filepath,\n", " obs_set_paths=[\"obs/leiden\"],\n", " obs_set_names=[\"Leiden\"],\n", " obs_embedding_paths=[\"obsm/X_umap\", \"obsm/X_pca\"],\n", " obs_embedding_names=[\"UMAP\", \"PCA\"],\n", " obs_feature_matrix_path=\"X\"\n", "))\n", "\n", "umap = vc.add_view(cm.SCATTERPLOT, dataset=dataset, mapping=\"UMAP\")\n", "pca = vc.add_view(cm.SCATTERPLOT, dataset=dataset, mapping=\"PCA\")\n", "cell_sets = vc.add_view(cm.OBS_SETS, dataset=dataset)\n", "genes = vc.add_view(cm.FEATURE_LIST, dataset=dataset)\n", "heatmap = vc.add_view(cm.HEATMAP, dataset=dataset)\n", "\n", "vc.layout((umap / pca) | ((cell_sets | genes) / heatmap));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Create the Vitessce widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A widget can be created with the `.widget()` method on the config instance. Here, the `proxy=True` parameter allows this widget to be used in a cloud notebook environment, such as Binder." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vw = vc.widget()\n", "vw" ] } ], "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.8.0" } }, "nbformat": 4, "nbformat_minor": 4 }