Vitessce SpatialQuery plugin usage demo

[ ]:
from os.path import join
from anndata import read_h5ad
from vitessce import (
    VitessceConfig,
    AnnDataWrapper,
    ViewType as vt,
    CoordinationType as ct,
    CoordinationLevel as CL,
    hconcat,
    vconcat,
)
from vitessce.widget_plugins import SpatialQueryPlugin

Download example dataset

Download the secondary_analysis.h5ad file for sample HBM838.LDFP.578 from the HuBMAP Portal at https://portal.hubmapconsortium.org/browse/dataset/5bf1e7b295343c4537206beda25aa4ca

[ ]:
import os
from os.path import join, isfile, isdir
from urllib.request import urlretrieve

adata_path = join("data", "HBM987_KWLK_254", "secondary_analysis.h5ad")
zarr_path = join("data", "HBM987_KWLK_254", "secondary_analysis.h5ad.zarr")

if not isdir(join("data")):
    os.makedirs(join("data"), exist_ok=True)
    if not isdir(join("data", "HBM987_KWLK_254")):
        os.makedirs(join("data", "HBM987_KWLK_254"), exist_ok=True)
        if not isfile(adata_path):
            urlretrieve('https://assets.hubmapconsortium.org/0a21f3fa27109790483f2a0729be53de/secondary_analysis.h5ad', adata_path)
[ ]:
adata = read_h5ad(adata_path)
adata.write_zarr(zarr_path)
[ ]:
adata
[ ]:
plugin = SpatialQueryPlugin(adata, label_key="predicted_label", spatial_key="X_spatial", feature_name="hugo_symbol")
[ ]:
vc = VitessceConfig(schema_version="1.0.16", name="Spatial-Query")
dataset = vc.add_dataset("Query results").add_object(AnnDataWrapper(
    adata_path=zarr_path,
    obs_feature_matrix_path="X",
    obs_set_paths=["obs/predicted_label"],
    obs_set_names=["Cell Type"],
    obs_spots_path="obsm/X_spatial",
    feature_labels_path="var/hugo_symbol",
    coordination_values={
        "featureLabelsType": "Gene symbol",
    }
))

spatial_view = vc.add_view("spatialBeta", dataset=dataset)
lc_view = vc.add_view("layerControllerBeta", dataset=dataset)
sets_view = vc.add_view("obsSets", dataset=dataset)
features_view = vc.add_view("featureList", dataset=dataset)
sq_view = vc.add_view("spatialQuery", dataset=dataset)
sq_heatmap = vc.add_view("spatialQueryHeatmap", dataset=dataset)


obs_set_selection_scope, = vc.add_coordination("obsSetSelection",)
obs_set_selection_scope.set_value(None)

sets_view.use_coordination(obs_set_selection_scope)
sq_view.use_coordination(obs_set_selection_scope)
spatial_view.use_coordination(obs_set_selection_scope)
features_view.use_coordination(obs_set_selection_scope)

vc.link_views([spatial_view, lc_view, sets_view, features_view],
    ["additionalObsSets", "obsSetColor"],
    [plugin.additional_obs_sets, plugin.obs_set_color]
)
vc.link_views_by_dict([spatial_view, lc_view], {
    "spotLayer": CL([
        {
            "obsType": "cell",
            "spatialSpotRadius": 15,
        },
    ])
})

vc.layout((spatial_view | (lc_view / features_view)) / hconcat(sets_view, sq_heatmap, sq_view, split=[1, 1, 1]));
[ ]:
vw = vc.widget(height=900, plugins=[plugin], remount_on_uid_change=False)
vw