CamilloH3K27me3#
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 joblib
import numpy as np
Instantiate model class#
[2]:
def print_entire_class(cls):
source = inspect.getsource(cls)
print(source)
print_entire_class(pya.models.CamilloH3K27me3)
class CamilloH3K27me3(pyagingModel):
def __init__(self):
super().__init__()
def preprocess(self, x):
return x
def postprocess(self, x):
return x
[3]:
model = pya.models.CamilloH3K27me3()
Define clock metadata#
[4]:
model.metadata["clock_name"] = 'camilloh3k27me3'
model.metadata["data_type"] = 'histone mark'
model.metadata["species"] = 'Homo sapiens'
model.metadata["year"] = 2023
model.metadata["approved_by_author"] = '✅'
model.metadata["citation"] = "de Lima Camillo, Lucas Paulo, et al. \"Histone mark age of human tissues and cells.\" bioRxiv (2023): 2023-08."
model.metadata["doi"] = 'https://doi.org/10.1101/2023.08.21.554165'
model.metadata["research_only"] = None
model.metadata["notes"] = None
Download clock dependencies#
Download GitHub repository#
[5]:
github_url = "https://github.com/rsinghlab/HistoneClocks.git"
github_folder_name = github_url.split('/')[-1].split('.')[0]
os.system(f"git clone {github_url}")
[5]:
0
Load features#
From CSV file#
[6]:
histone = 'H3K' + model.metadata["clock_name"].split('k')[1]
feature_selector_path = 'HistoneClocks/results/models/' + histone + '_feature_selector.pkl'
feature_selector = joblib.load(feature_selector_path)
dim_reduction_path = 'HistoneClocks/results/models/' + histone + '_dim_reduction.pkl'
dim_reduction = joblib.load(dim_reduction_path)
ard_model_path = 'HistoneClocks/results/models/' + histone + '_model.pkl'
ard_model = joblib.load(ard_model_path)
genes = pd.read_csv('HistoneClocks/metadata/Ensembl-105-EnsDb-for-Homo-sapiens-genes.csv')
chromosomes = ['1', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '2', '20', '21', '22', '3', '4', '5', '6', '7', '8', '9', 'X']
genes = genes[genes['chr'].apply(lambda x: x in chromosomes)]
genes.index = genes.gene_id
model.features = genes.gene_id[np.abs(feature_selector.coef_) > 0].tolist()
/Users/lucascamillo/mambaforge/envs/research/lib/python3.9/site-packages/sklearn/base.py:380: InconsistentVersionWarning: Trying to unpickle estimator ElasticNet from version 1.0.1 when using version 1.6.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
/Users/lucascamillo/mambaforge/envs/research/lib/python3.9/site-packages/sklearn/base.py:380: InconsistentVersionWarning: Trying to unpickle estimator TruncatedSVD from version 1.0.1 when using version 1.6.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
/Users/lucascamillo/mambaforge/envs/research/lib/python3.9/site-packages/sklearn/base.py:380: InconsistentVersionWarning: Trying to unpickle estimator ARDRegression from version 1.0.1 when using version 1.6.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:
https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations
warnings.warn(
Load weights into base model#
[7]:
weights = torch.tensor(ard_model.coef_).unsqueeze(0).float()
intercept = torch.tensor([ard_model.intercept_]).float()
rotation = torch.tensor(dim_reduction.components_.T).float()
center = torch.tensor(0)
PC linear model#
[8]:
base_model = pya.models.PCLinearModel(input_dim=len(model.features), pc_dim=rotation.shape[1])
base_model.center.data = center.float()
base_model.rotation.data = rotation.float()
base_model.linear.weight.data = weights.float()
base_model.linear.bias.data = intercept.float()
model.base_model = base_model
Load reference values#
[9]:
model.reference_values = None
Load preprocess and postprocess objects#
[10]:
model.preprocess_name = None
model.preprocess_dependencies = None
[11]:
model.postprocess_name = None
model.postprocess_dependencies = None
Check all clock parameters#
[12]:
pya.utils.print_model_details(model)
%==================================== Model Details ====================================%
Model Attributes:
training: True
metadata: {'approved_by_author': '✅',
'citation': 'de Lima Camillo, Lucas Paulo, et al. "Histone mark age of human '
'tissues and cells." bioRxiv (2023): 2023-08.',
'clock_name': 'camilloh3k27me3',
'data_type': 'histone mark',
'doi': 'https://doi.org/10.1101/2023.08.21.554165',
'notes': None,
'research_only': None,
'species': 'Homo sapiens',
'version': None,
'year': 2023}
reference_values: None
preprocess_name: None
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['ENSG00000230368', 'ENSG00000234711', 'ENSG00000283040', 'ENSG00000272438', 'ENSG00000230699', 'ENSG00000223764', 'ENSG00000187583', 'ENSG00000187642', 'ENSG00000272512', 'ENSG00000188290', 'ENSG00000187608', 'ENSG00000188157', 'LRG_198', 'ENSG00000217801', 'ENSG00000273443', 'ENSG00000237330', 'ENSG00000223823', 'ENSG00000207730', 'ENSG00000207607', 'ENSG00000198976', 'ENSG00000272141', 'ENSG00000162571', 'ENSG00000186891', 'ENSG00000186827', 'LRG_1319', 'ENSG00000184163', 'ENSG00000162572', 'ENSG00000162576', 'ENSG00000235098', 'ENSG00000225905']... [Total elements: 922]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: PCLinearModel(
(linear): Linear(in_features=290, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.center: tensor(0.)
base_model.rotation: [0.01818726770579815, 0.020942794159054756, -0.016425790265202522, -0.029995765537023544, 0.016731852665543556, -0.018333079293370247, 0.01603410392999649, -0.0038833124563097954, -0.010611171834170818, -0.04601435735821724, 0.015754137188196182, -0.0006687829736620188, 0.004315546713769436, 0.008097657933831215, -0.02072293870151043, 0.0412323921918869, -0.0015471483347937465, 0.017783096060156822, 0.03581244871020317, 0.011579958721995354, -0.0041507575660943985, 0.011536828242242336, 0.03996821492910385, -0.024992352351546288, 0.014561289921402931, -0.021883564069867134, 0.019289379939436913, 0.010160439647734165, -0.006656781304627657, 0.006293896585702896]... [Tensor of shape torch.Size([922, 290])]
base_model.linear.weight: [0.2194862961769104, -2.786182403564453, -0.44779086112976074, 2.2882213592529297, -0.6238448023796082, -2.2932820320129395, -1.911285638809204, 0.0, 2.6874823570251465, 1.843084692955017, 1.2139489650726318, -3.44547700881958, 0.6745961904525757, -1.6836811304092407, -1.1462115049362183, 1.99685800075531, 0.6163367033004761, 0.5939045548439026, 1.0635151863098145, 0.0, 0.07966688275337219, -0.5339570641517639, 2.3979337215423584, -0.17878352105617523, 1.148158073425293, -1.0248123407363892, -0.9859074950218201, -0.2077658772468567, 1.6150438785552979, -0.5271372199058533]... [Tensor of shape torch.Size([1, 290])]
base_model.linear.bias: tensor([41.9358])
%==================================== Model Details ====================================%
Basic test#
[13]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float)
model.eval()
model.to(float)
pred = model(input)
pred
[13]:
tensor([[ 39.9888],
[ 30.3743],
[101.3877],
[ 41.8950],
[ 24.2335],
[ 51.2079],
[ 49.4518],
[ 53.4217],
[ 38.0464],
[ 33.4102]], dtype=torch.float64, grad_fn=<AddmmBackward0>)
Save torch model#
[14]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")
Clear directory#
[15]:
# 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 folder: HistoneClocks