{ "cells": [ { "cell_type": "markdown", "id": "54261bf3", "metadata": {}, "source": [ "# EpiTOC3" ] }, { "cell_type": "markdown", "id": "1d4bcfd5", "metadata": {}, "source": [ "## Index\n", "1. [Instantiate model class](#Instantiate-model-class)\n", "2. [Define clock metadata](#Define-clock-metadata)\n", "3. [Download clock dependencies](#Download-clock-dependencies)\n", "5. [Load features](#Load-features)\n", "6. [Load weights into base model](#Load-weights-into-base-model)\n", "7. [Load reference values](#Load-reference-values)\n", "8. [Load preprocess and postprocess objects](#Load-preprocess-and-postprocess-objects)\n", "10. [Check all clock parameters](#Check-all-clock-parameters)\n", "10. [Basic test](#Basic-test)\n", "11. [Save torch model](#Save-torch-model)\n", "12. [Clear directory](#Clear-directory)" ] }, { "cell_type": "markdown", "id": "9923d5ad", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "9de93821", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:54.312184Z", "iopub.status.busy": "2026-07-04T19:34:54.312023Z", "iopub.status.idle": "2026-07-04T19:34:56.218349Z", "shell.execute_reply": "2026-07-04T19:34:56.217912Z" } }, "outputs": [], "source": [ "import os\n", "import inspect\n", "import shutil\n", "import json\n", "import torch\n", "import pandas as pd\n", "import pyaging as pya" ] }, { "cell_type": "markdown", "id": "6194e38f", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "56900f70", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.220015Z", "iopub.status.busy": "2026-07-04T19:34:56.219825Z", "iopub.status.idle": "2026-07-04T19:34:56.222560Z", "shell.execute_reply": "2026-07-04T19:34:56.222197Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class EpiTOC3(epiTOC2):\n", " pass\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.EpiTOC3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "c9004f70", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.223889Z", "iopub.status.busy": "2026-07-04T19:34:56.223803Z", "iopub.status.idle": "2026-07-04T19:34:56.225202Z", "shell.execute_reply": "2026-07-04T19:34:56.224919Z" } }, "outputs": [], "source": [ "model = pya.models.EpiTOC3()" ] }, { "cell_type": "markdown", "id": "b719befd", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "6a8f25ff", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.226300Z", "iopub.status.busy": "2026-07-04T19:34:56.226237Z", "iopub.status.idle": "2026-07-04T19:34:56.228275Z", "shell.execute_reply": "2026-07-04T19:34:56.227931Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"epitoc3\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The study constructs clocks from DNA methylation measurements.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The analyzed samples and clock are human.\n", "model.metadata[\"year\"] = 2020\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Teschendorff, Andrew E. \\\"A comparison of epigenetic mitotic-like clocks for cancer risk prediction.\\\" Genome Medicine 12 (2020): 56.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/s13073-020-00752-3\"\n", "model.metadata[\"notes\"] = \"Code-defined 170-CpG extension of the dynamic mitotic model. Official EpiMitClocks data show that all 170 sites are a subset of the 371 stemTOC vivo-mitCpGs derived from fetal/neonatal references, six normal proliferating cell lines, and three adult whole-blood cohorts. The assigned 2020 dynamic-model paper does not name or define epiTOC3.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"cultured primary human cells\", \"whole blood\", \"multi-tissue\", \"cord blood\"] # Paper: epiTOC3 uses 170 unmethylated population-doubling-associated CpGs.\n", "model.metadata[\"predicts\"] = [\"mitotic age\"] # Paper: The function directly estimates cumulative stem-cell divisions in a tissue.\n", "model.metadata[\"training_target\"] = [\"population doublings\"] # Paper: The epiTOC3 signature comprises population-doubling-associated CpGs.\n", "model.metadata[\"unit\"] = [\"cell divisions per stem cell\"] # Paper: The returned TNSC is cumulative stem-cell divisions.\n", "model.metadata[\"model_type\"] = \"dynamic methylation transmission model\" # Paper: The function describes epiTOC3 as building on a dynamic DNA methylation-gain model.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: Input is an Illumina 450k/EPIC DNAm beta matrix.\n", "model.metadata[\"population\"] = \"all ages\" # Paper: The derivation uses fetal/neonatal reference tissues, six normal cell lines, and adult whole-blood cohorts.\n", "model.metadata[\"journal\"] = \"Genome Medicine\"\n", "model.metadata[\"last_author\"] = \"Andrew E. Teschendorff\"\n", "model.metadata[\"n_features\"] = 170\n", "model.metadata[\"citations\"] = 155\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "dd266538", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "9fe34941", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.229526Z", "iopub.status.busy": "2026-07-04T19:34:56.229447Z", "iopub.status.idle": "2026-07-04T19:34:56.508620Z", "shell.execute_reply": "2026-07-04T19:34:56.507708Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supplementary_url = \"https://raw.githubusercontent.com/Duzhaozhen/OmniAge/c10fbe8cb92957520fbff1d55ae1def0691252e5/OmniAgePy/src/omniage/data/epiTOC3.csv\"\n", "supplementary_file_name = \"coefficients.csv\"\n", "os.system(f\"curl -sL -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "90d7d55b", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "abcc82f1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.510801Z", "iopub.status.busy": "2026-07-04T19:34:56.510636Z", "iopub.status.idle": "2026-07-04T19:34:56.516181Z", "shell.execute_reply": "2026-07-04T19:34:56.515554Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "model.features = df['probe'].astype(str).tolist()" ] }, { "cell_type": "markdown", "id": "e7cf714a", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "0878d97d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.518286Z", "iopub.status.busy": "2026-07-04T19:34:56.518148Z", "iopub.status.idle": "2026-07-04T19:34:56.522012Z", "shell.execute_reply": "2026-07-04T19:34:56.521391Z" } }, "outputs": [], "source": [ "model.delta = torch.tensor(df['delta'].values, dtype=torch.float32).unsqueeze(0)\n", "model.beta0 = torch.tensor(df['beta0'].values, dtype=torch.float32).unsqueeze(0)" ] }, { "cell_type": "code", "execution_count": 8, "id": "14ce9dc1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.523507Z", "iopub.status.busy": "2026-07-04T19:34:56.523405Z", "iopub.status.idle": "2026-07-04T19:34:56.525526Z", "shell.execute_reply": "2026-07-04T19:34:56.524933Z" } }, "outputs": [], "source": [ "model.base_model = None" ] }, { "cell_type": "markdown", "id": "aa0e5a5a", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "f58a0c2f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.526947Z", "iopub.status.busy": "2026-07-04T19:34:56.526843Z", "iopub.status.idle": "2026-07-04T19:34:56.528797Z", "shell.execute_reply": "2026-07-04T19:34:56.528342Z" } }, "outputs": [], "source": [ "model.reference_values = [-1]*len(model.features)" ] }, { "cell_type": "markdown", "id": "c4823572", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "a2d013da", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.530289Z", "iopub.status.busy": "2026-07-04T19:34:56.530200Z", "iopub.status.idle": "2026-07-04T19:34:56.532403Z", "shell.execute_reply": "2026-07-04T19:34:56.531683Z" } }, "outputs": [], "source": [ "model.preprocess_name = \"nan_to_zero\"\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "ae5ec67a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.533804Z", "iopub.status.busy": "2026-07-04T19:34:56.533689Z", "iopub.status.idle": "2026-07-04T19:34:56.535772Z", "shell.execute_reply": "2026-07-04T19:34:56.535327Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "acdc6c8c", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "33671fa4", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.537042Z", "iopub.status.busy": "2026-07-04T19:34:56.536949Z", "iopub.status.idle": "2026-07-04T19:34:56.539409Z", "shell.execute_reply": "2026-07-04T19:34:56.538914Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Teschendorff, Andrew E. \"A comparison of epigenetic mitotic-like '\n", " 'clocks for cancer risk prediction.\" Genome Medicine 12.1 (2020): '\n", " '56.',\n", " 'clock_name': 'epitoc3',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/s13073-020-00752-3',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2020}\n", "reference_values: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]... [Total elements: 170]\n", "preprocess_name: 'nan_to_zero'\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg23062112', 'cg18560328', 'cg21229268', 'cg11250773', 'cg12892303', 'cg12781700', 'cg12065366', 'cg17712694', 'cg12799781', 'cg01718742', 'cg10263370', 'cg16491617', 'cg11832210', 'cg07686635', 'cg26510017', 'cg15745900', 'cg15402399', 'cg05009934', 'cg10094616', 'cg06010588', 'cg23847712', 'cg17370163', 'cg13428480', 'cg22620221', 'cg16437728', 'cg25984671', 'cg07882671', 'cg04342955', 'cg14482313', 'cg14537533']... [Total elements: 170]\n", "base_model_features: None\n", "base_model: None\n", "delta: [9.999999747378752e-05, 9.999999747378752e-05, 9.999999747378752e-05, 0.0002500000118743628, 9.999999747378752e-05, 9.999999747378752e-05, 9.999999747378752e-05, 9.999999747378752e-05, 4.999999873689376e-05, 0.0002500000118743628, 0.0002500000118743628, 9.999999747378752e-05, 9.999999747378752e-06, 9.999999747378752e-06, 4.999999873689376e-05, 4.999999873689376e-05, 9.999999747378752e-06, 4.999999873689376e-05, 4.999999873689376e-05, 0.0002500000118743628, 0.0002500000118743628, 0.0002500000118743628, 4.999999873689376e-05, 0.0002500000118743628, 9.999999747378752e-05, 9.999999747378752e-05, 9.999999747378752e-05, 4.999999873689376e-05, 0.0002500000118743628, 9.999999747378752e-05]... [Tensor of shape torch.Size([1, 170])]\n", "beta0: [0.05000000074505806, 0.019999999552965164, 0.029999999329447746, 0.0, 0.03999999910593033, 0.03999999910593033, 0.05000000074505806, 0.05000000074505806, 0.029999999329447746, 0.009999999776482582, 0.019999999552965164, 0.029999999329447746, 0.03999999910593033, 0.05000000074505806, 0.019999999552965164, 0.03999999910593033, 0.05000000074505806, 0.03999999910593033, 0.05000000074505806, 0.05000000074505806, 0.05000000074505806, 0.05000000074505806, 0.03999999910593033, 0.05000000074505806, 0.05000000074505806, 0.05000000074505806, 0.019999999552965164, 0.05000000074505806, 0.0, 0.019999999552965164]... [Tensor of shape torch.Size([1, 170])]\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "5eaa8b63", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "3c76bc4f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.540875Z", "iopub.status.busy": "2026-07-04T19:34:56.540772Z", "iopub.status.idle": "2026-07-04T19:34:56.549368Z", "shell.execute_reply": "2026-07-04T19:34:56.548897Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1921.6122],\n", " [-4698.6198],\n", " [ 1365.4274],\n", " [-4247.0676],\n", " [ -35.5126],\n", " [ 116.7216],\n", " [ 7356.7134],\n", " [-3367.8507],\n", " [ 7780.6034],\n", " [-7170.8200]], dtype=torch.float64)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.manual_seed(42)\n", "input = torch.randn(10, len(model.features), dtype=float)\n", "model.eval()\n", "model.to(float)\n", "pred = model(input)\n", "pred" ] }, { "cell_type": "markdown", "id": "aca67e0a", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "c96ff956", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.550699Z", "iopub.status.busy": "2026-07-04T19:34:56.550614Z", "iopub.status.idle": "2026-07-04T19:34:56.554088Z", "shell.execute_reply": "2026-07-04T19:34:56.553609Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "68b814e5", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "20186cd6", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:56.555454Z", "iopub.status.busy": "2026-07-04T19:34:56.555368Z", "iopub.status.idle": "2026-07-04T19:34:56.559114Z", "shell.execute_reply": "2026-07-04T19:34:56.558697Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: coefficients.csv\n" ] } ], "source": [ "# Function to remove a folder and all its contents\n", "def remove_folder(path):\n", " try:\n", " shutil.rmtree(path)\n", " print(f\"Deleted folder: {path}\")\n", " except Exception as e:\n", " print(f\"Error deleting folder {path}: {e}\")\n", "\n", "# Get a list of all files and folders in the current directory\n", "all_items = os.listdir('.')\n", "\n", "# Loop through the items\n", "for item in all_items:\n", " # Check if it's a file and does not end with .ipynb\n", " if os.path.isfile(item) and not item.endswith('.ipynb'):\n", " os.remove(item)\n", " print(f\"Deleted file: {item}\")\n", " # Check if it's a folder\n", " elif os.path.isdir(item):\n", " remove_folder(item)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13.11" } }, "nbformat": 4, "nbformat_minor": 5 }