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.
Metadata
Release files for cme-methylation 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cme_methylation-0.1.0.tar.gz | 61.8 kB | With provenance | Details for cme_methylation-0.1.0.tar.gz |
Built distributions (wheels)
| File | Actions Reset | |||
|---|---|---|---|---|
| cme_methylation-0.1.0-cp38-abi3-win_amd64.whl | CPython 3.8 | abi3 | Windows x86-64 | Details for cme_methylation-0.1.0-cp38-abi3-win_amd64.whl |
| cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ x86-64 | Details for cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
| cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ ARM64 | Details for cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
| cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl | CPython 3.8 | abi3 | macOS 11.0+ ARM64 | Details for cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl |
Total release size: 2.2 MB
Release files / cme_methylation-0.1.0.tar.gz
| Download URL | cme_methylation-0.1.0.tar.gz |
|---|---|
| Size | 61.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
98ae915e899bfe6b1d9cf1c7ed0355f450d56e5e7764f0cee2e3314cd067bfd1
|
|
BLAKE2b-256 checksum How to use checksums |
7921c7255eebe2a99fddc8b026ec635c13a3ce4b86b449fd72129b891cee152c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 26, 2026.
Transparency logRelease files / cme_methylation-0.1.0-cp38-abi3-win_amd64.whl
| Download URL | cme_methylation-0.1.0-cp38-abi3-win_amd64.whl |
|---|---|
| Size | 498.3 kB |
| Tags | CPython 3.8 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
db901a348876be2efad51fd846de06b4bfa0684a34fa679fc8522058805c3924
|
|
BLAKE2b-256 checksum How to use checksums |
21a60885934b6512d33ebba0a1a06feafada059342746650c1ac838a9bcbda31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 26, 2026.
Transparency logRelease files / cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 597.4 kB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
961e5b06dc25f419bb818c7a17b77337f48600691273961f343fc6364473f84d
|
|
BLAKE2b-256 checksum How to use checksums |
662549f7100db8b13eceae6e664c6e82292e77aab1ab4b9eb3b47d560803ebc6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 26, 2026.
Transparency logRelease files / cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | cme_methylation-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 557.9 kB |
| Tags | CPython 3.8 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
2b5d6701002e170ae573b3777a42eebf2fe5d9659e2a6a77ffb185f6ac2aca72
|
|
BLAKE2b-256 checksum How to use checksums |
d7df88a31d3f4e680ee0478fda31ed6a31d9e0bd9f23e29be1a38d4c7ad62baf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 26, 2026.
Transparency logRelease files / cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
| Download URL | cme_methylation-0.1.0-cp38-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 518.8 kB |
| Tags | CPython 3.8 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
dc04285ee619e18e78272e23f643e0547dc9fb85263c0150c16cd89358f854a2
|
|
BLAKE2b-256 checksum How to use checksums |
9fc29ff91e1383862deb744b2124f075919502f95eccb943ecabede7ccca8601
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 26, 2026.
Transparency logSecurity for cme-methylation 0.1.0
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 5 files in this release have been uploaded with provenance metadata.
Attestation for cme_methylation-0.1.0.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 Jun 26, 2026.
Transparency logA 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.