Scanpy Documentation

repository·main·Indexed 25 days ago

https://github.com/scverse/scanpy

Scanpy is a scalable Python-based toolkit for single-cell gene expression analysis. Built to work with anndata, it provides tools for preprocessing, visualization, clustering, trajectory inference, and differential expression testing, capable of handling datasets exceeding one million cells. The library includes modules for IO, metrics, and a plotting API supporting both matplotlib and HoloViews backends.

Tokens
17.4K
Snippets
19
Records
127
Agent score
74%

What's inside scanpy

  1. Overview of Scanpy for single-cell analysis

    main

    Scanpy is a scalable Python toolkit designed for analyzing single-cell gene expression data. It is built to work jointly with anndata and is capable of efficiently handling datasets exceeding one million cells.

    Key features include:

    • Preprocessing
    • Visualization
    • Clustering
    • Trajectory inference
    • Differential expression testing

    Note on Large Datasets: For datasets that exceed available memory, many Scanpy functions are compatible with dask, though this feature is currently considered experimental.

  2. Use scanpy.queries for annotation and enrichment

    main

    The scanpy.queries module provides utility functions to assist with single-cell data annotation and enrichment tasks. Key capabilities include:

    • biomart_annotations: Retrieve annotations from Ensembl BioMart.
    • gene_coordinates: Fetch genomic coordinates for genes.
    • mitochondrial_genes: Identify mitochondrial genes in a dataset.
    • enrich: Perform functional enrichment analysis.
  3. Evaluate results using scanpy.metrics

    main

    The scanpy.metrics module provides collections of measurements used to evaluate the results of single-cell analysis workflows (such as clustering or spatial patterns).

    Available metric modules include:

    • scanpy.metrics.modularity: Measures the strength of division of a network into modules.
    • scanpy.metrics.confusion_matrix: Evaluates classification performance by comparing predicted labels against ground truth.
    • scanpy.metrics.gearys_c: A measure of spatial autocorrelation.
    • scanpy.metrics.morans_i: A measure of spatial autocorrelation.
  4. Retrieve data from AnnData using scanpy.get

    main
    The scanpy.get module provides convenience functions to extract data from an AnnData object and convert it into more usable formats, such as pandas DataFrames. This is useful for downstream analysis or visualization where you need specific metadata or results in a tabular format rather than accessing the AnnData object's internal attributes directly.
  5. Explore Scanpy external tool categories

    main

    The scanpy.external module contains wrappers categorized into the following functional areas:

    • preprocessing: Tools for data cleaning and preparation.
    • tools: Wrappers for various single-cell analysis algorithms.
    • plotting: External visualization utilities.
    • exporting: Tools for exporting data to different formats.
  6. Use the Scanpy plotting module `scanpy.pl`

    main

    The scanpy.pl module is used for visualizing single-cell data. It largely parallels the scanpy.tl (tools) and scanpy.pp (preprocessing) modules; for most tools and many preprocessing functions, a corresponding plotting function exists with the same name.

    Many functions support multiple backends (e.g., HoloViews and matplotlib). For global visual configurations, refer to the settings section of the documentation.

  7. Use external tools in scanpy.external.tl

    main
    Scanpy provides access to various specialized single-cell analysis tools through the scanpy.external.tl module. These tools are categorized by their biological or computational purpose, such as dimensionality reduction (embeddings), clustering/trajectory inference, or gene scoring/cell cycle analysis. Note that many of these functions are wrappers around external packages and may require additional dependencies to be installed in your environment.
  8. Use the `scanpy.pp` module for data preprocessing

    main

    The scanpy.pp module contains functions for transforming the data matrix. Unlike scanpy.tl (tools), preprocessing functions typically perform basic transformations on the data matrix (such as normalization, scaling, or filtering) rather than returning interpretable annotations.

    Common tasks include:

    • Filtering cells and genes
    • Normalization and log transformation
    • Identifying highly variable genes
    • Batch effect correction and data integration
    • Doublet detection
    • Computing neighbor graphs
  9. Typical Scanpy Workflow

    main

    The Scanpy workflow involves sequential calls to analysis tools located in the sc.tl module, which operate on an anndata.AnnData object. These tools typically add annotations to the expression matrix X. For most analysis tools, there is a corresponding plotting function in the sc.pl module.

    In-place operations: By default, Scanpy tools operate inplace on the adata object and return None to facilitate memory efficiency. If you need to preserve the original object and receive a new copy, pass copy=True or inplace=False to the function.

  10. Identify Scanpy pre-releases and development versions

    main

    Scanpy uses specific PEP 440 naming conventions for non-stable releases:

    • Pre-releases: Versions formatted like 1.7.0rc1 or 1.7.0rc2.
    • Development versions: Versions formatted like 1.8.0.dev0. These may include a commit hash as a local version identifier, for example: 1.8.0.dev2+g00ad77b.
  11. Use the Scanpy public API

    main

    When building tools or workflows with Scanpy, you should only rely on the documented Public API.

    Scanpy does not guarantee the stability of its internal APIs (e.g., functions or arguments that are not explicitly documented in the official API section). Using internal modules—even if they do not have a leading underscore—is not officially supported and may break in future updates.

    If you find a feature in the internal API that you need for your work, it is encouraged to open an issue to request that the feature be exported to the public API.