{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# ZhangBLUP" ] }, { "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": "2024-03-05T21:24:05.648919Z", "iopub.status.busy": "2024-03-05T21:24:05.648291Z", "iopub.status.idle": "2024-03-05T21:24:06.960359Z", "shell.execute_reply": "2024-03-05T21:24:06.960057Z" } }, "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": "2024-03-05T21:24:06.962342Z", "iopub.status.busy": "2024-03-05T21:24:06.962164Z", "iopub.status.idle": "2024-03-05T21:24:06.971585Z", "shell.execute_reply": "2024-03-05T21:24:06.971332Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class ZhangBLUP(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " \"\"\"\n", " Scales the input PyTorch tensor per row with mean 0 and std 1.\n", " \"\"\"\n", " row_means = torch.mean(x, dim=1, keepdim=True)\n", " row_stds = torch.std(x, dim=1, keepdim=True)\n", "\n", " # Avoid division by zero in case of a row with constant value\n", " row_stds = torch.where(row_stds == 0, torch.ones_like(row_stds), row_stds)\n", "\n", " x_scaled = (x - row_means) / row_stds\n", " return x_scaled\n", "\n", " def postprocess(self, x):\n", " return x\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.ZhangBLUP)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:06.973159Z", "iopub.status.busy": "2024-03-05T21:24:06.973058Z", "iopub.status.idle": "2024-03-05T21:24:06.974566Z", "shell.execute_reply": "2024-03-05T21:24:06.974329Z" } }, "outputs": [], "source": [ "model = pya.models.ZhangBLUP()" ] }, { "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": "2024-03-05T21:24:06.976007Z", "iopub.status.busy": "2024-03-05T21:24:06.975925Z", "iopub.status.idle": "2024-03-05T21:24:06.977763Z", "shell.execute_reply": "2024-03-05T21:24:06.977538Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"zhangblup\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The predictors use DNA-methylation beta values.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The training collection comprises human blood and saliva cohorts.\n", "model.metadata[\"year\"] = 2019\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Zhang, Q., Vallerga, C.L., Walker, R.M. et al. Improved precision of epigenetic clock estimates across tissues and its implication for biological ageing. Genome Medicine 11, 54 (2019).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/s13073-019-0667-1\"\n", "model.metadata[\"notes\"] = \"High-dimensional chronological-age predictor using best linear unbiased prediction across the full quality-controlled set of 319,607 methylation probes.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\", \"saliva\"] # Paper: The assembled training collection contained 13,402 blood and 259 saliva samples.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: Both released predictors estimate chronological age.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: Chronological age was the regression outcome.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: Predicted chronological age and prediction error are reported in years.\n", "model.metadata[\"model_type\"] = \"best linear unbiased prediction\" # Paper: BLUP was one of the two age-prediction methods.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: The 14 training cohorts were measured on HumanMethylation450 and EPIC arrays.\n", "model.metadata[\"population\"] = \"all ages\" # Paper: The assembled 14-cohort collection spanned ages 2–104 years.\n", "model.metadata[\"journal\"] = \"Genome Medicine\"\n", "model.metadata[\"last_author\"] = \"Peter M. Visscher\"\n", "model.metadata[\"n_features\"] = 319607\n", "model.metadata[\"citations\"] = 519\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": "c2844c3a-8e78-48bd-95b5-9301c8b46012", "metadata": {}, "source": [ "#### Download GitHub repository" ] }, { "cell_type": "code", "execution_count": 5, "id": "0ee560a3-8ab6-4202-b8b2-cebf75089bfb", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:06.979299Z", "iopub.status.busy": "2024-03-05T21:24:06.979222Z", "iopub.status.idle": "2024-03-05T21:24:17.301214Z", "shell.execute_reply": "2024-03-05T21:24:17.300886Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "github_url = \"https://github.com/qzhang314/DNAm-based-age-predictor.git\"\n", "github_folder_name = github_url.split('/')[-1].split('.')[0]\n", "os.system(f\"git clone {github_url}\")" ] }, { "cell_type": "markdown", "id": "eb5774d0-e6ea-4d18-949e-6ba1f0534d2a", "metadata": {}, "source": [ "#### Download from R package" ] }, { "cell_type": "code", "execution_count": 6, "id": "3460ce9a-719e-494c-9a94-21fc97dd0be4", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:17.302886Z", "iopub.status.busy": "2024-03-05T21:24:17.302775Z", "iopub.status.idle": "2024-03-05T21:24:17.305137Z", "shell.execute_reply": "2024-03-05T21:24:17.304884Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing download.r\n" ] } ], "source": [ "%%writefile download.r\n", "\n", "data = readRDS(\"DNAm-based-age-predictor/data.rds\")\n", "\n", "write.csv(data, \"example_data.csv\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "11ba180b-0ca9-40e0-8a89-72bc4e085451", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:17.306563Z", "iopub.status.busy": "2024-03-05T21:24:17.306464Z", "iopub.status.idle": "2024-03-05T21:24:20.964109Z", "shell.execute_reply": "2024-03-05T21:24:20.963851Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(\"Rscript download.r\")" ] }, { "cell_type": "markdown", "id": "5035b180-3d1b-4432-8ebe-b9c92bd93a7f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "15f4af76-b93c-438c-b57f-f129d6e9ec99", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": 8, "id": "8a3d5de6-6303-487a-8b4d-e6345792f7be", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:20.965851Z", "iopub.status.busy": "2024-03-05T21:24:20.965755Z", "iopub.status.idle": "2024-03-05T21:24:21.066559Z", "shell.execute_reply": "2024-03-05T21:24:21.066264Z" } }, "outputs": [], "source": [ "df = pd.read_table('DNAm-based-age-predictor/blup.coef', sep=' ')\n", "df['feature'] = df['probe']\n", "df['coefficient'] = df['coef']\n", "\n", "model.features = df['feature'][1:].tolist()" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 9, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:21.068372Z", "iopub.status.busy": "2024-03-05T21:24:21.068272Z", "iopub.status.idle": "2024-03-05T21:24:21.093386Z", "shell.execute_reply": "2024-03-05T21:24:21.093063Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)\n", "intercept = torch.tensor([df['coefficient'][0]])" ] }, { "cell_type": "markdown", "id": "ad261636-5b00-4979-bb1d-67a851f7aa19", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 10, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:21.095068Z", "iopub.status.busy": "2024-03-05T21:24:21.094975Z", "iopub.status.idle": "2024-03-05T21:24:21.097916Z", "shell.execute_reply": "2024-03-05T21:24:21.097694Z" } }, "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": "markdown", "id": "f7fdae64-096a-4640-ade7-6a17b78a01d5", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": 11, "id": "86de757f-fb38-4bcb-b91e-fc3372d22aad", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:21.099379Z", "iopub.status.busy": "2024-03-05T21:24:21.099292Z", "iopub.status.idle": "2024-03-05T21:24:36.755051Z", "shell.execute_reply": "2024-03-05T21:24:36.754740Z" } }, "outputs": [], "source": [ "reference_feature_values_df = pd.read_csv('example_data.csv', index_col=0)\n", "reference_feature_values_df = reference_feature_values_df.loc[:, model.features]\n", "model.reference_values = reference_feature_values_df.mean().tolist()" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 12, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:36.756917Z", "iopub.status.busy": "2024-03-05T21:24:36.756810Z", "iopub.status.idle": "2024-03-05T21:24:36.758463Z", "shell.execute_reply": "2024-03-05T21:24:36.758236Z" } }, "outputs": [], "source": [ "model.preprocess_name = 'scale_row'\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 13, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:36.759873Z", "iopub.status.busy": "2024-03-05T21:24:36.759794Z", "iopub.status.idle": "2024-03-05T21:24:36.761383Z", "shell.execute_reply": "2024-03-05T21:24:36.761159Z" } }, "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": 14, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:36.762791Z", "iopub.status.busy": "2024-03-05T21:24:36.762716Z", "iopub.status.idle": "2024-03-05T21:24:36.765304Z", "shell.execute_reply": "2024-03-05T21:24:36.765090Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Zhang, Qian, et al. \"Improved precision of epigenetic clock '\n", " 'estimates across tissues and its implication for biological '\n", " 'ageing.\" Genome medicine 11 (2019): 1-11.',\n", " 'clock_name': 'zhangblup',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/s13073-019-0667-1',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2019}\n", "reference_values: [0.05946290980445651, 0.9016779859564634, 0.8511621554128406, 0.07497523935546724, 0.08079601941558237, 0.13789119095690058, 0.959990162673912, 0.054840405638908254, 0.11271586156940745, 0.06867464793155438, 0.04092332774669377, 0.03122014881875939, 0.12091171597794977, 0.8626077673429406, 0.02002095456899887, 0.037161243530447204, 0.5228131230887364, 0.025038065219011623, 0.03411737762225109, 0.023966201717807785, 0.13213191286915785, 0.03613520841142101, 0.11053625925027737, 0.09303164766153527, 0.07697707482010466, 0.040677518106921974, 0.016422537053260692, 0.01646509240082735, 0.9634930275356334, 0.8664078943468241]... [Total elements: 319607]\n", "preprocess_name: 'scale_row'\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg18478105', 'cg14361672', 'cg01763666', 'cg02115394', 'cg13417420', 'cg12480843', 'cg26724186', 'cg24133276', 'cg19607165', 'cg11073926', 'cg08770523', 'cg24040570', 'cg15998406', 'cg11947782', 'cg11945228', 'cg14361409', 'cg00376553', 'cg12898275', 'cg21650422', 'cg24176744', 'cg08360726', 'cg07469408', 'cg08730728', 'cg22782271', 'cg26846647', 'cg24568647', 'cg01415275', 'cg00172270', 'cg27548741', 'cg09659208']... [Total elements: 319607]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=319607, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-0.011127361096441746, 0.0005570068024098873, -0.01769556663930416, 0.009089098311960697, -0.016315005719661713, -0.012607994489371777, -0.006776333786547184, 0.005497894249856472, -0.031007202342152596, 0.007024446967989206, 0.0072049410082399845, 0.006631432566791773, -0.028644423931837082, -0.0067995828576385975, -0.003207291942089796, -0.010230086743831635, 0.002228884259238839, -0.0012665422400459647, 0.010415713302791119, -0.0016215209616348147, 0.015202521346509457, -0.0019047950627282262, -0.01444777101278305, 0.01679779589176178, -0.0016255266964435577, -0.0027738267090171576, -0.0028552687726914883, 0.0026693870313465595, 0.0018644158262759447, 0.004981260746717453]... [Tensor of shape torch.Size([1, 319607])]\n", "base_model.linear.bias: tensor([91.1540])\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": 15, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:36.766917Z", "iopub.status.busy": "2024-03-05T21:24:36.766804Z", "iopub.status.idle": "2024-03-05T21:24:36.821344Z", "shell.execute_reply": "2024-03-05T21:24:36.820983Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[107.0536],\n", " [ 90.5363],\n", " [ 81.2531],\n", " [ 97.2001],\n", " [ 91.7565],\n", " [ 95.9860],\n", " [ 97.9471],\n", " [ 97.2143],\n", " [107.3615],\n", " [ 90.4772]], 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": "fe8299d7-9285-4e22-82fd-b664434b4369", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 16, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:36.823632Z", "iopub.status.busy": "2024-03-05T21:24:36.823512Z", "iopub.status.idle": "2024-03-05T21:24:36.964554Z", "shell.execute_reply": "2024-03-05T21:24:36.964090Z" } }, "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": 17, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:36.966595Z", "iopub.status.busy": "2024-03-05T21:24:36.966485Z", "iopub.status.idle": "2024-03-05T21:24:36.976316Z", "shell.execute_reply": "2024-03-05T21:24:36.975971Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: download.r\n", "Deleted folder: DNAm-based-age-predictor\n", "Deleted file: example_data.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", "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.17" } }, "nbformat": 4, "nbformat_minor": 5 }