GliaSin#
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
Instantiate model class#
[2]:
def print_entire_class(cls):
source = inspect.getsource(cls)
print(source)
print_entire_class(pya.models.GliaSin)
class GliaSin(LinearReferenceClock):
pass
[3]:
model = pya.models.GliaSin()
Define clock metadata#
[4]:
model.metadata["clock_name"] = 'gliasin'
model.metadata["data_type"] = 'methylation'
model.metadata["species"] = 'Homo sapiens'
model.metadata["year"] = 2024
model.metadata["approved_by_author"] = '⌛'
model.metadata["citation"] = "Tong, Huige, et al. \"Cell-type-specific and cell-type-independent DNA methylation clocks.\" Aging 16 (2024)."
model.metadata["doi"] = "https://doi.org/10.18632/aging.206184"
model.metadata["research_only"] = None
model.metadata["notes"] = "Glia-specific DNA-methylation age clock for human brain (prefrontal cortex), built by estimating glial cell-type fractions, identifying glia-specific age-associated CpGs via cell-type deconvolution, and fitting an elastic-net model on cell-type-unadjusted (\"semi-intrinsic\") methylation values; it shows pronounced epigenetic age acceleration in Alzheimer's disease."
model.metadata["tissue"] = 'brain prefrontal cortex (bulk)'
model.metadata["predicts"] = 'chronological age (glia-specific)'
model.metadata["unit"] = 'years'
model.metadata["model_type"] = 'Elastic net'
model.metadata["platform"] = 'Illumina 450K'
model.metadata["population"] = 'adults (18-97 years)'
model.metadata["journal"] = 'Aging'
model.metadata["last_author"] = 'Andrew E. Teschendorff'
model.metadata["n_features"] = 220
model.metadata["citations"] = 25
model.metadata["citations_date"] = '2026-07-05'
Download clock dependencies#
[5]:
supplementary_url = "https://raw.githubusercontent.com/Duzhaozhen/OmniAge/c10fbe8cb92957520fbff1d55ae1def0691252e5/OmniAgePy/src/omniage/data/CTS/Glia-Sin.csv"
supplementary_file_name = "coefficients.csv"
os.system(f"curl -sL -o {supplementary_file_name} {supplementary_url}")
[5]:
0
Load features#
[6]:
df = pd.read_csv('coefficients.csv')
if str(df.columns[0]).startswith('Unnamed'):
df = df.iloc[:, 1:]
mask = df['probe'].astype(str).str.lower().isin(['intercept', '(intercept)'])
intercept_value = float(df.loc[mask, 'coef'].iloc[0]) if mask.any() else 0.0
coef_df = df.loc[~mask].reset_index(drop=True)
model.features = coef_df['probe'].tolist()
Load weights into base model#
[7]:
weights = torch.tensor(coef_df['coef'].tolist()).unsqueeze(0).float()
intercept = torch.tensor([intercept_value]).float()
[8]:
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#
[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': 'Tong, Huige, et al. "Cell-type-specific and '
'cell-type-independent DNA methylation clocks." Aging 16 (2024).',
'clock_name': 'gliasin',
'data_type': 'methylation',
'doi': 'https://doi.org/10.18632/aging.206184',
'notes': None,
'research_only': None,
'species': 'Homo sapiens',
'version': None,
'year': 2024}
reference_values: None
preprocess_name: None
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['cg10501210', 'cg13782884', 'cg14419975', 'cg16909962', 'cg16361302', 'cg21122225', 'cg23606718', 'cg04187185', 'cg18336674', 'cg02578944', 'cg04190475', 'cg11744777', 'cg14848772', 'cg16695576', 'cg18468088', 'cg20223728', 'cg01217984', 'cg12001630', 'cg26830108', 'cg05066959', 'cg15627457', 'cg13327545', 'cg14334161', 'cg17485681', 'cg01560972', 'cg08715791', 'cg26942121', 'cg10378521', 'cg10906284', 'cg24894584']... [Total elements: 220]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=220, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [-6.912019729614258, 19.981544494628906, 2.626999855041504, 13.658905029296875, 1.2430390119552612, 1.00424325466156, 12.733586311340332, 2.4791877269744873, -0.152785524725914, 1.1130852699279785, -2.2575085163116455, 11.418535232543945, 1.824793815612793, -1.495959758758545, 2.821787118911743, -7.900047302246094, 5.732859134674072, 2.7955639362335205, 1.5466865301132202, 0.026381725445389748, 2.8932950496673584, 5.647821426391602, 12.655052185058594, 0.6008855700492859, 0.8880432844161987, 3.119521141052246, -6.521735191345215, -2.816964626312256, 2.2651326656341553, 8.25953197479248]... [Tensor of shape torch.Size([1, 220])]
base_model.linear.bias: tensor([0.9233])
%==================================== 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([[ -20.1317],
[ -18.5452],
[ -99.1609],
[ -98.8837],
[ 9.2466],
[-205.0112],
[-147.6273],
[ 118.3328],
[-114.6954],
[-142.6733]], 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 file: coefficients.csv