Note
This page was generated from api_overview.ipynb. Interactive online version: .
In this introductory tutorial, we go through the different steps of an scvi-tools workflow.
While we focus on scVI in this tutorial, the API is consistent across all models.
[1]:
import sys #if True, will install via pypi, else will install from source stable = True IN_COLAB = "google.colab" in sys.modules if IN_COLAB and stable: !pip install --quiet scvi-tools[tutorials] elif IN_COLAB and not stable: !pip install --quiet --upgrade jsonschema !pip install --quiet git+https://github.com/yoseflab/scvi-tools@master#egg=scvi-tools[tutorials]
[2]:
import scvi import scanpy as sc sc.set_figure_params(figsize=(4, 4))
Let us first load the CORTEX dataset described in Zeisel et al. (2015). scvi-tools has many “built-in” datasets as well as support for loading arbitrary .csv, .loom, and .h5ad (AnnData) files. Please see our tutorial on data loading for more examples.
.csv
.loom
.h5ad
Zeisel, Amit, et al. “Cell types in the mouse cortex and hippocampus revealed by single-cell RNA-seq.” Science 347.6226 (2015): 1138-1142.
Important
All scvi-tools models require AnnData objects as input.
[3]:
adata = scvi.data.cortex(run_setup_anndata=False)
INFO File /content/data/expression.bin already downloaded INFO Loading Cortex data from /content/data/expression.bin INFO Finished loading Cortex data
/usr/local/lib/python3.6/dist-packages/anndata/_core/anndata.py:119: ImplicitModificationWarning: Transforming to str index. warnings.warn("Transforming to str index.", ImplicitModificationWarning)
Now we preprocess the data to remove, for example, genes that are very lowly expressed and other outliers. For these tasks we prefer the Scanpy preprocessing module.
[4]:
sc.pp.filter_genes(adata, min_counts=3)
/usr/local/lib/python3.6/dist-packages/anndata/_core/anndata.py:1094: FutureWarning: is_categorical is deprecated and will be removed in a future version. Use is_categorical_dtype instead if not is_categorical(df_full[k]):
In scRNA-seq analysis, among others, it’s popular to normalize the data. These values are not used by scvi-tools, but given their popularity in other tasks as well as for visualization, we store them in the anndata object separately (via the .raw attribute).
.raw
Unless otherwise specific, scvi-tools models require the raw counts.
[5]:
adata.layers["counts"] = adata.X.copy() # preserve counts sc.pp.normalize_total(adata, target_sum=1e4) sc.pp.log1p(adata) adata.raw = adata # freeze the state in `.raw`
Finally, we perform feature selection, to reduce the number of features (genes in this case) used as input to the scvi-tools model. For best practices of how/when to perform feature selection, please refer to the model-specific tutorial.
[6]:
sc.pp.highly_variable_genes( adata, n_top_genes=2000, subset=True, layer="counts", flavor="seurat_v3" )
/usr/local/lib/python3.6/dist-packages/anndata/_core/anndata.py:1094: FutureWarning: is_categorical is deprecated and will be removed in a future version. Use is_categorical_dtype instead if not is_categorical(df_full[k]): /usr/local/lib/python3.6/dist-packages/scanpy/preprocessing/_highly_variable_genes.py:144: FutureWarning: Slicing a positional slice with .loc is not supported, and will raise TypeError in a future version. Use .loc with labels or .iloc with positions instead. df.loc[: int(n_top_genes), 'highly_variable'] = True
Now it’s time to run setup_anndata(), which alerts scvi-tools to the locations of various matrices inside the anndata. It’s important to run this function with the correct arguments so scvi-tools is notified that your dataset has batches, annotations, etc. For example, if batches are registered with scvi-tools, the subsequent model will correct for batch effects. See the full documentation for details.
setup_anndata()
[7]:
scvi.data.setup_anndata(adata, layer="counts")
INFO No batch_key inputted, assuming all cells are same batch INFO No label_key inputted, assuming all cells have same label INFO Using data from adata.layers["counts"] INFO Computing library size prior per batch INFO Successfully registered anndata object containing 3005 cells, 2000 vars, 1 batches, 1 labels, and 0 proteins. Also registered 0 extra categorical covariates and 0 extra continuous covariates. INFO Please do not further modify adata until model is trained.
Warning
If the adata is modified after running setup_anndata, please run setup_anndata again.
setup_anndata
While we highlight the scVI model here, the API is consistent across all scvi-tools models and is inspired by that of scikit-learn. For a full list of options, see the scvi documentation.
[8]:
model = scvi.model.SCVI(adata)
We can see an overview of the model by printing it.
[9]:
model
SCVI Model with the following params: n_hidden: 128, n_latent: 10, n_layers: 1, dropout_rate: 0.1, dispersion: gene, gene_likelihood: zinb, latent_distribution: normal Training status: Not Trained To print summary of associated AnnData, use: scvi.data.view_anndata_setup(model.adata)
[10]:
model.train()
INFO Training for 400 epochs INFO KL warmup for 400 epochs Training...: 100%|██████████| 400/400 [01:35<00:00, 4.18it/s] INFO Training time: 95 s. / 400 epochs
Saving consists of saving the model neural network weights, as well as parameters used to initialize the model.
[11]:
model.save("my_model/")
[12]:
model = scvi.model.SCVI.load("my_model/", adata, use_cuda=True)
INFO Using data from adata.layers["counts"] INFO Computing library size prior per batch INFO Registered keys:['X', 'batch_indices', 'local_l_mean', 'local_l_var', 'labels'] INFO Successfully registered anndata object containing 3005 cells, 2000 vars, 1 batches, 1 labels, and 0 proteins. Also registered 0 extra categorical covariates and 0 extra continuous covariates.
[13]:
latent = model.get_latent_representation()
It’s often useful to store the outputs of scvi-tools back into the original anndata, as it permits interoperability with Scanpy.
[14]:
adata.obsm["X_scVI"] = latent
The model.get...() functions default to using the anndata that was used to initialize the model. It’s possible to also query a subset of the anndata, or even use a completely independent anndata object as long as the anndata is organized in an equivalent fashion.
model.get...()
[15]:
adata_subset = adata[adata.obs.cell_type == "interneurons"] latent_subset = model.get_latent_representation(adata_subset)
INFO Received view of anndata, making copy.
[16]:
denoised = model.get_normalized_expression(adata_subset, library_size=10e4) denoised.iloc[:5, :5]
Let’s store the normalized values back in the anndata.
[17]:
adata.layers["scvi_normalized"] = model.get_normalized_expression( library_size=10e4 )
Scanpy is a powerful python library for visualization and downstream analysis of scRNA-seq data. We show here how to feed the objects produced by scvi-tools into a scanpy workflow.
[18]:
# use scVI latent space for UMAP generation sc.pp.neighbors(adata, use_rep="X_scVI") sc.tl.umap(adata, min_dist=0.2)
[19]:
sc.pl.umap( adata, color="cell_type", frameon=False, )
/usr/local/lib/python3.6/dist-packages/anndata/_core/anndata.py:1192: FutureWarning: is_categorical is deprecated and will be removed in a future version. Use is_categorical_dtype instead if is_string_dtype(df[key]) and not is_categorical(df[key]) ... storing 'precise_labels' as categorical ... storing 'cell_type' as categorical
The user will note that we imported curated labels from the original publication. Our interface with scanpy makes it easy to cluster the data with scanpy from scVI’s latent space and then reinject them into scVI (e.g., for differential expression).
[20]:
# neighbors were already computed using scVI sc.tl.leiden(adata, key_added="leiden_scVI")
We can also use many scvi-tools models for differential expression. For further details on the methods underlying these functions as well as additional options, please see TODO.
[21]:
adata.obs.cell_type.head()
0 interneurons 1 interneurons 2 interneurons 3 interneurons 4 interneurons Name: cell_type, dtype: category Categories (7, object): ['astrocytes_ependymal', 'endothelial-mural', 'interneurons', 'microglia', 'oligodendrocytes', 'pyramidal CA1', 'pyramidal SS']
For example, a 1-vs-1 DE test is as simple as:
[22]:
de_df = model.differential_expression( groupby="cell_type", group1="oligodendrocytes", group2="pyramidal CA1" ) de_df.head()
DE...: 100%|██████████| 1/1 [00:04<00:00, 4.15s/it]
We can also do a 1-vs-all DE test, which compares each cell type with the rest of the dataset:
[23]:
de_df = model.differential_expression( groupby="cell_type", ) de_df.head()
DE...: 100%|██████████| 7/7 [00:29<00:00, 4.28s/it]
We now extract top markers for each cluster using the DE results.
[24]:
markers = {} cats = adata.obs.cell_type.cat.categories for i, c in enumerate(cats): cid = "{} vs Rest".format(c) cell_type_df = de_df.loc[de_df.comparison == cid] cell_type_df = cell_type_df.sort_values("lfc_mean", ascending=False) cell_type_df = cell_type_df[cell_type_df.lfc_mean > 0] cell_type_df = cell_type_df[cell_type_df["bayes_factor"] > 3] cell_type_df = cell_type_df[cell_type_df["non_zeros_proportion1"] > 0.1] markers[c] = cell_type_df.index.tolist()[:3]
[25]:
sc.tl.dendrogram(adata, groupby="cell_type", use_rep="X_scVI")
[26]:
sc.pl.dotplot( adata, markers, groupby='cell_type', dendrogram=True, color_map="Blues", swap_axes=True, use_raw=True, standard_scale="var", )
We can also visualize the scVI normalized gene expression values with the layer option.
layer
[27]:
sc.pl.heatmap( adata, markers, groupby='cell_type', layer="scvi_normalized", standard_scale="var", dendrogram=True, )
WARNING: dendrogram data not found (using key=dendrogram_cell_type). Running `sc.tl.dendrogram` with default parameters. For fine tuning it is recommended to run `sc.tl.dendrogram` independently. WARNING: You’re trying to run this on 2000 dimensions of `.X`, if you really want this, set `use_rep='X'`. Falling back to preprocessing with `sc.pp.pca` and default params.
Verbosity varies in the following way:
logger.setLevel(logging.WARNING) will show a progress bar.
logger.setLevel(logging.WARNING)
logger.setLevel(logging.INFO) will show global logs including the number of jobs done.
logger.setLevel(logging.INFO)
logger.setLevel(logging.DEBUG) will show detailed logs for each training (e.g the parameters tested).
logger.setLevel(logging.DEBUG)
This function’s behaviour can be customized, please refer to its documentation for information about the different parameters available.
In general, you can use scvi.settings.verbosity to set the verbosity of the scvi package. Note that verbosity corresponds to the logging levels of the standard python logging module. By default, that verbosity level is set to INFO (=20). As a reminder the logging levels are:
scvi.settings.verbosity
verbosity
logging
INFO
Level
Numeric value
CRITICAL
50
ERROR
40
WARNING
30
20
DEBUG
10
NOTSET
0