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

models-atac-seq.md

111 lines · skills/scvi-tools/references/models-atac-seq.md at 5f2bc03

Open on GitHub
1# Chromatin accessibility models
2
3Targets scvi-tools 1.5.1. These source-verified examples are illustrative;
4short PEAKVI/POISSONVI CPU fits and accessibility output shapes were tested.
5Genome downloads, SCBASSET training and motif inference were not executed.
6

Chromatin accessibility models

Targets scvi-tools 1.5.1. These source-verified examples are illustrative; short PEAKVI/POISSONVI CPU fits and accessibility output shapes were tested. Genome downloads, SCBASSET training and motif inference were not executed.

Raw lines 1 to 6
7## PEAKVI and POISSONVI
8
9Use a consistent genome assembly, genomic regions and feature order across samples.
10Apply assay-specific cell QC (library size, TSS enrichment, fragment length, blacklist
11regions) and remove poorly supported peaks before setup. Do not indiscriminately
12remove sex-chromosome peaks when sex-linked biology is relevant.
14```python
15import scvi
17# atac_adata: cells by peaks; layer counts contains measured accessibility counts.
18scvi.model.PEAKVI.setup_anndata(atac_adata, layer="counts", batch_key="batch")
19peakvi = scvi.model.PEAKVI(atac_adata, n_latent=20)
20peakvi.train()
21atac_adata.obsm["X_PeakVI"] = peakvi.get_latent_representation()
22accessibility = peakvi.get_normalized_accessibility()
23da = peakvi.differential_accessibility(
24 groupby="cell_type", group1="TypeA", group2="TypeB",
25 mode="change", delta=0.05,
27```
29PEAKVI models binarized accessibility, so input counts do not make it a quantitative
30fragment model. `get_normalized_accessibility()` returns accessibility probabilities,
31optionally with cell/region normalization choices. It is not named
32`get_accessibility_estimates`. `n_hidden` and `n_latent` default to `None` and are
33inferred from feature count; encoder and decoder each default to two layers
34(`n_layers_encoder`, `n_layers_decoder`), not a single `n_layers` option.
35`get_region_factors()` exposes the learned region factors when enabled.
37```python
38# fragment_adata contains region-level fragment counts, not raw fragments.tsv rows.
39scvi.external.POISSONVI.setup_anndata(
40 fragment_adata, layer="counts", batch_key="batch",
42poissonvi = scvi.external.POISSONVI(fragment_adata)
43poissonvi.train()
44fragment_latent = poissonvi.get_latent_representation()
45fragment_estimates = poissonvi.get_normalized_accessibility()
46```
48POISSONVI retains quantitative count information. Do not confuse read counts,
49Tn5 insertions and fragments; document the counting convention and use the
50upstream `reads_to_fragments` helper only when its read-pair assumptions match the
51assay. The 1.5.1 fragment validator requires at least 100 observations; it raises on
52smaller inputs before model construction. Its count-frequency heuristic can warn
53on legitimate count distributions, so verify provenance rather than modifying counts
54to satisfy the heuristic. No universal speed or accuracy ordering between PEAKVI and POISSONVI follows
55from the API. Compare held-out fit and biological preservation.
57Accessibility DE is not RNA log fold-change: PEAKVI reports an absolute probability
58effect `scale2 - scale1`. Its `delta=0.05` denotes accessibility difference. Apply
59replicate-aware interpretation and the caveats in
60[differential-expression.md](differential-expression.md).

PEAKVI and POISSONVI

Use a consistent genome assembly, genomic regions and feature order across samples. Apply assay-specific cell QC (library size, TSS enrichment, fragment length, blacklist regions) and remove poorly supported peaks before setup. Do not indiscriminately remove sex-chromosome peaks when sex-linked biology is relevant.

import scvi

# atac_adata: cells by peaks; layer counts contains measured accessibility counts.
scvi.model.PEAKVI.setup_anndata(atac_adata, layer="counts", batch_key="batch")
peakvi = scvi.model.PEAKVI(atac_adata, n_latent=20)
peakvi.train()
atac_adata.obsm["X_PeakVI"] = peakvi.get_latent_representation()
accessibility = peakvi.get_normalized_accessibility()
da = peakvi.differential_accessibility(
    groupby="cell_type", group1="TypeA", group2="TypeB",
    mode="change", delta=0.05,
)

PEAKVI models binarized accessibility, so input counts do not make it a quantitative fragment model. get_normalized_accessibility() returns accessibility probabilities, optionally with cell/region normalization choices. It is not named get_accessibility_estimates. n_hidden and n_latent default to None and are inferred from feature count; encoder and decoder each default to two layers (n_layers_encoder, n_layers_decoder), not a single n_layers option. get_region_factors() exposes the learned region factors when enabled.

# fragment_adata contains region-level fragment counts, not raw fragments.tsv rows.
scvi.external.POISSONVI.setup_anndata(
    fragment_adata, layer="counts", batch_key="batch",
)
poissonvi = scvi.external.POISSONVI(fragment_adata)
poissonvi.train()
fragment_latent = poissonvi.get_latent_representation()
fragment_estimates = poissonvi.get_normalized_accessibility()

