{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# Meer" ] }, { "cell_type": "markdown", "id": "a3f514a3-772c-4a14-afdf-5a8376851ff4", "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": "d95fafdc-643a-40ea-a689-200bd132e90c", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "4adfb4de-cd79-4913-a1af-9e23e9e236c9", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:26.345739Z", "iopub.status.busy": "2025-04-07T18:07:26.345377Z", "iopub.status.idle": "2025-04-07T18:07:27.890266Z", "shell.execute_reply": "2025-04-07T18:07:27.889940Z" } }, "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": "145082e5-ced4-47ae-88c0-cb69773e3c5a", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "8aa77372-7ed3-4da7-abc9-d30372106139", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:27.891920Z", "iopub.status.busy": "2025-04-07T18:07:27.891696Z", "iopub.status.idle": "2025-04-07T18:07:27.900254Z", "shell.execute_reply": "2025-04-07T18:07:27.899972Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Meer(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " return x\n", "\n", " def postprocess(self, x):\n", " \"\"\"\n", " Transforms age in days to age in months.\n", " \"\"\"\n", " return x / 30.5\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.Meer)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:27.901558Z", "iopub.status.busy": "2025-04-07T18:07:27.901460Z", "iopub.status.idle": "2025-04-07T18:07:27.903244Z", "shell.execute_reply": "2025-04-07T18:07:27.902985Z" } }, "outputs": [], "source": [ "model = pya.models.Meer()" ] }, { "cell_type": "markdown", "id": "51f8615e-01fa-4aa5-b196-3ee2b35d261c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "6601da9e-8adc-44ee-9308-75e3cd31b816", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:27.904531Z", "iopub.status.busy": "2025-04-07T18:07:27.904447Z", "iopub.status.idle": "2025-04-07T18:07:27.906569Z", "shell.execute_reply": "2025-04-07T18:07:27.906335Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"meer\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: A whole-lifespan mouse multi-tissue DNA methylation age predictor.\n", "model.metadata[\"species\"] = \"Mus musculus\" # Paper: A whole-lifespan mouse multi-tissue DNA methylation age predictor.\n", "model.metadata[\"year\"] = 2018\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Meer, M. V., Podolskiy, D. I., Tyshkovskiy, A. & Gladyshev, V. N. A whole lifespan mouse multi-tissue DNA methylation clock. eLife 7, e40675 (2018).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.7554/elife.40675\"\n", "model.metadata[\"notes\"] = \"Whole Lifespan Multi-Tissue (WLMT) mouse clock trained by elastic net on RRBS methylation percentages from untreated wild-type C57BL/6 samples.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"multi-tissue\"] # Paper: The combined dataset represented 11 tissues and cell types.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: A whole-lifespan mouse multi-tissue DNA methylation age predictor.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: The method is based on chronological age.\n", "model.metadata[\"unit\"] = [\"days\"] # Paper: Training and test MAE were reported in days.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: Elastic net regression with 10-fold cross-validation.\n", "model.metadata[\"platform\"] = [\"RRBS\"] # Paper: Only RRBS data were used to construct the clock.\n", "model.metadata[\"population\"] = \"mice\" # Paper: Mice ranged from 1 week to 35 months; untreated wild-type C57BL/6 were used.\n", "model.metadata[\"journal\"] = \"eLife\"\n", "model.metadata[\"last_author\"] = \"Vadim N. Gladyshev\"\n", "model.metadata[\"n_features\"] = 435\n", "model.metadata[\"citations\"] = 203\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "74492239-5aae-4026-9d90-6bc9c574c110", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "markdown", "id": "d382bb34-56d2-498f-b5a6-22cde82a5790", "metadata": {}, "source": [ "#### Download directly with curl" ] }, { "cell_type": "code", "execution_count": 5, "id": "b48a0c22-99c0-4e11-95ce-05ff2a556e06", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:27.907899Z", "iopub.status.busy": "2025-04-07T18:07:27.907820Z", "iopub.status.idle": "2025-04-07T18:07:28.427572Z", "shell.execute_reply": "2025-04-07T18:07:28.426774Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supplementary_url = \"https://elifesciences.org/download/aHR0cHM6Ly9jZG4uZWxpZmVzY2llbmNlcy5vcmcvYXJ0aWNsZXMvNDA2NzUvZWxpZmUtNDA2NzUtc3VwcDMtdjIueGxzeA--/elife-40675-supp3-v2.xlsx?_hash=qzOMc4yUFACfDFG%2FlgxkFTHWt%2BSXSmP9zz1BM3oOTRM%3D\"\n", "supplementary_file_name = \"coefficients.xlsx\"\n", "os.system(f\"curl -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "5035b180-3d1b-4432-8ebe-b9c92bd93a7f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "793d7b24-b1ef-4dd2-ac26-079f7b67fba7", "metadata": {}, "source": [ "#### From Excel file" ] }, { "cell_type": "code", "execution_count": 6, "id": "110a5ded-d25f-4cef-8e84-4f51210dfc26", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.431037Z", "iopub.status.busy": "2025-04-07T18:07:28.430745Z", "iopub.status.idle": "2025-04-07T18:07:28.555299Z", "shell.execute_reply": "2025-04-07T18:07:28.554961Z" } }, "outputs": [], "source": [ "df = pd.read_excel('coefficients.xlsx', sheet_name='Whole lifespan multi-tissue', nrows=435)\n", "df['feature'] = df['Chromosome'].astype(str) + ':' + df['Position'].astype(int).astype(str)\n", "df['coefficient'] = df['Weight']*100\n", "\n", "model.features = features = df['feature'].tolist()" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.556898Z", "iopub.status.busy": "2025-04-07T18:07:28.556799Z", "iopub.status.idle": "2025-04-07T18:07:28.558889Z", "shell.execute_reply": "2025-04-07T18:07:28.558655Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['coefficient'].tolist()).unsqueeze(0)\n", "intercept = torch.tensor([234.64])" ] }, { "cell_type": "markdown", "id": "ad261636-5b00-4979-bb1d-67a851f7aa19", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 8, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.560223Z", "iopub.status.busy": "2025-04-07T18:07:28.560120Z", "iopub.status.idle": "2025-04-07T18:07:28.562273Z", "shell.execute_reply": "2025-04-07T18:07:28.562021Z" } }, "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": "ad8b4c1d-9d57-48b7-9a30-bcfea7b747b1", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "ade0f4c9-2298-4fc3-bb72-d200907dd731", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.563535Z", "iopub.status.busy": "2025-04-07T18:07:28.563424Z", "iopub.status.idle": "2025-04-07T18:07:28.565041Z", "shell.execute_reply": "2025-04-07T18:07:28.564807Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.566363Z", "iopub.status.busy": "2025-04-07T18:07:28.566280Z", "iopub.status.idle": "2025-04-07T18:07:28.567955Z", "shell.execute_reply": "2025-04-07T18:07:28.567709Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.569136Z", "iopub.status.busy": "2025-04-07T18:07:28.569055Z", "iopub.status.idle": "2025-04-07T18:07:28.570649Z", "shell.execute_reply": "2025-04-07T18:07:28.570378Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "86e3d6b1-e67e-4f3d-bd39-0ebec5726c3c", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.571859Z", "iopub.status.busy": "2025-04-07T18:07:28.571779Z", "iopub.status.idle": "2025-04-07T18:07:28.575093Z", "shell.execute_reply": "2025-04-07T18:07:28.574864Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Meer, Margarita V., et al. \"A whole lifespan mouse multi-tissue '\n", " 'DNA methylation clock.\" Elife 7 (2018): e40675.',\n", " 'clock_name': 'meer',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.7554/eLife.40675',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Mus musculus',\n", " 'version': None,\n", " 'year': 2018}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['chr10:111559529', 'chr10:115250413', 'chr10:118803606', 'chr10:121498258', 'chr10:127620127', 'chr10:19970189', 'chr10:19970209', 'chr10:33624567', 'chr10:42644582', 'chr10:4621215', 'chr10:57795976', 'chr10:60698046', 'chr10:61828281', 'chr10:63820265', 'chr10:67979197', 'chr10:68137630', 'chr10:75014983', 'chr10:78273866', 'chr10:78487669', 'chr10:80182188', 'chr10:80534973', 'chr10:84760142', 'chr11:100367639', 'chr11:102255939', 'chr11:107372077', 'chr11:114423667', 'chr11:114479395', 'chr11:115989660', 'chr11:116515706', 'chr11:117175709']... [Total elements: 435]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=435, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [9.368049621582031, -4.492888927459717, -2.932995080947876, -2.1226966381073, -16.623489379882812, -2.7541234493255615, -9.462428092956543, 15.378312110900879, 22.142602920532227, 2.114746570587158, -7.513723373413086, 11.303533554077148, -12.452217102050781, -0.46204015612602234, -14.366745948791504, -9.18408203125, -6.437520503997803, -8.584598541259766, -7.610683917999268, -6.5796918869018555, 5.641702175140381, 1.8415330648422241, -12.470565795898438, 1.1288938522338867, -17.48339080810547, 14.172774314880371, 0.42071831226348877, -0.3068951964378357, 0.634797215461731, 6.434844493865967]... [Tensor of shape torch.Size([1, 435])]\n", "base_model.linear.bias: tensor([234.6400])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "986d0262-e0c7-4036-b687-dee53ba392fb", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.576366Z", "iopub.status.busy": "2025-04-07T18:07:28.576280Z", "iopub.status.idle": "2025-04-07T18:07:28.580120Z", "shell.execute_reply": "2025-04-07T18:07:28.579879Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[20.9082],\n", " [ 4.7668],\n", " [17.3136],\n", " [12.6321],\n", " [-2.8595],\n", " [ 1.7717],\n", " [-4.0258],\n", " [ 8.8037],\n", " [21.6245],\n", " [13.8154]], dtype=torch.float64, grad_fn=)" ] }, "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": "fe8299d7-9285-4e22-82fd-b664434b4369", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.581350Z", "iopub.status.busy": "2025-04-07T18:07:28.581263Z", "iopub.status.idle": "2025-04-07T18:07:28.583879Z", "shell.execute_reply": "2025-04-07T18:07:28.583639Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "bac6257b-8d08-4a90-8d0b-7f745dc11ac1", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:07:28.585094Z", "iopub.status.busy": "2025-04-07T18:07:28.585008Z", "iopub.status.idle": "2025-04-07T18:07:28.588129Z", "shell.execute_reply": "2025-04-07T18:07:28.587904Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: coefficients.xlsx\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", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }