Libra R Package

repository·main·Indexed 41 days ago

https://github.com/neurorestore/libra

An R package for differential expression (DE) and differential accessibility (DA) analysis on scRNA-seq and scATAC-seq data. Libra provides a unified interface via the run_de function to access 22 statistical tests across single-cell, pseudobulk, and mixed-model frameworks. It supports integration with Seurat, monocle3, SingleCellExperiment, Signac, ArchR, and snap objects, and includes utilities for pseudobulk conversion and delta variance calculation to assess result reliability.

Tokens
2K
Snippets
5
Records
5
Agent score
2%

What's inside Libra

  1. Calculate delta variance to assess differential expression reliability

    main

    Delta variance is a measure used to account for the intrinsic variability of biological replicates in single-cell data. It helps distinguish between inherent biological/technical variability and the actual effect of a perturbation.

    High delta variance in genes identified as differentially expressed by 'singlecell' methods (those that do not account for biological replicates) suggests those results may be false positives and should be treated with caution.

    Use the calculate_delta_variance function to compute this measure. The function returns a list of vectors—one for each cell type—containing the delta variance for the genes present in the input expression matrix.

    DV = calculate_delta_variance(hagai_toy)
  2. Install Libra via devtools

    main

    To install Libra, you must first install devtools. Additionally, you need to install several Bioconductor packages for differential expression testing and the Seurat package for single-cell methods.

    Follow these steps in R:

    1. Install devtools.
    2. Install Bioconductor dependencies (edgeR, DESeq2, limma).
    3. Install Seurat (required for single-cell methods).
    4. Install Libra from GitHub.
    # 1. Install devtools
    install.packages("devtools")
    
    # 2. Install Bioconductor dependencies
    if (!requireNamespace("BiocManager", quietly = TRUE))
        install.packages("BiocManager")
    BiocManager::install(c("edgeR", "DESeq2", "limma"))
    
    # 3. Install Seurat for single-cell methods
    install.packages("Seurat")
    
    # 4. Install Libra from GitHub
    devtools::install_github("neurorestore/Libra")
  3. Convert data to pseudobulk using to_pseudobulk

    main

    If you want to manually inspect or use the pseudobulk matrices before running differential expression, use the to_pseudobulk function.

    # Returns pseudobulk matrices based on the provided expression matrix and metadata
    matrices = to_pseudobulk(expr, meta = meta)
  4. Use run_de for differential expression/accessibility

    main

    The run_de function is the primary interface for performing differential expression (DE) or differential accessibility (DA) analysis. It accepts a features-by-cells matrix and a metadata data frame, or directly accepts single-cell objects (Seurat, monocle3, SingleCellExperiment, Signac, ArchR, or snap).

    Input Requirements

    • Matrix Input: A preprocessed features-by-cells matrix and a metadata data frame containing cell_type, replicate, and label columns.
    • Object Input: A Seurat, monocle3, SingleCellExperiment, Signac, ArchR, or snap object. For these, Libra uses the metadata stored in the object.

    Key Arguments

    • expr: The features-by-cells matrix or single-cell object.
    • meta: Metadata data frame (if using matrix input).
    • cell_type_col: Name of the column containing cell type annotations (default: cell_type).
    • replicate_col: Name of the column containing replicates (default: replicate).
    • label_col: Name of the column containing experimental conditions/labels (default: label).
    • input_type: Set to scATAC for differential accessibility. Defaults to scRNA.
    • de_family: Select the statistical framework (singlecell, pseudobulk, mixedmodel, or snapatac_findDAR).
    • de_method: Select the specific statistical method within the family.
    • de_type: Select the testing framework (e.g., LRT, Wald, QLF, voom, trend).
    • n_threads: Number of cores to use (relevant for mixedmodel family).
    library(Libra)
    
    # Using matrix and metadata
    DE = run_de(expr, meta = meta)
    
    # Using custom column names
    DE = run_de(expr, meta = meta, cell_type_col = "cell.type", label_col = "condition")
    
    # Using a Seurat object directly
    DE = run_de(sc)
    
    # Running differential accessibility (scATAC)
    DE = run_de(sc, input_type = 'scATAC')
  5. Configure differential expression methods in run_de

    main

    Libra provides 22 different statistical tests categorized into four families. You can control which test is performed using the de_family, de_method, and de_type arguments in run_de.

    | Method | de_family | de_method | de_type |
    |--------|-----------|-----------|---------|
    | Wilcoxon Rank-Sum test | singlecell | wilcox | |
    | Likelihood ratio test | singecell | bimod | |
    | Student's t-test | singlecell | t | |
    | Negative binomial linear model | singlecell | negbinom | |
    | Logistic regression | singlecell | LR | |
    | MAST | singlecell | MAST | |
    | Fisher exact test | singlecell | fisher | |
    | Binomial test | singlecell | binomial | |
    | Logistic regression based on peaks | singlecell | LR_peaks | |
    | Permutation test | singlecell | permutation | |
    | edgeR-LRT | pseudobulk | edgeR | LRT |
    | edgeR-QLF | pseudobulk | edgeR | QLF |
    | DESeq2-LRT | pseudobulk | DESeq2 | LRT |
    | DESeq2-Wald | pseudobulk | DESeq2 | Wald |
    | limma-trend | pseudobulk | limma | trend |
    | limma-voom | pseudobulk | limma | voom |
    | Linear mixed model | mixedmodel | linear | Wald |
    | Linear mixed model-LRT | mixedmodel | linear | LRT |
    | Negative binomial generalized linear mixed model | mixedmodel | negbinom | Wald |
    | Negative binomial generalized linear mixed model-LRT | mixedmodel | negbinom | LRT |
    | Negative binomial generalized linear mixed model with offset | mixedmodel | negbinom_offset | Wald |
    | Negative binomial generalized linear mixed model with offset-LRT | mixedmodel | negbinom_offset | LRT |
    | Poisson generalized linear mixed model | mixedmodel | poisson | Wald |
    | Poisson generalized linear mixed model-LRT | mixedmodel | poisson | LRT |
    | Poisson generalized linear mixed model with offset | mixedmodel | poisson_offset | Wald |
    | Poisson generalized linear mixed model with offset-LRT | mixedmodel | poisson_offset | LRT |
    | SnapATAC findDAR | snapatac_findDAR | | |