Rule-Based Model Example

Inspect biological labels derived from learned rule cutoffs before HMM segmentation.

This example loads a packaged saved model so that the focus stays on the demonstrated operation.

Parameters

Change these explicit values for your own data or output location.

[1]:
from pathlib import Path

from methylseg import MethylSegPathway, MethylStateAssignmentMethod, MethylationStates
from methylseg.helper_classes import DATA_DIR
[2]:
REFERENCE_DIR = DATA_DIR / "reference_files"
OUTPUT_ROOT = Path("out")
CHROM = "chr1"

RESOLUTION = "450k"
SAMPLE_NAME = "TCGA-BD-A3EP-01A"
SAMPLE_FILE = REFERENCE_DIR / "TCGA-BD-A3EP-01A_450k.tsv.gz"
OUT_DIR = OUTPUT_ROOT / "rule_based_model_example"

Load a saved model and prepare the sample

[3]:
model = MethylSegPathway.get_pretrained_model(
    out_dir=OUT_DIR,
    resolution=RESOLUTION,
)
sample_info, removed_df = MethylSegPathway.prepare_sample_info(
    sample_name=SAMPLE_NAME,
    sample_file=SAMPLE_FILE,
    resolution=RESOLUTION,
    min_coverage=10,
)
sample_info.sample_id
[3]:
'TCGA-BD-A3EP-01A'

Rule-based labels and HMM regions

[4]:
model.state_assignment_method = MethylStateAssignmentMethod.DEFINITION
model.segmentor.state_assignment_method = MethylStateAssignmentMethod.DEFINITION
model.analyzer.pretty_print_rules()
regions_chr1 = model.generate_regions(sample_info=sample_info, chrom=CHROM)
model.plot_labels(label_source="rule_based", sample_info=sample_info, sample_info_removed=removed_df, chrom=CHROM)
model.plot_labels(label_source="hmm", sample_info=sample_info, sample_info_removed=removed_df, chrom=CHROM)
regions_chr1.head()
PMD:
0.158 <= beta <= 0.743
AND (40kb_int_pct >= 0.616 AND 40kb_std <= 0.290 AND 40kb_high_pct <= 0.374 AND 40kb_low_pct <= 0.376) OR (450kb_int_pct >= 0.596 AND 450kb_std <= 0.199 AND 450kb_high_pct <= 0.208 AND 450kb_low_pct <= 0.249)

Low methylation:
beta <= 0.158 AND NOT PMD

Intermediate methylation:
0.158 < beta < 0.743 AND NOT PMD

High methylation:
beta >= 0.743 AND NOT PMD

[4]:
CpG_chrm start end avg_beta probe_count state length
0 chr1 69590 855650 0.469783 46 PMD 786060
2 chr1 860612 872386 0.518261 23 INTERMEDIATE 11774
4 chr1 877488 889216 0.859375 16 HIGH 11728
7 chr1 900423 916059 0.382414 29 INTERMEDIATE 15636
8 chr1 916497 922011 0.459375 16 PMD 5514

Outputs

Raw region BED files are written under OUT_DIR. Cleaning writes chromosome-local metadata under OUT_DIR / "clean_regions", which is then used by cleaned overlays.