Skip to main content

pymagical

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

The methodology is based on the framework described in:

Chen et al., "Mapping disease regulatory circuits at cell-type resolution from single-cell multiomics data" Nature Computational Science 2023. (Available here)

Key Features

  • IO Caching: Caches large sparse matrices and genomic metadata into PyArrow-backed Parquet and NumPy formats for near-instant subsequent loads (~15x faster than re-parsing text).
  • Numba-Accelerated Sampling: JIT-compiled kernels give ~28x faster Gibbs sampling than the original MATLAB implementation (astrocytes, 2000 iterations).
  • Biological Directionality: Classifies inferred circuits as activators (+) or repressors (-) from the continuous regression weights.

Documentation

For detailed information on setup, biological methodology, and validation, please refer to the following guides:

Installation

For users (from PyPI)

Install pymagical in one step with pip (or uv pip):

pip install pymagical

For the interactive HTML report (pymagical viz), install the optional viz extra:

pip install "pymagical[viz]"

Requires Python ≥ 3.10.

For developers (from source)

Clone the repository and sync the environment with uv; this creates a .venv and installs the package (editable) plus the dev dependency group (pytest, plotting, and the viz libraries):

git clone https://github.com/csun0/pymagical.git
cd pymagical
uv sync

Then run commands inside the environment with uv run:

uv run pymagical --help     # CLI
uv run pytest               # test suite

The package version is derived from git tags via hatch-vcs. A full clone (or a release tarball) builds fine; if you build from a source tree with no git history the version falls back to 0.0.0.

Quick Start

1. Command Line Usage

# Setup a new workspace
mkdir pymagical_demo
cd pymagical_demo

# Download demo input files
curl -fLC - --retry 3 -O /
https://github.com/csun0/pymagical/releases/download/data-v1.0/magical_demo_input_files.tar.gz
tar -xzf magical_demo_input_files.tar.gz

# Install pymagical in a virtual environment
python -m venv .venv
source .venv/bin/activate
pip install pymagical

# Run with default data for 500 iterations using Numba
pymagical run --main-dir Demo_input_files --cell-dir Demo_input_files --iter 500 --use-numba --outdir results/

# Generate an interactive HTML visualization report (requires [viz] extra)
pymagical viz results/magical_py_500.txt

# Open the light circuit dashboard for all result sets in a directory
pymagical dashboard --input_dir results/

Run pymagical --help to see all available flags and subcommands.

The dashboard is included in the installed package; no clone or separate update script is needed. --input_dir is required (--input-dir also works). It reads result .txt files directly from that directory, skipping matrix and timing sidecars, saves results/dashboard.html, and opens it in your browser. Run again after adding or updating results to refresh the saved dashboard.

On a remote machine, use pymagical dashboard --input_dir results/ --no-open, then copy the saved HTML to your computer and open it. Use --output PATH.html to save elsewhere. Charts, tables, and fonts use CDNs, so the browser needs internet access. No [viz] extra is required for the dashboard.

The legacy helper accepts the same required input flag: uv run src/pymagical/ui/update_ui.py --input_dir results/ --no-open.

2. Run the Downloadable Demo

From the cloned repository root, run uv sync as described above, then download and extract the demo inputs (about 353 MB compressed, 1.5 GB extracted):

curl --fail --location --retry 3 --continue-at - \
  --output magical_demo_input_files.tar.gz \
  https://github.com/csun0/pymagical/releases/download/data-v1.0/magical_demo_input_files.tar.gz
tar -xzf magical_demo_input_files.tar.gz

The CLI defaults match the demo filenames. All files are in the same folder, so use --cell-dir Demo_input_files with --main-dir Demo_input_files. Both directory options resolve independently from the working directory. The bundled hg38_Refseq.txt is header-free and ready to use. Start with a short smoke run:

uv run pymagical run \
  --main-dir Demo_input_files --cell-dir Demo_input_files \
  --iter 10 --use-numba \
  --prefix demo_smoke --outdir outputs/demo_smoke

After the smoke run succeeds, rerun the same command with --iter 2000, --prefix demo, and --outdir outputs/demo. The main result will be outputs/demo/demo_py_2000.txt, alongside B/L matrices and timing statistics. Keep Demo_input_files/.magical_cache/ to reuse parsed inputs on subsequent runs. Sampling starts a new chain each time; it does not resume from the smoke run.

3. Programmatic Usage

run_magical takes individual file paths (all required); see the tutorial for the full argument list.

from pymagical import run_magical

run_magical(
    cand_gene_file="genes.txt",
    cand_peak_file="peaks.txt",
    # ... remaining RNA/ATAC/motif/TAD/refseq file paths (all required) ...
    iteration_num=2000,
    use_numba=True,
    output_file="my_results.txt",
)

Citation

If you use MAGICAL in your research, please cite:

@article{chen_mapping_2023,
	title = {Mapping disease regulatory circuits at cell-type resolution from single-cell multiomics data},
	author = {Chen, Xi and Wang, Yuan and Cappuccio, Antonio and Cheng, Wan-Sze and Zamojski, Frederique Ruf and Nair, Venugopalan D. and Miller, Clare M. and Rubenstein, Aliza B. and Nudelman, German and Tadych, Alicja and Theesfeld, Chandra L. and Vornholt, Alexandria and George, Mary-Catherine and Ruffin, Felicia and Dagher, Michael and Chawla, Daniel G. and Soares-Schanoski, Alessandra and Spurbeck, Rachel R. and Ndhlovu, Lishomwa C. and Sebra, Robert and Kleinstein, Steven H. and Letizia, Andrew G. and Ramos, Irene and Fowler, Vance G. and Woods, Christopher W. and Zaslavsky, Elena and Troyanskaya, Olga G. and Sealfon, Stuart C.},
	journal = {Nature Computational Science},
	year = {2023},
	month = jul,
	doi = {10.1038/s43588-023-00476-5},
	url = {https://www.nature.com/articles/s43588-023-00476-5},
}

Metadata

Release files for pymagical 1.5.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymagical 1.5.4
File Size Uploaded Provenance Actions
pymagical-1.5.4.tar.gz 36.7 kB With provenance Details for pymagical-1.5.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pymagical 1.5.4
File Interpreter ABI Platform Actions
pymagical-1.5.4-py3-none-any.whl Python 3 none any Details for pymagical-1.5.4-py3-none-any.whl

Total release size: 77.6 kB

Release files / pymagical-1.5.4.tar.gz

Download URL pymagical-1.5.4.tar.gz
Size 36.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7bd97b9c15a389733ed92582b93d2c3d78445b6b179412e4142d4c2421efca9c
BLAKE2b-256 checksum
How to use checksums
20cb16ca237efabf3be79c377db92e220c0728a501562b899f88f2407fa976a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / pymagical-1.5.4-py3-none-any.whl

Download URL pymagical-1.5.4-py3-none-any.whl
Size 40.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb5efea17ab9f4d6d44b1c01be77d57cca27a9acc06c337ec30912cf96145c2e
BLAKE2b-256 checksum
How to use checksums
4206ee83348676dfc04e184908daa772818930cf149b37817d38516b5eeb9fa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Security for pymagical 1.5.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pymagical-1.5.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page