{ "cells": [ { "cell_type": "markdown", "id": "f6e7d8c9", "metadata": {}, "source": [ "# Saving a MethylSeg Model" ] }, { "cell_type": "code", "execution_count": 1, "id": "6dea5bfa", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "id": "56805eb7", "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import yaml\n", "from pathlib import Path\n", "\n", "import os\n", "\n", "\n", "from methylseg import MethylSegPathway, MethylDataPrep, MethylStateAssignmentMethod, MethylationStates\n", "from methylseg.helper_classes import DATA_DIR" ] }, { "cell_type": "code", "execution_count": 3, "id": "66b4e992", "metadata": {}, "outputs": [], "source": [ "REFERENCE_DIR = DATA_DIR / Path(\"reference_files\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "d15dc1b9", "metadata": {}, "outputs": [], "source": [ "tcga_sample_info, tcga_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()" ] }, { "cell_type": "code", "execution_count": 5, "id": "56603441", "metadata": {}, "outputs": [], "source": [ "hm450k_model = MethylSegPathway(\n", " train_sample_info=tcga_sample_info,\n", " out_dir=Path(\"out\") / \"hm450k_model_outdir\",\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "id": "23ca4994", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Random search: 100%|██████████| 500/500 [04:43<00:00, 1.77it/s]\n" ] } ], "source": [ "hm450k_model.fit_pathway(force_optimize_rules=True)" ] }, { "cell_type": "code", "execution_count": 7, "id": "fcedce3d", "metadata": {}, "outputs": [], "source": [ "hm450k_model.to_yaml(REFERENCE_DIR / \"tcga_hm450k_model.yaml\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "980df81f", "metadata": {}, "outputs": [], "source": [ "test_model = MethylSegPathway.from_yaml(REFERENCE_DIR / \"tcga_hm450k_model.yaml\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "9347ed64", "metadata": {}, "outputs": [], "source": [ "wgbs_test_sample_info, wgbs_test_sample_info_removed = MethylDataPrep(\n", " meth_file=REFERENCE_DIR / \"WGBS_colon-primary-tumor_1_wgbs.tsv.gz\",\n", " sample_id=\"colon-primary-tumor_1\",\n", " resolution=\"wgbs\",\n", " min_coverage=10,\n", " remove_low_coverage_like_cpgs=True,\n", ").prepare()" ] }, { "cell_type": "code", "execution_count": 10, "id": "8463f6e4", "metadata": {}, "outputs": [], "source": [ "wgbs_model = MethylSegPathway(\n", " train_sample_info=wgbs_test_sample_info,\n", " out_dir= Path(\"out\") / \"wgbs_model_outdir\",\n", " hmm_type=\"sticky\"\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "id": "024e8f38", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Random search: 100%|██████████| 500/500 [13:29<00:00, 1.62s/it]\n" ] } ], "source": [ "wgbs_model.fit_pathway(force_optimize_rules=True)" ] }, { "cell_type": "code", "execution_count": 12, "id": "92dd4a59", "metadata": {}, "outputs": [], "source": [ "wgbs_model.to_yaml(REFERENCE_DIR / \"wgbs_colon_model.yaml\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "b35f3cda", "metadata": {}, "outputs": [], "source": [ "test_model = MethylSegPathway.from_yaml(REFERENCE_DIR / \"wgbs_colon_model.yaml\")" ] } ], "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 }