MammalianBlood3#

Index#

  1. Instantiate model class

  2. Define clock metadata

  3. Download clock dependencies

  4. Load features

  5. Load weights into base model

  6. Load reference values

  7. Load preprocess and postprocess objects

  8. Check all clock parameters

  9. Basic test

  10. Save torch model

  11. Clear directory

Let’s first import some packages:

[1]:
import os
import inspect
import shutil
import json
import torch
import pandas as pd
import pyaging as pya
import numpy as np

Instantiate model class#

[2]:
def print_entire_class(cls):
    source = inspect.getsource(cls)
    print(source)

print_entire_class(pya.models.MammalianBlood3)
class MammalianBlood3(pyagingModel):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        x_cpg = x[:, :-1707]  # number of species in lookup table
        x_species = x[:, -1707:]  # number of species in lookup table
        x = self.base_model(x_cpg)
        x = self.postprocess(x, x_species)
        return x

    def preprocess(self, x):
        return x

    def postprocess(self, x, x_species):
        """
        Converts output of to units of years.
        """
        indices = torch.argmax(x_species, dim=1)
        anage_array = self.postprocess_dependencies[0]
        anage_tensor = torch.tensor(anage_array, dtype=x.dtype, device=x.device)

        gestation_time = anage_tensor[indices, 0].unsqueeze(1)
        average_maturity_age = anage_tensor[indices, 1].unsqueeze(1)
        m_hat = 5 * (gestation_time / average_maturity_age) ** (0.38)

        # Create a mask for negative and non-negative values
        mask_negative = x < 0
        mask_non_negative = ~mask_negative

        x_pos = x[mask_non_negative]
        x_neg = x[mask_negative]

        gestation_time_pos = gestation_time[mask_non_negative]
        gestation_time_neg = gestation_time[mask_negative]

        average_maturity_age_pos = average_maturity_age[mask_non_negative]
        average_maturity_age_neg = average_maturity_age[mask_negative]

        m_hat_pos = m_hat[mask_non_negative]
        m_hat_neg = m_hat[mask_negative]

        # Initialize the result tensor
        age_tensor = torch.empty_like(x)

        # Exponential transformation for negative values
        age_tensor[mask_non_negative] = (
            m_hat_pos * (average_maturity_age_pos + gestation_time_pos) * (x_pos + 1) - gestation_time_pos
        )

        # Linear transformation for non-negative values
        age_tensor[mask_negative] = (
            m_hat_neg * (average_maturity_age_neg + gestation_time_neg) * torch.exp(x_neg) - gestation_time_neg
        )

        return age_tensor

[3]:
model = pya.models.MammalianBlood3()

Define clock metadata#

[4]:
model.metadata["clock_name"] = 'mammalianblood3'
model.metadata["data_type"] = 'methylation'
model.metadata["species"] = 'multi'
model.metadata["year"] = 2023
model.metadata["approved_by_author"] = '⌛'
model.metadata["citation"] = "Lu, A. T., et al. \"Universal DNA methylation age across mammalian tissues.\" Nature aging 3.9 (2023): 1144-1166."
model.metadata["doi"] = "https://doi.org/10.1038/s43587-023-00462-6"
model.metadata["research_only"] = None
model.metadata["notes"] = None

Download clock dependencies#

Download GitHub repository#

[5]:
github_url = "https://github.com/shorvath/MammalianMethylationConsortium.git"
github_folder_name = github_url.split('/')[-1].split('.')[0]
os.system(f"git clone {github_url}")
[5]:
0

Download from R package#

[6]:
%%writefile download.r

options(repos = c(CRAN = "https://cloud.r-project.org/"))

myinput.list=readRDS('MammalianMethylationConsortium/UniversalPanMammalianClock/ClockParameters/mydata_GitHub.Rds')
anage=myinput.list[[3]]
anage=subset(anage,select=c(SpeciesLatinName,GestationTimeInYears, averagedMaturity.yrs,maxAge))
anage$HighmaxAge=1.3*anage$maxAge
anage$HighmaxAge[anage$SpeciesLatinName=='Homo sapiens']=anage$maxAge[anage$SpeciesLatinName=='Homo sapiens']
anage$HighmaxAge[anage$SpeciesLatinName=='Mus musculus']=anage$maxAge[anage$SpeciesLatinName=='Mus musculus']
write.csv(anage, "species_annotation.csv")
Writing download.r
[7]:
os.system("Rscript download.r")
[7]:
0

Load features#

From CSV file#

[8]:
df = pd.read_csv('MammalianMethylationConsortium/UniversalPanMammalianClock/ClockParameters/tissue_specific_clock/UniversalBloodClock3_final.csv')
df['feature'] = df['var']
df['coefficient'] = df['beta']
cpg_features = df['feature'][1:].tolist()

anage_df = pd.read_csv('species_annotation.csv', index_col=0)
anage_df = anage_df[~anage_df['averagedMaturity.yrs'].isna()]
anage_df = anage_df[~anage_df['GestationTimeInYears'].isna()]
anage_df = anage_df.reset_index().drop('index', axis=1)
anage_df = anage_df.fillna(0)
species_features = anage_df['SpeciesLatinName'].tolist()

model.features = cpg_features + species_features

Load weights into base model#

[9]:
weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)
intercept = torch.tensor([df['coefficient'][0]])

Linear model#

[10]:
base_model = pya.models.LinearModel(input_dim=len(model.features))

