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-1.0.3.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymagical-1.0.3.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pymagical-1.0.3.tar.gz
Algorithm Hash digest
SHA256 c2ff1efd9a574ef7b8dc04d0fc150feef4333eaeb44b89aa9e515ab0c2e9268c
MD5 60e0ad75d6cf2b5f4447e5cbf8d28ea4
BLAKE2b-256 2005a86ce91e42c1232380e23cc4998ecc8694686fdbaaf1f76b5329d51ebfd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymagical-1.0.3.tar.gz:

Publisher: python-publish.yml on csun0/pymagical

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pymagical-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pymagical-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dc95141ac7875b43d89ce0d50cad17ab347cded12033d6610b959ba415ecb96c
MD5 4b913a8a31289bf6a7d9e721495fd0e6
BLAKE2b-256 31a0f9f865db0bd40fb6daf40e79c5c41c2b121468a8a871c7dfeebf655c65c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymagical-1.0.3-py3-none-any.whl:

Publisher: python-publish.yml on csun0/pymagical

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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