This page was generated from data_loading.ipynb. Interactive online version: Colab badge. Some tutorial content may look better in light mode.

Data loading and preparation#

Here we walk through the necessary steps to get your data into ready for scvi-tools.

!pip install --quiet scvi-colab
from scvi_colab import install
import scvi
import scanpy as sc
Global seed set to 0
/Users/jhong/miniforge3/envs/scvi-env/lib/python3.9/site-packages/docrep/ SyntaxWarning: 'param_size_factor_key' is not a valid key!
  doc = func(self, args[0].__doc__, *args[1:], **kwargs)

Loading data#

scvi-tools supports the AnnData data format, which also underlies Scanpy. AnnData is quite similar to other popular single cell objects like that of Seurat and SingleCellExperiment. In particular, it allows cell-level and feature-level metadata to coexist in the same data structure as the molecular counts.

It’s also now possible to automatically convert these R-based objects to AnnData within a Jupyter notebook. See the following tutorial for more information.

scvi-tools has a number of convenience methods for loading data from .csv, .loom, and .h5ad formats. To load outputs from Cell Ranger, please use Scanpy’s reading functionality.

Let us now download an AnnData object (.h5ad format) and load it using scvi-tools.


!wget ''
--2022-02-18 12:12:58--
Resolving (
Connecting to (||:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: [following]
--2022-02-18 12:12:58--
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: [following]
--2022-02-18 12:12:59--
Resolving (
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5855727 (5.6M) [application/octet-stream]
Saving to: ‘pbmc3k_raw.h5ad’

pbmc3k_raw.h5ad     100%[===================>]   5.58M  10.6MB/s    in 0.5s

2022-02-18 12:13:00 (10.6 MB/s) - ‘pbmc3k_raw.h5ad’ saved [5855727/5855727]

pbmc3k ="pbmc3k_raw.h5ad")
AnnData object with n_obs × n_vars = 2700 × 32738
    var: 'gene_ids'

This is a fairly simple object, it just contains the count data and the ENSEMBL ids for the genes.

MIR1302-10 ENSG00000243485
FAM138A ENSG00000237613
OR4F5 ENSG00000186092
RP11-34P13.7 ENSG00000238009
RP11-34P13.8 ENSG00000239945


As another example, let’s download a dataset from 10x Genomics. This data was obtained from a CITE-seq experiment, so it also contains protein count data.

--2022-02-18 12:13:01--
Resolving (,
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17129253 (16M) [binary/octet-stream]
Saving to: ‘5k_pbmc_protein_v3_filtered_feature_bc_matrix.h5’

5k_pbmc_protein_v3_ 100%[===================>]  16.33M  9.91MB/s    in 1.6s

2022-02-18 12:13:03 (9.91 MB/s) - ‘5k_pbmc_protein_v3_filtered_feature_bc_matrix.h5’ saved [17129253/17129253]

pbmc5k = sc.read_10x_h5(
Variable names are not unique. To make them unique, call `.var_names_make_unique`.

It’s often helpful to give the gene names unique names.


We can see that adata.X contains the concatenated gene and protein expression data.

Index(['Antibody Capture', 'Gene Expression'], dtype='object')

We can use scvi-tools to organize this object, which places the protein expression in adata.obms["protein_expression].

AnnData object with n_obs × n_vars = 5247 × 33538
    var: 'gene_ids', 'feature_types', 'genome'
    obsm: 'protein_expression'

Concatenate the datasets#

adata = pbmc5k.concatenate(pbmc3k)

Notice that the resulting AnnData has a batch key in .obs.


Preprocessing the data#

It is common to remove outliers, and even perform feature selection before model fitting. We prefer the Scanpy preprocessing module at this stage.

sc.pp.filter_genes(adata, min_counts=3)
sc.pp.filter_cells(adata, min_counts=3)

As it is popular to normalize the data for many methods, we can use Scanpy for this; however, it’s important to keep the count information intact for scvi-tools models.

adata.layers["counts"] = adata.X.copy()

Now we can proceed with common normalization methods.

sc.pp.normalize_total(adata, target_sum=1e4)

We can store the normalized values in .raw to keep them safe in the event the anndata gets subsetted feature-wise.

adata.raw = adata

Register the data with scvi-tools#

Now that we have an AnnData object, we need to alert scvi-tools of all the interesting data in our object. For example, now that we have batches in our AnnData, we can alert the models that we’d like to perform batch correction. Also, because we have the count data in a layer, we can use the layer argument.

Normally, we set up the data right before using a model, thus we would call the setup_anndata method specific to that model. However, we are not using any particular model here since we are just demonstrating data usage and handling in this tutorial. We will use the SCVI model’s setup_anndata method here and in what follows for sake of example.

Basic case#

scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="batch")

Notice the info messages notify us that batches were detected in the data. Just to demonstrate what happens if we don’t include this option:

scvi.model.SCVI.setup_anndata(adata, layer="counts")

Now integration-based tasks can no longer be performed in subsequent models because there is no knowledge of such information.

CITE-seq case#

As PBMC5k is a CITE-seq dataset, we can use scvi-tools to register the protein expression. Note that totalVI is the only current model that uses the protein expression. The usage of registered items is model specific. As another example, registering the labels in the AnnData object will not affect totalVI or scVI, but is necessary to run scANVI.

We have not preprocessed the pbmc5k object, which we do recommend. We show how to run setup_anndata in this case for illustrative purposes.

scvi.model.TOTALVI.setup_anndata(pbmc5k, protein_expression_obsm_key="protein_expression")
INFO     Using column names from columns of adata.obsm['protein_expression']


After setup_anndata has been run, the adata object should not be modified. In other words, the very next step in the workflow is to initialize and train the model of interest (e.g., scVI, totalVI). If you do modify the adata, it’s ok, just run setup_anndata again – and then reinitialize the model.

Viewing the scvi-tools data setup#

model = scvi.model.TOTALVI(pbmc5k)
INFO     Computing empirical prior initialization for protein background.
Anndata setup with scvi-tools version 0.15.0a0.
         Summary Statistics         
┃     Summary Stat Key      Value ┃
│         n_cells           5247  │
│          n_vars           33538 │
│         n_labels            1   │
│         n_batch             1   │
│ n_extra_categorical_covs    0   │
│ n_extra_continuous_covs     0   │
│        n_proteins          32   │
                   Data Registry                   
┃ Registry Key        scvi-tools Location        ┃
│      X                    adata.X              │
│    labels        adata.obs['_scvi_labels']     │
│    batch          adata.obs['_scvi_batch']     │
│   proteins    adata.obsm['protein_expression'] │
                     labels State Registry                      
┃      Source Location       Categories  scvi-tools Encoding ┃
│ adata.obs['_scvi_labels']      0                0          │
                     batch State Registry                      
┃     Source Location       Categories  scvi-tools Encoding ┃
│ adata.obs['_scvi_batch']      0                0          │