{ "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": [ "
| \n", " | CpG_chrm | \n", "start | \n", "end | \n", "avg_beta | \n", "probe_count | \n", "state | \n", "length | \n", "
|---|---|---|---|---|---|---|---|
| 0 | \n", "chr1 | \n", "69590 | \n", "855650 | \n", "0.469783 | \n", "46 | \n", "PMD | \n", "786060 | \n", "
| 2 | \n", "chr1 | \n", "860612 | \n", "872386 | \n", "0.518261 | \n", "23 | \n", "INTERMEDIATE | \n", "11774 | \n", "
| 4 | \n", "chr1 | \n", "877488 | \n", "889216 | \n", "0.859375 | \n", "16 | \n", "HIGH | \n", "11728 | \n", "
| 7 | \n", "chr1 | \n", "900423 | \n", "916059 | \n", "0.382414 | \n", "29 | \n", "INTERMEDIATE | \n", "15636 | \n", "
| 8 | \n", "chr1 | \n", "916497 | \n", "922011 | \n", "0.459375 | \n", "16 | \n", "PMD | \n", "5514 | \n", "