scglue Documentation

repository·master·Indexed 19 days ago

https://github.com/gao-lab/glue

scglue (version 0.4.1) provides Graph-linked unified embedding for unpaired single-cell multi-omics data integration. The repository includes tools for single-cell multi-omics integration, a PyTorch implementation of MMD-MA for unsupervised manifold alignment, and detailed specifications for standardizing RNA and ATAC single-cell genomics data in .h5ad format.

Tokens
30.2K
Snippets
98
Records
116
Agent score
61%

What's inside scglue

  1. Available GLUE case studies

    master

    The repository contains several documented case studies demonstrating GLUE's capabilities in multi-omic integration:

    • TripleOmics: Triple omic integration of scRNA-seq, scATAC-seq and snmC-seq in the mouse cortex.
    • RegInf: Regulatory inference in PBMC via integrating scRNA-seq, scATAC-seq along with pcHi-C and eQTL.
    • Atlas: Mega-scale scRNA-seq, scATAC-seq integration of a human cell atlas.
  2. What is scglue and the GLUE model?

    master

    scglue implements the GLUE (Graph Linked Unified Embedding) model, designed for the integration of unpaired single-cell multi-omics data.

    In this context:

    • Unpaired data refers to scenarios where different omics layers (e.g., scRNA-seq, scATAC-seq, snmC-seq) are measured in independent samples of the same cell population rather than the same individual cells.
    • Prior knowledge is used to bridge the gap between different feature spaces. This knowledge typically consists of regulatory interactions (e.g., between RNA genes and ATAC peaks) which are compiled into a guidance graph to orient the multi-omics integration.
  3. Understand the main components of a GLUE model

    master

    A GLUE model is a modular PyTorch-based framework composed of four primary components. When extending the framework, you will likely interact with these abstractions:

    • Data Encoders: Receive data input $x$ and return a distribution corresponding to the data posterior (cell embeddings) $q(u|x)$. There is typically one encoder per domain.
    • Data Decoders: Receive cell embedding input $u$ and feature embedding input $v$, and return a distribution corresponding to the data likelihood $p(x|u, v)$. There is typically one decoder per domain.
    • Graph Encoder: Receives graph input $\mathcal{G}$ (edge index, edge weight, and edge sign) and returns a distribution corresponding to the graph posterior (feature embeddings) $q(v|\mathcal{G})$.
    • Graph Decoder: Receives feature embedding input $v$ and a subset of query edges (edge index and edge sign), returning a distribution corresponding to the likelihood of these query edges $p(\mathcal{G}|v)$.

    Existing implementations are located in scglue.models.sc.

  4. Implement a custom DataDecoder

    master

    To implement a custom decoder, inherit from the DataDecoder class.

    Constructor Requirements: Your __init__ should accept out_features (output dimensionality) and n_batches (number of batches to account for batch effects).

    The forward method: You must implement the forward method, which accepts the following four arguments:

    • u: Cell embeddings
    • v: Feature embeddings
    • b: Batch index
    • l: Library size (computed by the encoder)

    The method must return a likelihood distribution (e.g., a distribution object from scglue.models.prob).

    Example of a Negative Binomial decoder using batch-specific trainable parameters:

    class NBDataDecoder(DataDecoder):
    
        def __init__(self, out_features: int, n_batches: int = 1) -> None:
            super().__init__(out_features, n_batches=n_batches)
            self.scale_lin = torch.nn.Parameter(torch.zeros(n_batches, out_features))
            self.bias = torch.nn.Parameter(torch.zeros(n_batches, out_features))
            self.log_theta = torch.nn.Parameter(torch.zeros(n_batches, out_features))
    
        def forward(
                self, u: torch.Tensor, v: torch.Tensor,
                b: torch.Tensor, l: torch.Tensor
        ) -> D.NegativeBinomial:
            scale = F.softplus(self.scale_lin[b])
            logit_mu = scale * (u @ v.t()) + self.bias[b]
            mu = F.softmax(logit_mu, dim=1) * l
            log_theta = self.log_theta[b]
            return D.NegativeBinomial(
                log_theta.exp(),
                logits=(mu + EPS).log() - log_theta
            )
  5. Execute method evaluation using Snakemake

    master

    The method evaluation pipeline is executed using snakemake. Depending on your hardware environment, use the appropriate profile and job count:

    • On a local server: Use the profiles/local profile with a moderate number of jobs (e.g., -j32).
    • On an HPC cluster: Use the profiles/cls profile with a high number of jobs (e.g., -j500).
    # On local server
    snakemake -j32 --profile profiles/local
    
    # On HPC cluster
    snakemake -j500 --profile profiles/cls
  6. Install scglue via conda or pip

    master

    You can install the scglue package using either conda or pip. It is recommended to install scglue within a conda environment.

    Conda Installation

    For CPU-only usage:

    conda install -c conda-forge -c bioconda scglue

    For usage with GPU support:

    conda install -c conda-forge -c bioconda scglue pytorch-gpu

    Pip Installation

    pip install scglue
    # CPU only
    conda install -c conda-forge -c bioconda scglue
    
    # With GPU support
    conda install -c conda-forge -c bioconda scglue pytorch-gpu
    
    # Via pip
    pip install scglue
  7. Download the Yao-2021 dataset

    master

    The Yao-2021 dataset (A transcriptomic and epigenomic cell atlas of the mouse primary motor cortex) can be downloaded from the NeMO Archive.

    Dataset Details:

    • Source: Yao, Z. et al. Nature 598, 103-110 (2021).
    • DOI: 10.1038/s41586-021-03500-8
    • File Name: MOp_MiniAtlas_2020_bdbag_2021_04_28.tgz
    • MD5 Checksum: 11f3a6305b7a4fb7facf872d743abdb7
    https://data.nemoarchive.org/publication_release/MOp_MiniAtlas_2020_bdbag_2021_04_28.tgz
  8. Download data for Muto-2021 dataset

    master

    The Muto-2021 dataset (Muto, Y. et al., Nat. Commun. 2021) contains single-cell transcriptional and chromatin accessibility profiling data for the adult human kidney. The following files are available for download:

    • GSE151302_RAW.tar: Raw data from GEO (NCBI).
    • atac.h5ad: ATAC-seq data in AnnData format, hosted via AWS/cellxgene.
    • rna.h5ad: RNA-seq data in AnnData format, hosted via AWS/cellxgene.

    Always verify the integrity of downloaded files using the provided MD5SUMs.

    | File name | URL | MD5SUM |
    |:----------|:----|:-------|
    |GSE151302_RAW.tar|ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE151nnn/GSE151302/suppl/GSE151302_RAW.tar|b3ef40144d85e1b7f99529a5dec8b8e7|
    |atac.h5ad|https://corpora-data-prod.s3.amazonaws.com/...|6848fad62cc702b50a1afcc26deeeeab|
    |rna.h5ad|https://corpora-data-prod.s3.amazonaws.com/...|28fa00260b997bb8d23fd7ebc6733a58|
  9. Implement a custom DataEncoder

    master

    To support new data modalities, you can define a custom encoder by inheriting from the DataEncoder class. The base class provides an MLP implementation, but you must implement two abstract methods:

    1. compute_l(self, x: torch.Tensor) -> torch.Tensor: Computes the library size from the input data.
    2. normalize(self, x: torch.Tensor, l: torch.Tensor) -> torch.Tensor: Normalizes the input data (potentially using the computed library size $l$) before it is passed to the MLP.

    Example of a Negative Binomial (NB) encoder that performs row normalization to a constant size and applies a log-transform:

    class NBDataEncoder(DataEncoder):
    
        TOTAL_COUNT = 1e4
    
        def compute_l(self, x: torch.Tensor) -> torch.Tensor:
            return x.sum(dim=1, keepdim=True)
    
        def normalize(
                self, x: torch.Tensor, l: torch.Tensor
        ) -> torch.Tensor:
            return (x * (self.TOTAL_COUNT / l)).log1p()
  10. Prepare data for GLUE

    master

    Data preparation for GLUE involves two main steps: downloading/preprocessing raw resource directories and formatting single-cell data into h5ad files.

    1. Download and Preprocess Resources

    Several directories require manual data acquisition and preprocessing. For each of the following directories, you must download the files specified in their respective README files and then execute the preprocess.sh script located within that directory:

    • genome
    • conservation
    • download/ (includes datasets like 10x-ATAC-Brain5k, 10x-Multiome-Pbmc10k, Cao-2020, Chen-2019, Domcke-2020, Luo-2017, Ma-2020, Saunders-2018)
    • eqtl/GTEx-v8
    • hic/Javierre-2016
    • chip/ENCODE/TF-human
    • database/TRRUST-v2

    2. Format Single-Cell Data

    Once the resources are preprocessed, use snakemake to collect the single-cell data into a unified h5ad format. This will generate a dataset directory containing *.h5ad files.

    snakemake -j4 -prk
  11. Generate Goslim annotations using owltools

    master

    To generate goslim annotations from standard GO annotations, use the owltools command-line tool with the --map2slim flag. This maps the ontology to a slimmed version (e.g., goslim_generic).

    owltools go.obo --gaf goa_human.gaf --map2slim -s goslim_generic -u goa_human_goslim_unmapped.gaf -w goa_human_goslim.gaf