MammalianSkin2#
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.MammalianSkin2)
class MammalianSkin2(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.MammalianSkin2()
Define clock metadata#
[4]:
model.metadata["clock_name"] = 'mammalianskin2'
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/UniversalSkinClock2_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': 'mammalianskin2',
'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: ['cg00006213', 'cg00012521', 'cg00038376', 'cg00106940', 'cg00138195', 'cg00147638', 'cg00149213', 'cg00153046', 'cg00200794', 'cg00207040', 'cg00247020', 'cg00281640', 'cg00384342', 'cg00386499', 'cg00422680', 'cg00437193', 'cg00438946', 'cg00573041', 'cg00595237', 'cg00619575', 'cg00623160', 'cg00662491', 'cg00689651', 'cg00694357', 'cg00823476', 'cg00917676', 'cg00973544', 'cg01019040', 'cg01052727', 'cg01134518']... [Total elements: 2240]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=2240, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [0.10117671638727188, -0.014433339238166809, 0.01582280360162258, -0.16444504261016846, 0.2054484486579895, 0.0524430125951767, -0.001450160751119256, -0.043094977736473083, 0.11684814095497131, 0.044266875833272934, -0.4065758287906647, 0.12912878394126892, -0.08310002088546753, -0.24308426678180695, -0.03736037760972977, -0.03317352384328842, -0.027269257232546806, 0.16627272963523865, 0.0917305126786232, -1.0780762434005737, 0.008392410352826118, 0.008044207468628883, 0.0011250120587646961, -0.02045692503452301, -0.16058066487312317, 0.02744181826710701, -0.09590887278318405, -1.1931358575820923, -0.025111977010965347, 0.05292962118983269]... [Tensor of shape torch.Size([1, 484])]
base_model.linear.bias: tensor([-1.8751])
%==================================== 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.6164e-01],
[-1.0493e+00],
[ 2.7006e+01],
[-5.7418e-02],
[ 8.0249e+01],
[ 2.4591e+01],
[-8.7671e-02],
[ 1.5445e+01],
[-3.2877e-02],
[ 1.2089e+00]], 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