OmicVerse Cross-Modal Cell Type Transfer
Goal
Transfer cell labels from a reference AnnData to a query AnnData using a shared latent space such as X_glue, then write predicted labels and uncertainty scores back to the query object. Keep this as one skill: the notebook's plots are optional presentation on top of the same transfer job, not a separate capability.
Quick Workflow
- Confirm the query and reference expose the same embedding basis in
.obsm, or fall back to X.
- Confirm the reference has the target label column in
.obs.
- Train
ov.utils.weighted_knn_trainer(...) on the reference embedding.
- Transfer labels with
ov.utils.weighted_knn_transfer(...).
- Store the first label column and uncertainty column in
query_adata.obs.
- Plot on an existing basis for smoke validation; use
mde only if pymde is available.
Interface Summary
weighted_knn_trainer(train_adata, train_adata_emb, n_neighbors=50) builds the reference KNN model.
weighted_knn_transfer(query_adata, query_adata_emb, ref_adata_obs, label_keys, knn_model, threshold=1, pred_unknown=False, mode='package') returns prediction and uncertainty DataFrames.
embedding(adata, basis, ..., show=None, save=None, return_fig=None, ...) plots any existing basis.
mde(data, device=None, **kwargs) is optional; it depends on pymde and is only for visualization.
Branch Selection
- Use
mode='package' for transfer. The current source only implements this branch; mode='paper' is documented in the docstring but raises in the live code.
- Leave
pred_unknown=False for the default label-assignment path.
- Set
pred_unknown=True only when you want threshold-based Unknown labels.
- Use
mde only as an optional visual branch. If pymde is missing, skip it and plot on X_glue or another precomputed basis instead.
Input Contract
train_adata and query_adata must expose the same latent basis name in .obsm, or use X.
ref_adata_obs[label_keys] must exist; the function selects columns whose names start with label_keys.
- If you want one label column, make the prefix unique.
- The notebook's
transf_celltype and transf_celltype_unc are the reuse convention for storing outputs on the query object.
Minimal Execution Patterns
Core transfer:
knn = ov.utils.weighted_knn_trainer(train_adata=rna, train_adata_emb="X_glue", n_neighbors=15)
labels, uncert = ov.utils.weighted_knn_transfer(
query_adata=atac,
query_adata_emb="X_glue",
ref_adata_obs=rna.obs,
label_keys="major_celltype",
knn_model=knn,
mode="package",
)
atac.obs["transf_celltype"] = labels.loc[atac.obs.index, "major_celltype"]
atac.obs["transf_celltype_unc"] = uncert.loc[atac.obs.index, "major_celltype"]
Optional smoke plot:
ov.utils.embedding(
atac,
basis="X_glue",
color="transf_celltype",
show=False,
)
Validation
labels and uncert must both be DataFrames indexed by query_adata.obs_names.
- The expected label column should appear in both outputs.
query_adata.obs["transf_celltype"] and query_adata.obs["transf_celltype_unc"] should exist after writeback.
- If you call
mde, verify pymde is installed first; otherwise skip it.
- For plots, confirm the basis you pass actually exists in
.obsm.
Resource Map
- Read
references/integration-method-selection.md for skill boundary and branch selection.
- Read
references/source-notebook-map.md to map notebook cells to the reusable workflow.
- Read
references/source-grounding.md for inspected signatures and source behavior.
- Read
references/compatibility.md for known runtime caveats.
1---2name: omicverse-cross-modal-celltype-transfer3description: Transfer cell-type labels from a reference AnnData to a query AnnData with OmicVerse weighted KNN over a shared embedding. Use when converting OmicVerse cross-modal annotation notebooks into a reusable skill, when labeling an ATAC query from an RNA reference, or when you need the weighted_knn_trainer / weighted_knn_transfer workflow plus optional visualization.4---56# OmicVerse Cross-Modal Cell Type Transfer78## Goal910Transfer cell labels from a reference `AnnData` to a query `AnnData` using a shared latent space such as `X_glue`, then write predicted labels and uncertainty scores back to the query object. Keep this as one skill: the notebook's plots are optional presentation on top of the same transfer job, not a separate capability.1112## Quick Workflow13141. Confirm the query and reference expose the same embedding basis in `.obsm`, or fall back to `X`.152. Confirm the reference has the target label column in `.obs`.163. Train `ov.utils.weighted_knn_trainer(...)` on the reference embedding.174. Transfer labels with `ov.utils.weighted_knn_transfer(...)`.185. Store the first label column and uncertainty column in `query_adata.obs`.196. Plot on an existing basis for smoke validation; use `mde` only if `pymde` is available.2021## Interface Summary2223- `weighted_knn_trainer(train_adata, train_adata_emb, n_neighbors=50)` builds the reference KNN model.24- `weighted_knn_transfer(query_adata, query_adata_emb, ref_adata_obs, label_keys, knn_model, threshold=1, pred_unknown=False, mode='package')` returns prediction and uncertainty DataFrames.25- `embedding(adata, basis, ..., show=None, save=None, return_fig=None, ...)` plots any existing basis.26- `mde(data, device=None, **kwargs)` is optional; it depends on `pymde` and is only for visualization.2728## Branch Selection2930- Use `mode='package'` for transfer. The current source only implements this branch; `mode='paper'` is documented in the docstring but raises in the live code.31- Leave `pred_unknown=False` for the default label-assignment path.32- Set `pred_unknown=True` only when you want threshold-based `Unknown` labels.33- Use `mde` only as an optional visual branch. If `pymde` is missing, skip it and plot on `X_glue` or another precomputed basis instead.3435## Input Contract3637- `train_adata` and `query_adata` must expose the same latent basis name in `.obsm`, or use `X`.38- `ref_adata_obs[label_keys]` must exist; the function selects columns whose names start with `label_keys`.39- If you want one label column, make the prefix unique.40- The notebook's `transf_celltype` and `transf_celltype_unc` are the reuse convention for storing outputs on the query object.4142## Minimal Execution Patterns4344Core transfer:4546```python47knn = ov.utils.weighted_knn_trainer(train_adata=rna, train_adata_emb="X_glue", n_neighbors=15)48labels, uncert = ov.utils.weighted_knn_transfer(49 query_adata=atac,50 query_adata_emb="X_glue",51 ref_adata_obs=rna.obs,52 label_keys="major_celltype",53 knn_model=knn,54 mode="package",55)56atac.obs["transf_celltype"] = labels.loc[atac.obs.index, "major_celltype"]57atac.obs["transf_celltype_unc"] = uncert.loc[atac.obs.index, "major_celltype"]58```5960Optional smoke plot:6162```python63ov.utils.embedding(64 atac,65 basis="X_glue",66 color="transf_celltype",67 show=False,68)69```7071## Validation7273- `labels` and `uncert` must both be DataFrames indexed by `query_adata.obs_names`.74- The expected label column should appear in both outputs.75- `query_adata.obs["transf_celltype"]` and `query_adata.obs["transf_celltype_unc"]` should exist after writeback.76- If you call `mde`, verify `pymde` is installed first; otherwise skip it.77- For plots, confirm the basis you pass actually exists in `.obsm`.7879## Resource Map8081- Read `references/integration-method-selection.md` for skill boundary and branch selection.82- Read `references/source-notebook-map.md` to map notebook cells to the reusable workflow.83- Read `references/source-grounding.md` for inspected signatures and source behavior.84- Read `references/compatibility.md` for known runtime caveats.