$ show dpeerlab/spectra
This repository implements SPECTRA (Supervised discovery of interpretable gene programs from single-cell data), a PyTorch-based library for analyzing single-cell RNA sequencing data. The core functionality involves fitting a model that combines gene expression data with prior knowledge from gene sets to discover biologically interpretable factors. The system takes an AnnData object containing single-cell expression data, cell type annotations, and a gene set dictionary, then learns factors representing gene programs associated with cellular processes. Key features include cell-type specific factor modeling, GPU acceleration options, and multiple optimization approaches (standard gradient descent and EM variants). The codebase demonstrates good software engineering practices with separable components for utility functions, initialization strategies, and eigenvalue-based factor estimation.
The LLM identified 3 files worth highlighting for reuse:
Implements the BEMA (Bulk Eigenvalue Matching Analysis) algorithm for estimating the number of factors from single-cell data. This self-contained component performs bootstrap-based eigenvalue analysis to determine optimal model complexity without supervision. It demonstrates non-obvious statistical skill by implementing Algorithm 2 from arXiv:2006.00436 and could be reused in other factor analysis contexts with minimal dependencies (numpy, scipy, opt_einsum, tqdm).
Contains reusable utilities for gene set analysis including overlap_coefficient() (a clean implementation of the Szymkiewicz-Simpson overlap metric), label_marker_genes() (which resolves ties by gene set size), and functions to convert gene sets to adjacency matrices. These functions are largely self-contained, solve recurring problems in genomic data processing, and demonstrate careful handling of biological data constraints. The overlap coefficient implementation is particularly notable for its correctness and efficiency.
Provides intelligent model initialization using Mimno coherence scores from topic modeling to set initial parameter values based on gene set coherence. This approach improves convergence by starting optimization in promising regions of parameter space. The component is self-contained (requiring only numpy, torch, and collections), implements a non-obvious initialization strategy adapted from topic modeling, and could be extracted for use in other matrix factorization or neural network initialization scenarios.