SKILL.md
BulkTrajBlend trajectory interpolation
Overview
Invoke this skill when users need to bridge gaps in single-cell developmental trajectories using matched bulk RNA-seq. It follows [tbulktrajblend.ipynb](../../omicverseguide/docs/Tutorials-bulk2single/t_bulktrajblend.ipynb), showcasing how BulkTrajBlend deconvolves PDAC bulk samples, identifies overlapping communities with a GNN, and interpolates "interrupted" cell states.
Instructions
- Prepare libraries and inputs
- Import omicverse as ov, scanpy as sc, scvelo as scv, and helper functions like from omicverse.utils import mde; run ov.plotset(). - Load the reference scRNA-seq AnnData (scv.datasets.dentategyrus()) and raw bulk counts with ov.utils.read(...) followed by ov.bulk.MatrixID_mapping(...) for gene ID harmonisation.
- Configure BulkTrajBlend
- Instantiate ov.bulk2single.BulkTrajBlend(bulkseq=bulkdf, singleseq=adata, bulkgroup=['dgd1','dgd2','dgd3'], celltypekey='clusters'). - Explain that bulkgroup names correspond to raw bulk columns and the method expects unscaled counts.
- Set beta-VAE expectations
- Call bulktb.vaeconfigure(celltarget_num=100) (or pass a dictionary) to define expected cell counts per cluster. Mention that omitting the argument triggers TAPE-based estimation.
- Train or load the beta-VAE
- Use bulktb.vaetrain(batchsize=512, learningrate=1e-4, hiddensize=256, epochnum=3500, vaesavedir='...', vaesavename='dgbtbvae', generatesavedir='...', generatesavename='dgbtb'). - Highlight resuming with bulktb.vaeload('.../dgbtb_vae.pth') and the need to regenerate cells with consistent random seeds for reproducibility.
- Generate synthetic cells
- Produce filtered AnnData via bulktb.vaegenerate(leidensize=25) and inspect compositions with ov.bulk2single.bulk2singleplotcellprop(...). - Save outputs to disk for reuse (adata.write_h5ad).
- Configure and train the GNN
- Call bulktb.gnnconfigure(maxepochs=2000, userep='X', neighborrep='Xpca', gpu=0, ...) to set hyperparameters. - Train using bulktb.gnntrain(); reload checkpoints with bulktb.gnnload('savemodel/gnn.pth'). - Generate overlapping community assignments through bulktb.gnn_generate().
- Visualise community structure
- Create MDE embeddings: bulktb.nocdobj.adata.obsm['Xmde'] = mde(bulktb.nocdobj.adata.obsm['Xpca']). - Plot clusters vs. discovered communities using sc.pl.embedding(..., color=['clusters','nocdn'], palette=ov.utils.pyomicpalette()) and filtered subsets excluding synthetic labels with hyphens.
- Interpolate missing states
- Run bulktb.interpolation('OPC') (replace with target lineage) to synthesise continuity, then preprocess the interpolated AnnData (HVG selection, scaling, PCA). - Compute embeddings with mde, visualise with ov.pl.embedding, and compare to the original atlas.
- Analyse trajectories
- Initialise ov.single.pyVIA on both original and interpolated data to derive pseudotime, followed by getpseudotime, ov.pp.neighbors, ov.utils.calpaga, and ov.utils.plot_paga for topology validation.
- Defensive validation
``python # Before BulkTrajBlend: verify bulkgroup columns exist for g in bulkgroup: assert g in bulkdf.columns, f"Bulk group '{g}' not in bulk data columns" # Verify celltypekey exists in reference assert celltypekey in adata.obs.columns, f"Cell type column '{celltypekey}' not in reference AnnData" # Verify gene name overlap shared = set(bulkdf.index) & set(adata.varnames) assert len(shared) > 100, f"Only {len(shared)} shared genes — harmonize gene IDs first" ``
- Troubleshooting tips
- If the VAE collapses (high reconstruction loss), lower learningrate or reduce hiddensize. - Ensure the same generated dataset is used before calling gnntrain; regenerating cells changes the graph and can break checkpoint loading. - Sparse clusters may need adjusted celltargetnum thresholds or a smaller leidensize filter to retain rare populations.
Examples
- "Train BulkTrajBlend on PDAC cohorts, then interpolate missing OPC states in the trajectory."
- "Load saved beta-VAE and GNN weights to regenerate overlapping communities and plot cluster vs. nocd labels."
- "Run VIA on interpolated cells and compare PAGA graphs with the original scRNA-seq trajectory."
References
- Tutorial notebook: [
tbulktrajblend.ipynb](../../omicverseguide/docs/Tutorials-bulk2single/t_bulktrajblend.ipynb) - Example datasets and checkpoints: [
omicverseguide/docs/Tutorials-bulk2single/data/](../../omicverseguide/docs/Tutorials-bulk2single/data/) - Quick copy/paste commands: [
reference.md](reference.md)