Chemical-master-equation model fitter for CpG methylation density data (Rust-accelerated)
Project description
CME Methylation Model
A chemical master equation (CME) kinetic model of CpG DNA methylation, fit to observed methylation distributions binned by local CpG density. The model captures cooperative methylation and demethylation dynamics and was used in the analyses reported in [paper title and citation here].
This code builds on prior work by Bonsu et al. (Ultrasens_DNAMethylation).
Repository layout
.
├── pyproject.toml # Packaging: builds the `cme-methylation` wheel
│ # (scripts as CLIs + bundled Rust backend)
├── cpg_density/ # Rust CLI: genome FASTA → per-CpG density BED
│ # (preprocessing step 1; build with cargo)
├── python/
│ └── cme_methylation/ # Installed Python package
│ ├── fit_cme.py # Main fitting / profile-likelihood / bootstrap script
│ │ # → installed as the `fit-cme` command
│ ├── build_density_distributions.py
│ │ # Preprocess WGBS bedGraph data into
│ │ # density-binned methylation histograms
│ │ # → installed as `build-density-distributions`
│ └── cpg_densities.py # Pure-Python CpG-density helper
├── modeling/
│ └── cme_rust/ # Rust backend (PyO3 + maturin), bundled into the wheel
│
├── notebooks/
│ ├── Plot Density and Methylation.ipynb
│ │ # Plots of raw WGBS methylation summary
│ │ # statistics by density bin
│ ├── Plot Model Output.ipynb
│ │ # Plots fit-vs-data, StE/StW, CoE/CoW etc.
│ │ # from fit_cme.py outputs
│ └── Plot Profile Likelihood Simple Model.ipynb
│ # Plots profile-likelihood curves
│
├── density_data/ # Example .distribution.npz files (PBMC, ages 0/26/103)
├── requirements.txt
├── LICENSE # MIT
└── notebook_cell_tool.py # Helper CLI for editing notebooks cell-by-cell
Installation
From PyPI
pip install cme-methylation
This installs the Rust backend (prebuilt wheel — no compiler needed) and two
command-line tools, fit-cme and build-density-distributions.
From source (development)
The package is a single maturin project that compiles
the Rust crate (modeling/cme_rust/) and installs the scripts as CLIs. From the
repo root:
pip install maturin
maturin develop --release # builds the Rust backend + installs fit-cme / build-density-distributions
The fitter falls back to a slower pure-Python path if the Rust backend is
unavailable, but the prebuilt wheel / maturin develop always includes it.
CpG density tool (Rust CLI)
cpg_density/ is a standalone Rust CLI that computes per-CpG density from a
genome FASTA — the first step of the from-scratch pipeline (see Usage §3). It is
a separate, plain cargo build (no Python, no maturin):
cd cpg_density
cargo build --release
# binary: cpg_density/target/release/cpg_density
Usage
1. Fit the model on the example data
The included density_data/ directory contains three example
.distribution.npz files (PBMC samples at ages 0, 26, and 103). To fit the
collaborative model on these:
fit-cme \
--input-dir ./density_data \
--output-dir ./figures/example_fit \
--name example_fit \
--normalize StW \
--shared-st --weight-by-cpg \
--n-u 1.25 --n-m 1.25 \
--bootstrap --n-jobs 8
Outputs land in ./figures/example_fit/:
example_fit_params.csv— fitted kinetic parameters + fit statisticsexample_fit_{age}_profile.csv— profile-likelihood curves (with--profile)example_fit_bootstrap_*— bootstrap replicates (with--bootstrap)
2. Plot the results
Open the notebooks under notebooks/. The paths in
Plot Model Output.ipynb and
Plot Profile Likelihood Simple Model.ipynb are relative and assume you run
Jupyter with the repo root as cwd.
Run one of the "CONFIG" cells in Plot Model Output.ipynb to
choose a dataset. The default All CpGs only config points at
modeling/figures/allcpg_stw_5meth_weight/ and the example density_data/
shipped with the repo. Other CONFIG cells reference dataset-specific paths
(/path/to/your/...) that you need to edit to point at your own data.
Notes on Plot Density and Methylation.ipynb
This notebook reads raw WGBS bedGraph data (via polars_bio) and is included
for reference. It is not runnable on the example .distribution.npz
files — you need your own WGBS dataset and absolute paths pointing at it.
3. (Optional) Run the full pipeline on your own data
Starting from a genome FASTA and per-CpG WGBS methylation, two preprocessing
steps produce the .distribution.npz that the fitter consumes.
3a. Compute CpG densities from the genome. cpg_density scans a genome
FASTA and writes, for every CpG, the number of CpGs within ±window bp divided
by window, as a 4-column BED (chrom start end density). The FASTA needs a
.fai index (samtools faidx genome.fa):
samtools faidx genome.fa # once, if no .fai exists yet
cpg_density/target/release/cpg_density genome.fa --window 50 --threads 8
# -> genome_50_cpg_densities.bed
Works with any genome/assembly (hg19, hg38, mm10, …); CpG matching is
case-insensitive, so soft-masked references are fine. Output is identical to the
reference Python implementation but ~75× faster (full hg19 in ~8 s). Only
--threads (default: all cores) and --window (default: 50) are tunable;
--window sets the density definition and must match across a study.
3b. Build .distribution.npz. build_density_distributions.py combines the
density BED from 3a with raw per-CpG methylation bedGraphs into the
.distribution.npz format fit_cme.py expects:
build-density-distributions \
--input-dir path/to/wgbs_bed_input \
--density-file genome_50_cpg_densities.bed \
--output-dir path/to/density_data_5meth \
--n-meth-bins 5 \
--n-bootstrap 100
Then fit as in §1, pointing --input-dir at your new output directory.
Input data format
.distribution.npz files contain:
| key | shape | description |
|---|---|---|
density_bins |
(D,) |
CpG density bin centers |
meth_bins |
(M+1,) |
Methylation ratio bin edges |
histograms |
(D, M) |
Count of CpGs per (density bin, methylation bin) |
spacings |
per-density-bin sequences | CpG spacings used in the cooperative-interaction kernel |
total_cpg |
(D,) |
Total CpGs per density bin |
Editing notebooks without opening them
Jupyter notebooks here are large. notebook_cell_tool.py is a small CLI for
listing, viewing, replacing, inserting and deleting individual cells without
loading the whole file:
python notebook_cell_tool.py list notebooks/<file>.ipynb
python notebook_cell_tool.py show notebooks/<file>.ipynb <cell-id-prefix>
python notebook_cell_tool.py replace notebooks/<file>.ipynb <cell-id> --source-file new.py
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cme_methylation-0.1.0.tar.gz.
File metadata
- Download URL: cme_methylation-0.1.0.tar.gz
- Upload date:
- Size: 61.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98ae915e899bfe6b1d9cf1c7ed0355f450d56e5e7764f0cee2e3314cd067bfd1
|
|
| MD5 |
b2bf17134e8b799047bbc5682137dcf6
|
|
| BLAKE2b-256 |
7921c7255eebe2a99fddc8b026ec635c13a3ce4b86b449fd72129b891cee152c
|
Provenance
The following attestation bundles were made for cme_methylation-0.1.0.tar.gz:
Publisher:
release.yml on nglaszik/cme-methylation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cme_methylation-0.1.0.tar.gz -
Subject digest:
98ae915e899bfe6b1d9cf1c7ed0355f450d56e5e7764f0cee2e3314cd067bfd1 - Sigstore transparency entry: 1960101685
- Sigstore integration time:
-
Permalink:
nglaszik/cme-methylation@b67cef57e18f976d527a4ac955f175a55aab855c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nglaszik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b67cef57e18f976d527a4ac955f175a55aab855c -
Trigger Event:
push
-
Statement type:
File details
Details for the file cme_methylation-0.1.0-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: cme_methylation-0.1.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 498.3 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db901a348876be2efad51fd846de06b4bfa0684a34fa679fc8522058805c3924
|
|
| MD5 |
aa5962b18b227ff4c718e26505908ce4
|
|
| BLAKE2b-256 |
21a60885934b6512d33ebba0a1a06feafada059342746650c1ac838a9bcbda31
|
Provenance
The following attestation bundles were made for cme_methylation-0.1.0-cp38-abi3-win_amd64.whl:
Publisher:
release.yml on nglaszik/cme-methylation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cme_methylation-0.1.0-cp38-abi3-win_amd64.whl -
Subject digest:
db901a348876be2efad51fd846de06b4bfa0684a34fa679fc8522058805c3924 - Sigstore transparency entry: 1960101875
- Sigstore integration time:
-
Permalink:
nglaszik/cme-methylation@b67cef57e18f976d527a4ac955f175a55aab855c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nglaszik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b67cef57e18f976d527a4ac955f175a55aab855c -
Trigger Event:
push
-
Statement type:
File details
Details for the file cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 597.4 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
961e5b06dc25f419bb818c7a17b77337f48600691273961f343fc6364473f84d
|
|
| MD5 |
32018541e6b945eef56a2896c4a4ec5f
|
|
| BLAKE2b-256 |
662549f7100db8b13eceae6e664c6e82292e77aab1ab4b9eb3b47d560803ebc6
|
Provenance
The following attestation bundles were made for cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
release.yml on nglaszik/cme-methylation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
961e5b06dc25f419bb818c7a17b77337f48600691273961f343fc6364473f84d - Sigstore transparency entry: 1960101755
- Sigstore integration time:
-
Permalink:
nglaszik/cme-methylation@b67cef57e18f976d527a4ac955f175a55aab855c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nglaszik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b67cef57e18f976d527a4ac955f175a55aab855c -
Trigger Event:
push
-
Statement type:
File details
Details for the file cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 557.9 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b5d6701002e170ae573b3777a42eebf2fe5d9659e2a6a77ffb185f6ac2aca72
|
|
| MD5 |
f032e26944624eb145fb80a54f9de9f1
|
|
| BLAKE2b-256 |
d7df88a31d3f4e680ee0478fda31ed6a31d9e0bd9f23e29be1a38d4c7ad62baf
|
Provenance
The following attestation bundles were made for cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:
Publisher:
release.yml on nglaszik/cme-methylation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -
Subject digest:
2b5d6701002e170ae573b3777a42eebf2fe5d9659e2a6a77ffb185f6ac2aca72 - Sigstore transparency entry: 1960101949
- Sigstore integration time:
-
Permalink:
nglaszik/cme-methylation@b67cef57e18f976d527a4ac955f175a55aab855c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nglaszik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b67cef57e18f976d527a4ac955f175a55aab855c -
Trigger Event:
push
-
Statement type:
File details
Details for the file cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 518.8 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc04285ee619e18e78272e23f643e0547dc9fb85263c0150c16cd89358f854a2
|
|
| MD5 |
66dca503b0f2228c90e1fad2232f5d7d
|
|
| BLAKE2b-256 |
9fc29ff91e1383862deb744b2124f075919502f95eccb943ecabede7ccca8601
|
Provenance
The following attestation bundles were made for cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl:
Publisher:
release.yml on nglaszik/cme-methylation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl -
Subject digest:
dc04285ee619e18e78272e23f643e0547dc9fb85263c0150c16cd89358f854a2 - Sigstore transparency entry: 1960102070
- Sigstore integration time:
-
Permalink:
nglaszik/cme-methylation@b67cef57e18f976d527a4ac955f175a55aab855c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nglaszik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b67cef57e18f976d527a4ac955f175a55aab855c -
Trigger Event:
push
-
Statement type: