Skill Tracer
Skills › Scientific Agent Skills › scvi-tools › models-specialized.md

models-specialized.md

137 lines · skills/scvi-tools/references/models-specialized.md at 5f2bc03

Open on GitHub
1# Methylation, cytometry, system integration and trajectories
2
3Targets scvi-tools 1.5.1. Specialized examples are illustrative and source-verified;
4see SKILL.md for the narrower set of native synthetic checks performed.
5

Methylation, cytometry, system integration and trajectories

Targets scvi-tools 1.5.1. Specialized examples are illustrative and source-verified; see SKILL.md for the narrower set of native synthetic checks performed.

Raw lines 1 to 5
6## METHYLVI and METHYLANVI
7
8Both models require **MuData**. Their `setup_anndata` methods raise
9`NotImplementedError`. Each methylation context (for example `mCG`, `mCH`) is a
10modality containing a cells-by-regions matrix with two layers: methylated cytosine
11counts and total coverage counts. Require integer `0 <= mc <= cov`, shared cell
12order and unique region IDs. Zero coverage means no observation, not an observed
13unmethylated region; methylation ratios cannot replace the two count arrays.
15```python
16import scvi
17from mudata import MuData
19# mcg_adata.layers['mc'] and ['cov'] have already been validated.
20mdata = MuData({"mCG": mcg_adata})
21mdata.obs["batch"] = mcg_adata.obs["batch"].reindex(mdata.obs_names)
22scvi.external.METHYLVI.setup_mudata(
23 mdata, mc_layer="mc", cov_layer="cov",
24 methylation_contexts=["mCG"], batch_key="batch",
26methylvi = scvi.external.METHYLVI(mdata)
27methylvi.train()
28mdata.obsm["X_MethylVI"] = methylvi.get_latent_representation()
29methylation_by_context = methylvi.get_normalized_methylation()
30mcg_estimates = methylvi.get_normalized_methylation(context="mCG")
31```
33The first methylation result is a dictionary keyed by context; specifying `context`
34returns that context's array/DataFrame. Region/coverage QC must use coverage,
35not the number of methylated counts, or it preferentially removes hypomethylated
36regions. Default likelihood is beta-binomial; binomial is also supported. The
37relevant dispersion setting is `region`/`region-cell`, not `region_factors`.
39```python
40# Use a separate prepared MuData object for the semi-supervised example.
41scvi.external.METHYLANVI.setup_mudata(
42 labeled_mdata, mc_layer="mc", cov_layer="cov",
43 methylation_contexts=["mCG"], labels_key="cell_type",
44 unlabeled_category="Unknown", batch_key="batch",
46methylanvi = scvi.external.METHYLANVI(labeled_mdata)
47methylanvi.train()
48labels = methylanvi.predict()
49probabilities = methylanvi.predict(soft=True)
50```
52Put label/batch columns in global `mdata.obs`, or explicitly map those arguments
53to the modality holding them through `modalities`. For a fitted methylation model,
54`differential_methylation(groupby=..., group1=..., group2=..., mode='change')`
55returns context-specific results and uses methylation differences, not RNA LFCs.
56Annotation and region tests require independent validation and biological replicates.

METHYLVI and METHYLANVI

Both models require MuData. Their setup_anndata methods raise NotImplementedError. Each methylation context (for example mCG, mCH) is a modality containing a cells-by-regions matrix with two layers: methylated cytosine counts and total coverage counts. Require integer 0 <= mc <= cov, shared cell order and unique region IDs. Zero coverage means no observation, not an observed unmethylated region; methylation ratios cannot replace the two count arrays.

import scvi
from mudata import MuData

# mcg_adata.layers['mc'] and ['cov'] have already been validated.
mdata = MuData({"mCG": mcg_adata})
mdata.obs["batch"] = mcg_adata.obs["batch"].reindex(mdata.obs_names)
scvi.external.METHYLVI.setup_mudata(
    mdata, mc_layer="mc", cov_layer="cov",
    methylation_contexts=["mCG"], batch_key="batch",
)
methylvi = scvi.external.METHYLVI(mdata)
methylvi.train()
mdata.obsm["X_MethylVI"] = methylvi.get_latent_representation()
methylation_by_context = methylvi.get_normalized_methylation()
mcg_estimates = methylvi.get_normalized_methylation(context="mCG")

