MammalianBlood2#
Index#
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.MammalianBlood2)
class MammalianBlood2(pyagingModel):
def __init__(self):
super().__init__()
def forward(self, x):
x_cpg = x[:, :-1756] # number of species in lookup table
x_species = x[:, -1756:] # 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 relative age to age in 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)
max_age = anage_tensor[indices, 3].unsqueeze(1)
x = torch.exp(-torch.exp(-x))
x = x * (max_age + gestation_time) - gestation_time
return x
[3]:
model = pya.models.MammalianBlood2()
Define clock metadata#
[4]:
model.metadata["clock_name"] = 'mammalianblood2'
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/UniversalBloodClock2_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['HighmaxAge'].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 = 'mammalian2'
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': 'mammalianblood2',
'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: 'mammalian2'
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],
...,
[5.60876712e-01, 3.05123288e+00, 2.30000000e+01, 2.99000000e+01],
[9.87671233e-02, 1.01706849e+00, 3.50000000e+00, 4.55000000e+00],
[3.15068493e-01, 4.58334000e-01, 2.70000000e+01, 3.51000000e+01]])]
features: ['cg00005271', 'cg00114412', 'cg00227334', 'cg00258262', 'cg00264615', 'cg00370791', 'cg00393367', 'cg00439117', 'cg00458823', 'cg00559067', 'cg00587168', 'cg00756693', 'cg00855890', 'cg00915004', 'cg00953859', 'cg00988788', 'cg01053290', 'cg01137681', 'cg01153166', 'cg01156745', 'cg01342051', 'cg01393939', 'cg01454752', 'cg01468874', 'cg01486146', 'cg01528792', 'cg01585174', 'cg01639959', 'cg01701526', 'cg01787104']... [Total elements: 2257]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=2257, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [0.08963627368211746, -0.001239112694747746, -0.00347901857458055, 0.009207980707287788, -7.227044989122078e-05, -0.055550578981637955, 0.04472345858812332, -0.36105695366859436, -0.018508171662688255, 0.07516830414533615, 0.011418496258556843, 0.008542953990399837, 0.10099010914564133, 0.26528534293174744, 0.013976222835481167, -0.01059863530099392, -0.05601461976766586, 0.12440702319145203, 0.0005542261060327291, -0.06668984144926071, 0.03973907604813576, 0.08093400299549103, 0.018934927880764008, 0.002540371846407652, 0.07929009199142456, -0.14058975875377655, -0.01051307376474142, -0.11011394113302231, -0.06954656541347504, -0.06167547032237053]... [Tensor of shape torch.Size([1, 501])]
base_model.linear.bias: tensor([1.0883])
%==================================== 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([[ 3.8852],
[ 0.5774],
[-0.1589],
[11.6877],
[-0.1233],
[ 5.4960],
[-0.0658],
[-0.5753],
[10.2756],
[42.2815]], dtype=torch.float64, grad_fn=<SubBackward0>)
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