{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# Pasta" ] }, { "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)\n" ] }, { "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-07T17:47:34.133350Z", "iopub.status.busy": "2025-04-07T17:47:34.132902Z", "iopub.status.idle": "2025-04-07T17:47:35.476506Z", "shell.execute_reply": "2025-04-07T17:47:35.476150Z" } }, "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-07T17:47:35.478214Z", "iopub.status.busy": "2025-04-07T17:47:35.477997Z", "iopub.status.idle": "2025-04-07T17:47:35.484695Z", "shell.execute_reply": "2025-04-07T17:47:35.484415Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Pasta(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " @staticmethod\n", " def _rank_average(values):\n", " \"\"\"\n", " Assign average ranks (1-based) per vector, handling ties.\n", " \"\"\"\n", " sorted_vals, sorted_idx = torch.sort(values)\n", " ranks = torch.empty_like(sorted_vals, dtype=values.dtype)\n", "\n", " n = values.numel()\n", " start = 0\n", " while start < n:\n", " end = start + 1\n", " while end < n and sorted_vals[end] == sorted_vals[start]:\n", " end += 1\n", " avg_rank = (start + end - 1) / 2.0 + 1.0\n", " ranks[sorted_idx[start:end]] = avg_rank\n", " start = end\n", "\n", " return ranks\n", "\n", " def preprocess(self, x):\n", " \"\"\"\n", " Fill missing values with the global median then rank-normalize per sample.\n", " \"\"\"\n", " median = torch.nanmedian(x)\n", " if torch.isnan(median):\n", " median = torch.tensor(0.0, device=x.device, dtype=x.dtype)\n", " x = torch.where(torch.isnan(x), median, x)\n", "\n", " ranked = torch.empty_like(x, dtype=x.dtype)\n", " for i in range(x.size(0)):\n", " ranked[i] = self._rank_average(x[i])\n", "\n", " return ranked\n", "\n", " def postprocess(self, x):\n", " \"\"\"\n", " Apply linear scaling and shifting constants from the original Pasta definition.\n", " \"\"\"\n", " scale = self.postprocess_dependencies[0]\n", " offset_factor = self.postprocess_dependencies[1]\n", " return x * scale + offset_factor * scale\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.Pasta)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:35.485963Z", "iopub.status.busy": "2025-04-07T17:47:35.485866Z", "iopub.status.idle": "2025-04-07T17:47:35.487577Z", "shell.execute_reply": "2025-04-07T17:47:35.487295Z" } }, "outputs": [], "source": [ "model = pya.models.Pasta()" ] }, { "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-07T17:47:35.488812Z", "iopub.status.busy": "2025-04-07T17:47:35.488727Z", "iopub.status.idle": "2025-04-07T17:47:35.490718Z", "shell.execute_reply": "2025-04-07T17:47:35.490459Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"pasta\"\n", "model.metadata[\"data_type\"] = \"transcriptomics\" # Paper: Pasta is a multi-platform, multi-tissue transcriptomic aging clock.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: Pasta is a multi-platform, multi-tissue transcriptomic aging clock.\n", "model.metadata[\"year\"] = 2025\n", "model.metadata[\"approved_by_author\"] = \"✅\"\n", "model.metadata[\"citation\"] = \"Salignon, J. et al. Pasta, a versatile transcriptomic clock, maps the chemical and genetic determinants of aging and rejuvenation. bioRxiv 2025.06.04.657785 (2025).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1101/2025.06.04.657785\"\n", "model.metadata[\"notes\"] = \"Human Pasta age-shift classifier applied to within-sample rank-transformed expression. It converts a ridge-logistic older-versus-younger log-odds score to an age score.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"multi-tissue\"] # Paper: 17,212 healthy samples spanned multiple tissues and 21 studies.\n", "model.metadata[\"predicts\"] = [\"transcriptomic age\"] # Paper: Classifier predictions were converted into age differences called age scores.\n", "model.metadata[\"training_target\"] = [\"age ordering\"] # Paper: The output variable was binary: sample 1 younger or older than sample 2.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: Scaling factors converted predictions into age differences using 10-year bins.\n", "model.metadata[\"model_type\"] = \"ridge logistic regression\" # Paper: Ridge-regularized generalized linear models used 10-fold cross-validation.\n", "model.metadata[\"platform\"] = [\"RNA-seq\", \"gene expression microarray\"] # Paper: Training studies included bulk RNA-seq and microarray.\n", "model.metadata[\"population\"] = \"human, age unspecified\" # Paper: Healthy human donor data from GTEx, GEO, and Expression Atlas.\n", "model.metadata[\"journal\"] = \"bioRxiv\"\n", "model.metadata[\"last_author\"] = \"Christian G. Riedel\"\n", "model.metadata[\"n_features\"] = 8113\n", "model.metadata[\"citations\"] = 1\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": "7bec474f-80ce-4884-9472-30c193327117", "metadata": {}, "source": [ "#### Download coefficient file" ] }, { "cell_type": "code", "execution_count": 5, "id": "aa4a1b59-dda3-4ea8-8f34-b3c53ecbc310", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:35.492080Z", "iopub.status.busy": "2025-04-07T17:47:35.491992Z", "iopub.status.idle": "2025-04-07T17:47:36.204837Z", "shell.execute_reply": "2025-04-07T17:47:36.204380Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", "100 322k 100 322k 0 0 1174k 0 --:--:-- --:--:-- --:--:-- 1176k\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coeff_url = \"https://raw.githubusercontent.com/bio-learn/biolearn/master/biolearn/data/Pasta.csv\"\n", "os.system(f\"curl -L {coeff_url} -o Pasta.csv\")\n" ] }, { "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": 6, "id": "f26a49e3-7389-416c-9080-539f50e9abd0", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:36.206985Z", "iopub.status.busy": "2025-04-07T17:47:36.206832Z", "iopub.status.idle": "2025-04-07T17:47:36.211174Z", "shell.execute_reply": "2025-04-07T17:47:36.210783Z" } }, "outputs": [], "source": [ "coeffs = pd.read_csv('Pasta.csv')\n", "coeffs['feature'] = coeffs['GeneID']\n", "coeffs['coefficient'] = coeffs['CoefficientTraining']\n", "\n", "model.features = coeffs['feature'].tolist()\n" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "markdown", "id": "d79e5690-e284-4de6-8460-d3545a8192af", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": 7, "id": "7f6187ed-fcff-4ff2-bcb1-b5bcef8190e8", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:36.213121Z", "iopub.status.busy": "2025-04-07T17:47:36.212960Z", "iopub.status.idle": "2025-04-07T17:47:36.216800Z", "shell.execute_reply": "2025-04-07T17:47:36.216396Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coeffs['coefficient'].tolist()).unsqueeze(0)\n", "intercept = torch.tensor([0.0])\n" ] }, { "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-07T17:47:36.218646Z", "iopub.status.busy": "2025-04-07T17:47:36.218507Z", "iopub.status.idle": "2025-04-07T17:47:36.221310Z", "shell.execute_reply": "2025-04-07T17:47:36.220958Z" } }, "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": "90d45266-962d-41b6-927c-6a147ed41305", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:36.222862Z", "iopub.status.busy": "2025-04-07T17:47:36.222749Z", "iopub.status.idle": "2025-04-07T17:47:36.224649Z", "shell.execute_reply": "2025-04-07T17:47:36.224309Z" } }, "outputs": [], "source": [ "model.reference_values = [float(\"nan\")] * len(model.features)" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "f7d32b69-e20e-42ff-aba9-d07b9b44dbd1", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:36.226270Z", "iopub.status.busy": "2025-04-07T17:47:36.226156Z", "iopub.status.idle": "2025-04-07T17:47:36.228064Z", "shell.execute_reply": "2025-04-07T17:47:36.227750Z" } }, "outputs": [], "source": [ "model.preprocess_name = \"median_fill_and_rank_normalization\"\n", "model.preprocess_dependencies = None\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:47:36.229604Z", "iopub.status.busy": "2025-04-07T17:47:36.229468Z", "iopub.status.idle": "2025-04-07T17:47:36.231233Z", "shell.execute_reply": "2025-04-07T17:47:36.230916Z" } }, "outputs": [], "source": [ "model.postprocess_name = \"scale_and_shift\"\n", "model.postprocess_dependencies = [-4.76348378687217, -0.0502893445253186]\n" ] }, { "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-07T17:47:36.232808Z", "iopub.status.busy": "2025-04-07T17:47:36.232688Z", "iopub.status.idle": "2025-04-07T17:47:36.236913Z", "shell.execute_reply": "2025-04-07T17:47:36.236596Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '✅',\n", " 'citation': 'Salignon, Jerome, et al. \"Pasta, an age-shift transcriptomic '\n", " 'clock, maps the chemical and genetic determinants of aging and '\n", " 'rejuvenation.\" bioRxiv (2025): 2025-06.',\n", " 'clock_name': 'pasta',\n", " 'data_type': 'transcriptomics',\n", " 'doi': 'https://doi.org/10.1101/2025.06.04.657785',\n", " 'notes': 'Rank-normalized transcriptomic clock.',\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2025}\n", "reference_values: [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]... [Total elements: 8113]\n", "preprocess_name: 'median_fill_and_rank_normalization'\n", "preprocess_dependencies: None\n", "postprocess_name: 'scale_and_shift'\n", "postprocess_dependencies: [-4.76348378687217, -0.0502893445253186]\n", "features: ['ENSG00000196839', 'ENSG00000170558', 'ENSG00000133997', 'ENSG00000168060', 'ENSG00000101473', 'ENSG00000136754', 'ENSG00000113552', 'ENSG00000177485', 'ENSG00000136560', 'ENSG00000094631', 'ENSG00000108840', 'ENSG00000170248', 'ENSG00000153094', 'ENSG00000159921', 'ENSG00000165879', 'ENSG00000135451', 'ENSG00000142892', 'ENSG00000179776', 'ENSG00000167670', 'ENSG00000129484', 'ENSG00000041880', 'ENSG00000113361', 'ENSG00000141198', 'ENSG00000100284', 'ENSG00000013619', 'ENSG00000010017', 'ENSG00000105993', 'ENSG00000113810', 'ENSG00000182963', 'ENSG00000126261']... [Total elements: 8113]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=8113, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-2.4399256290053017e-05, -1.774273368937429e-05, 1.554851587570738e-05, 1.1031659596483223e-05, 1.6993128156173043e-05, 3.9308954001171514e-05, -0.00012627331307157874, 2.8949250463483622e-06, -6.271281017689034e-05, 2.9893646569689736e-05, 3.6174697015667334e-05, 6.864466558909044e-05, -2.3814825908630155e-05, 3.11008479911834e-05, 1.0880126865231432e-05, 9.605172635929193e-06, 1.1990639904979616e-05, 9.29949510464212e-06, 6.331568147288635e-05, -3.362866482348181e-05, -0.00022874546993989497, -2.7509766368893906e-05, 6.674586074950639e-06, 1.986255301744677e-05, -3.5506527638062835e-05, 2.922421663242858e-05, -4.5067787141306326e-05, 5.991863872623071e-05, 3.728850060724653e-05, 4.235586311551742e-05]... [Tensor of shape torch.Size([1, 8113])]\n", "base_model.linear.bias: tensor([0.])\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-07T17:47:36.238465Z", "iopub.status.busy": "2025-04-07T17:47:36.238354Z", "iopub.status.idle": "2025-04-07T17:47:36.242868Z", "shell.execute_reply": "2025-04-07T17:47:36.242565Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-12.0950],\n", " [-36.7366],\n", " [ 2.5806],\n", " [ 29.9285],\n", " [-21.5258],\n", " [ -3.6489],\n", " [-39.3746],\n", " [ 36.6380],\n", " [ 27.4081],\n", " [-33.9218]], 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-07T17:47:36.244263Z", "iopub.status.busy": "2025-04-07T17:47:36.244159Z", "iopub.status.idle": "2025-04-07T17:47:36.246682Z", "shell.execute_reply": "2025-04-07T17:47:36.246395Z" } }, "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-07T17:47:36.248184Z", "iopub.status.busy": "2025-04-07T17:47:36.248077Z", "iopub.status.idle": "2025-04-07T17:47:36.255709Z", "shell.execute_reply": "2025-04-07T17:47:36.255449Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: EPIC_salas_18_reference.csv\n", "Deleted file: Pasta.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": ".venv", "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }