Deep learning classifier for tumor-derived long reads using CpG methylation
Project description
ROCIT
ROCIT (Read Origin Classifier In Tumors) is a deep learning tool that classifies individual sequencing reads from long-read bulk tumor sequencing as tumor-derived or normal-derived. By leveraging CpG methylation patterns, ROCIT enables read-level resolution of tumor heterogeneity from PacBio sequencing data.
ROCIT currently supports training and prediction on PacBio HiFi Tumor BAMs with CpG methylation probabilities produced by Jasmine. Oxford Nanopore support is planned for future releases.
How It Works
ROCIT uses a multi-step approach:
- Data Preprocessing: Extracts CpG methylation from PacBio BAM files and labels the origin of a subset of reads based on somatic variants (SNVs) and loss of heterozygosity (LOH) events
- Input Features: Combines read-level methylation patterns with cell-type reference atlases and bulk sample methylation distributions
- Model Training: Trains a transformer-based neural network to classify the labelled read subset using chromosomal cross-validation
- Prediction: Applies the trained model to classify all reads in the sample
Installation
Via pip (Recommended)
pip install rocit
From Source
git clone https://github.com/tobybaker/rocit.git
cd rocit
pip install -e .
Requirements
- Python ≥ 3.10
- PyTorch ≥ 2.9.1
- PyTorch Lightning ≥ 2.6.0
- Polars ≥ 1.36.1
- pysam == 0.22.1
- Additional dependencies listed in pyproject.toml
Quick Start
Cell Atlas Requirement
ROCIT requires a reference cell-type methylation atlas derived from whole-genome bisulfite sequencing data (GSE186458). Download the pre-computed atlas:
wget [PLACEHOLDER_URL] -O reference/cell_atlas.parquet
Alternatively, you can generate the atlas from source.
Citation: Loyfer, N., et al. (2023). A DNA methylation atlas of normal human cell types. Nature.
Running ROCIT
ROCIT provides a complete end-to-end pipeline via the rocit run command:
rocit run --config config.yaml
For more control, you can run individual steps:
# 1. Extract methylation from BAM
rocit extract-bam-methylation --sample-id SAMPLE01 \
--sample-bam aligned.bam \
--output-dir methylation/
# 2. Compute methylation distribution
rocit extract-cpg-distribution --sample-id SAMPLE01 \
--methylation-dir methylation/ \
--output-dir distribution/
# 3. Label reads for training
rocit preprocess --config preprocess_config.yaml
# 4. Train the model
rocit train --config train_config.yaml
# 5. Run predictions
rocit predict --config predict_config.yaml
Configuration Files
ROCIT uses YAML configuration files for reproducibility and ease of use. Below are templates for each command.
Training Configuration (train_config.yaml)
sample_id: "SAMPLE01"
labelled_data: "preprocessing/labelled_methylation_data.parquet"
sample_distribution: "distribution/SAMPLE01_methylation_distribution.parquet"
cell_atlas: "reference/cell_atlas.parquet"
val_chromosomes: ["chr20", "chr21"]
test_chromosomes: ["chr22"]
output_dir: "training/"
cache_dir: "/scratch/"
Required fields:
sample_id: Unique identifier for the samplelabelled_data: Path to labelled methylation data from preprocessingsample_distribution: Path to sample methylation distributioncell_atlas: Path to cell-type methylation referenceval_chromosomes: Chromosomes reserved for validation (must not overlap with test)test_chromosomes: Chromosomes reserved for testing (must not overlap with validation)output_dir: Directory for training outputscache_dir: Cache directory for dataset processing (default:/scratch)
Prediction Configuration (predict_config.yaml)
sample_id: "SAMPLE01"
best_checkpoint_path: "training/SAMPLE01/version_0/checkpoints/best-checkpoint.ckpt"
sample_distribution: "distribution/SAMPLE01_methylation_distribution.parquet"
cell_atlas: "reference/cell_atlas.parquet"
read_store_dir: "methylation/" # OR use read_store for single file
output_dir: "predictions/"
cache_dir: "/scratch/"
Required fields:
sample_id: Unique identifier for the samplebest_checkpoint_path: Path to trained model checkpointsample_distribution: Path to sample methylation distributioncell_atlas: Path to cell-type methylation referenceread_storeORread_store_dir: Single file or directory of methylation parquet filesoutput_dir: Directory for prediction outputscache_dir: Cache directory (default:/scratch)
Preprocessing Configuration (preprocess_config.yaml)
sample_id: "SAMPLE01"
bam: "data/aligned.bam"
methylation_dir: "methylation/"
copy_number: "data/copy_number_segments.parquet"
variants: "data/somatic_variants.parquet"
haplotags: "data/haplotags.parquet"
haploblocks: "data/haploblocks.parquet"
snv_clusters: "data/snv_clusters.parquet"
snv_cluster_assignments: "data/snv_cluster_assignments.parquet"
output_dir: "preprocessing/"
Required fields:
sample_id: Unique identifier for the samplebam: Path to aligned BAM file with methylation tagsmethylation_dir: Directory containing extracted methylation datacopy_number: Path to copy number segments filevariants: Path to somatic variants filehaplotags: Path to read haplotype assignmentshaploblocks: Path to phased haplotype blockssnv_clusters: Path to SNV cluster assignmentsoutput_dir: Directory for preprocessing outputs
Optional fields:
snv_cluster_assignments: Path to SNV cluster assignments (if not provided, will be inferred)
Full Pipeline Configuration (run_config.yaml)
sample_id: "SAMPLE01"
bam: "data/aligned.bam"
bam_index: "data/aligned.bam.bai"
copy_number: "data/copy_number_segments.parquet"
variants: "data/somatic_variants.parquet"
haplotags: "data/haplotags.parquet"
haploblocks: "data/haploblocks.parquet"
snv_clusters: "data/snv_clusters.parquet"
snv_cluster_assignments: "data/snv_cluster_assignments.parquet"
cell_atlas: "reference/cell_atlas.parquet"
val_chromosomes: ["chr20", "chr21"]
test_chromosomes: ["chr22"]
min_mapq: 0
workers: 8
output_dir: "output/"
cache_dir: "/scratch/"
Required fields:
sample_id: Unique identifier for the samplebam: Path to aligned BAM file with methylation tagscopy_number: Path to copy number segments filevariants: Path to somatic variants filehaplotags: Path to read haplotype assignmentshaploblocks: Path to phased haplotype blockssnv_clusters: Path to SNV cluster assignmentscell_atlas: Path to cell-type methylation referenceval_chromosomes: Chromosomes reserved for validation (must not overlap with test)test_chromosomes: Chromosomes reserved for testing (must not overlap with validation)output_dir: Directory for all pipeline outputscache_dir: Cache directory for dataset processing
Optional fields:
bam_index: BAM index file (auto-detected if not provided)snv_cluster_assignments: Path to SNV cluster assignments (if not provided, will be inferred)chromosomes: Specific chromosomes to process (defaults to chr1-chrY)min_mapq: Minimum mapping quality for reads (default: 0)workers: Number of parallel workers for BAM processing (default: 1)
Command Reference
rocit run
Run the complete ROCIT pipeline from BAM to predictions.
rocit run --config run_config.yaml
Pipeline steps:
- Extract BAM methylation
- Compute CpG distribution
- Label reads using somatic variants
- Train classification model
- Generate predictions
Outputs:
output/methylation/: Per-chromosome methylation dataoutput/distribution/: Sample methylation distributionoutput/preprocessing/: Labelled reads and methylation dataoutput/training/: Model checkpoints and training logsoutput/predictions/: Final tumor/normal predictions
rocit train
Train a ROCIT classification model.
rocit train --config train_config.yaml
Outputs:
{output_dir}/{sample_id}/version_X/checkpoints/best-checkpoint.ckpt: Best model{output_dir}/{sample_id}/version_X/metrics.csv: Training metrics (loss, AUROC, etc.)
Training parameters (modifiable in code via TrainingParams in the python API):
- Model architecture: 384-dim, 6 heads, 3 layers
- Max epochs: 100 with early stopping (patience=10)
- Learning rate: 1e-4 with warmup
- Batch size: 256
- Early Stopping Metric: Validation AUROC
rocit predict
Generate predictions using a trained model.
rocit predict --config predict_config.yaml
Outputs:
{output_dir}/{sample_id}_tumor_origin_predictions.parquet: Read-level predictions with columns:read_index: Unique read identifierchromosome: Chromosome nametumor_probability: Predicted probability of tumor origin (0-1)
rocit preprocess
Label reads for training using somatic variant information.
rocit preprocess --config preprocess_config.yaml
Outputs:
{output_dir}/labelled_reads.parquet: Read labels (tumor/normal){output_dir}/labelled_methylation_data.parquet: Methylation data with labels
rocit extract-bam-methylation
Extract CpG methylation from PacBio BAM files.
rocit extract-bam-methylation \
--sample-id SAMPLE01 \
--sample-bam aligned.bam \
--output-dir methylation/ \
--workers 8 \
--min-mapq 0 \
--chromosomes "chr1 chr2 chr3"
Options:
--sample-id: Sample identifier for output naming--sample-bam: Input BAM file with MM/ML tags--output-dir: Output directory--index: BAM index file (optional, auto-detected)--min-mapq: Minimum mapping quality (default: 0)--workers: Number of parallel workers (default: 1)--chromosomes: Space-separated chromosomes to process (default: chr1-chrY)
Outputs:
{output_dir}/{chromosome}_cpg_methylation_data.parquetfor each chromosome
rocit extract-cpg-distribution
Aggregate methylation distribution from extracted data.
rocit extract-cpg-distribution \
--sample-id SAMPLE01 \
--methylation-dir methylation/ \
--output-dir distribution/
Outputs:
{output_dir}/{sample_id}_methylation_distribution.parquet: An aggregated distribution of methylation values across the sample, used for model context.
Output Files
Prediction Output
The primary output from ROCIT is a parquet file with read-level predictions:
import polars as pl
predictions = pl.read_parquet("predictions/SAMPLE01_tumor_origin_predictions.parquet")
print(predictions.head())
# Example output:
# ┌────────────┬────────────┬───────────────────┐
# │ read_index │ chromosome │ tumor_probability │
# ├────────────┼────────────┼───────────────────┤
# │ 1001 │ chr1 │ 0.87 │
# │ 1002 │ chr1 │ 0.12 │
# │ 1003 │ chr1 │ 0.94 │
# └────────────┴────────────┴───────────────────┘
Training Metrics
Training progress is logged to CSV:
metrics = pl.read_csv("training/SAMPLE01/version_0/metrics.csv")
# Contains: epoch, train_loss, train_auroc, val_loss, val_auroc, etc.
Model Architecture
ROCIT uses a transformer-based architecture designed for long-read methylation data:
- Input: CpG methylation patterns, cell atlas features, sample distribution features
Python API
ROCIT can also be used programmatically:
import rocit
from pathlib import Path
# Training
train_result = rocit.train(
sample_id="SAMPLE01",
labelled_data=labelled_df,
sample_distribution=distribution_df,
cell_atlas=atlas_df,
val_chromosomes=["chr20", "chr21"],
test_chromosomes=["chr22"],
output_dir=Path("training/"),
cache_dir=Path("/scratch/")
)
# Prediction
predictions = rocit.predict(
sample_id="SAMPLE01",
best_checkpoint_path=Path("training/best-checkpoint.ckpt"),
read_store=[methylation_lazy_df], # List of polars DataFrames or LazyFrames
sample_distribution=distribution_df,
cell_atlas=atlas_df,
output_dir=Path("predictions/"),
cache_dir=Path("/scratch/")
)
Generating the Cell Atlas from Source
If you prefer to build the cell-type methylation atlas yourself rather than using the pre-computed version, you can use the provided generation script. This process downloads and processes whole-genome bisulfite sequencing data from GEO accession GSE186458, which contains methylation profiles across diverse normal human cell types.
Requirements
pip install pyBigWig polars tqdm
Usage
The script provides two modes: automatic download and processing, or processing from pre-downloaded files.
Automatic Download and Processing
This will download ~328 GB of raw data from GEO:
python setup_scripts/generate_cell_map_df.py \
--download /path/to/download_directory/ \
--output reference/cell_atlas.parquet
You will be prompted to confirm before the download begins.
Process Pre-Downloaded Files
If you already have the bigwig files:
python setup_scripts/generate_cell_map_df.py \
--data-dir /path/to/extracted_bigwig_files/ \
--output reference/cell_atlas.parquet
What the Script Does
- Downloads (if using
--download): Fetches the GSE186458 tar archive from NCBI GEO - Extracts: Unpacks
*.hg38.bigwigfiles containing methylation data per cell type - Processes: For each cell type, aggregates methylation values across biological replicates
- Combines: Joins all cell types into a single reference atlas
- Outputs: Saves a Parquet file with columns:
chromosome: chr1-chr22, chrXposition: CpG genomic positionaverage_methylation_{cell_type}: Mean methylation value (0-1) for each cell type
The resulting atlas enables ROCIT to contextualize read-level methylation patterns using cell-type-specific reference signatures.
Dataset Information
GSE186458 contains whole-genome bisulfite sequencing (WGBS) data from normal human tissues and cell types. Each cell type typically has multiple biological replicates, which the script averages to produce robust methylation estimates.
Citation: Loyfer, N., et al. (2023). A DNA methylation atlas of normal human cell types. Nature.
Data Format Specifications
Copy Number Segments
Required columns:
chromosome: Chromosome name (e.g., "chr1")start: Segment start positionend: Segment end positionminor_cn: Minor allele copy numbermajor_cn: Major allele copy numbertotal_cn: Total copy numberpurity: Tumor purity estimatenormal_total_cn: Normal total copy number (typically 2 except for chrX/chrY in XY subjects)
Somatic Variants
Required columns:
chromosome: Chromosome nameposition: Variant positionref: Reference allelealt: Alternate allele- Additional variant metadata as needed
Haplotags
Required columns:
read_index: Unique read identifierchromosome: Chromosome namehaplotag: Haplotype assignment (1 or 2)start: Read start positionend: Read end position
Haploblocks
Required columns:
chromosome: Chromosome nameblock_start: Block start positionblock_end: Block end positionblock_size: Size of phased blockhaploblock_id: Unique block identifier
SNV Clusters
Required columns:
cluster_id: Unique cluster identifiercluster_ccf: Cancer cell fraction for the cluster (0-1)cluster_fraction: Fraction of variants assigned to this cluster (0-1)
SNV Cluster Assignments
This file is optional. If not provided, cluster assignments will be inferred using a binomial model.
Required columns:
chromosome: Chromosome nameposition: Variant positioncluster_id: Cluster identifier (must match IDs in SNV Clusters)n_copies: Number of allelic copies of the variant.
License
ROCIT is licensed under the BSD 3-Clause License. See the LICENSE file for details.
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
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 rocit-0.1.1.tar.gz.
File metadata
- Download URL: rocit-0.1.1.tar.gz
- Upload date:
- Size: 38.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb7e99e51468753c451e4c78838cad25306b697b84aed22f2bfed7576c55bc52
|
|
| MD5 |
7d1b12dcedf7e2b7452648a423458d1b
|
|
| BLAKE2b-256 |
1ef790f33a2a9dde51fb203b3f3aa0de9ec1966db1f86caa2a1f01faaf570b5e
|
Provenance
The following attestation bundles were made for rocit-0.1.1.tar.gz:
Publisher:
python-publish.yml on tobybaker/rocit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rocit-0.1.1.tar.gz -
Subject digest:
eb7e99e51468753c451e4c78838cad25306b697b84aed22f2bfed7576c55bc52 - Sigstore transparency entry: 1021042838
- Sigstore integration time:
-
Permalink:
tobybaker/rocit@084bd48b546f751d133c81223ddcb888e0856b55 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/tobybaker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@084bd48b546f751d133c81223ddcb888e0856b55 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rocit-0.1.1-py3-none-any.whl.
File metadata
- Download URL: rocit-0.1.1-py3-none-any.whl
- Upload date:
- Size: 39.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d88379a4dc0659274e8adc6553c5b24142d144e53f510f182d8695aeeb85785d
|
|
| MD5 |
6c1343bfa0b7f3a791fcda3e635f41d7
|
|
| BLAKE2b-256 |
a4307d3ce714562a92613f1bcb2d9387c26fb31f8a50595426fabc8cad0bfb35
|
Provenance
The following attestation bundles were made for rocit-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on tobybaker/rocit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rocit-0.1.1-py3-none-any.whl -
Subject digest:
d88379a4dc0659274e8adc6553c5b24142d144e53f510f182d8695aeeb85785d - Sigstore transparency entry: 1021042934
- Sigstore integration time:
-
Permalink:
tobybaker/rocit@084bd48b546f751d133c81223ddcb888e0856b55 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/tobybaker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@084bd48b546f751d133c81223ddcb888e0856b55 -
Trigger Event:
push
-
Statement type: