Mammalian2#
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.Mammalian2)
class Mammalian2(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.Mammalian2()
Define clock metadata#
[4]:
model.metadata["clock_name"] = 'mammalian2'
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/clock2.csv')
df['feature'] = df['var']
df['coefficient'] = df['beta_clock2']
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': 'mammalian2',
'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: ['cg00020468', 'cg00096922', 'cg00098422', 'cg00106940', 'cg00195533', 'cg00250826', 'cg00258262', 'cg00314427', 'cg00362836', 'cg00384342', 'cg00454305', 'cg00467357', 'cg00489428', 'cg00568797', 'cg00593462', 'cg00643485', 'cg00689383', 'cg00694357', 'cg00788877', 'cg00864083', 'cg00871371', 'cg00929113', 'cg00975350', 'cg00984060', 'cg01019040', 'cg01038105', 'cg01061575', 'cg01063757', 'cg01090334', 'cg01137681']... [Total elements: 2572]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=2572, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [-0.0023850149009376764, -0.009445006027817726, 0.05580539628863335, -0.05914490297436714, -0.17265929281711578, 0.007619396783411503, 0.05367890000343323, -0.0786888375878334, 0.059616584330797195, -0.06562892347574234, -0.03485381603240967, -0.04281875863671303, 0.25934743881225586, 0.0007044760277494788, 0.10228588432073593, 0.10615047067403793, -0.019021064043045044, -0.2263999730348587, -0.010860024020075798, 0.0790414959192276, -0.10834652930498123, -0.01996506378054619, -0.08477586507797241, 0.026488320901989937, -0.022070782259106636, -0.09377875924110413, 0.03151073679327965, -0.09552916139364243, 0.04006564989686012, 0.03320020064711571]... [Tensor of shape torch.Size([1, 816])]
base_model.linear.bias: tensor([-1.9114])
%==================================== 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([[-6.2466e-01],
[-3.6986e-01],
[ 1.0588e+01],
[ 1.4034e+01],
[ 3.4747e+01],
[ 1.1834e-02],
[-6.6003e-01],
[-8.2192e-02],
[ 9.9949e-01],
[-3.5616e-02]], 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: species_annotation.csv
Deleted folder: MammalianMethylationConsortium
Deleted file: download.r