Simulate DNA methylation and generate synthetic NGS reads with methylation tags.
Project description
pymethyl2sam
A Python package for simulating DNA methylation and generating synthetic NGS reads with methylation tags (MM/ML) in SAM/BAM format.
Overview
pymethyl2sam provides a comprehensive framework for simulating DNA methylation patterns and generating synthetic next-generation sequencing reads with proper methylation tags. The package supports both stochastic (random) and deterministic (pattern-based) read generation modes, making it suitable for testing methylation analysis pipelines and educational purposes.
Features
- Dual Read Generation Modes: Random (stochastic) and pattern-based (deterministic) simulation
- Flexible Methylation Modeling: Support for fully, partially, or unmethylated sites
- Valid SAM/BAM Output: Generates properly formatted files with MM/ML methylation tags
- Configurable Parameters: Coverage, read length, error rates, strand bias
- Zero-based Coordinates: Consistent with bioinformatics conventions
- Extensible Architecture: Modular, layered design for easy extension
Installation
From PyPI (when available)
pip install pymethyl2sam
From Source
git clone https://github.com/yoni-w/pymethyl2sam.git
cd pymethyl2sam
pip install -e .
Development Installation
git clone https://github.com/yoni-w/pymethyl2sam.git
cd pymethyl2sam
pip install -e ".[dev]"
Quick Start
Basic Usage (Random Mode)
from pymethyl2sam import MethylationSimulator
from pymethyl2sam.core import MethylationSite
from pymethyl2sam.core.genomics import StrandOrientation
from pymethyl2sam.core.reference_genome import Hg38ReferenceGenome
from pymethyl2sam.core.sequencing import ReadGenerator, RandomStrategy
from pymethyl2sam.simulator import SequencedChromosome, SequencedRegion
# Create simulator with random read generation
simulator = MethylationSimulator(
chromosomes=[
SequencedChromosome(
name="chr1",
length=20_000,
regions=[
SequencedRegion(
start=10100,
end=10450,
read_generator=ReadGenerator(strategy=RandomStrategy()),
)
],
cpg_sites=[
MethylationSite(position=10100, methylation_prob=1.0),
MethylationSite(position=10149, methylation_prob=1.0),
MethylationSite(position=10155, methylation_prob=0.0),
MethylationSite(position=10200, methylation_prob=0.0),
MethylationSite(position=10220, methylation_prob=1.0),
],
)
],
reference_genome=Hg38ReferenceGenome(),
)
# Generate reads and write to BAM
simulator.simulate_reads("output.bam")
Pattern-Based Simulation
from pymethyl2sam import MethylationSimulator
from pymethyl2sam.core import MethylationSite
from pymethyl2sam.core.genomics import StrandOrientation
from pymethyl2sam.core.reference_genome import Hg38ReferenceGenome
from pymethyl2sam.core.sequencing import ReadGenerator, PatternStrategy
from pymethyl2sam.simulator import SequencedChromosome, SequencedRegion
# Create simulator with pattern-based read generation
simulator = MethylationSimulator(
chromosomes=[
SequencedChromosome(
name="chr1",
length=20_000,
regions=[
SequencedRegion(
start=10100,
end=10250,
read_generator=ReadGenerator(
read_length=150,
strategy=PatternStrategy.from_offsets(
offsets=[0] * 10,
orientation=StrandOrientation.RANDOM,
),
),
)
],
cpg_sites=[
MethylationSite(position=10100, methylation_prob=1.0),
MethylationSite(position=10149, methylation_prob=1.0),
MethylationSite(position=10155, methylation_prob=0.0),
MethylationSite(position=10200, methylation_prob=0.0),
MethylationSite(position=10220, methylation_prob=1.0),
],
)
],
reference_genome=Hg38ReferenceGenome(),
)
# Generate reads and write to BAM
simulator.simulate_reads("pattern_output.bam")
Architecture
The package is organized in layers by functional responsibility:
- core/: Domain logic for methylation modeling, sequencing, and errors
- simulator/: Orchestration of read and methylation simulation
- io/: Parsers and I/O for FASTA, BED, YAML, JSON
- utils/: Logging, constants, shared helpers
Core Components
MethylationSimulator: Main class for orchestrating simulationSequencedChromosome: Represents a chromosome with defined regions and methylation sitesSequencedRegion: Defines genomic regions where reads should be generatedReadGenerator: Handles read generation with different strategies (Random/Pattern)MethylationSite: Represents individual methylation sites with probabilitiesReferenceGenomeProvider: Interface for accessing reference genome sequences
Configuration Examples
YAML Configuration Files
See examples/config_random.yaml and examples/templates_pattern.yaml for configuration file examples.
Coordinate System
All genomic coordinates follow zero-based, half-open intervals [start, end):
- A 5-base interval starting at position 100 is
[100, 105) - Read coordinates and methylation positions adhere to this system
- MM/ML tags follow strand-specific SAM/BAM conventions
Testing
Run the test suite:
pytest
Run with coverage:
pytest --cov=pymethyl2sam --cov-report=html
Development
Code Formatting
black src/ tests/
Linting
pylint src/pymethyl2sam/
Pre-commit Hooks
pre-commit install
Examples
See the examples/ directory for complete working examples:
simulate_cpgs.py: Demonstrates both random and pattern-based simulationconfig_random.yaml: YAML configuration for random modetemplates_pattern.yaml: YAML template for pattern-based simulation
Documentation
For detailed API documentation, visit: https://pymethyl2sam.readthedocs.io
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Citation
If you use pymethyl2sam in your research, please cite:
pymethyl2sam: A Python package for simulating DNA methylation and generating synthetic NGS reads
Yoni Weissler, 2025
Support
- Issues: https://github.com/yoni-w/pymethyl2sam/issues
- Documentation: https://pymethyl2sam.readthedocs.io
- Email: yoni.weissler@gmail.com
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 Distribution
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 pymethyl2sam-0.1.2.tar.gz.
File metadata
- Download URL: pymethyl2sam-0.1.2.tar.gz
- Upload date:
- Size: 27.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7f86effe787beabca64b4bd13e5a45a0ddb16a897f12153a6320e57b78a95c4
|
|
| MD5 |
8fd996a812dbeaf20b58598286bd407c
|
|
| BLAKE2b-256 |
f5ce77f0e7433a3c75b43cad17b3391207faa92af9838446e648b0c0bc60b418
|
File details
Details for the file pymethyl2sam-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pymethyl2sam-0.1.2-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25a7240b6d8673652816e6f7112b38ffb006b3608efc716eade2113c5fd3e7b1
|
|
| MD5 |
ee9b0244019f57e6a852de5030c06dcd
|
|
| BLAKE2b-256 |
f2915cbc6454fbb889c460f06f90031aae4db3322deb38bf207d7ebf4d00139e
|