{ "cells": [ { "cell_type": "markdown", "id": "2e3d5791", "metadata": {}, "source": [ "# DNAmFILi" ] }, { "cell_type": "markdown", "id": "0f244a43", "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": "d5b77527", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "2bbbd86d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:47.574508Z", "iopub.status.busy": "2026-07-04T19:45:47.574408Z", "iopub.status.idle": "2026-07-04T19:45:50.642340Z", "shell.execute_reply": "2026-07-04T19:45:50.641856Z" } }, "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": "7a073804", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "6e42b7f8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:50.643940Z", "iopub.status.busy": "2026-07-04T19:45:50.643760Z", "iopub.status.idle": "2026-07-04T19:45:50.646324Z", "shell.execute_reply": "2026-07-04T19:45:50.645994Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class DNAmFILi(LinearReferenceClock):\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.DNAmFILi)" ] }, { "cell_type": "code", "execution_count": 3, "id": "8aeebf77", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:50.647583Z", "iopub.status.busy": "2026-07-04T19:45:50.647496Z", "iopub.status.idle": "2026-07-04T19:45:50.649118Z", "shell.execute_reply": "2026-07-04T19:45:50.648738Z" } }, "outputs": [], "source": [ "model = pya.models.DNAmFILi()" ] }, { "cell_type": "markdown", "id": "7bd5a3fe", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "d9318220", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:50.650228Z", "iopub.status.busy": "2026-07-04T19:45:50.650160Z", "iopub.status.idle": "2026-07-04T19:45:50.652007Z", "shell.execute_reply": "2026-07-04T19:45:50.651746Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"dnamfili\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: A DNA methylation-based score was derived to predict frailty.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: A DNA methylation-based score was derived to predict frailty.\n", "model.metadata[\"year\"] = 2022\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Li, X. et al. Derivation and validation of an epigenetic frailty risk score in population-based cohorts of older adults. Nature Communications 13, 5269 (2022).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1038/s41467-022-32893-x\"\n", "model.metadata[\"notes\"] = \"Epigenetic frailty risk score (eFRS), a weighted 20-CpG whole-blood DNA-methylation score selected by LASSO from replicated frailty-associated loci.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: A DNA methylation-based score was derived to predict frailty.\n", "model.metadata[\"predicts\"] = [\"frailty risk\"] # Paper: A DNA methylation-based score was derived to predict frailty.\n", "model.metadata[\"training_target\"] = [\"frailty\"] # Paper: Frailty was defined by a deficit-accumulation frailty index.\n", "model.metadata[\"unit\"] = [\"unitless\"] # Paper: The eFRS was constructed as a weighted CpG score.\n", "model.metadata[\"model_type\"] = \"LASSO regression\" # Paper: LASSO regression selected candidates and constructed eFRS.\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\", \"Illumina 450K\"] # Paper: EPIC was used in subsets I/II; 450K in subset III.\n", "model.metadata[\"population\"] = \"older adults\" # Paper: ESTHER recruited adults aged 50-75; KORA-Age enrolled adults aged at least 65.\n", "model.metadata[\"journal\"] = \"Nature Communications\"\n", "model.metadata[\"last_author\"] = \"Hermann Brenner\"\n", "model.metadata[\"n_features\"] = 20\n", "model.metadata[\"citations\"] = 22\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "36999624", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "2e6a270e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:50.653306Z", "iopub.status.busy": "2026-07-04T19:45:50.653240Z", "iopub.status.idle": "2026-07-04T19:45:51.051673Z", "shell.execute_reply": "2026-07-04T19:45:51.050989Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o DNAmFI_Li_CpGs.rda https://raw.githubusercontent.com/HigginsChenLab/methylCIPHER/19b12296b0d7eb7055a97d068064df635f44ce3e/data/DNAmFI_Li_CpGs.rda\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "3f54f618", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.053726Z", "iopub.status.busy": "2026-07-04T19:45:51.053553Z", "iopub.status.idle": "2026-07-04T19:45:51.057119Z", "shell.execute_reply": "2026-07-04T19:45:51.056389Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing download.r\n" ] } ], "source": [ "%%writefile download.r\n", "\n", "library(jsonlite)\n", "load(\"DNAmFI_Li_CpGs.rda\")\n", "write_json(DNAmFI_Li_CpGs, \"coefficients.json\", digits = 10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "ab163ae5", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.058913Z", "iopub.status.busy": "2026-07-04T19:45:51.058786Z", "iopub.status.idle": "2026-07-04T19:45:51.521018Z", "shell.execute_reply": "2026-07-04T19:45:51.520606Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(\"Rscript download.r\")" ] }, { "cell_type": "markdown", "id": "1d32160c", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 8, "id": "479b810b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.522271Z", "iopub.status.busy": "2026-07-04T19:45:51.522190Z", "iopub.status.idle": "2026-07-04T19:45:51.526269Z", "shell.execute_reply": "2026-07-04T19:45:51.525853Z" } }, "outputs": [], "source": [ "coef_df = pd.DataFrame(json.load(open('coefficients.json')))\n", "model.features = coef_df['CpG'].tolist()" ] }, { "cell_type": "markdown", "id": "d06e7cda", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 9, "id": "1774f835", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.527432Z", "iopub.status.busy": "2026-07-04T19:45:51.527356Z", "iopub.status.idle": "2026-07-04T19:45:51.533006Z", "shell.execute_reply": "2026-07-04T19:45:51.532638Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['Beta'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([0.204]).float()" ] }, { "cell_type": "code", "execution_count": 10, "id": "a951fb9f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.534190Z", "iopub.status.busy": "2026-07-04T19:45:51.534123Z", "iopub.status.idle": "2026-07-04T19:45:51.538773Z", "shell.execute_reply": "2026-07-04T19:45:51.538400Z" } }, "outputs": [], "source": [ "base_model = pya.models.LinearModel(input_dim=len(model.features))\n", "\n", "base_model.linear.weight.data = weights.float()\n", "base_model.linear.bias.data = intercept.float()\n", "\n", "model.base_model = base_model" ] }, { "cell_type": "markdown", "id": "8ccf6acb", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 11, "id": "2d2677c2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.539944Z", "iopub.status.busy": "2026-07-04T19:45:51.539875Z", "iopub.status.idle": "2026-07-04T19:45:51.541371Z", "shell.execute_reply": "2026-07-04T19:45:51.541006Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "c486bab2", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 12, "id": "9c926dd2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.542477Z", "iopub.status.busy": "2026-07-04T19:45:51.542412Z", "iopub.status.idle": "2026-07-04T19:45:51.543840Z", "shell.execute_reply": "2026-07-04T19:45:51.543558Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 13, "id": "a1f90561", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.544959Z", "iopub.status.busy": "2026-07-04T19:45:51.544886Z", "iopub.status.idle": "2026-07-04T19:45:51.546374Z", "shell.execute_reply": "2026-07-04T19:45:51.545997Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "4aafda21", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 14, "id": "5a84f932", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.547447Z", "iopub.status.busy": "2026-07-04T19:45:51.547387Z", "iopub.status.idle": "2026-07-04T19:45:51.564412Z", "shell.execute_reply": "2026-07-04T19:45:51.564001Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Li, Xiaoyu, et al. \"A DNA methylation-based epigenetic frailty '\n", " 'score in older adults.\" Nature Communications 13.1 (2022): 5269.',\n", " 'clock_name': 'dnamfili',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1038/s41467-022-32893-x',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2022}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg00921350',\n", " 'cg01234420',\n", " 'cg02867102',\n", " 'cg03725309',\n", " 'cg04955914',\n", " 'cg07312601',\n", " 'cg07349348',\n", " 'cg08463758',\n", " 'cg10408430',\n", " 'cg11700584',\n", " 'cg12510708',\n", " 'cg13570972',\n", " 'cg15058210',\n", " 'cg15380836',\n", " 'cg17860366',\n", " 'cg17971578',\n", " 'cg18791730',\n", " 'cg19267254',\n", " 'cg21656937',\n", " 'cg23458887']\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=20, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: tensor([[-0.2090, -0.1000, -0.0160, -0.2930, -0.1460, -0.0840, 0.1580, 0.1370,\n", " 0.2480, -0.1010, -0.0490, 0.0640, -0.0570, -0.1800, -0.1440, 0.3150,\n", " -0.0750, -0.1760, -0.0250, -0.0770]])\n", "base_model.linear.bias: tensor([0.2040])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "2667b5ca", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 15, "id": "9d682d4b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.565564Z", "iopub.status.busy": "2026-07-04T19:45:51.565496Z", "iopub.status.idle": "2026-07-04T19:45:51.573993Z", "shell.execute_reply": "2026-07-04T19:45:51.573612Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-1.3837],\n", " [-0.2305],\n", " [-0.8730],\n", " [ 0.7649],\n", " [ 0.6257],\n", " [ 0.3401],\n", " [ 0.5390],\n", " [-1.0176],\n", " [ 0.3744],\n", " [ 0.9479]], dtype=torch.float64, grad_fn=)" ] }, "execution_count": 15, "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": "70cf6b12", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 16, "id": "d26521ad", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.575268Z", "iopub.status.busy": "2026-07-04T19:45:51.575192Z", "iopub.status.idle": "2026-07-04T19:45:51.579918Z", "shell.execute_reply": "2026-07-04T19:45:51.579503Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "fd9eb3d9", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 17, "id": "09207c59", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:51.581202Z", "iopub.status.busy": "2026-07-04T19:45:51.581134Z", "iopub.status.idle": "2026-07-04T19:45:51.584736Z", "shell.execute_reply": "2026-07-04T19:45:51.584264Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: download.r\n", "Deleted file: DNAmFI_Li_CpGs.rda\n", "Deleted file: coefficients.json\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 }