The first methylation result is a dictionary keyed by context; specifying context returns that context's array/DataFrame. Region/coverage QC must use coverage, not the number of methylated counts, or it preferentially removes hypomethylated regions. Default likelihood is beta-binomial; binomial is also supported. The relevant dispersion setting is region/region-cell, not region_factors.

# Use a separate prepared MuData object for the semi-supervised example.
scvi.external.METHYLANVI.setup_mudata(
    labeled_mdata, mc_layer="mc", cov_layer="cov",
    methylation_contexts=["mCG"], labels_key="cell_type",
    unlabeled_category="Unknown", batch_key="batch",
)
methylanvi = scvi.external.METHYLANVI(labeled_mdata)
methylanvi.train()
labels = methylanvi.predict()
probabilities = methylanvi.predict(soft=True)

Put label/batch columns in global mdata.obs, or explicitly map those arguments to the modality holding them through modalities. For a fitted methylation model, differential_methylation(groupby=..., group1=..., group2=..., mode='change') returns context-specific results and uses methylation differences, not RNA LFCs. Annotation and region tests require independent validation and biological replicates.

Raw lines 6 to 57
58## CYTOVI
60CYTOVI registers transformed cytometry intensities from `.X` or `layer`, not a
61TOTALVI-style `protein_expression_obsm_key`. Apply compensation/unmixing, instrument
62QC and an assay-appropriate transform (for example arcsinh with a recorded cofactor)
63before modeling. Default normal likelihood accepts transformed continuous values;
64beta likelihood requires suitable bounded values and care at distribution boundaries.
66```python
67scvi.external.CYTOVI.setup_anndata(
68 cyto_adata, layer="transformed", batch_key="batch", sample_key="sample",
70cyto = scvi.external.CYTOVI(cyto_adata, protein_likelihood="normal")
71cyto.train()
72cyto_adata.obsm["X_CytoVI"] = cyto.get_latent_representation()
73corrected_intensities = cyto.get_normalized_expression()
74```
76Partially overlapping panels need measurement masks, shared markers and appropriate
77controls. Use the documented `scvi.external.cytovi` preprocessing utilities;
78do not treat missing panel markers as measured zeros. Version 1.5.1 fixes a beta
79likelihood NaN issue for merged masked panels. Assess retained biological signal
80and agreement in control samples, not batch mixing alone.

CYTOVI

CYTOVI registers transformed cytometry intensities from .X or layer, not a TOTALVI-style protein_expression_obsm_key. Apply compensation/unmixing, instrument QC and an assay-appropriate transform (for example arcsinh with a recorded cofactor) before modeling. Default normal likelihood accepts transformed continuous values; beta likelihood requires suitable bounded values and care at distribution boundaries.

scvi.external.CYTOVI.setup_anndata(
    cyto_adata, layer="transformed", batch_key="batch", sample_key="sample",
)
cyto = scvi.external.CYTOVI(cyto_adata, protein_likelihood="normal")
cyto.train()
cyto_adata.obsm["X_CytoVI"] = cyto.get_latent_representation()
corrected_intensities = cyto.get_normalized_expression()

Partially overlapping panels need measurement masks, shared markers and appropriate controls. Use the documented scvi.external.cytovi preprocessing utilities; do not treat missing panel markers as measured zeros. Version 1.5.1 fixes a beta likelihood NaN issue for merged masked panels. Assess retained biological signal and agreement in control samples, not batch mixing alone.

