{ "cells": [ { "cell_type": "markdown", "id": "b87610a0", "metadata": {}, "source": [ "# DownSyndrome" ] }, { "cell_type": "markdown", "id": "6fe58220", "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": "b0d96cab", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "aa5aa921", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:57.220510Z", "iopub.status.busy": "2026-07-04T20:00:57.220350Z", "iopub.status.idle": "2026-07-04T20:00:59.366749Z", "shell.execute_reply": "2026-07-04T20:00:59.366057Z" } }, "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": "90356a5e", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "f9cce368", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.368742Z", "iopub.status.busy": "2026-07-04T20:00:59.368512Z", "iopub.status.idle": "2026-07-04T20:00:59.371690Z", "shell.execute_reply": "2026-07-04T20:00:59.371260Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class DownSyndrome(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.DownSyndrome)" ] }, { "cell_type": "code", "execution_count": 3, "id": "4814dc04", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.373088Z", "iopub.status.busy": "2026-07-04T20:00:59.373002Z", "iopub.status.idle": "2026-07-04T20:00:59.374745Z", "shell.execute_reply": "2026-07-04T20:00:59.374314Z" } }, "outputs": [], "source": [ "model = pya.models.DownSyndrome()" ] }, { "cell_type": "markdown", "id": "fdd0be16", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "fa67b5a8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.376003Z", "iopub.status.busy": "2026-07-04T20:00:59.375916Z", "iopub.status.idle": "2026-07-04T20:00:59.378026Z", "shell.execute_reply": "2026-07-04T20:00:59.377603Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"downsyndrome\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: genome-wide DNA methylation data\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: 196 DS and 439 non-DS newborns\n", "model.metadata[\"year\"] = 2021\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Muskens, Ivo S., et al. \\\"The genome-wide impact of trisomy 21 on DNA methylation and its implications for hematopoiesis.\\\" Nature Communications 12 (2021): 821.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1038/s41467-021-21064-z\"\n", "model.metadata[\"notes\"] = \"Implementation-derived Down-syndrome-associated methylation projection score: pyaging computes a zero-intercept weighted sum of 652 neonatal blood-spot beta values using the paper's autosomal EWAS beta_overall effect estimates. The paper presents an EWAS, not a trained or validated Down syndrome classifier.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"neonatal blood spots\"] # Paper: newborn blood samples\n", "model.metadata[\"predicts\"] = [\"Down syndrome methylation score\"] # Paper: The output is the sum of methylation beta values weighted by beta_overall EWAS estimates.\n", "model.metadata[\"training_target\"] = [\"not applicable\"] # Paper: The EWAS compares 196 newborns with Down syndrome and 439 without Down syndrome.\n", "model.metadata[\"unit\"] = [\"unitless\"] # Paper: Methylation beta values and beta_overall weights are summed without calibration.\n", "model.metadata[\"model_type\"] = \"weighted linear score\" # Paper: weights = beta_overall; intercept = 0.0\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\"] # Paper: Illumina Infinium MethylationEPIC Beadchip\n", "model.metadata[\"population\"] = \"newborns\" # Paper: 196 DS and 439 non-DS newborns\n", "model.metadata[\"journal\"] = \"Nature Communications\"\n", "model.metadata[\"last_author\"] = \"Adam J. de Smith\"\n", "model.metadata[\"n_features\"] = 652\n", "model.metadata[\"citations\"] = 62\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "b6431b55", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "dbd81250", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.379354Z", "iopub.status.busy": "2026-07-04T20:00:59.379267Z", "iopub.status.idle": "2026-07-04T20:00:59.626967Z", "shell.execute_reply": "2026-07-04T20:00:59.626471Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o downsyndrome.xlsx https://static-content.springer.com/esm/art%3A10.1038%2Fs41467-021-21064-z/MediaObjects/41467_2021_21064_MOESM6_ESM.xlsx\")" ] }, { "cell_type": "markdown", "id": "15e64390", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "6f7a291f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.628313Z", "iopub.status.busy": "2026-07-04T20:00:59.628209Z", "iopub.status.idle": "2026-07-04T20:00:59.840432Z", "shell.execute_reply": "2026-07-04T20:00:59.839503Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/lucascamillo/pyaging/.venv/lib/python3.13/site-packages/openpyxl/worksheet/_reader.py:329: UserWarning: Unknown extension is not supported and will be removed\n", " warn(msg)\n" ] } ], "source": [ "df = pd.read_excel('downsyndrome.xlsx', sheet_name='EWAS_autosomes', skiprows=2)\n", "model.features = df['CpG'].tolist()" ] }, { "cell_type": "markdown", "id": "52f591e0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "813b5c2c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.842194Z", "iopub.status.busy": "2026-07-04T20:00:59.842084Z", "iopub.status.idle": "2026-07-04T20:00:59.844716Z", "shell.execute_reply": "2026-07-04T20:00:59.844210Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['beta_overall'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([0.0]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "d870db88", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.845954Z", "iopub.status.busy": "2026-07-04T20:00:59.845852Z", "iopub.status.idle": "2026-07-04T20:00:59.848349Z", "shell.execute_reply": "2026-07-04T20:00:59.847820Z" } }, "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": "84481b74", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "5419366e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.849670Z", "iopub.status.busy": "2026-07-04T20:00:59.849567Z", "iopub.status.idle": "2026-07-04T20:00:59.851319Z", "shell.execute_reply": "2026-07-04T20:00:59.850989Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "49e4fdec", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "e98f49e7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.852678Z", "iopub.status.busy": "2026-07-04T20:00:59.852577Z", "iopub.status.idle": "2026-07-04T20:00:59.854085Z", "shell.execute_reply": "2026-07-04T20:00:59.853713Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "f6b7f345", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.855360Z", "iopub.status.busy": "2026-07-04T20:00:59.855268Z", "iopub.status.idle": "2026-07-04T20:00:59.857362Z", "shell.execute_reply": "2026-07-04T20:00:59.856978Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "a437e727", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "3d681f34", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.858587Z", "iopub.status.busy": "2026-07-04T20:00:59.858504Z", "iopub.status.idle": "2026-07-04T20:00:59.861609Z", "shell.execute_reply": "2026-07-04T20:00:59.861231Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Muskens, Ivo S., et al. \"Germline aberrant methylation '\n", " 'associated with Down syndrome and its impact on childhood acute '\n", " 'lymphoblastic leukemia risk.\" Nature Communications 12.1 (2021): '\n", " '821.',\n", " 'clock_name': 'downsyndrome',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1038/s41467-021-21064-z',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2021}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg07741821', 'cg02993069', 'cg12477880', 'cg08882472', 'cg24942416', 'cg07841633', 'cg00994804', 'cg11218872', 'cg02451831', 'cg13382072', 'cg24020235', 'cg17239923', 'cg19030331', 'cg03142697', 'cg24999883', 'cg12679760', 'cg23719650', 'cg11972401', 'cg23565347', 'cg19765472', 'cg04599341', 'cg16831070', 'cg06758350', 'cg11151981', 'cg08620751', 'cg08849601', 'cg02680932', 'cg22539182', 'cg11075561', 'cg04131721']... [Total elements: 652]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=652, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-0.28867992758750916, 0.19754931330680847, 0.38263070583343506, 0.15737323462963104, -0.17506907880306244, -0.3285122215747833, 0.38840898871421814, -0.12085756659507751, -0.15854349732398987, 0.179888516664505, 0.18759849667549133, 0.23218844830989838, 0.16510215401649475, 0.3955685794353485, -0.0671553760766983, 0.18286938965320587, -0.10707408934831619, 0.05787608027458191, -0.09203432500362396, 0.2067182958126068, 0.19878044724464417, -0.061179906129837036, 0.28260132670402527, -0.062310606241226196, 0.1342443823814392, -0.07032307982444763, -0.07384317368268967, -0.14031463861465454, -0.22928495705127716, -0.0856233760714531]... [Tensor of shape torch.Size([1, 652])]\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "512347b2", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "ae50d83b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.862812Z", "iopub.status.busy": "2026-07-04T20:00:59.862726Z", "iopub.status.idle": "2026-07-04T20:00:59.866987Z", "shell.execute_reply": "2026-07-04T20:00:59.866594Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-4.8313],\n", " [-1.6036],\n", " [ 4.0192],\n", " [-1.2427],\n", " [ 1.9975],\n", " [-1.7729],\n", " [ 0.8015],\n", " [-1.8500],\n", " [ 1.6546],\n", " [ 0.3752]], 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": "cd022878", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "fbc36ccc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.868554Z", "iopub.status.busy": "2026-07-04T20:00:59.868472Z", "iopub.status.idle": "2026-07-04T20:00:59.871908Z", "shell.execute_reply": "2026-07-04T20:00:59.871419Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "2b66ba55", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "2aa157b8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:59.873229Z", "iopub.status.busy": "2026-07-04T20:00:59.873151Z", "iopub.status.idle": "2026-07-04T20:00:59.877137Z", "shell.execute_reply": "2026-07-04T20:00:59.876655Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: downsyndrome.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 (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 }