CellTypist Documentation

repository·main·Indexed 19 days ago

https://github.com/teichlab/celltypist

CellTypist 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.

Tokens
11.4K
Snippets
40
Records
48
Agent score
65%

What's inside CellTypist

  1. Overview of CellTypist

    main
    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.
  2. Use the majority voting classifier

    main

    By default, CellTypist performs independent cell predictions. To incorporate cell-cell transcriptomic relationships, enable the majority voting classifier by passing majority_voting = True to annotate(). 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_clustering argument, which accepts:

    1. A plain file with one cell per line.
    2. A string key for an existing metadata column in the AnnData.
    3. A list-like object (e.g., a numpy 1D array).

    Results and Visualization

    When using majority voting, the AnnotationResult object's .predicted_labels attribute includes extra columns: over_clustering and majority_voting.

    When using to_adata(), you can specify insert_conf_by = 'majority_voting' to include confidence scores for the majority-voting results instead of raw predictions.

    When using celltypist.dotplot(), you can set use_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')
  3. Prepare AnnData for CellTypist classification

    main

    CellTypist requires a logarithmised and normalised expression matrix stored in an AnnData object. Specifically, the data should be log1p normalised to 10,000 counts per cell.

    CellTypist searches for the expression matrix in the following order:

    1. The .X attribute.
    2. The .raw.X attribute.

    Important: To ensure maximal overlap with the model, provide all genes during the normalisation and logarithmisation process. If you subset the genes in the AnnData after normalisation, the prediction results may not be optimal.

  4. Run CellTypist using Docker

    main

    You can run CellTypist using Docker containers. For simple usage, mount your data directory to /data inside the container. To use custom models, mount your models directory to /opt/celltypist/data/models inside 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/output
  5. Run CellTypist using Singularity

    main

    To use CellTypist with Singularity, first pull the image from the registry, then run it using the singularity run command 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/output
  6. Generate a custom CellTypist model

    main

    You can train a custom model using the celltypist.train function 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 an AnnData (.h5ad). Tables should contain raw counts; AnnData should contain log1p normalised expression (to 10,000 counts per cell) in .X or .raw.X. You can also pass in-memory objects like csr_matrix or AnnData. 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 using AnnData, 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

    1. Traditional Logistic Regression: Default for datasets $\le$ 100k cells. Uses solver, C (inverse L2 regularization), and max_iter.
    2. SGD Logistic Regression: Enabled via use_SGD = True. Recommended for large datasets to reduce training time. Uses alpha (L2 regularization) and max_iter.
    3. Mini-batch SGD: For very large datasets (>500k cells), use use_SGD = True and mini_batch = True. This bins cells into batches (default batch_size = 1000) and trains over multiple epochs (default 10). Use balance_cell_type = True to prevent undersampling rare cell types.
    4. Two-pass Training (Feature Selection): Use feature_selection = True to perform fast feature selection based on importance (absolute regression coefficients) before re-running the classifier on the top genes (default top_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)
  7. Manage CellTypist models

    main

    CellTypist uses serialized models for cell type predictions. You can discover, download, and inspect these models using the celltypist.models module.

    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 the CELLTYPIST_FOLDER environment 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)
  8. Access CellTypist interactive tutorials

    main

    CellTypist 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.
  9. Save and load custom CellTypist models

    main

    Once a model is trained, it is an instance of the Model class. You can save it to disk and reload it for later use in celltypist.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')
  10. Download and inspect CellTypist built-in models

    main

    CellTypist provides a suite of pre-trained models. You can download the latest versions using models.download_models().

    • Use force_update = True to 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')