OmicVerse Reference Label Transfer
Goal
Map labels from a reference AnnData onto a query AnnData by building a shared integrated space, training a weighted kNN transfer model, and writing predicted labels plus uncertainty scores back to the query object.
Treat harmony, scVI, and scanorama as alternative integration branches. They share the same query/reference contract, so keep them in one skill rather than splitting into thin wrappers.
Quick Workflow
- Start from a query
AnnData and a reference AnnData that share gene names.
- Make sure the reference has a cell-type column such as
celltype in adata_ref.obs.
- If the data are already log-normalized, recover or restore counts before concatenating.
- Construct
ov.single.AnnotationRef(adata_query, adata_ref, celltype_key=...).
- Run
preprocess(mode=...) on the combined object.
- Choose one integration backend with
train(method=...).
- Transfer labels with
predict(method=...).
- Validate the method-specific prediction keys before plotting or writing outputs.
Interface Summary
AnnotationRef(adata_query, adata_ref, celltype_key='celltype') checks shared genes, concatenates query and reference, and stores the integrated object on self.adata_new.
AnnotationRef.preprocess(mode='shiftlog|pearson', n_HVGs=3000, batch_key='integrate_batch') prepares the concatenated object for transfer.
AnnotationRef.train(method='harmony', **kwargs) computes the integrated latent space used for transfer.
AnnotationRef.predict(method='harmony', n_neighbors=15, pred_key=None, uncert_key=None) transfers labels with weighted kNN.
- The current source also exposes
batch_correction(..., methods='harmony') with additional methods such as combat, scanorama, scVI, CellANOVA, and Concord; the notebook uses only the harmony and scVI transfer path.
Read references/source-grounding.md before documenting narrower parameter behavior than the live source supports.
Branch Selection
- Use
harmony when you want the notebook's default transfer path and a fast, interpretable integration backend.
- Use
scVI when you want the scVI latent-space branch and the environment has scvi-tools.
- Use
scanorama when you explicitly want Scanorama integration and its dependencies are available.
- Keep
harmony as the default unless the user asks for a different integration backend.
- Keep
mode='shiftlog|pearson' as the default preprocessing choice unless the user wants a different normalization/HVG strategy.
Input Contract
- Query and reference must share feature names before
AnnotationRef can concatenate them.
- The reference must carry a cell-type label column in
adata_ref.obs.
- If the notebook reference was stored with a
feature_name column, align adata_ref.var_names first.
- If
adata_query.X.max() or adata_ref.X.max() suggests the data are already log-normalized, recover counts before transfer.
AnnotationRef.preprocess(...) uses the combined query/reference object, so treat the pair as one integrated preprocessing unit.
Minimal Execution Patterns
For harmony-based transfer:
import omicverse as ov
ref_anno = ov.single.AnnotationRef(adata_query, adata_ref, celltype_key="celltype")
ref_anno.preprocess(mode="shiftlog|pearson", n_HVGs=3000, batch_key="integrate_batch")
ref_anno.train(method="harmony", n_pcs=50)
ad_pred = ref_anno.predict(method="harmony", n_neighbors=15)
ov.pp.mde(ad_pred, use_rep="X_pca_harmony_anno")
ov.pl.embedding(ad_pred, basis="X_mde", color="harmony_prediction")
For scVI-based transfer:
import omicverse as ov
ref_anno = ov.single.AnnotationRef(adata_query, adata_ref, celltype_key="celltype")
ref_anno.preprocess(mode="shiftlog|pearson", n_HVGs=3000, batch_key="integrate_batch")
ref_anno.train(method="scVI", n_layers=2, n_latent=30, gene_likelihood="nb")
ad_pred = ref_anno.predict(method="scVI", n_neighbors=15)
ov.pl.embedding(ad_pred, basis="X_mde", color="scVI_prediction")
For prompt-driven label transfer around the reference selection step, keep the notebook's reference selection logic separate from the transfer logic; this skill starts once you already have the paired query/reference objects.
Validation
After preprocess(...), check:
self.adata_new has integrate_batch in obs
self.adata_new.obsm["X_pca"] exists
self.adata_new.var["highly_variable_features"] exists
After train(method="harmony"), check:
self.adata_query.obsm["X_pca_harmony_anno"] exists
self.adata_ref.obsm["X_pca_harmony_anno"] exists
After train(method="scVI"), check:
self.adata_query.obsm["X_scVI_anno"] exists
self.adata_ref.obsm["X_scVI_anno"] exists
After train(method="scanorama"), check:
self.adata_query.obsm["X_scanorama_anno"] exists
self.adata_ref.obsm["X_scanorama_anno"] exists
After predict(...), check:
harmony_prediction and harmony_uncertainty for the harmony branch
scVI_prediction and scVI_uncertainty for the scVI branch
scanorama_prediction and scanorama_uncertainty for the Scanorama branch
Before any plotting, check:
- the prediction columns are present in
ad_pred.obs
- the integrated embedding key you pass to
ov.pp.mde(...) exists
Constraints
- Do not bake notebook-specific download URLs, file names, or absolute paths into the reusable workflow.
- Do not assume the reference dataset is already in the correct gene-name convention; align feature names first when needed.
- Do not treat
scVI as free: it depends on scvi-tools and can be materially heavier than the harmony path.
- Do not collapse the notebook into one monolithic "load data and run" step; the reusable spine is query/reference construction, preprocessing, backend selection, prediction, and validation.
- Treat
scanorama as a source-present branch even though the notebook focuses on harmony and scVI.
Resource Map
- Read
references/integration-method-selection.md when choosing between harmony, scVI, and scanorama.
- Read
references/source-notebook-map.md to trace notebook sections into this reusable skill.
- Read
references/source-grounding.md for inspected signatures, source-level branch behavior, and compatibility notes.
- Read
references/compatibility.md when notebook prose, input assumptions, or current runtime behavior diverge.
1---2name: omicverse-reference-label-transfer3description: Transfer cell labels from a reference AnnData to a query AnnData with OmicVerse AnnotationRef. Use when converting OmicVerse reference-annotation notebooks into a reusable, triggerable skill, choosing a label-transfer backend such as harmony, scVI, or scanorama, or preparing paired query/reference single-cell data for weighted kNN annotation.4---56# OmicVerse Reference Label Transfer78## Goal910Map labels from a reference `AnnData` onto a query `AnnData` by building a shared integrated space, training a weighted kNN transfer model, and writing predicted labels plus uncertainty scores back to the query object.1112Treat `harmony`, `scVI`, and `scanorama` as alternative integration branches. They share the same query/reference contract, so keep them in one skill rather than splitting into thin wrappers.1314## Quick Workflow15161. Start from a query `AnnData` and a reference `AnnData` that share gene names.172. Make sure the reference has a cell-type column such as `celltype` in `adata_ref.obs`.183. If the data are already log-normalized, recover or restore counts before concatenating.194. Construct `ov.single.AnnotationRef(adata_query, adata_ref, celltype_key=...)`.205. Run `preprocess(mode=...)` on the combined object.216. Choose one integration backend with `train(method=...)`.227. Transfer labels with `predict(method=...)`.238. Validate the method-specific prediction keys before plotting or writing outputs.2425## Interface Summary2627- `AnnotationRef(adata_query, adata_ref, celltype_key='celltype')` checks shared genes, concatenates query and reference, and stores the integrated object on `self.adata_new`.28- `AnnotationRef.preprocess(mode='shiftlog|pearson', n_HVGs=3000, batch_key='integrate_batch')` prepares the concatenated object for transfer.29- `AnnotationRef.train(method='harmony', **kwargs)` computes the integrated latent space used for transfer.30- `AnnotationRef.predict(method='harmony', n_neighbors=15, pred_key=None, uncert_key=None)` transfers labels with weighted kNN.31- The current source also exposes `batch_correction(..., methods='harmony')` with additional methods such as `combat`, `scanorama`, `scVI`, `CellANOVA`, and `Concord`; the notebook uses only the `harmony` and `scVI` transfer path.3233Read `references/source-grounding.md` before documenting narrower parameter behavior than the live source supports.3435## Branch Selection3637- Use `harmony` when you want the notebook's default transfer path and a fast, interpretable integration backend.38- Use `scVI` when you want the scVI latent-space branch and the environment has `scvi-tools`.39- Use `scanorama` when you explicitly want Scanorama integration and its dependencies are available.40- Keep `harmony` as the default unless the user asks for a different integration backend.41- Keep `mode='shiftlog|pearson'` as the default preprocessing choice unless the user wants a different normalization/HVG strategy.4243## Input Contract4445- Query and reference must share feature names before `AnnotationRef` can concatenate them.46- The reference must carry a cell-type label column in `adata_ref.obs`.47- If the notebook reference was stored with a `feature_name` column, align `adata_ref.var_names` first.48- If `adata_query.X.max()` or `adata_ref.X.max()` suggests the data are already log-normalized, recover counts before transfer.49- `AnnotationRef.preprocess(...)` uses the combined query/reference object, so treat the pair as one integrated preprocessing unit.5051## Minimal Execution Patterns5253For harmony-based transfer:5455```python56import omicverse as ov5758ref_anno = ov.single.AnnotationRef(adata_query, adata_ref, celltype_key="celltype")59ref_anno.preprocess(mode="shiftlog|pearson", n_HVGs=3000, batch_key="integrate_batch")60ref_anno.train(method="harmony", n_pcs=50)61ad_pred = ref_anno.predict(method="harmony", n_neighbors=15)62ov.pp.mde(ad_pred, use_rep="X_pca_harmony_anno")63ov.pl.embedding(ad_pred, basis="X_mde", color="harmony_prediction")64```6566For scVI-based transfer:6768```python69import omicverse as ov7071ref_anno = ov.single.AnnotationRef(adata_query, adata_ref, celltype_key="celltype")72ref_anno.preprocess(mode="shiftlog|pearson", n_HVGs=3000, batch_key="integrate_batch")73ref_anno.train(method="scVI", n_layers=2, n_latent=30, gene_likelihood="nb")74ad_pred = ref_anno.predict(method="scVI", n_neighbors=15)75ov.pl.embedding(ad_pred, basis="X_mde", color="scVI_prediction")76```7778For prompt-driven label transfer around the reference selection step, keep the notebook's reference selection logic separate from the transfer logic; this skill starts once you already have the paired query/reference objects.7980## Validation8182After `preprocess(...)`, check:8384- `self.adata_new` has `integrate_batch` in `obs`85- `self.adata_new.obsm["X_pca"]` exists86- `self.adata_new.var["highly_variable_features"]` exists8788After `train(method="harmony")`, check:8990- `self.adata_query.obsm["X_pca_harmony_anno"]` exists91- `self.adata_ref.obsm["X_pca_harmony_anno"]` exists9293After `train(method="scVI")`, check:9495- `self.adata_query.obsm["X_scVI_anno"]` exists96- `self.adata_ref.obsm["X_scVI_anno"]` exists9798After `train(method="scanorama")`, check:99100- `self.adata_query.obsm["X_scanorama_anno"]` exists101- `self.adata_ref.obsm["X_scanorama_anno"]` exists102103After `predict(...)`, check:104105- `harmony_prediction` and `harmony_uncertainty` for the harmony branch106- `scVI_prediction` and `scVI_uncertainty` for the scVI branch107- `scanorama_prediction` and `scanorama_uncertainty` for the Scanorama branch108109Before any plotting, check:110111- the prediction columns are present in `ad_pred.obs`112- the integrated embedding key you pass to `ov.pp.mde(...)` exists113114## Constraints115116- Do not bake notebook-specific download URLs, file names, or absolute paths into the reusable workflow.117- Do not assume the reference dataset is already in the correct gene-name convention; align feature names first when needed.118- Do not treat `scVI` as free: it depends on `scvi-tools` and can be materially heavier than the harmony path.119- Do not collapse the notebook into one monolithic "load data and run" step; the reusable spine is query/reference construction, preprocessing, backend selection, prediction, and validation.120- Treat `scanorama` as a source-present branch even though the notebook focuses on harmony and scVI.121122## Resource Map123124- Read `references/integration-method-selection.md` when choosing between `harmony`, `scVI`, and `scanorama`.125- Read `references/source-notebook-map.md` to trace notebook sections into this reusable skill.126- Read `references/source-grounding.md` for inspected signatures, source-level branch behavior, and compatibility notes.127- Read `references/compatibility.md` when notebook prose, input assumptions, or current runtime behavior diverge.