diffxpy

repository·master·Indexed 41 days ago

https://github.com/theislab/diffxpy

A fast and scalable differential expression analysis tool for single-cell RNA-seq data designed to work with scanpy. It provides support for single and multiple differential expression tests per gene—including Wald, Likelihood Ratio (LRT), t-tests, and Wilcoxon rank-sum tests—as well as gene set enrichment analysis and GLM model fitting via batchglm.

Tokens
3.2K
Snippets
11
Records
22
Agent score
52%

What's inside diffxpy

  1. Overview of diffxpy for single-cell RNA-seq analysis

    master

    diffxpy is a tool designed for fast and scalable differential expression analysis on single-cell RNA-seq data. It is designed to integrate into scanpy workflows.

    Note: This repository is unmaintained. If you require pure-python differential expression analysis, it is recommended to use PyDESeq2 instead.

  2. How GLM estimator objects work in diffxpy

    master

    In diffxpy, Generalized Linear Models (GLMs) are the primary model class used for differential expression analysis via Wald tests (de.test.wald) or Likelihood Ratio Tests (LRT) (de.test.lrt).

    Users can specify models using formulas or by providing design matrices directly. The choice of GLM is driven by the noise_model argument.

    Estimator objects are produced in two ways:

    1. Indirectly: By running a test. Wald tests return an object with a model_estim attribute. LRTs return objects with full_estim and reduced_estim attributes.
    2. Directly: By calling de.fit.model to produce an estimator object independently of a test.
  3. Understand parameter estimation in diffxpy

    master

    diffxpy performs parameter estimation for Generalized Linear Models (GLMs) using the batchglm module.

    Key concepts:

    • GLMs vs. Tests: GLMs are required for performing Wald tests and likelihood ratio tests. They are not required for t-tests or Wilcoxon rank-sum tests.
    • Estimation Methods: While some GLMs allow for closed-form maximum likelihood estimators, batchglm primarily uses numerical parameter estimation based on local approximations of the objective function (e.g., gradient descent). This approach supports a wider range of variance and noise models.
    • Transparency: Unlike many differential expression frameworks that hide the training process, diffxpy exposes training details. This allows users to monitor training progress and optimize hyperparameters.
  4. Select a differential testing method

    master

    Depending on your hypothesis, you can choose from several statistical test types within diffxpy:

    • Likelihood-ratio tests (LRT): For comparing nested models.
    • Wald tests: For testing a single parameter or multiple parameters.
    • t-tests: For comparing means.
    • Wilcoxon (rank sum) tests: For non-parametric testing.
  5. Run multiple differential tests per gene

    master

    For complex experimental designs, diffxpy supports running multiple tests for a single gene. This includes:

    • Pairwise tests: Comparing specific groups against each other.
    • Group versus rest tests: Comparing one group against the combined remainder of the dataset.
    • Partitioned tests: Conducting tests within specific partitions of the dataset.

    You can also define data set partitions to map single tests across these partitions.

  6. Install TensorFlow and TensorFlow Probability dependencies

    master

    diffxpy requires batchglm, which in turn depends on tensorflow and tensorflow-probability. You can install the stable major releases or the nightly versions (which are more up-to-date). Using tf-nightly and tfp-nightly is recommended for the latest features and updates.

    To install the nightly versions:

    pip install tf-nightly
    pip install tfp-nightly

    To install the stable major releases:

    pip install tensorflow
    pip install tensorflow-probability
  7. Limit TensorFlow parallelization in diffxpy

    master

    Since diffxpy relies on batchglm, which uses TensorFlow for linear algebra operations, you can control TensorFlow's multi-threading behavior. This is useful for limiting core usage on certain operating systems.

    Important: You must set these environment variables before importing diffxpy.api or batchglm.api in your Python session. If you need to change these settings, you must restart your Python session.

    Use the following environment variables:

    • TF_NUM_THREADS: Controls the number of threads used for linear algebra operations (training).
    • TF_LOOP_PARALLEL_ITERATIONS: Controls the number of threads used during TensorFlow while_loops (used during Hessian computation).

    To restrict TensorFlow to a single thread, execute this at the very beginning of your script:

    import os
    os.environ.setdefault("TF_NUM_THREADS", "1")
    os.environ.setdefault("TF_LOOP_PARALLEL_ITERATIONS", "1")
    import diffxpy.api as de
  8. Accessing diffxpy APIs

    master

    To use the differential expression and enrichment tools, import the API module as de:

    import diffxpy.api as de

    Once imported, you can access two main functional areas:

    1. Differential expression analysis: Available in the de.test.* module.
    2. Gene set enrichment analysis: Available in the de.enrich.* module (based on differential expression calls).
    import diffxpy.api as de
    
    # Access differential expression tests
    # de.test.some_test_function(...)
    
    # Access gene set enrichment
    # de.enrich.some_enrichment_function(...)
  9. Customize training with training strategies

    master

    To allow for monitoring and hyperparameter optimization without requiring deep technical knowledge of optimization algorithms, diffxpy provides "training strategies".

    Use training strategies to modify optimizer defaults, including:

    • Optimizer algorithms
    • Learning rates
    • Optimizer schedules (e.g., using multiple optimizers)
    • Convergence criteria

    If your model fails to converge using the default optimizer, you should use these strategies to adjust the hyperparameters or report the issue on GitHub.

  10. Limit Numpy/Scipy parallelization in diffxpy

    master

    To limit the number of threads used by Numpy/Scipy linalg sub-modules, you must set environment variables in the shell before starting your Python session (e.g., in the terminal from which you launch a Jupyter Notebook or a Python script).

    This is primarily relevant on some Linux operating systems. Use the following variables to restrict Numpy to a single thread:

    export MKL_NUM_THREADS=1
    export NUMEXPR_NUM_THREADS=1
    export OMP_NUM_THREADS=1
  11. Install diffxpy from source

    master

    To install diffxpy, follow these steps:

    1. Clone the diffxpy repository from https://github.com/theislab/diffxpy into your desired directory.
    2. Navigate to the root directory of the cloned diffxpy repository.
    3. Install it using pip in editable mode.
    pip install -e .