{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "# Vitessce Widget Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The from_object shortcut" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Import dependencies\n", "\n", "Import the functions and classes that we will be using." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from os.path import join\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", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Download the data\n", "\n", "For this example, we need to download a dataset from the COVID-19 Cell Atlas https://www.covid19cellatlas.org/index.healthy.html#habib17." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.makedirs(\"data\", exist_ok=True)\n", "adata_filepath = join(\"data\", \"habib17.processed.h5ad\")\n", "urlretrieve('https://covid19.cog.sanger.ac.uk/habib17.processed.h5ad', adata_filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Load the data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata = read_h5ad(join(\"data\", \"habib17.processed.h5ad\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.1. Preprocess the Data For Visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top_dispersion = adata.var[\"dispersions_norm\"][\n", " sorted(\n", " range(len(adata.var[\"dispersions_norm\"])),\n", " key=lambda k: adata.var[\"dispersions_norm\"][k],\n", " )[-51:][0]\n", "]\n", "adata.var[\"top_highly_variable\"] = (\n", " adata.var[\"dispersions_norm\"] > top_dispersion\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With one line of code, you may create a Vitessce widget based on an automatically inferred configuration." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vw = VitessceConfig.from_object(AnnDataWrapper(\n", " adata,\n", " obs_embedding_paths=[\"obsm/X_umap\"],\n", " obs_embedding_names=[\"UMAP\"],\n", " obs_set_paths=[\"obs/CellType\"],\n", " obs_set_names=[\"Cell Type\"],\n", " obs_feature_matrix_path=\"X\",\n", " feature_filter_path=\"var/top_highly_variable\"\n", "), schema_version=\"1.0.15\").widget(height=800)\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 }