CellTypist Documentation
repository·main·Indexed 19 days ago
https://github.com/teichlab/celltypistCellTypist is an automated tool for annotating cell types in scRNA-seq datasets using logistic regression classifiers. It supports the use of pre-trained models, such as immune cell models, or custom models trained via the celltypist.train function. The tool provides a Python API and a CLI for classification, supporting input formats including count tables (.txt, .csv, .tsv, .tab, .mtx) and AnnData objects. Key features include multi-label classification, a majority voting classifier to incorporate cell-cell relationships, and visualization tools like dot plots and UMAPs.
What's inside CellTypist
- CellTypist is an automated cell type annotation tool designed for scRNA-seq datasets. It uses logistic regression classifiers optimized by the stochastic gradient descent algorithm to predict cell types and subtypes. It supports both built-in models (primarily focused on immune sub-populations) and custom models.
Use the majority voting classifier
mainBy default, CellTypist performs independent cell predictions. To incorporate cell-cell transcriptomic relationships, enable the majority voting classifier by passing
majority_voting = Truetoannotate(). This approach assumes similar cell subtypes are likely to form clusters.Over-clustering
To define cell-cell relations, CellTypist uses a Leiden clustering pipeline with a heuristic over-clustering approach. You can provide your own over-clustering via the
over_clusteringargument, which accepts:- A plain file with one cell per line.
- A string key for an existing metadata column in the
AnnData. - A list-like object (e.g., a numpy 1D array).
Results and Visualization
When using majority voting, the
AnnotationResultobject's.predicted_labelsattribute includes extra columns:over_clusteringandmajority_voting.When using
to_adata(), you can specifyinsert_conf_by = 'majority_voting'to include confidence scores for the majority-voting results instead of raw predictions.When using
celltypist.dotplot(), you can setuse_as_prediction = 'majority_voting'to visualize the match between majority-voting results and manual annotations.#Turn on the majority voting classifier as well. predictions = celltypist.annotate(input_file, model = 'Immune_All_Low.pkl', majority_voting = True) #Add your own over-clustering result. predictions = celltypist.annotate(input_file, model = 'Immune_All_Low.pkl', majority_voting = True, over_clustering = '/path/to/over_clustering/file')Prepare AnnData for CellTypist classification
mainCellTypist requires a logarithmised and normalised expression matrix stored in an
AnnDataobject. Specifically, the data should belog1pnormalised to 10,000 counts per cell.CellTypist searches for the expression matrix in the following order:
- The
.Xattribute. - The
.raw.Xattribute.
Important: To ensure maximal overlap with the model, provide all genes during the normalisation and logarithmisation process. If you subset the genes in the
AnnDataafter normalisation, the prediction results may not be optimal.- The
Run CellTypist via Docker
mainA Docker image is available from the Quay.io Container Registry:
quay.io/teichlab/celltypist:latest# Example pull command docker pull quay.io/teichlab/celltypist:latestRun CellTypist using Docker
mainYou can run CellTypist using Docker containers. For simple usage, mount your data directory to
/datainside the container. To use custom models, mount your models directory to/opt/celltypist/data/modelsinside the container.# Simple usage docker run --rm -it \ -v /path/to/data:/data \ quay.io/teichlab/celltypist:latest \ celltypist --indata /data/file --model Immune_All_Low.pkl --outdir /data/output # Usage with custom models docker run --rm -it \ -v /path/to/data:/data \ -v /path/to/models:/opt/celltypist/data/models \ quay.io/teichlab/celltypist:latest \ celltypist --indata /data/file --model My_Custom_Model.pkl --outdir /data/outputInstall CellTypist via conda
mainYou can install CellTypist using
condaby accessing thebiocondaandconda-forgechannels.conda install -c bioconda -c conda-forge celltypistRun CellTypist using Singularity
mainTo use CellTypist with Singularity, first pull the image from the registry, then run it using the
singularity runcommand with appropriate bind mounts (-B).# Pull the image singularity pull celltypist-latest.sif docker://quay.io/teichlab/celltypist:latest # Simple usage singularity run \ -B /path/to/data:/data \ celltypist-latest.sif \ celltypist --indata /data/file --model Immune_All_Low.pkl --outdir /data/output # Usage with custom models singularity run \ -B /path/to/data:/data \ -B /path/to/models:/opt/celltypist/data/models \ celltypist-latest.sif \ celltypist --indata /data/file --model My_Custom_Model.pkl --outdir /data/outputGenerate a custom CellTypist model
mainYou can train a custom model using the
celltypist.trainfunction to transfer cell type labels to other scRNA-seq datasets.Input Formats
- Gene Expression Data: Can be a path to a table (
.csv,.mtx) or anAnnData(.h5ad). Tables should contain raw counts;AnnDatashould contain log1p normalised expression (to 10,000 counts per cell) in.Xor.raw.X. You can also pass in-memory objects likecsr_matrixorAnnData. A cell-by-gene format is required. - Cell Type Labels: A path to a file with one label per line, or a list-like object (e.g.,
tuple,series). If usingAnnData, you can provide a column name from.obs. - Genes: Automatically extracted from tables/AnnData. Otherwise, provide a path to a file with one gene per line or a list-like object.
Training Methods
- Traditional Logistic Regression: Default for datasets $\le$ 100k cells. Uses
solver,C(inverse L2 regularization), andmax_iter. - SGD Logistic Regression: Enabled via
use_SGD = True. Recommended for large datasets to reduce training time. Usesalpha(L2 regularization) andmax_iter. - Mini-batch SGD: For very large datasets (>500k cells), use
use_SGD = Trueandmini_batch = True. This bins cells into batches (defaultbatch_size = 1000) and trains over multipleepochs(default 10). Usebalance_cell_type = Trueto prevent undersampling rare cell types. - Two-pass Training (Feature Selection): Use
feature_selection = Trueto perform fast feature selection based on importance (absolute regression coefficients) before re-running the classifier on the top genes (defaulttop_genes = 300).
# Basic training new_model = celltypist.train(expression_input, labels = label_input, genes = gene_input) # Training with subset of genes (e.g. highly variable genes) # Use check_expression = False to skip normalization checks when using subsets new_model = celltypist.train(some_adata[:, some_adata.var.highly_variable], labels = label_input, check_expression = False) # Training with SGD new_model = celltypist.train(expression_input, labels = label_input, genes = gene_input, use_SGD = True) # Training with SGD mini-batch new_model = celltypist.train(expression_input, labels = label_input, genes = gene_input, use_SGD = True, mini_batch = True) # Two-pass training with feature selection new_model = celltypist.train(expression_input, labels = label_input, genes = gene_input, feature_selection = True)- Gene Expression Data: Can be a path to a table (
Manage CellTypist models
mainCellTypist uses serialized models for cell type predictions. You can discover, download, and inspect these models using the
celltypist.modelsmodule.Download and Update Models
models.models_description(): Lists all available models.models.download_models(model='name.pkl'): Downloads a specific model or a list of models.models.download_models(force_update=True): Updates all models to their latest versions.models.download_models(): Downloads all available models.
Model Storage
By default, models are stored in
~/.celltypist/. You can change this location by setting theCELLTYPIST_FOLDERenvironment variable.export CELLTYPIST_FOLDER='/path/to/model/folder/'import celltypist from celltypist import models # Show all available models models.models_description() # Download a specific model models.download_models(model='Immune_All_Low.pkl') # Download all available models models.download_models() # Update all models models.download_models(force_update=True)Access CellTypist interactive tutorials
mainCellTypist provides several interactive tutorials via Google Colab to demonstrate different use cases:
- Cell type classification: Standard usage for predicting cell types.
- Multi-label classification: For datasets where cells may belong to multiple categories.
- Large-scale cross-dataset label transfer: Best practices for transferring labels across different datasets.
Save and load custom CellTypist models
mainOnce a model is trained, it is an instance of the
Modelclass. You can save it to disk and reload it for later use incelltypist.annotate.# Save the model locally new_model.write('/path/to/local/folder/some_model_name.pkl') # Or save to the default models path new_model.write(f'{models.models_path}/some_model_name.pkl') # Load the model from celltypist import models new_model = models.Model.load('/path/to/local/folder/some_model_name.pkl') # Use the loaded model for annotation import celltypist predictions = celltypist.annotate(input_file, model = '/path/to/local/folder/some_model_name.pkl') # If the model is in models.models_path, you can just use the filename predictions = celltypist.annotate(input_file, model = 'some_model_name.pkl')Download and inspect CellTypist built-in models
mainCellTypist provides a suite of pre-trained models. You can download the latest versions using
models.download_models().- Use
force_update = Trueto overwrite existing local models with newer versions. - Models are stored in the directory specified by
models.models_path. - Use
models.models_description()to see an overview of available models. - Use
models.Model.load(model='MODEL_NAME.pkl')to load a specific model by its filename (recommended) or a loaded model object.
# Download latest models models.download_models(force_update = True) # Inspect available models models.models_description() # Load a specific model model = models.Model.load(model = 'Immune_All_Low.pkl')- Use