{ "cells": [ { "cell_type": "markdown", "id": "6c379230", "metadata": {}, "source": [ "# CellPopAge" ] }, { "cell_type": "markdown", "id": "6419584a", "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": "07d0fa09", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "85b8df28", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:45:58.451845Z", "iopub.status.busy": "2026-07-04T19:45:58.451640Z", "iopub.status.idle": "2026-07-04T19:46:00.395599Z", "shell.execute_reply": "2026-07-04T19:46:00.395166Z" } }, "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": "52bfd2b6", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "078323d8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:00.397380Z", "iopub.status.busy": "2026-07-04T19:46:00.397183Z", "iopub.status.idle": "2026-07-04T19:46:00.399885Z", "shell.execute_reply": "2026-07-04T19:46:00.399462Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class CellPopAge(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.CellPopAge)" ] }, { "cell_type": "code", "execution_count": 3, "id": "35240ac6", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:00.401008Z", "iopub.status.busy": "2026-07-04T19:46:00.400928Z", "iopub.status.idle": "2026-07-04T19:46:00.402564Z", "shell.execute_reply": "2026-07-04T19:46:00.402178Z" } }, "outputs": [], "source": [ "model = pya.models.CellPopAge()" ] }, { "cell_type": "markdown", "id": "bd11061c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "cf2487d6", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:00.403823Z", "iopub.status.busy": "2026-07-04T19:46:00.403746Z", "iopub.status.idle": "2026-07-04T19:46:00.405609Z", "shell.execute_reply": "2026-07-04T19:46:00.405295Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"cellpopage\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The model is based on DNA methylation measurements.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The study samples are Homo sapiens.\n", "model.metadata[\"year\"] = 2024\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Lujan, Celia, et al. \\\"An expedited screening platform for the discovery of anti-ageing compounds in vitro and in vivo.\\\" Genome Medicine 16 (2024): 85.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/s13073-024-01349-w\"\n", "model.metadata[\"notes\"] = \"CellPopAge is an elastic-net DNA-methylation clock using 42 selected CpGs to predict passage-based age of serially cultured adult primary human fibroblast populations and screen compounds that decelerate this measure.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"cultured fibroblasts\"] # Paper: The listed tissue is the model-development sample material.\n", "model.metadata[\"predicts\"] = [\"cell-population passage age\"] # Paper: The reported predictor output is cell-population passage age.\n", "model.metadata[\"training_target\"] = [\"cell passage number\"] # Paper: The fitting outcome is cell passage number.\n", "model.metadata[\"unit\"] = [\"passages\"] # Paper: The returned construct is expressed as cell passage number.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: The clock was fitted using Elastic net.\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\"] # Paper: Training/selection used Illumina EPIC.\n", "model.metadata[\"population\"] = \"human cell cultures\" # Paper: adult primary human fibroblast cultures (39 training samples, passages 10–20)\n", "model.metadata[\"journal\"] = \"Genome Medicine\"\n", "model.metadata[\"last_author\"] = \"Ivana Bjedov\"\n", "model.metadata[\"n_features\"] = 42\n", "model.metadata[\"citations\"] = 6\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "ae9b254b", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "a8e2c135", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:00.406893Z", "iopub.status.busy": "2026-07-04T19:46:00.406825Z", "iopub.status.idle": "2026-07-04T19:46:00.657381Z", "shell.execute_reply": "2026-07-04T19:46:00.656857Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o CellPopAge_ref.rda https://raw.githubusercontent.com/HigginsChenLab/methylCIPHER/19b12296b0d7eb7055a97d068064df635f44ce3e/data/CellPopAge_ref.rda\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "8d66b13e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:00.658766Z", "iopub.status.busy": "2026-07-04T19:46:00.658671Z", "iopub.status.idle": "2026-07-04T19:46:00.660953Z", "shell.execute_reply": "2026-07-04T19:46:00.660648Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing download.r\n" ] } ], "source": [ "%%writefile download.r\n", "\n", "library(jsonlite)\n", "load(\"CellPopAge_ref.rda\")\n", "write_json(CellPopAge_ref$all_CpGs, \"coefficients.json\", digits = 10)\n", "write_json(CellPopAge_ref$intercept, \"intercept.json\", digits = 10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "53dbf945", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:00.662217Z", "iopub.status.busy": "2026-07-04T19:46:00.662127Z", "iopub.status.idle": "2026-07-04T19:46:01.032942Z", "shell.execute_reply": "2026-07-04T19:46:01.032560Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(\"Rscript download.r\")" ] }, { "cell_type": "markdown", "id": "cfee76a8", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 8, "id": "a8334694", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.034363Z", "iopub.status.busy": "2026-07-04T19:46:01.034283Z", "iopub.status.idle": "2026-07-04T19:46:01.040414Z", "shell.execute_reply": "2026-07-04T19:46:01.039980Z" } }, "outputs": [], "source": [ "coef_df = pd.DataFrame(json.load(open('coefficients.json')))\n", "model.features = coef_df['CpG'].tolist()\n", "iv = json.load(open('intercept.json'))\n", "intercept_value = iv[0] if isinstance(iv, list) else iv" ] }, { "cell_type": "markdown", "id": "cb23ac70", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 9, "id": "7dac5b06", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.041749Z", "iopub.status.busy": "2026-07-04T19:46:01.041674Z", "iopub.status.idle": "2026-07-04T19:46:01.043859Z", "shell.execute_reply": "2026-07-04T19:46:01.043539Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['coefficient'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 10, "id": "6c894382", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.045013Z", "iopub.status.busy": "2026-07-04T19:46:01.044937Z", "iopub.status.idle": "2026-07-04T19:46:01.046967Z", "shell.execute_reply": "2026-07-04T19:46:01.046627Z" } }, "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": "fa73a479", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 11, "id": "adc90abc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.048292Z", "iopub.status.busy": "2026-07-04T19:46:01.048212Z", "iopub.status.idle": "2026-07-04T19:46:01.049570Z", "shell.execute_reply": "2026-07-04T19:46:01.049275Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "fb898eb8", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 12, "id": "cf384da9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.050703Z", "iopub.status.busy": "2026-07-04T19:46:01.050642Z", "iopub.status.idle": "2026-07-04T19:46:01.052284Z", "shell.execute_reply": "2026-07-04T19:46:01.051902Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 13, "id": "94773e69", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.053694Z", "iopub.status.busy": "2026-07-04T19:46:01.053596Z", "iopub.status.idle": "2026-07-04T19:46:01.055218Z", "shell.execute_reply": "2026-07-04T19:46:01.054898Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "4419e365", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 14, "id": "f4c16793", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.056577Z", "iopub.status.busy": "2026-07-04T19:46:01.056501Z", "iopub.status.idle": "2026-07-04T19:46:01.059606Z", "shell.execute_reply": "2026-07-04T19:46:01.059245Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Lujan, Gabriel M., et al. \"A DNA methylation clock to measure '\n", " 'the epigenetic age of cell populations.\" Genome Medicine 16 '\n", " '(2024).',\n", " 'clock_name': 'cellpopage',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/s13073-024-01349-w',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2024}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg03662648', 'cg07230471', 'cg09689790', 'cg00283934', 'cg27617964', 'cg21734265', 'cg21184340', 'cg17342766', 'cg00312508', 'cg08907118', 'cg12071423', 'cg16622870', 'cg08126590', 'cg04121501', 'cg18571156', 'cg22900075', 'cg26475539', 'cg27500194', 'cg09992376', 'cg26450896', 'cg06863239', 'cg25651783', 'cg08461941', 'cg11738198', 'cg23407476', 'cg01159380', 'cg05369308', 'cg27537795', 'cg15563081', 'cg19920898']... [Total elements: 2543]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=2543, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5474669337272644, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]... [Tensor of shape torch.Size([1, 2543])]\n", "base_model.linear.bias: tensor([11.5268])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "dd0adc7c", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 15, "id": "5d51015a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.060740Z", "iopub.status.busy": "2026-07-04T19:46:01.060657Z", "iopub.status.idle": "2026-07-04T19:46:01.065577Z", "shell.execute_reply": "2026-07-04T19:46:01.065187Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 8.1066],\n", " [21.9362],\n", " [13.3644],\n", " [31.7122],\n", " [30.1219],\n", " [-3.8962],\n", " [ 9.8756],\n", " [-2.5270],\n", " [ 2.6161],\n", " [ 9.8903]], 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": "6671255d", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 16, "id": "e8a65c42", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.066911Z", "iopub.status.busy": "2026-07-04T19:46:01.066840Z", "iopub.status.idle": "2026-07-04T19:46:01.070482Z", "shell.execute_reply": "2026-07-04T19:46:01.070088Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "9625b988", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 17, "id": "24a38a70", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:01.071814Z", "iopub.status.busy": "2026-07-04T19:46:01.071745Z", "iopub.status.idle": "2026-07-04T19:46:01.075216Z", "shell.execute_reply": "2026-07-04T19:46:01.074829Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: intercept.json\n", "Deleted file: download.r\n", "Deleted file: coefficients.json\n", "Deleted file: CellPopAge_ref.rda\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 }