OpenClaw-Medical-Skills spatial-transcriptomics-tutorials-with-omicverse
Guide users through omicverse's spatial transcriptomics tutorials covering preprocessing, deconvolution, and downstream modelling workflows across Visium, Visium HD, Stereo-seq, and Slide-seq datasets.
git clone https://github.com/FreedomIntelligence/OpenClaw-Medical-Skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/FreedomIntelligence/OpenClaw-Medical-Skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/spatial-tutorials" ~/.claude/skills/freedomintelligence-openclaw-medical-skills-spatial-transcriptomics-tutorials-wi && rm -rf "$T"
T=$(mktemp -d) && git clone --depth=1 https://github.com/FreedomIntelligence/OpenClaw-Medical-Skills "$T" && mkdir -p ~/.openclaw/skills && cp -r "$T/skills/spatial-tutorials" ~/.openclaw/skills/freedomintelligence-openclaw-medical-skills-spatial-transcriptomics-tutorials-wi && rm -rf "$T"
skills/spatial-tutorials/SKILL.mdSpatial transcriptomics tutorials with omicverse
Overview
Use this skill to navigate the spatial analysis tutorials located under
. The notebooks span preprocessing utilities (Tutorials-space
, t_crop_rotate.ipynb
), deconvolution frameworks (t_cellpose.ipynb
, t_decov.ipynb
), and downstream spatial modelling or integration tasks (t_starfysh.ipynb
, t_cluster_space.ipynb
, t_staligner.ipynb
, t_spaceflow.ipynb
, t_commot_flowsig.ipynb
, t_gaston.ipynb
, t_slat.ipynb
). Follow the staged instructions below to match the "Preprocess", "Deconvolution", and "Downstream" groupings presented in the notebooks.t_stt.ipynb
Instructions
Preprocess
- Load spatial slides and manipulate coordinates
- Import
,omicverse as ov
, and enable plotting defaults withscanpy as sc
orov.plot_set()
.ov.plot_set(font_path='Arial')t_crop_rotate.ipynb - Fetch public Visium data via
, inspectsc.datasets.visium_sge(...)
, and respectadata.obsm['spatial']
when rescaling coordinates for high-resolution overlays.uns['spatial'][library_id]['scalefactors'] - Apply region selection and alignment helpers:
for bounding-box crops,ov.space.crop_space_visium(...)
followed byov.space.rotate_space_visium(...)
, and refine offsets withov.space.map_spatial_auto(..., method='phase')
before plotting usingov.space.map_spatial_manual(...)
.sc.pl.embedding(..., basis='spatial')
- Import
- Segment Visium HD tiles into cells
- Organise Visium HD outputs (binned parquet counts,
histology) and load them through.btf
.ov.space.read_visium_10x(path, source_image_path=...)t_cellpose.ipynb - Filter sparse bins (
andov.pp.filter_genes(..., min_cells=3)
) prior to segmentation.ov.pp.filter_cells(..., min_counts=1) - Run nucleus/cell segmentation variants:
for H&E,ov.space.visium_10x_hd_cellpose_he(...)
to grow labels across neighbouring bins, andov.space.visium_10x_hd_cellpose_expand(...)
for gene-expression driven seeds. Harmonise labels withov.space.visium_10x_hd_cellpose_gex(...)
and aggregate to cell-level AnnData usingov.space.salvage_secondary_labels(...)
.ov.space.bin2cell(..., labels_key='labels_joint')
- Organise Visium HD outputs (binned parquet counts,
- Initial QC for downstream tasks
- For Visium/DLPFC re-analyses, compute QC metrics (
) and persist intermediate AnnData snapshots (sc.pp.calculate_qc_metrics(adata, inplace=True)
) for reuse across tutorials.adata.write('data/cluster_svg.h5ad', compression='gzip')t_cluster_space.ipynb
- For Visium/DLPFC re-analyses, compute QC metrics (
Deconvolution
- Configure single-cell references and spatial targets
- Load scRNA-seq references (
) with harmonised gene IDs and spatial slides (adata_sc = ov.read('data/sc.h5ad')
).adata_sp = sc.datasets.visium_sge(...)t_decov.ipynb - Instantiate the unified wrapper
, passing shared keys likeov.space.Deconvolution(...)
,celltype_key
, andadata_sc
.adata_sp
- Load scRNA-seq references (
- Execute Tangram and cell2location pipelines
- Call
/decov_obj.preprocess_sc(...)
to align matrices, then rundecov_obj.preprocess_sp(...)
and persist outputs withdecov_obj.deconvolution(method='tangram', ...)
plusov.utils.save(...)
hooks for AnnData members..write(...) - For cell2location, reinitialise
, train (ov.space.Deconvolution(..., method='cell2location')
), monitor viadecov_obj.deconvolution(max_epochs=...)
, and store models (decov_obj.mod_sc.plot_history(...)
).decov_obj.save_model(...) - Visualise inferred proportions using
,ov.space.plot_cell2location(...)
, and ROI-focused pie charts after cropping (sc.pl.spatial(..., color=list_of_celltypes)
).ov.space.crop_space_visium(...)
- Call
- Run Starfysh archetypal deconvolution
- Import Starfysh utilities (
) and prepare expression counts plus optional signature sets.from omicverse.external.starfysh import AA, utils, plot_utils, post_analysist_starfysh.ipynb - Identify anchor spots with
, optionally infer archetypes viautils.prepare_data(...)
, and refine signatures usingAA.ArchetypalAnalysis(...)
.utils.refine_anchors(...) - Train Starfysh models (
orutils.run_starfysh(poe=False, ...)
with histology) across multiple restarts, then parse outputs throughpoe=True
,post_analysis.load_model(...)
, andplot_utils.pl_spatial_inf_feature(...)
for per-cell-type maps.cell2proportion(...)
- Import Starfysh utilities (
Downstream
- Spatial clustering and denoising
- Generate embeddings using omicverse wrappers:
,ov.utils.cluster(..., use_rep='graphst|original|X_pca', method='mclust')
, and evaluate ARI (ov.space.merge_cluster(...)
).adjusted_rand_score(...)t_cluster_space.ipynb - Explore algorithm-specific toggles: GraphST/BINARY require precalculated latent spaces, STAGATE training (
), and CAST for multi-slice single-cell resolution data.ov.utils.cluster(..., use_rep='STAGATE', ...)
- Generate embeddings using omicverse wrappers:
- Integrate multi-slice datasets
- Concatenate Stereo-seq/Slide-seqV2 batches (
) and initialisead.concat(Batch_list, label='slice_name', keys=section_ids)
.ov.space.pySTAligner(...)t_staligner.ipynb - Train with
, callSTAligner_obj.train_STAligner_subgraph(...)
, and retrieve latent embeddings viaSTAligner_obj.train()
before clustering (STAligner_obj.predicted()
,sc.pp.neighbors(..., use_rep='STAligner')
).ov.utils.cluster(...)
- Concatenate Stereo-seq/Slide-seqV2 batches (
- Model spatial gradients and trajectories
- For pseudo-spatial maps, build
and train usingsf_obj = ov.space.pySpaceFlow(adata)
, then computesf_obj.train(spatial_regularization_strength=0.1, ...)
to populatesf_obj.cal_pSM(...)
.adata.obs['pSM_spaceflow']t_spaceflow.ipynb - Analyse transition dynamics with
, followed bySTT_obj = ov.space.STT(adata, spatial_loc='xy_loc', region='Region')
,STT_obj.train(...)
, and downstream visualisations (STT_obj.stage_estimate()
,STT_obj.plot_pathway(...)
).STT_obj.infer_lineage(...)t_stt.ipynb
- For pseudo-spatial maps, build
- Infer communication and flow networks
- Pull ligand–receptor resources via
, filter withov.external.commot.pp.ligand_receptor_database(species='human')
, and compute signaling usingfilter_lr_database(...)
.ov.external.commot.tl.spatial_communication(...)t_commot_flowsig.ipynb - Construct FlowSig inputs (
,adata.layers['normalized'] = adata.X.copy()
), retain spatially informative modules (Moran’s I filtering), and validate edges through bootstrapping thresholds (ov.external.flowsig.tl.construct_intercellular_flow_network(...)
).edge_threshold = 0.7
- Pull ligand–receptor resources via
- Extract structural layers and align developmental slices
- Train GASTON with
, rescale GLM-PC matrices viagas_obj = ov.space.GASTON(adata)
, and infer iso-depths usinggas_obj.load_rescale(A)
. Visualise withgas_obj.cal_iso_depth(n_layers)
,gas_obj.plot_isodepth(...)
, and probe continuous/discontinuous gene lists (gas_obj.plot_clusters_restrict(...)
).gas_obj.cont_genes_layert_gaston.ipynb - For SLAT, construct spatial graphs (
), run alignment (Cal_Spatial_Net(adata1, k_cutoff=20)
,run_SLAT(...)
), and examine correspondences through Sankey diagrams (spatial_match(...)
) and lineage-focused subsetting (Sankey_multi(...)
).cal_matching_cell(...)t_slat.ipynb
- Train GASTON with
Dependencies
- Core:
,omicverse
,scanpy
,anndata
,numpy
,matplotlib
(deconvolution + QC),squidpy
(FlowSig graphs).networkx - Segmentation:
,cellpose
,stardist
/opencv-python
, optional GPU-enabled PyTorch for acceleration.tifffilet_cellpose.ipynb - Deconvolution:
,tangram
,cell2location
,pytorch-lightning
,pandas
, plus optional GPU/CUDA stacks; Starfysh additionally needsh5py
,torch
, and curated signature CSVs.scikit-learn
,t_decov.ipynbt_starfysh.ipynb - Downstream modelling:
(clustering, KMeans, ARI),scikit-learn
for STT enrichment,gseapy==1.0.4
,commot
,flowsig
-backed modules (STAligner, SpaceFlow, GASTON, SLAT), plus HTML exporters (Plotly) for Sankey plots.torch
Critical functions and artefacts to surface quickly
- Spatial preprocessing:
,ov.space.crop_space_visium
,ov.space.rotate_space_visium
,ov.space.map_spatial_auto
,ov.space.map_spatial_manual
.ov.space.bin2cell - Deconvolution containers:
,ov.space.Deconvolution.preprocess_sc
,.preprocess_sp
,.deconvolution
,.adata_cell2location
..adata_impute - Archetypal/Starfysh:
,AA.ArchetypalAnalysis
,utils.refine_anchors
,utils.run_starfysh
.plot_utils.pl_spatial_inf_feature - Clustering/integration:
,ov.utils.cluster
,ov.space.merge_cluster
,ov.space.pySTAligner
,ov.space.pySpaceFlow
,ov.space.STT
,ov.space.GASTON
,Cal_Spatial_Net
,run_SLAT
.Sankey_multi - Communication:
,ov.external.commot.pp.ligand_receptor_database
,ov.external.commot.tl.spatial_communication
.ov.external.flowsig.tl.construct_intercellular_flow_network
Troubleshooting
- Coordinate mismatches after rotation/cropping: ensure scalefactors are applied when plotting and cast
toadata.obsm['spatial']
before runningfloat64
.map_spatial_autot_crop_rotate.ipynb - Cellpose runtime errors: verify
image paths, memory-map large TIFFs via.btf
, and adjustbackend='tifffile'
plusmpp
for dense tissues; GPU runs require matching CUDA/PyTorch builds.buffert_cellpose.ipynb - Gene ID overlap failures in Tangram/cell2location: harmonise identifiers (ENSEMBL vs gene symbols) and drop non-overlapping genes before
.decov_obj.preprocess_*t_decov.ipynb - mclust errors in spatial clustering: install
and the Rrpy2
package, or switch to the pure Pythonmclust
fallback when R bindings are unavailable.method='mclust't_cluster_space.ipynb - STAligner/SpaceFlow convergence: confirm
exists and scale coordinates; tune learning rates/regularisation strength when embeddings collapse to a point.adata.obsm['spatial']
,t_staligner.ipynbt_spaceflow.ipynb - FlowSig network sparsity: build spatial graphs prior to Moran’s I filtering and raise
or increase bootstraps to stabilise edges.edge_thresholdt_commot_flowsig.ipynb - STT pathway downloads:
lookups need network access; cache gene sets locally and reuse viagseapy
to avoid repeated requests.ov.utils.geneset_prepare(...)t_stt.ipynb - GASTON output directories: provide writable
paths and account for PyTorch nondeterminism when comparing replicate runs.out_dirt_gaston.ipynb - SLAT alignment quality: regenerate spatial graphs with appropriate
and inspectk_cutoff
flags before trusting downstream lineage analyses.low_quality_indext_slat.ipynb
Examples
- "Crop, rotate, and manually re-align Visium coordinates before running Visium HD cell segmentation, then aggregate bins into cell-level AnnData."
- "Execute Tangram and cell2location through
, save trained models, and plot lymph node cell-type proportions."ov.space.Deconvolution - "Train STAligner and SpaceFlow on DLPFC slices, infer communication networks with COMMOT+FlowSig, and visualise iso-depth layers via GASTON."
References
- Tutorials:
Tutorials-space/ - Notebook index:
,t_crop_rotate.ipynb
,t_cellpose.ipynb
,t_cluster_space.ipynb
,t_decov.ipynb
,t_starfysh.ipynb
,t_staligner.ipynb
,t_spaceflow.ipynb
,t_commot_flowsig.ipynb
,t_gaston.ipynb
,t_slat.ipynbt_stt.ipynb - Quick copy/paste commands:
reference.md