forked from chemprop/chemprop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.py
executable file
·280 lines (220 loc) · 9.55 KB
/
interpret.py
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
import math
import os
from typing import Callable, Dict, List, Set, Tuple
import numpy as np
from rdkit import Chem
import torch
from tap import Tap # pip install typed-argument-parser (https://github.com/swansonk14/typed-argument-parser)
from chemprop.data import MoleculeDataLoader, MoleculeDataset
from chemprop.data.utils import get_data_from_smiles, get_header, get_smiles
from chemprop.train import predict
from chemprop.utils import load_args, load_checkpoint, load_scalers
MIN_ATOMS = 15
C_PUCT = 10
class Args(Tap):
data_path: str
checkpoint_dir: str
rollout: int = 20
c_puct: float = 10.0
max_atoms: int = 20
min_atoms: int = 8
prop_delta: float = 0.5
property_id: int = 1
no_cuda: bool = False
gpu: int = 0
@property
def device(self) -> torch.device:
if not self.cuda:
return torch.device('cpu')
return torch.device('cuda', self.gpu)
@property
def cuda(self) -> bool:
return not self.no_cuda and torch.cuda.is_available()
class ChempropModel:
def __init__(self, checkpoint_dir: str, device: torch.device) -> None:
self.checkpoints = []
for root, _, files in os.walk(checkpoint_dir):
for fname in files:
if fname.endswith('.pt'):
fname = os.path.join(root, fname)
self.scaler, self.features_scaler = load_scalers(fname)
self.train_args = load_args(fname)
model = load_checkpoint(fname, device=device)
self.checkpoints.append(model)
def __call__(self, smiles: List[str], batch_size: int = 500) -> List[List[float]]:
test_data = get_data_from_smiles(smiles=smiles, skip_invalid_smiles=False, features_generator=self.train_args.features_generator)
valid_indices = [i for i in range(len(test_data)) if test_data[i].mol is not None]
test_data = MoleculeDataset([test_data[i] for i in valid_indices])
if self.train_args.features_scaling:
test_data.normalize_features(self.features_scaler)
test_data_loader = MoleculeDataLoader(dataset=test_data, batch_size=batch_size)
sum_preds = []
for model in self.checkpoints:
model_preds = predict(
model=model,
data_loader=test_data_loader,
scaler=self.scaler,
disable_progress_bar=True
)
sum_preds.append(np.array(model_preds))
# Ensemble predictions
sum_preds = sum(sum_preds)
avg_preds = sum_preds / len(self.checkpoints)
return avg_preds
class MCTSNode:
def __init__(self, smiles: str, atoms, W: float = 0, N: int = 0, P: float = 0) -> None:
self.smiles = smiles
self.atoms = set(atoms)
self.children = []
self.W = W
self.N = N
self.P = P
def Q(self) -> float:
return self.W / self.N if self.N > 0 else 0
def U(self, n: int) -> float:
return C_PUCT * self.P * math.sqrt(n) / (1 + self.N)
def find_clusters(mol: Chem.Mol) -> Tuple[List[Tuple[int, ...]], List[List[int]]]:
n_atoms = mol.GetNumAtoms()
if n_atoms == 1: # special case
return [(0,)], [[0]]
clusters = []
for bond in mol.GetBonds():
a1 = bond.GetBeginAtom().GetIdx()
a2 = bond.GetEndAtom().GetIdx()
if not bond.IsInRing():
clusters.append((a1, a2))
ssr = [tuple(x) for x in Chem.GetSymmSSSR(mol)]
clusters.extend(ssr)
atom_cls = [[] for _ in range(n_atoms)]
for i in range(len(clusters)):
for atom in clusters[i]:
atom_cls[atom].append(i)
return clusters, atom_cls
def __extract_subgraph(mol: Chem.Mol, selected_atoms: Set[int]) -> Tuple[Chem.Mol, List[int]]:
selected_atoms = set(selected_atoms)
roots = []
for idx in selected_atoms:
atom = mol.GetAtomWithIdx(idx)
bad_neis = [y for y in atom.GetNeighbors() if y.GetIdx() not in selected_atoms]
if len(bad_neis) > 0:
roots.append(idx)
new_mol = Chem.RWMol(mol)
for atom_idx in roots:
atom = new_mol.GetAtomWithIdx(atom_idx)
atom.SetAtomMapNum(1)
aroma_bonds = [bond for bond in atom.GetBonds() if bond.GetBondType() == Chem.rdchem.BondType.AROMATIC]
aroma_bonds = [bond for bond in aroma_bonds if
bond.GetBeginAtom().GetIdx() in selected_atoms and bond.GetEndAtom().GetIdx() in selected_atoms]
if len(aroma_bonds) == 0:
atom.SetIsAromatic(False)
remove_atoms = [atom.GetIdx() for atom in new_mol.GetAtoms() if atom.GetIdx() not in selected_atoms]
remove_atoms = sorted(remove_atoms, reverse=True)
for atom in remove_atoms:
new_mol.RemoveAtom(atom)
return new_mol.GetMol(), roots
def extract_subgraph(smiles: str, selected_atoms: Set[int]) -> Tuple[str, List[int]]:
# try with kekulization
mol = Chem.MolFromSmiles(smiles)
Chem.Kekulize(mol)
subgraph, roots = __extract_subgraph(mol, selected_atoms)
subgraph = Chem.MolToSmiles(subgraph, kekuleSmiles=True)
subgraph = Chem.MolFromSmiles(subgraph)
mol = Chem.MolFromSmiles(smiles) # de-kekulize
if subgraph is not None and mol.HasSubstructMatch(subgraph):
return Chem.MolToSmiles(subgraph), roots
# If fails, try without kekulization
subgraph, roots = __extract_subgraph(mol, selected_atoms)
subgraph = Chem.MolToSmiles(subgraph)
subgraph = Chem.MolFromSmiles(subgraph)
if subgraph is not None:
return Chem.MolToSmiles(subgraph), roots
else:
return None, None
def mcts_rollout(node: MCTSNode,
state_map: Dict[str, MCTSNode],
orig_smiles: str,
clusters: List[Set[int]],
atom_cls: List[Set[int]],
nei_cls: List[Set[int]],
scoring_function: Callable[[List[str]], List[float]]) -> float:
cur_atoms = node.atoms
if len(cur_atoms) <= MIN_ATOMS:
return node.P
# Expand if this node has never been visited
if len(node.children) == 0:
cur_cls = set([i for i, x in enumerate(clusters) if x <= cur_atoms])
for i in cur_cls:
leaf_atoms = [a for a in clusters[i] if len(atom_cls[a] & cur_cls) == 1]
if len(nei_cls[i] & cur_cls) == 1 or len(clusters[i]) == 2 and len(leaf_atoms) == 1:
new_atoms = cur_atoms - set(leaf_atoms)
new_smiles, _ = extract_subgraph(orig_smiles, new_atoms)
if new_smiles in state_map:
new_node = state_map[new_smiles] # merge identical states
else:
new_node = MCTSNode(new_smiles, new_atoms)
if new_smiles:
node.children.append(new_node)
state_map[node.smiles] = node
if len(node.children) == 0:
return node.P # cannot find leaves
scores = scoring_function([x.smiles for x in node.children])
for child, score in zip(node.children, scores):
child.P = score
sum_count = sum(c.N for c in node.children)
selected_node = max(node.children, key=lambda x: x.Q() + x.U(sum_count))
v = mcts_rollout(selected_node, state_map, orig_smiles, clusters, atom_cls, nei_cls, scoring_function)
selected_node.W += v
selected_node.N += 1
return v
def mcts(smiles: str,
scoring_function: Callable[[List[str]], List[float]],
n_rollout: int,
max_atoms: int,
prop_delta: float) -> List[MCTSNode]:
mol = Chem.MolFromSmiles(smiles)
if mol.GetNumAtoms() > 50:
n_rollout = 1
clusters, atom_cls = find_clusters(mol)
nei_cls = [0] * len(clusters)
for i, cls in enumerate(clusters):
nei_cls[i] = [nei for atom in cls for nei in atom_cls[atom]]
nei_cls[i] = set(nei_cls[i]) - {i}
clusters[i] = set(list(cls))
for a in range(len(atom_cls)):
atom_cls[a] = set(atom_cls[a])
root = MCTSNode(smiles, set(range(mol.GetNumAtoms())))
state_map = {smiles: root}
for _ in range(n_rollout):
mcts_rollout(root, state_map, smiles, clusters, atom_cls, nei_cls, scoring_function)
rationales = [node for _, node in state_map.items() if len(node.atoms) <= max_atoms and node.P >= prop_delta]
return rationales
if __name__ == "__main__":
args = Args().parse_args()
chemprop_model = ChempropModel(checkpoint_dir=args.checkpoint_dir, device=args.device)
def scoring_function(smiles: List[str]) -> List[float]:
return chemprop_model(smiles)[:, args.property_id - 1]
C_PUCT = args.c_puct
MIN_ATOMS = args.min_atoms
all_smiles = get_smiles(path=args.data_path)
header = get_header(path=args.data_path)
property_name = header[args.property_id] if len(header) > args.property_id else 'score'
print(f'smiles,{property_name},rationale,rationale_score')
for smiles in all_smiles:
score = scoring_function([smiles])[0]
if score > args.prop_delta:
rationales = mcts(
smiles=smiles,
scoring_function=scoring_function,
n_rollout=args.rollout,
max_atoms=args.max_atoms,
prop_delta=args.prop_delta
)
else:
rationales = []
if len(rationales) == 0:
print(f'{smiles},{score:.3f},,')
else:
min_size = min(len(x.atoms) for x in rationales)
min_rationales = [x for x in rationales if len(x.atoms) == min_size]
rats = sorted(min_rationales, key=lambda x: x.P, reverse=True)
print(f'{smiles},{score:.3f},{rats[0].smiles},{rats[0].P:.3f}')