Spaces:
Running on Zero
Running on Zero
File size: 14,353 Bytes
d686612 5d4afe2 d686612 5d4afe2 d686612 e00f001 012754b e00f001 012754b e00f001 012754b e00f001 012754b e00f001 d686612 5d4afe2 d686612 e00f001 d686612 e00f001 5d4afe2 012754b e00f001 012754b e00f001 012754b e00f001 012754b e00f001 012754b d686612 e00f001 012754b d686612 e00f001 012754b d686612 012754b b412b3c 012754b 4273e47 e00f001 4273e47 012754b e00f001 012754b e00f001 012754b d686612 012754b d686612 012754b d686612 012754b d686612 012754b d686612 e00f001 012754b d686612 012754b e00f001 012754b e00f001 012754b d686612 012754b d686612 e00f001 d686612 e00f001 d686612 012754b e00f001 012754b d686612 012754b d686612 012754b 5d4afe2 d686612 e00f001 012754b 5d4afe2 012754b d686612 e00f001 d686612 012754b d686612 012754b d686612 012754b d686612 e00f001 012754b 5d4afe2 d686612 012754b e00f001 012754b e00f001 012754b d686612 012754b 5d4afe2 d686612 012754b d686612 e00f001 012754b d686612 e00f001 012754b d686612 012754b d686612 e00f001 012754b ee07e36 d686612 ee07e36 5d4afe2 ee07e36 5d4afe2 ee07e36 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | import random
import re
from typing import Any
import numpy as np
import torch
# Suppress RDKit C++ stderr noise
try:
from rdkit import RDLogger
RDLogger.DisableLog('rdApp.*')
except Exception:
pass
# ─────────────────────────────────────────────────────────────────
# Atom & Bond Lookup Tables
# ─────────────────────────────────────────────────────────────────
ATOM_NUMBERS = {
'H': 1, 'C': 6, 'N': 7, 'O': 8, 'F': 9,
'P': 15, 'S': 16, 'Cl': 17, 'Br': 35, 'I': 53,
'Si': 14, 'B': 5, 'Se': 34, 'Na': 11, 'K': 19, 'Pt': 78,
}
ATOM_MASSES = {
'H': 1.008, 'C': 12.011, 'N': 14.007, 'O': 15.999, 'F': 18.998,
'P': 30.974, 'S': 32.06, 'Cl': 35.45, 'Br': 79.904, 'I': 126.90,
'Si': 28.085, 'B': 10.811, 'Se': 78.971, 'Na': 22.990, 'K': 39.098, 'Pt': 195.08,
}
ELECTRONEGATIVITY = {
'H': 2.20, 'C': 2.55, 'N': 3.04, 'O': 3.44, 'F': 3.98,
'P': 2.19, 'S': 2.58, 'Cl': 3.16, 'Br': 2.96, 'I': 2.66,
'Si': 1.90, 'B': 2.04, 'Se': 2.55, 'Na': 0.93, 'K': 0.82, 'Pt': 2.28,
}
VDW_RADIUS = {
'H': 1.20, 'C': 1.70, 'N': 1.55, 'O': 1.52, 'F': 1.47,
'P': 1.80, 'S': 1.80, 'Cl': 1.75, 'Br': 1.85, 'I': 1.98,
'Si': 2.10, 'B': 1.92, 'Se': 1.90, 'Na': 2.27, 'K': 2.75, 'Pt': 1.75,
}
HALOGENS = {'F', 'Cl', 'Br', 'I'}
HETEROATOMS = {'P', 'S', 'Se', 'B', 'Si', 'Pt'}
def smiles_to_graph(smiles: str) -> tuple[torch.Tensor, torch.Tensor, list[str]]:
"""
Converts a SMILES string into a rich molecular graph with 24-dimensional atom features x_i in R^24
and edge_index E in R^(2 x M).
Node Features x_i in R^24:
[0] Atomic Number / 100.0
[1] Atomic Mass / 200.0
[2] Degree / 6.0
[3] Formal Charge clamped [-2, +2] / 2.0
[4] Hybridization code (1=sp, 2=sp2, 3=sp3, 4=sp3d, 5=sp3d2, 0=other) / 5.0
[5] Is Aromatic (binary)
[6] Implicit Valence / 6.0
[7] Is In Ring (binary)
[8] Is Stereocenter / Chiral Flag (binary)
[9] Total Hydrogen Count / 4.0
[10-13] Element Group One-Hot: [C/N/O, Halogens, Heteroatoms, Other]
[14] Pauling Electronegativity / 4.0
[15] vdW Radius / 3.0
[16] Is Ring Size 3 (binary)
[17] Is Ring Size 4 (binary)
[18] Is Ring Size 5 (binary)
[19] Is Ring Size 6 (binary)
[20] Is Ring Size 7 (binary)
[21] Is Ring Size 8 (binary)
[22] Is Conjugated Atom (binary)
[23] Gasteiger Charge Proxy (normalized [-1, +1])
"""
def _atom_to_features(symbol, num, deg, chg, hyb, aromatic,
imp_val, in_ring, mass, chiral, h_count,
ring_sizes, conjugated, charge_proxy) -> list[float]:
group = [0.0, 0.0, 0.0, 0.0]
if symbol in {'C', 'N', 'O'}:
group[0] = 1.0
elif symbol in HALOGENS:
group[1] = 1.0
elif symbol in HETEROATOMS:
group[2] = 1.0
else:
group[3] = 1.0
en = ELECTRONEGATIVITY.get(symbol, 2.0) / 4.0
vdw = VDW_RADIUS.get(symbol, 1.7) / 3.0
r3 = 1.0 if 3 in ring_sizes else 0.0
r4 = 1.0 if 4 in ring_sizes else 0.0
r5 = 1.0 if 5 in ring_sizes else 0.0
r6 = 1.0 if 6 in ring_sizes else 0.0
r7 = 1.0 if 7 in ring_sizes else 0.0
r8 = 1.0 if 8 in ring_sizes else 0.0
return [
float(num) / 100.0, # [0]
float(mass) / 200.0, # [1]
min(float(deg), 6.0) / 6.0, # [2]
max(-2.0, min(2.0, float(chg))) / 2.0, # [3]
float(hyb) / 5.0, # [4]
float(aromatic), # [5]
min(float(imp_val), 6.0) / 6.0, # [6]
float(in_ring), # [7]
float(chiral), # [8]
min(float(h_count), 4.0) / 4.0, # [9]
*group, # [10-13]
en, # [14]
vdw, # [15]
r3, r4, r5, r6, r7, r8, # [16-21]
float(conjugated), # [22]
max(-1.0, min(1.0, float(charge_proxy))), # [23]
]
try:
from rdkit import Chem
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
Chem.SanitizeMol(mol)
atoms, atom_symbols = [], []
for atom in mol.GetAtoms():
symbol = atom.GetSymbol()
num = atom.GetAtomicNum()
deg = atom.GetDegree()
chg = atom.GetFormalCharge()
hyb_val = int(atom.GetHybridization())
hyb = {2: 1, 3: 2, 4: 3, 5: 4, 6: 5}.get(hyb_val, 0)
aromatic = 1.0 if atom.GetIsAromatic() else 0.0
try:
imp_val = float(atom.GetValence(Chem.ValenceType.IMPLICIT))
except Exception:
imp_val = float(atom.GetImplicitValence())
in_ring = 1.0 if atom.IsInRing() else 0.0
mass = float(atom.GetMass())
chiral = 1.0 if (atom.HasProp('_ChiralityPossible') or atom.GetChiralTag() != Chem.ChiralType.CHI_UNSPECIFIED) else 0.0
h_count = float(atom.GetTotalNumHs())
ring_sizes = [size for size in range(3, 9) if atom.IsInRingSize(size)]
conjugated = 1.0 if atom.GetIsAromatic() or any(b.GetIsConjugated() for b in atom.GetBonds()) else 0.0
charge_proxy = float(chg) + (0.1 if symbol in {'N', 'O'} else (-0.1 if symbol in {'C'} else 0.0))
feats = _atom_to_features(
symbol, num, deg, chg, hyb, aromatic,
imp_val, in_ring, mass, chiral, h_count,
ring_sizes, conjugated, charge_proxy
)
atoms.append(feats)
atom_symbols.append(symbol)
edges = []
for bond in mol.GetBonds():
i = bond.GetBeginAtomIdx()
j = bond.GetEndAtomIdx()
edges.extend([[i, j], [j, i]])
if not edges:
edges = [[0, 0]]
node_feats = torch.tensor(atoms, dtype=torch.float32)
edge_index = torch.tensor(edges, dtype=torch.long).t().contiguous()
return node_feats, edge_index, atom_symbols
except Exception:
pass
# ── Regex Fallback Parser ───────────────────────────────────
tokens = re.findall(r'Cl|Br|Si|Se|Pt|[A-Z][a-z]?|[a-z]|[\=\#\-\+\(\)]', smiles)
atoms, atom_symbols, edges = [], [], []
stack, prev_idx = [], None
for tok in tokens:
sym = tok.upper() if tok.isalpha() else tok
if sym in ATOM_NUMBERS or tok in ATOM_NUMBERS:
key = sym if sym in ATOM_NUMBERS else tok
num = ATOM_NUMBERS.get(key, 6)
mass = ATOM_MASSES.get(key, 12.0)
aromatic = 1.0 if tok.islower() else 0.0
idx = len(atoms)
feats = _atom_to_features(
key, num, deg=2 if aromatic else 1, chg=0, hyb=2 if aromatic else 3,
aromatic=aromatic, imp_val=0.0, in_ring=aromatic, mass=mass,
chiral=0.0, h_count=1.0, ring_sizes=[6] if aromatic else [],
conjugated=aromatic, charge_proxy=0.0
)
atoms.append(feats)
atom_symbols.append(key)
if prev_idx is not None:
edges.extend([[prev_idx, idx], [idx, prev_idx]])
prev_idx = idx
elif tok == '(':
if prev_idx is not None:
stack.append(prev_idx)
elif tok == ')':
if stack:
prev_idx = stack.pop()
if not atoms:
atoms = [_atom_to_features('C', 6, 1, 0, 3, 0, 0, 0, 12.011, 0, 1, [], 0, 0.0)]
atom_symbols = ['C']
edges = [[0, 0]]
if not edges:
edges = [[0, 0]]
node_feats = torch.tensor(atoms, dtype=torch.float32)
edge_index = torch.tensor(edges, dtype=torch.long).t().contiguous()
return node_feats, edge_index, atom_symbols
# ─────────────────────────────────────────────────────────────────
# Bemis-Murcko Scaffold Splitter
# ─────────────────────────────────────────────────────────────────
def get_bemis_murcko_scaffold(smiles: str) -> str:
try:
from rdkit import Chem
from rdkit.Chem.Scaffolds import MurckoScaffold
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
return MurckoScaffold.MurckoScaffoldSmiles(mol=mol, includeChirality=False)
except Exception:
pass
rings = re.findall(r'c1[a-z0-9\=\#\-]+1|C1[A-Za-z0-9\=\#\-]+1', smiles)
if rings:
return "-".join(sorted(rings))
c_count = smiles.upper().count('C')
return f"Framework_C{c_count}"
def bemis_murcko_scaffold_split(
dataset,
smiles_list: list[str],
frac_train: float = 0.8,
frac_val: float = 0.1,
frac_test: float = 0.1,
seed: int = 42,
) -> tuple[list[int], list[int], list[int]]:
scaffolds: dict[str, list[int]] = {}
for idx, smi in enumerate(smiles_list):
sc = get_bemis_murcko_scaffold(smi)
scaffolds.setdefault(sc, []).append(idx)
scaffold_sets = sorted(scaffolds.values(), key=len, reverse=True)
rng = random.Random(seed)
rng.shuffle(scaffold_sets)
total = len(dataset)
train_cut = int(frac_train * total)
val_cut = int((frac_train + frac_val) * total)
train_idx, val_idx, test_idx = [], [], []
for cluster in scaffold_sets:
if len(train_idx) + len(cluster) <= train_cut:
train_idx.extend(cluster)
elif len(train_idx) + len(val_idx) + len(cluster) <= val_cut:
val_idx.extend(cluster)
else:
test_idx.extend(cluster)
return train_idx, val_idx, test_idx
def random_split(
dataset,
frac_train: float = 0.8,
frac_val: float = 0.1,
frac_test: float = 0.1,
seed: int = 42,
) -> tuple[list[int], list[int], list[int]]:
indices = list(range(len(dataset)))
random.Random(seed).shuffle(indices)
n = len(indices)
train_cut = int(frac_train * n)
val_cut = int((frac_train + frac_val) * n)
return indices[:train_cut], indices[train_cut:val_cut], indices[val_cut:]
# ─────────────────────────────────────────────────────────────────
# XAI — Toxic Hotspot Highlighting
# ─────────────────────────────────────────────────────────────────
def highlight_toxic_subgraph(
smiles: str,
attention_scores: torch.Tensor,
top_k: int = 3,
) -> dict[str, Any]:
_node_feats, _edge_index, atom_symbols = smiles_to_graph(smiles)
num_nodes = len(atom_symbols)
if attention_scores is None or len(attention_scores) == 0:
scores = np.ones(num_nodes) / max(num_nodes, 1)
else:
scores = attention_scores.detach().cpu().numpy()
if len(scores) < num_nodes:
scores = np.pad(scores, (0, num_nodes - len(scores)), 'constant')
elif len(scores) > num_nodes:
scores = scores[:num_nodes]
max_s = np.max(scores) if np.max(scores) > 0 else 1.0
norm = scores / max_s
top_idxs = np.argsort(norm)[::-1][:min(top_k, num_nodes)].tolist()
hotspots = [
{
"atom_index": int(i),
"atom_symbol": atom_symbols[i],
"attention_score": round(float(norm[i]), 4),
"is_toxic_hotspot": True,
}
for i in top_idxs
]
return {
"smiles": smiles,
"total_atoms": num_nodes,
"atom_symbols": atom_symbols,
"attention_weights": [round(float(s), 4) for s in norm],
"top_toxic_hotspots": hotspots,
"plot_title": "GAT Layer Attention Distribution Map (Highlight = High Attention Weight)",
}
def calculate_tanimoto_applicability_domain(
query_smiles: str,
training_smiles_list: list[str]
) -> dict[str, Any]:
"""
Computes maximum Tanimoto similarity between query molecule and training dataset.
Flags applicability domain confidence:
- High Confidence: Max Tanimoto >= 0.70
- Moderate Confidence: 0.40 <= Max Tanimoto < 0.70
- Low Confidence (Out of Domain): Max Tanimoto < 0.40
"""
try:
from rdkit import Chem, DataStructs
from rdkit.Chem import RDKFingerprint
q_mol = Chem.MolFromSmiles(query_smiles)
if q_mol is None:
return {"max_tanimoto": 0.0, "applicability_domain": "Out-of-Domain (Invalid SMILES)"}
q_fp = RDKFingerprint(q_mol)
max_sim = 0.0
for tr_smi in training_smiles_list:
tr_mol = Chem.MolFromSmiles(tr_smi)
if tr_mol is not None:
tr_fp = RDKFingerprint(tr_mol)
sim = DataStructs.TanimotoSimilarity(q_fp, tr_fp)
max_sim = max(max_sim, sim)
max_sim = round(float(max_sim), 4)
if max_sim >= 0.70:
domain = "High Confidence (In-Domain)"
elif max_sim >= 0.40:
domain = "Moderate Confidence"
else:
domain = "Out-of-Domain (Novel Scaffold)"
return {"max_tanimoto": max_sim, "applicability_domain": domain}
except Exception:
return {"max_tanimoto": 0.50, "applicability_domain": "Unknown Domain"}
|