Skip to main content

A Python port of the MAGICAL hierarchical Bayesian Gibbs sampler for regulatory circuit inference.

Project description

pymagical

pymagical is a high-performance Python port of the MAGICAL (Multiome Accessibility Gene Integration Calling and Looping) algorithm. It provides an automated pipeline for inferring functional regulatory circuits—triads of Transcription Factors (TFs), cis-regulatory elements (Peaks), and target Genes—from paired single-cell RNA-seq and ATAC-seq data.

Key Features

  • Fast & Optimized: Significantly faster than the original MATLAB implementation (~1.9x speedup in sampling, up to 15x speedup in data loading).
  • Intelligent IO Caching: Automatically caches large sparse matrices and genomic metadata into PyArrow-backed Parquet and NumPy formats for near-instant subsequent loads.
  • Biological Directionality: Unlike the original version, pymagical automatically classifies inferred circuits as activators (+) or repressors (-) by analyzing the continuous regression weights calculated during Gibbs sampling.
  • HPC Ready: Includes built-in support for high-memory Slurm environments and allows for detailed weight-history dumping for downstream statistical distribution analysis.

Installation

This project uses uv for environment management.

# Clone the repository
git clone <repo-url>
cd pymagical

# Sync the environment
uv sync

Quick Start

1. Command Line Usage

Run the circuit inference directly from your terminal:

# Run with default demo data (astrocytes) for 500 iterations
uv run python main.py --iter 500 --outdir results/

# Run with custom data and dump weight history
uv run python main.py 
    --iter 1000 
    --prefix my_sample
    --rna-counts path/to/rna.txt 
    --atac-counts path/to/atac.txt 
    --dump-weights

2. Programmatic Usage

Integrate pymagical into your own Python pipelines:

from pymagical import run_magical

run_magical(
    cand_gene_file="genes.txt",
    cand_peak_file="peaks.txt",
    # ... other file paths ...
    iteration_num=500,
    output_file="my_results.txt"
)

Output Notation

The final triad list includes a biological effect annotation for every identified TF:

TF_Name (Confidence_Probability, Overall_Effect [L_dir, B_dir])

  • Overall Effect: + (Activator) or - (Repressor).
  • L_dir (Looping): Direction of Peak-to-Gene effect.
  • B_dir (Binding): Direction of TF-to-Peak effect.

Example: STAT5B (0.85, + [+,+]) indicates an 85% confident activator that opens a peak which subsequently increases gene expression.

Documentation

Evaluation and Comparison

The eval/ directory contains tools for comparing pymagical against the original MATLAB implementation and profiling performance.

  • eval/tests/compare_results.py: Compare fidelity and performance across implementations.
  • eval/benchmarks/profile_run.py: Profile the runtime of different execution stages.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymagical-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymagical-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pymagical-0.1.0.tar.gz.

File metadata

  • Download URL: pymagical-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pymagical-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23c09e600a85e1800c12368d98f0bc7459f8ed4691f852e18da3c54eeb878a6f
MD5 5cd60341fd7e955984d8cd03e26933b7
BLAKE2b-256 c4845f1367734b6f7d5b5802fbee3a1949162007de636729908815731da6608b

See more details on using hashes here.

File details

Details for the file pymagical-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pymagical-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pymagical-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8fa8f35e41faa1457c1050a641a070cf4ae061fcbda5814234bdfaaaf8f5d7e
MD5 b7e9affe0c09fce9443ff98d5bca6b67
BLAKE2b-256 57be77038578d0ea617861539a094678970658d90d84f296c5acfedf8014fb42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page