POISSONVI retains quantitative count information. Do not confuse read counts, Tn5 insertions and fragments; document the counting convention and use the upstream reads_to_fragments helper only when its read-pair assumptions match the assay. The 1.5.1 fragment validator requires at least 100 observations; it raises on smaller inputs before model construction. Its count-frequency heuristic can warn on legitimate count distributions, so verify provenance rather than modifying counts to satisfy the heuristic. No universal speed or accuracy ordering between PEAKVI and POISSONVI follows from the API. Compare held-out fit and biological preservation.

Accessibility DE is not RNA log fold-change: PEAKVI reports an absolute probability effect scale2 - scale1. Its delta=0.05 denotes accessibility difference. Apply replicate-aware interpretation and the caveats in differential-expression.md.

Raw lines 7 to 61
62## SCBASSET: sequence-aware accessibility
64SCBASSET has a different orientation: **regions by cells**. The DNA sequence codes
65belong to observations in `.obsm`, and batch annotations for cells belong to `.var`.
66The sequence helper first operates on ordinary cells-by-regions AnnData and writes
67to `.varm`; transposition then moves those arrays to `.obsm`.
69```python
70# Before this block: atac_adata.var has chr, start, end in the correct assembly;
71# the hg38 genome is already installed locally with genomepy.
72scvi.data.add_dna_sequence(
73 atac_adata, genome_name="hg38", install_genome=False,
75bdata = atac_adata.T.copy() # regions x cells; varm -> obsm
76scvi.external.SCBASSET.setup_anndata(
77 bdata, dna_code_key="dna_code", layer="counts", batch_key="batch",
79scbasset = scvi.external.SCBASSET(bdata, n_bottleneck_layer=32)
80scbasset.train()
81cell_embeddings = scbasset.get_latent_representation()
82# Rows correspond to bdata.var_names, which are the original cell IDs.
83```
85Install the `regseq` extra (`genomepy`, Biopython) for sequence extraction. Explicitly
86check chromosome naming, peak coordinate convention, sequence length (default
871344), bounds and genome build. The helper's default `install_genome=True` downloads
88a genome; choose it only when the download is intended.
90The constructor's cell embedding size is `n_bottleneck_layer`, not `n_latent`.
91Do not supply invented `conv_layers`/`n_filters`/`filter_size` arguments as if they
92were documented high-level constructor parameters.
94`get_tf_activity(tf, genome='human', motif_dir=...)` scores motif injection against
95background sequences and may download its motif library. A TF activity score is
96sequence-model evidence, not direct binding or causal regulatory activity. Validate
97with held-out regions/cells, motif controls and independent experiments.

SCBASSET: sequence-aware accessibility

SCBASSET has a different orientation: regions by cells. The DNA sequence codes belong to observations in .obsm, and batch annotations for cells belong to .var. The sequence helper first operates on ordinary cells-by-regions AnnData and writes to .varm; transposition then moves those arrays to .obsm.

# Before this block: atac_adata.var has chr, start, end in the correct assembly;
# the hg38 genome is already installed locally with genomepy.
scvi.data.add_dna_sequence(
    atac_adata, genome_name="hg38", install_genome=False,
)
bdata = atac_adata.T.copy()  # regions x cells; varm -> obsm
scvi.external.SCBASSET.setup_anndata(
    bdata, dna_code_key="dna_code", layer="counts", batch_key="batch",
)
scbasset = scvi.external.SCBASSET(bdata, n_bottleneck_layer=32)
scbasset.train()
cell_embeddings = scbasset.get_latent_representation()
# Rows correspond to bdata.var_names, which are the original cell IDs.

Install the regseq extra (genomepy, Biopython) for sequence extraction. Explicitly check chromosome naming, peak coordinate convention, sequence length (default 1344), bounds and genome build. The helper's default install_genome=True downloads a genome; choose it only when the download is intended.

The constructor's cell embedding size is n_bottleneck_layer, not n_latent. Do not supply invented conv_layers/n_filters/filter_size arguments as if they were documented high-level constructor parameters.

get_tf_activity(tf, genome='human', motif_dir=...) scores motif injection against background sequences and may download its motif library. A TF activity score is sequence-model evidence, not direct binding or causal regulatory activity. Validate with held-out regions/cells, motif controls and independent experiments.

Raw lines 62 to 98
99## Downstream and multimodal analysis
101Store latent embeddings on the **cell-indexed** object, construct Scanpy neighbors
102using `use_rep`, then visualize/cluster. UMAP layout does not establish integration
103quality. For paired or partially paired RNA/ATAC use MULTIVI's MuData workflow in
104[models-multimodal.md](models-multimodal.md), preserving measurement missingness and
105feature identities. Avoid dense whole-genome probability arrays when only a region
106subset is needed; use `region_list`, `indices` or a justified output threshold.
108Sources: [PEAKVI](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/model/_peakvi.py),
109[POISSONVI](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/poissonvi/_model.py),
110[SCBASSET](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/external/scbasset/_model.py),
111[sequence/count preprocessing](https://github.com/scverse/scvi-tools/blob/1.5.1/src/scvi/data/_preprocessing.py).

Downstream and multimodal analysis

Store latent embeddings on the cell-indexed object, construct Scanpy neighbors using use_rep, then visualize/cluster. UMAP layout does not establish integration quality. For paired or partially paired RNA/ATAC use MULTIVI's MuData workflow in models-multimodal.md, preserving measurement missingness and feature identities. Avoid dense whole-genome probability arrays when only a region subset is needed; use region_list, indices or a justified output threshold.

Sources: PEAKVI, POISSONVI, SCBASSET, sequence/count preprocessing.

Raw lines 99 to 111