Raw lines 58 to 81
82## SysVI
84SysVI is intended for substantial cross-system effects (for example protocol or
85model-system differences). It models approximately normal inputs, commonly
86library-normalized and log1p-transformed RNA, rather than raw RNA counts.
88```python
89import scanpy as sc
91# sys_adata starts with measured counts; preserve before transforming.
92sys_adata.layers["counts"] = sys_adata.X.copy()
93sc.pp.normalize_total(sys_adata, target_sum=1e4)
94sc.pp.log1p(sys_adata)
95sys_adata.layers["log_normalized"] = sys_adata.X.copy()
96scvi.external.SysVI.setup_anndata(
97 sys_adata, layer="log_normalized", batch_key="system",
99sysvi = scvi.external.SysVI(sys_adata)
100sysvi.train()
101sys_adata.obsm["X_SysVI"] = sysvi.get_latent_representation()
102```
104Tune cycle-consistency strength with biological-preservation checks and seeds;
105more correction is not always better. A system covariate is treated differently
106from auxiliary covariates. Gene orthology and shared feature definitions require
107explicit decisions for cross-species data. SysVI is not a guarantee that confounded
108or nonoverlapping biological states can be aligned safely.

SysVI

SysVI is intended for substantial cross-system effects (for example protocol or model-system differences). It models approximately normal inputs, commonly library-normalized and log1p-transformed RNA, rather than raw RNA counts.

import scanpy as sc

# sys_adata starts with measured counts; preserve before transforming.
sys_adata.layers["counts"] = sys_adata.X.copy()
sc.pp.normalize_total(sys_adata, target_sum=1e4)
sc.pp.log1p(sys_adata)
sys_adata.layers["log_normalized"] = sys_adata.X.copy()
scvi.external.SysVI.setup_anndata(
    sys_adata, layer="log_normalized", batch_key="system",
)
sysvi = scvi.external.SysVI(sys_adata)
sysvi.train()
sys_adata.obsm["X_SysVI"] = sysvi.get_latent_representation()

Tune cycle-consistency strength with biological-preservation checks and seeds; more correction is not always better. A system covariate is treated differently from auxiliary covariates. Gene orthology and shared feature definitions require explicit decisions for cross-species data. SysVI is not a guarantee that confounded or nonoverlapping biological states can be aligned safely.

Raw lines 82 to 109
110## Decipher
112```python
113scvi.external.Decipher.setup_anndata(adata, layer="counts")
114decipher = scvi.external.Decipher(adata)
115decipher.train()
116adata.obsm["X_decipher"] = decipher.get_latent_representation()
117```
119Decipher learns its own low-dimensional representation from counts. It does not
120consume an SCVI embedding as the same input, and fitting alone does not return
121validated pseudotime. Its trajectory utilities require a specified trajectory
122and cluster annotation; evaluate alternative roots/branches and independent time
123or lineage evidence. UMAP on its latent representation is a visualization, not a
124trajectory inference validation.
126SOLO, CellAssign and AmortizedLDA are covered in
127[models-scrna-seq.md](models-scrna-seq.md); reference mapping is in
128[workflows.md](workflows.md). Separate models' latent coordinates are not directly
129comparable across assays without a justified correspondence or integration method.
131Sources: [METHYLVI](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/methylvi/_methylvi_model.py),
132[METHYLANVI](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/methylvi/_methylanvi_model.py),
133[methylation outputs](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/methylvi/_base_components.py),
134[CYTOVI](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/cytovi/_model.py),
135[cytometry assumptions](https://github.com/scverse/scvi-tools/blob/1.5.1/docs/user_guide/models/cytovi.md),
136[SysVI](https://github.com/scverse/scvi-tools/blob/1.5.1/docs/user_guide/models/sysvi.md),
137[Decipher](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/decipher/_model.py).

Decipher

scvi.external.Decipher.setup_anndata(adata, layer="counts")
decipher = scvi.external.Decipher(adata)
decipher.train()
adata.obsm["X_decipher"] = decipher.get_latent_representation()

Decipher learns its own low-dimensional representation from counts. It does not consume an SCVI embedding as the same input, and fitting alone does not return validated pseudotime. Its trajectory utilities require a specified trajectory and cluster annotation; evaluate alternative roots/branches and independent time or lineage evidence. UMAP on its latent representation is a visualization, not a trajectory inference validation.

SOLO, CellAssign and AmortizedLDA are covered in models-scrna-seq.md; reference mapping is in workflows.md. Separate models' latent coordinates are not directly comparable across assays without a justified correspondence or integration method.

Sources: METHYLVI, METHYLANVI, methylation outputs, CYTOVI, cytometry assumptions, SysVI, Decipher.

Raw lines 110 to 137