﻿# scVI reference mapping: query genes must match the saved feature registry

Keep scVI reference feature order, missing-gene padding, count-layer provenance, categorical registry, and query adaptation distinct from ordinary inference.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-scvi-query-reference-feature-order-registry/
Language: en

## The same gene set can arrive in a different order

A query dataset may contain the reference genes in another column order or lack some features entirely. Passing its matrix directly to an scVI reference model can therefore change the meaning of input dimensions. Equal width does not establish that columns describe the expected genes.

The [SCVI API reference](https://docs.scvi-tools.org/en/stable/api/reference/scvi.model.SCVI.html) documents query preparation against the saved reference. Preserve that feature contract alongside the trained parameters.

## Query preparation handles a defined alignment

The documented `prepare_query_anndata` path sorts features to match the reference and pads missing features with zeros. This is a concrete preparation rule, not evidence that an absent measurement was experimentally observed as zero.

Retain the original measured-gene inventory and a preparation report. A later analyst should be able to distinguish source zeros from model-compatible padding. If missing features are numerous, scientific suitability requires separate evaluation rather than being inferred from successful shape alignment.

## The registry contains more than gene names

The [reference-mapping tutorial](https://docs.scvi-tools.org/en/stable/tutorials/notebooks/multimodal/scarches_scvi_tools.html) explains the reference/query workflow. Matrix layer selection and categorical information must remain consistent with the saved setup.

Do not recreate batch categories by independently assigning arbitrary integer codes. Those codes need their relationship to the reference registry. A familiar category string and a matching numeric dtype are not substitutes for preserving the model setup that gives them meaning.

## Adaptation differs from a forward pass

The [query-loading implementation](https://raw.githubusercontent.com/scverse/scvi-tools/main/src/scvi/model/base/_archesmixin.py) supports a reference-update workflow with explicit freezing options. Loading and training query adaptation is not equivalent to merely encoding data through an unchanged model.

Label adapted models as new artifacts and record their reference, query preparation, and training configuration. The [model-update guide](/journal/model-update-canary/) provides context for comparing changed model behavior rather than attributing every difference to preprocessing alone.

## Verify correspondence before biological conclusions

Inspect reference feature names, prepared query order, missing-feature padding, and registry categories on a small example. Keep cell identities aligned through any filtering or reordering before joining embeddings to annotations.

The [artifact validation guide](/journal/safetensors-artifact-validation/) motivates preserving this complete bundle. Correct reference mapping establishes computational compatibility; a latent embedding or integrated visualization does not itself prove the biological equivalence of cells, elimination of every batch effect, or clinical validity of downstream labels.
