{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "# Vitessce Widget Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization of a Loom file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Import dependencies" ] }, { "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_loom\n", "import numpy as np\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", " to_diamond,\n", " VAR_CHUNK_SIZE,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Download data\n", "\n", "Download `osmFISH_SScortex_mouse_all_cells.loom` from http://loom.linnarssonlab.org/." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loom_filepath = join(\"data\", \"osmFISH_SScortex_mouse_all_cells.loom\")\n", "if not isfile(loom_filepath):\n", " os.makedirs(\"data\", exist_ok=True)\n", " urlretrieve('http://loom.linnarssonlab.org/clone/osmFISH/osmFISH_SScortex_mouse_all_cells.loom', loom_filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Open Loom file with AnnData's read_loom" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata = read_loom(loom_filepath, obsm_names={\"tSNE\": [\"_tSNE_1\", \"_tSNE_2\"], \"spatial\": [\"X\", \"Y\"]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate pseudo-segmentations as diamond-shaped polygons centered on the spatial coordinate of each cell, and store in `adata.obsm[\"segmentations\"]`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num_cells = adata.obs.shape[0]\n", "adata.obsm[\"segmentations\"] = np.zeros((num_cells, 4, 2))\n", "radius = 100\n", "for i in range(num_cells):\n", " adata.obsm[\"segmentations\"][i, :, :] = to_diamond(adata.obsm['spatial'][i, 0], adata.obsm['spatial'][i, 1], radius)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the AnnData object to a Zarr store:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "zarr_filepath = join(\"data\", \"osmFISH_SScortex_mouse_all_cells.zarr\")\n", "if not isdir(zarr_filepath) or True:\n", " adata = optimize_adata(\n", " adata,\n", " obs_cols=[\"ClusterName\"],\n", " obsm_keys=[\"tSNE\", \"spatial\", \"segmentations\"],\n", " optimize_X=True,\n", " )\n", " adata.write_zarr(zarr_filepath, chunks=[adata.shape[0], VAR_CHUNK_SIZE])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Configure Vitessce\n", "\n", "Create a Vitessce view config." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vc = VitessceConfig(schema_version=\"1.0.15\", name='Loom Example', description='osmFISH dataset of the mouse cortex including all cells')\n", "w = AnnDataWrapper(adata_path=zarr_filepath, obs_set_paths=[\"obs/ClusterName\"], obs_set_names=[\"Clusters\"], obs_locations_path=\"obsm/spatial\", obs_segmentations_path=\"obsm/segmentations\", obs_embedding_paths=[\"obsm/tSNE\"])\n", "dataset = vc.add_dataset(name='SScortex').add_object(w)\n", "\n", "tsne = vc.add_view(cm.SCATTERPLOT, dataset=dataset, mapping=\"tSNE\")\n", "cell_sets = vc.add_view(cm.OBS_SETS, dataset=dataset)\n", "spatial = vc.add_view(cm.SPATIAL, dataset=dataset)\n", "\n", "spatial_segmentation_layer_value = {\n", " \"opacity\": 1,\n", " \"radius\": 0,\n", " \"visible\": True,\n", " \"stroked\": False\n", "}\n", "\n", "vc.link_views([spatial], [ct.SPATIAL_ZOOM, ct.SPATIAL_TARGET_X, ct.SPATIAL_TARGET_Y, ct.SPATIAL_SEGMENTATION_LAYER], [-6.43, 10417.69, 24885.55, spatial_segmentation_layer_value])\n", "vc.layout(spatial | (tsne / cell_sets));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Render the widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A widget can be created with the `.widget()` method on the config instance." ] }, { "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 }