base_model.linear.weight.data = weights.float()
base_model.linear.bias.data = intercept.float()

model.base_model = base_model

Load reference values#

[11]:
reference_list = np.array([0] * len(model.features))
reference_list[len(cpg_features) + np.where(anage_df.SpeciesLatinName == 'Homo sapiens')[0][0]] = 0.5
model.reference_values = reference_list

Load preprocess and postprocess objects#

[12]:
model.preprocess_name = None
model.preprocess_dependencies = None
[13]:
model.postprocess_name = 'mammalian3'
anage_df = anage_df.loc[:, ['GestationTimeInYears','averagedMaturity.yrs', 'maxAge',        'HighmaxAge']]
anage_array = np.array(anage_df)
model.postprocess_dependencies = [anage_array]

Check all clock parameters#

[14]:
pya.utils.print_model_details(model)

%==================================== Model Details ====================================%
Model Attributes:

training: True
metadata: {'approved_by_author': '⌛',
 'citation': 'Lu, A. T., et al. "Universal DNA methylation age across '
             'mammalian tissues." Nature aging 3.9 (2023): 1144-1166.',
 'clock_name': 'mammalianblood3',
 'data_type': 'methylation',
 'doi': 'https://doi.org/10.1038/s43587-023-00462-6',
 'notes': None,
 'research_only': None,
 'species': 'multi',
 'version': None,
 'year': 2023}
reference_values: array([0, 0, 0, ..., 0, 0, 0])
preprocess_name: None
preprocess_dependencies: None
postprocess_name: 'mammalian3'
postprocess_dependencies: [array([[2.19178082e-02, 2.49315068e+00, 3.60000000e+01, 4.68000000e+01],
       [1.64383562e-02, 3.00000000e+00, 1.15000000e+01, 1.49500000e+01],
       [3.01369863e-02, 4.50000000e+00, 1.50000000e+01, 1.95000000e+01],
       ...,
       [1.48575342e-01, 1.15317808e+00, 0.00000000e+00, 0.00000000e+00],
       [6.95616438e-02, 1.16958904e-01, 0.00000000e+00, 0.00000000e+00],
       [3.15068493e-01, 4.58334000e-01, 2.70000000e+01, 3.51000000e+01]])]
features: ['cg00114412', 'cg00295657', 'cg00296110', 'cg00310215', 'cg00439117', 'cg00471897', 'cg00559067', 'cg00578937', 'cg00587168', 'cg00728976', 'cg00742557', 'cg00780852', 'cg00833227', 'cg00910419', 'cg00915004', 'cg00918089', 'cg00935831', 'cg00953859', 'cg01053290', 'cg01079397.1', 'cg01079397.2', 'cg01137681', 'cg01190601', 'cg01235968', 'cg01393939', 'cg01486146', 'cg01528792', 'cg01566077', 'cg01701526', 'cg01932632']... [Total elements: 2097]
base_model_features: None

%==================================== Model Details ====================================%
Model Structure:

base_model: LinearModel(
  (linear): Linear(in_features=2097, out_features=1, bias=True)
)

%==================================== Model Details ====================================%
Model Parameters and Weights:

base_model.linear.weight: [-0.014848549850285053, -0.015909673646092415, -0.03707726672291756, 0.9694833755493164, -0.9203034043312073, -0.026087677106261253, 0.33000633120536804, -0.7896574139595032, 0.04147997871041298, -0.49385330080986023, 0.09399742633104324, -0.002576522994786501, 0.25313621759414673, -0.9668075442314148, 0.591387927532196, -0.04230678081512451, 0.014051662757992744, 0.5414583086967468, -0.41466763615608215, 0.13480553030967712, -0.48314857482910156, 0.3226204514503479, 0.036402441561222076, -0.20592643320560455, 0.476875901222229, 0.17880657315254211, -0.5554146766662598, -1.3004117012023926, -0.031236404553055763, -0.15437926352024078]... [Tensor of shape torch.Size([1, 390])]
base_model.linear.bias: tensor([3.0563])

%==================================== Model Details ====================================%

Basic test#

[15]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float)
model.eval()
model.to(float)
pred = model(input)
pred
[15]:
tensor([[-8.2191e-02],
        [-4.9315e-02],
        [ 5.2099e+00],
        [ 4.4614e+01],
        [ 2.0724e+01],
        [-2.6487e-02],
        [-4.5110e-02],
        [-4.7053e-01],
        [ 4.8666e+01],
        [ 3.5803e+02]], dtype=torch.float64, grad_fn=<IndexPutBackward0>)

Save torch model#

[16]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")

Clear directory#

[17]:
# Function to remove a folder and all its contents
def remove_folder(path):
    try:
        shutil.rmtree(path)
        print(f"Deleted folder: {path}")
    except Exception as e:
        print(f"Error deleting folder {path}: {e}")

# Get a list of all files and folders in the current directory
all_items = os.listdir('.')

# Loop through the items
for item in all_items:
    # Check if it's a file and does not end with .ipynb
    if os.path.isfile(item) and not item.endswith('.ipynb'):
        os.remove(item)
        print(f"Deleted file: {item}")
    # Check if it's a folder
    elif os.path.isdir(item):
        remove_folder(item)
Deleted file: coefficients.xlsx
Deleted file: species_annotation.csv
Deleted folder: MammalianMethylationConsortium
Deleted file: download.r