{ "cells": [ { "cell_type": "markdown", "id": "cfeef651-0cae-4d14-b011-3b78c46fa2e4", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/lucascamillomd/pyaging/blob/main/tutorials/tutorial_utils.ipynb) [![Open In nbviewer](https://img.shields.io/badge/View%20in-nbviewer-orange)](https://nbviewer.jupyter.org/github/lucascamillomd/pyaging/blob/main/tutorials/tutorial_utils.ipynb)" ] }, { "cell_type": "markdown", "id": "c5aac698-f7ed-4489-8c6c-f75ec3f3df73", "metadata": {}, "source": [ "# Search, cite, get metadata and clock parameters" ] }, { "cell_type": "markdown", "id": "9d132efc-15f4-4b4d-bdd7-c1aa21f7d13e", "metadata": {}, "source": [ "This tutorial shows the use of some `pyaging` helper functions." ] }, { "cell_type": "code", "execution_count": 1, "id": "445d6e41-c30f-4cbc-b5b3-97dc1020f09e", "metadata": {}, "outputs": [], "source": [ "import pyaging as pya" ] }, { "cell_type": "markdown", "id": "72174a28-ddef-4ab1-ad7b-7c306920df23", "metadata": {}, "source": [ "## Search" ] }, { "cell_type": "markdown", "id": "aac87908-def9-40d9-a2cc-ffb34fae2dc5", "metadata": {}, "source": [ "There are two main ways to search for a clock in `pyaging`. The first is through the doi of the paper in which the clock was developed." ] }, { "cell_type": "code", "execution_count": 2, "id": "d5281558-f1b6-4a5c-ba52-708d31265374", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----> 🏗️ Starting find_clock_by_doi function\n", "|-----> ⚙️ Load all clock metadata started\n", "|-----------> Data found in pyaging_data/all_clock_metadata.pt\n", "|-----> ✅ Load all clock metadata finished [0.4988s]\n", "|-----> ⚙️ Searching for clock based on DOI started\n", "|-----------> in progress: 100.0000%\n", "|-----------> Clocks with DOI https://doi.org/10.1038/s43587-022-00248-2: pchorvath2013, pcphenoage, pcgrimage, pchannum, pcdnamtl, hrsinchphenoage, pcskinandblood\n", "|-----> ✅ Searching for clock based on DOI finished [0.0485s]\n", "|-----> 🎉 Done! [0.5502s]\n" ] } ], "source": [ "pya.utils.find_clock_by_doi('https://doi.org/10.1038/s43587-022-00248-2')" ] }, { "cell_type": "markdown", "id": "125e90ad-25f5-46e2-a2ad-0f87c6729ea9", "metadata": {}, "source": [ "The second way is by simply showing the names of all the clocks that are available." ] }, { "cell_type": "code", "execution_count": 3, "id": "48c37121-3b2d-40cf-80aa-c1f58fbad127", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----> 🏗️ Starting show_all_clocks function\n", "|-----> ⚙️ Load all clock metadata started\n", "|-----------> Data found in pyaging_data/all_clock_metadata.pt\n", "|-----> ✅ Load all clock metadata finished [0.4589s]\n", "|-----> ⚙️ Showing all available clock names started\n", "|-----------> altumage\n", "|-----------> bitage\n", "|-----------> camilloh3k27ac\n", "|-----------> camilloh3k27me3\n", "|-----------> camilloh3k36me3\n", "|-----------> camilloh3k4me1\n", "|-----------> camilloh3k4me3\n", "|-----------> camilloh3k9ac\n", "|-----------> camilloh3k9me3\n", "|-----------> camillopanhistone\n", "|-----------> dnamphenoage\n", "|-----------> dnamtl\n", "|-----------> dunedinpace\n", "|-----------> encen100\n", "|-----------> encen40\n", "|-----------> grimage\n", "|-----------> grimage2\n", "|-----------> han\n", "|-----------> hannum\n", "|-----------> horvath2013\n", "|-----------> hrsinchphenoage\n", "|-----------> knight\n", "|-----------> leecontrol\n", "|-----------> leerefinedrobust\n", "|-----------> leerobust\n", "|-----------> lin\n", "|-----------> mammalian1\n", "|-----------> mammalian2\n", "|-----------> mammalian3\n", "|-----------> mammalianblood2\n", "|-----------> mammalianblood3\n", "|-----------> mammalianfemale\n", "|-----------> mammalianlifespan\n", "|-----------> mammalianskin2\n", "|-----------> mammalianskin3\n", "|-----------> meer\n", "|-----------> ocampoatac1\n", "|-----------> ocampoatac2\n", "|-----------> pcdnamtl\n", "|-----------> pcgrimage\n", "|-----------> pchannum\n", "|-----------> pchorvath2013\n", "|-----------> pcphenoage\n", "|-----------> pcskinandblood\n", "|-----------> pedbe\n", "|-----------> petkovich\n", "|-----------> phenoage\n", "|-----------> replitali\n", "|-----------> skinandblood\n", "|-----------> stubbs\n", "|-----------> thompson\n", "|-----------> zhangblup\n", "|-----------> zhangen\n", "|-----------> zhangmortality\n", "|-----> ✅ Showing all available clock names finished [0.0280s]\n", "|-----> 🎉 Done! [0.4903s]\n" ] } ], "source": [ "pya.utils.show_all_clocks()" ] }, { "cell_type": "markdown", "id": "6e579e9f-073d-4a26-9bd6-271068ac8601", "metadata": {}, "source": [ "## Cite" ] }, { "cell_type": "markdown", "id": "fb15a3ba-4dd1-4526-ab45-732979fcd676", "metadata": {}, "source": [ "`pyaging` also provides citations for all available clocks." ] }, { "cell_type": "code", "execution_count": 4, "id": "61f0ac9d-b26c-493b-a576-5d7cde67775c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----> 🏗️ Starting cite_clock function\n", "|-----> ⚙️ Load all clock metadata started\n", "|-----------> Data found in pyaging_data/all_clock_metadata.pt\n", "|-----> ✅ Load all clock metadata finished [0.5150s]\n", "|-----> ⚙️ Searching for citation of clock altumage started\n", "|-----------> Citation for altumage:\n", "|-----------> de Lima Camillo, Lucas Paulo, Louis R. Lapierre, and Ritambhara Singh. \"A pan-tissue DNA-methylation epigenetic clock based on deep learning.\" npj Aging 8.1 (2022): 4.\n", "|-----------> Please also consider citing pyaging :)\n", "|-----------> de Lima Camillo, Lucas Paulo. \"pyaging: a Python-based compendium of GPU-optimized aging clocks.\" bioRxiv (2023): 2023-11.\n", "|-----> ✅ Searching for citation of clock altumage finished [0.0024s]\n", "|-----> 🎉 Done! [0.5205s]\n" ] } ], "source": [ "pya.utils.cite_clock('AltumAge')" ] }, { "cell_type": "markdown", "id": "9838c1b6-47a6-44a4-8b3e-8c5e760c6172", "metadata": {}, "source": [ "## Get metadata" ] }, { "cell_type": "markdown", "id": "99fc2253-7f33-479f-b946-97d0f77c6d19", "metadata": {}, "source": [ "To get all of the metadata for a clock, including citation and doi, just run the following." ] }, { "cell_type": "code", "execution_count": 5, "id": "2b65ef03-485a-4631-b831-8e87d3ce0f64", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----> 🏗️ Starting get_clock_metadata function\n", "|-----> ⚙️ Load all clock metadata started\n", "|-----------> Data found in pyaging_data/all_clock_metadata.pt\n", "|-----> ✅ Load all clock metadata finished [0.5505s]\n", "|-----> ⚙️ Showing altumage metadata started\n", "|-----------> clock_name: altumage\n", "|-----------> data_type: methylation\n", "|-----------> species: Homo sapiens\n", "|-----------> year: 2022\n", "|-----------> approved_by_author: ✅\n", "|-----------> citation: de Lima Camillo, Lucas Paulo, Louis R. Lapierre, and Ritambhara Singh. \"A pan-tissue DNA-methylation epigenetic clock based on deep learning.\" npj Aging 8.1 (2022): 4.\n", "|-----------> doi: https://doi.org/10.1038/s41514-022-00085-y\n", "|-----------> notes: None\n", "|-----------> version: None\n", "|-----------> reference_values: True\n", "|-----------> preprocess: scale\n", "|-----> ✅ Showing altumage metadata finished [0.0062s]\n", "|-----> 🎉 Done! [0.5622s]\n" ] } ], "source": [ "pya.utils.get_clock_metadata('AltumAge')" ] }, { "cell_type": "markdown", "id": "b95a3ad8-efdd-4c73-8851-ec17071a6e78", "metadata": {}, "source": [ "## Get clock parameters" ] }, { "cell_type": "markdown", "id": "02a494ca-d06b-4b0b-90b8-f4013a1c100c", "metadata": {}, "source": [ "To easily analyze the weights and features of a particular clock, please use:" ] }, { "cell_type": "code", "execution_count": 6, "id": "7c853695-bc26-4f66-bd9d-39ec2c381f80", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----> ⚙️ Load clock started\n", "|-----------> Data found in pyaging_data/altumage.pt\n", "|-----> ✅ Load clock finished [0.5409s]\n" ] } ], "source": [ "logger = pya.logger.Logger('test_logger')\n", "device = 'cpu'\n", "dir = 'pyaging_data'\n", "indent_level = 1\n", "\n", "clock = pya.pred.load_clock('AltumAge', device, dir, logger, indent_level=indent_level)" ] }, { "cell_type": "code", "execution_count": 7, "id": "a9b2e3bc-d8a2-4625-9d6a-77ba1dede403", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AltumAge(\n", " (base_model): AltumAgeNeuralNetwork(\n", " (linear1): Linear(in_features=20318, out_features=32, bias=True)\n", " (linear2): Linear(in_features=32, out_features=32, bias=True)\n", " (linear3): Linear(in_features=32, out_features=32, bias=True)\n", " (linear4): Linear(in_features=32, out_features=32, bias=True)\n", " (linear5): Linear(in_features=32, out_features=32, bias=True)\n", " (linear6): Linear(in_features=32, out_features=1, bias=True)\n", " (bn1): BatchNorm1d(20318, eps=0.001, momentum=0.99, affine=True, track_running_stats=True)\n", " (bn2): BatchNorm1d(32, eps=0.001, momentum=0.99, affine=True, track_running_stats=True)\n", " (bn3): BatchNorm1d(32, eps=0.001, momentum=0.99, affine=True, track_running_stats=True)\n", " (bn4): BatchNorm1d(32, eps=0.001, momentum=0.99, affine=True, track_running_stats=True)\n", " (bn5): BatchNorm1d(32, eps=0.001, momentum=0.99, affine=True, track_running_stats=True)\n", " (bn6): BatchNorm1d(32, eps=0.001, momentum=0.99, affine=True, track_running_stats=True)\n", " )\n", ")" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clock" ] }, { "cell_type": "markdown", "id": "f25f68da-0c13-4403-85ab-88f8a8d70c29", "metadata": {}, "source": [ "Let's check the weights of the first linear layer for AltumAge." ] }, { "cell_type": "code", "execution_count": 8, "id": "8660aec5-82a8-4b70-a71a-bd3571a81ded", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Parameter containing:\n", "tensor([[ 1.2465e-05, -2.4719e-04, 5.4308e-02, ..., -2.5304e-02,\n", " 5.2822e-02, 8.9800e-02],\n", " [ 3.5401e-04, -3.0528e-03, 2.8799e-02, ..., 6.8214e-03,\n", " 6.9691e-02, 1.2179e-01],\n", " [ 1.6119e-04, -6.7272e-06, -4.6887e-02, ..., 1.3132e-02,\n", " 9.2417e-02, -4.2074e-02],\n", " ...,\n", " [ 1.9902e-04, 9.0495e-04, -8.5197e-03, ..., -9.6892e-02,\n", " 2.9396e-02, 5.9170e-02],\n", " [-1.2038e-04, 3.7530e-04, 1.7924e-01, ..., -4.9997e-02,\n", " -1.2819e-02, 2.8045e-02],\n", " [ 1.1584e-04, 2.2752e-04, -3.0746e-02, ..., 1.7930e-02,\n", " 8.3116e-03, -2.0979e-02]], dtype=torch.float64, requires_grad=True)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clock.base_model.linear1.weight" ] }, { "cell_type": "markdown", "id": "a508d495-e2dd-4757-ad4d-732836f08c64", "metadata": {}, "source": [ "A quick look at the features:" ] }, { "cell_type": "code", "execution_count": 9, "id": "7e5ec51f-146a-45a6-ba7f-5a825b8315fd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['cg00000292',\n", " 'cg00002426',\n", " 'cg00003994',\n", " 'cg00007981',\n", " 'cg00008493',\n", " 'cg00008713',\n", " 'cg00009407',\n", " 'cg00011459',\n", " 'cg00012199',\n", " 'cg00012386']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(clock.features[0:10])" ] }, { "cell_type": "markdown", "id": "8a6b9591-5c1e-4665-88fd-ee633cd92798", "metadata": {}, "source": [ "And the reference values used:" ] }, { "cell_type": "code", "execution_count": 10, "id": "2f73647b-277f-4b93-b49d-a4907f49a892", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.7598633952352156,\n", " 0.7863788078967272,\n", " 0.06324422321924528,\n", " 0.029943418029386736,\n", " 0.9363471225552753,\n", " 0.05054944899168823,\n", " 0.0351571456459043,\n", " 0.9114132733331861,\n", " 0.037064057665286136,\n", " 0.039170308280475935]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(clock.reference_values[0:10])" ] }, { "cell_type": "markdown", "id": "6429c0d5-6737-4c76-b8c4-16b030325ad6", "metadata": {}, "source": [ "We can also get the metadata directly from the clock object:" ] }, { "cell_type": "code", "execution_count": 11, "id": "cac05fd5-5480-460e-801f-d3504a37f8d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'clock_name': 'altumage',\n", " 'data_type': 'methylation',\n", " 'species': 'Homo sapiens',\n", " 'year': 2022,\n", " 'approved_by_author': '✅',\n", " 'citation': 'de Lima Camillo, Lucas Paulo, Louis R. Lapierre, and Ritambhara Singh. \"A pan-tissue DNA-methylation epigenetic clock based on deep learning.\" npj Aging 8.1 (2022): 4.',\n", " 'doi': 'https://doi.org/10.1038/s41514-022-00085-y',\n", " 'notes': None,\n", " 'version': None}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clock.metadata" ] }, { "cell_type": "markdown", "id": "c4987574-3efc-4b63-8dcc-9938c94bc275", "metadata": {}, "source": [ "For a more in depth look at how the clock was setup, including the model type and the source of the weights, please look at our [clocks notebook folder](https://github.com/lucascamillomd/pyaging/tree/main/clocks/notebooks) on GitHub." ] } ], "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 }