{ "cells": [ { "cell_type": "markdown", "id": "b84b0de9", "metadata": {}, "source": [ "# Rule-Based Model Example\n", "\n", "Inspect biological labels derived from learned rule cutoffs before HMM segmentation.\n", "\n", "This example loads a packaged saved model so that the focus stays on the demonstrated operation." ] }, { "cell_type": "markdown", "id": "4e3f108f", "metadata": {}, "source": [ "## Parameters\n", "\n", "Change these explicit values for your own data or output location." ] }, { "cell_type": "code", "execution_count": 1, "id": "9c0d4ba7", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "from methylseg import MethylSegPathway, MethylStateAssignmentMethod, MethylationStates\n", "from methylseg.helper_classes import DATA_DIR" ] }, { "cell_type": "code", "execution_count": 2, "id": "05534bea", "metadata": {}, "outputs": [], "source": [ "REFERENCE_DIR = DATA_DIR / \"reference_files\"\n", "OUTPUT_ROOT = Path(\"out\")\n", "CHROM = \"chr1\"\n", "\n", "RESOLUTION = \"450k\"\n", "SAMPLE_NAME = \"TCGA-BD-A3EP-01A\"\n", "SAMPLE_FILE = REFERENCE_DIR / \"TCGA-BD-A3EP-01A_450k.tsv.gz\"\n", "OUT_DIR = OUTPUT_ROOT / \"rule_based_model_example\"" ] }, { "cell_type": "markdown", "id": "5f85b923", "metadata": {}, "source": [ "## Load a saved model and prepare the sample" ] }, { "cell_type": "code", "execution_count": 3, "id": "dfdd1720", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'TCGA-BD-A3EP-01A'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = MethylSegPathway.get_pretrained_model(\n", " out_dir=OUT_DIR,\n", " resolution=RESOLUTION,\n", ")\n", "sample_info, removed_df = MethylSegPathway.prepare_sample_info(\n", " sample_name=SAMPLE_NAME,\n", " sample_file=SAMPLE_FILE,\n", " resolution=RESOLUTION,\n", " min_coverage=10,\n", ")\n", "sample_info.sample_id" ] }, { "cell_type": "markdown", "id": "1f883f32", "metadata": {}, "source": [ "## Rule-based labels and HMM regions" ] }, { "cell_type": "code", "execution_count": 4, "id": "5a5090c1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PMD:\n", "0.158 <= beta <= 0.743\n", "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)\n", "\n", "Low methylation:\n", "beta <= 0.158 AND NOT PMD\n", "\n", "Intermediate methylation:\n", "0.158 < beta < 0.743 AND NOT PMD\n", "\n", "High methylation:\n", "beta >= 0.743 AND NOT PMD\n", "\n" ] }, { "data": { "text/html": [ " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "int64", "type": "integer" }, { "name": "CpG_chrm", "rawType": "str", "type": "string" }, { "name": "start", "rawType": "int64", "type": "integer" }, { "name": "end", "rawType": "int64", "type": "integer" }, { "name": "avg_beta", "rawType": "float64", "type": "float" }, { "name": "probe_count", "rawType": "int64", "type": "integer" }, { "name": "state", "rawType": "object", "type": "unknown" }, { "name": "length", "rawType": "int64", "type": "integer" } ], "ref": "ed0330b1-5189-490b-bee6-85a3f771c54b", "rows": [ [ "0", "chr1", "69590", "855650", "0.46978260869565225", "46", "PMD", "786060" ], [ "2", "chr1", "860612", "872386", "0.5182608695652173", "23", "INTERMEDIATE", "11774" ], [ "4", "chr1", "877488", "889216", "0.8593750000000001", "16", "HIGH", "11728" ], [ "7", "chr1", "900423", "916059", "0.38241379310344825", "29", "INTERMEDIATE", "15636" ], [ "8", "chr1", "916497", "922011", "0.45937500000000003", "16", "PMD", "5514" ] ], "shape": { "columns": 7, "rows": 5 } }, "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CpG_chrmstartendavg_betaprobe_countstatelength
0chr1695908556500.46978346PMD786060
2chr18606128723860.51826123INTERMEDIATE11774
4chr18774888892160.85937516HIGH11728
7chr19004239160590.38241429INTERMEDIATE15636
8chr19164979220110.45937516PMD5514
\n", "
" ], "text/plain": [ " CpG_chrm start end avg_beta probe_count state length\n", "0 chr1 69590 855650 0.469783 46 PMD 786060\n", "2 chr1 860612 872386 0.518261 23 INTERMEDIATE 11774\n", "4 chr1 877488 889216 0.859375 16 HIGH 11728\n", "7 chr1 900423 916059 0.382414 29 INTERMEDIATE 15636\n", "8 chr1 916497 922011 0.459375 16 PMD 5514" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.state_assignment_method = MethylStateAssignmentMethod.DEFINITION\n", "model.segmentor.state_assignment_method = MethylStateAssignmentMethod.DEFINITION\n", "model.analyzer.pretty_print_rules()\n", "regions_chr1 = model.generate_regions(sample_info=sample_info, chrom=CHROM)\n", "model.plot_labels(label_source=\"rule_based\", sample_info=sample_info, sample_info_removed=removed_df, chrom=CHROM)\n", "model.plot_labels(label_source=\"hmm\", sample_info=sample_info, sample_info_removed=removed_df, chrom=CHROM)\n", "regions_chr1.head()" ] }, { "cell_type": "markdown", "id": "dad2e8cc", "metadata": {}, "source": [ "## Outputs\n", "\n", "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." ] } ], "metadata": { "kernelspec": { "display_name": "jt_wgbs_analysis", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.14" } }, "nbformat": 4, "nbformat_minor": 5 }