{ "cells": [ { "cell_type": "markdown", "id": "307989be", "metadata": {}, "source": [ "# KMeans-Based Model Example\n", "\n", "This notebook shows how to run the packaged methylseg pathway using KMeans-derived state assignments before HMM segmentation.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "e90dbbc1", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import pandas as pd\n", "\n", "from methylseg import (\n", " MethylDataPrep,\n", " MethylSegPathway,\n", " MethylStateAssignmentMethod,\n", " MethylationStates,\n", ")\n", "from methylseg.helper_classes import DATA_DIR" ] }, { "cell_type": "code", "execution_count": 3, "id": "c1c7433a", "metadata": {}, "outputs": [], "source": [ "REFERENCE_DIR = DATA_DIR / \"reference_files\"\n", "\n", "OUT_DIR = Path(\"out\") / \"kmeans_based_model_output\"\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "86edc67d", "metadata": {}, "outputs": [], "source": [ "model = MethylSegPathway.get_pretrained_model(OUT_DIR, resolution=\"450k\")\n", "model.state_assignment_method = MethylStateAssignmentMethod.KMEANS\n", "model.segmentor.state_assignment_method = MethylStateAssignmentMethod.KMEANS\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "938c5b7d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'TCGA-BD-A3EP-01A'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_info, sample_info_removed = MethylDataPrep(\n", " meth_file=REFERENCE_DIR / \"TCGA-BD-A3EP-01A_450k.tsv.gz\",\n", " sample_id=\"TCGA-BD-A3EP-01A\",\n", " resolution=\"450k\",\n", " remove_low_coverage_like_cpgs=True,\n", ").prepare()\n", "\n", "sample_info.sample_id\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "1ebbf2c6", "metadata": {}, "outputs": [ { "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": "9a8b4af2-eaa6-4351-8d6e-4e7a0faa2f53", "rows": [ [ "0", "chr1", "69590", "856915", "0.44145833333333334", "48", "PMD", "787325" ], [ "1", "chr1", "857656", "906364", "0.4556521739130434", "92", "INTERMEDIATE", "48708" ], [ "2", "chr1", "906492", "922011", "0.44304347826086954", "23", "PMD", "15519" ], [ "3", "chr1", "931326", "956300", "0.4671428571428573", "70", "INTERMEDIATE", "24974" ], [ "4", "chr1", "956798", "989393", "0.8241791044776118", "67", "HIGH", "32595" ] ], "shape": { "columns": 7, "rows": 5 } }, "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", "856915 | \n", "0.441458 | \n", "48 | \n", "PMD | \n", "787325 | \n", "
| 1 | \n", "chr1 | \n", "857656 | \n", "906364 | \n", "0.455652 | \n", "92 | \n", "INTERMEDIATE | \n", "48708 | \n", "
| 2 | \n", "chr1 | \n", "906492 | \n", "922011 | \n", "0.443043 | \n", "23 | \n", "PMD | \n", "15519 | \n", "
| 3 | \n", "chr1 | \n", "931326 | \n", "956300 | \n", "0.467143 | \n", "70 | \n", "INTERMEDIATE | \n", "24974 | \n", "
| 4 | \n", "chr1 | \n", "956798 | \n", "989393 | \n", "0.824179 | \n", "67 | \n", "HIGH | \n", "32595 | \n", "