-
Notifications
You must be signed in to change notification settings - Fork 9
/
evaluators.py
319 lines (262 loc) · 9.68 KB
/
evaluators.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
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
import os
import warnings
from abc import ABC, abstractmethod
import numpy as np
import useful_rdkit_utils as uru
try:
from openeye import oechem
from openeye import oeomega
from openeye import oeshape
from openeye import oedocking
import joblib
except ImportError:
# Since openeye is a commercial software package, just pass with a warning if not available
warnings.warn(f"Openeye packages not available in this environment; do not attempt to use ROCSEvaluator or "
f"FredEvaluator")
from rdkit import Chem, DataStructs
import pandas as pd
from sqlitedict import SqliteDict
class Evaluator(ABC):
@abstractmethod
def evaluate(self, mol):
pass
@property
@abstractmethod
def counter(self):
pass
class MWEvaluator(Evaluator):
"""A simple evaluation class that calculates molecular weight, this was just a development tool
"""
def __init__(self):
self.num_evaluations = 0
@property
def counter(self):
return self.num_evaluations
def evaluate(self, mol):
self.num_evaluations += 1
return uru.MolWt(mol)
class FPEvaluator(Evaluator):
"""An evaluator class that calculates a fingerprint Tanimoto to a reference molecule
"""
def __init__(self, input_dict):
self.ref_smiles = input_dict["query_smiles"]
self.ref_fp = uru.smi2morgan_fp(self.ref_smiles)
self.num_evaluations = 0
@property
def counter(self):
return self.num_evaluations
def evaluate(self, rd_mol_in):
self.num_evaluations += 1
rd_mol_fp = uru.mol2morgan_fp(rd_mol_in)
return DataStructs.TanimotoSimilarity(self.ref_fp, rd_mol_fp)
class ROCSEvaluator(Evaluator):
"""An evaluator class that calculates a ROCS score to a reference molecule
"""
def __init__(self, input_dict):
ref_filename = input_dict['query_molfile']
ref_fs = oechem.oemolistream(ref_filename)
self.ref_mol = oechem.OEMol()
oechem.OEReadMolecule(ref_fs, self.ref_mol)
self.max_confs = 50
self.score_cache = {}
self.num_evaluations = 0
@property
def counter(self):
return self.num_evaluations
def set_max_confs(self, max_confs):
"""Set the maximum number of conformers generated by Omega
:param max_confs:
"""
self.max_confs = max_confs
def evaluate(self, rd_mol_in):
"""Generate conformers with Omega and evaluate the ROCS overlay of conformers to a reference molecule
:param rd_mol_in: Input RDKit molecule
:return: ROCS Tanimoto Combo score, returns -1 if conformer generation fails
"""
self.num_evaluations += 1
smi = Chem.MolToSmiles(rd_mol_in)
# Look up to see if we already processed this molecule
arc_tc = self.score_cache.get(smi)
if arc_tc is not None:
tc = arc_tc
else:
fit_mol = oechem.OEMol()
oechem.OEParseSmiles(fit_mol, smi)
ret_code = generate_confs(fit_mol, self.max_confs)
if ret_code:
tc = self.overlay(fit_mol)
else:
tc = -1.0
self.score_cache[smi] = tc
return tc
def overlay(self, fit_mol):
"""Use ROCS to overlay two molecules
:param fit_mol: OEMolecule
:return: Combo Tanimoto for the overlay
"""
prep = oeshape.OEOverlapPrep()
prep.Prep(self.ref_mol)
overlay = oeshape.OEMultiRefOverlay()
overlay.SetupRef(self.ref_mol)
prep.Prep(fit_mol)
score = oeshape.OEBestOverlayScore()
overlay.BestOverlay(score, fit_mol, oeshape.OEHighestTanimoto())
return score.GetTanimotoCombo()
class LookupEvaluator(Evaluator):
"""A simple evaluation class that looks up values from a file.
This is primarily used for testing.
"""
def __init__(self, input_dictionary):
self.num_evaluations = 0
ref_filename = input_dictionary['ref_filename']
ref_df = pd.read_csv(ref_filename)
self.ref_dict = dict([(a, b) for a, b in ref_df[['SMILES', 'val']].values])
@property
def counter(self):
return self.num_evaluations
def evaluate(self, mol):
self.num_evaluations += 1
smi = Chem.MolToSmiles(mol)
return self.ref_dict[smi]
class DBEvaluator(Evaluator):
"""A simple evaluator class that looks up values from a database.
This is primarily used for benchmarking
"""
def __init__(self, input_dictionary):
self.num_evaluations = 0
self.db_prefix = input_dictionary['db_prefix']
db_filename = input_dictionary['db_filename']
self.ref_dict = SqliteDict(db_filename)
def __repr__(self):
return "DBEvalutor"
@property
def counter(self):
return self.num_evaluations
def evaluate(self, smiles):
self.num_evaluations += 1
res = self.ref_dict.get(f"{self.db_prefix}{smiles}")
if res is None:
return np.nan
else:
if res == -500:
return np.nan
return res
class FredEvaluator(Evaluator):
"""An evaluator class that docks a molecule with the OEDocking Toolkit and returns the score
"""
def __init__(self, input_dict):
du_file = input_dict["design_unit_file"]
if not os.path.isfile(du_file):
raise FileNotFoundError(f"{du_file} was not found or is a directory")
self.dock = read_design_unit(du_file)
self.num_evaluations = 0
self.max_confs = 50
@property
def counter(self):
return self.num_evaluations
def set_max_confs(self, max_confs):
"""Set the maximum number of conformers generated by Omega
:param max_confs:
"""
self.max_confs = max_confs
def evaluate(self, mol):
self.num_evaluations += 1
smi = Chem.MolToSmiles(mol)
mc_mol = oechem.OEMol()
oechem.OEParseSmiles(mc_mol, smi)
confs_ok = generate_confs(mc_mol, self.max_confs)
score = 1000.0
docked_mol = oechem.OEGraphMol()
if confs_ok:
ret_code = self.dock.DockMultiConformerMolecule(docked_mol, mc_mol)
else:
ret_code = oedocking.OEDockingReturnCode_ConformerGenError
if ret_code == oedocking.OEDockingReturnCode_Success:
dock_opts = oedocking.OEDockOptions()
sd_tag = oedocking.OEDockMethodGetName(dock_opts.GetScoreMethod())
# this is a stupid hack, I need to figure out how to do this correctly
oedocking.OESetSDScore(docked_mol, self.dock, sd_tag)
score = float(oechem.OEGetSDData(docked_mol, sd_tag))
return score
def generate_confs(mol, max_confs):
"""Generate conformers with Omega
:param max_confs: maximum number of conformers to generate
:param mol: input OEMolecule
:return: Boolean Omega return code indicating success of conformer generation
"""
rms = 0.5
strict_stereo = False
omega = oeomega.OEOmega()
omega.SetRMSThreshold(rms) # Word to the wise: skipping this step can lead to significantly different charges!
omega.SetStrictStereo(strict_stereo)
omega.SetMaxConfs(max_confs)
error_level = oechem.OEThrow.GetLevel()
# Turn off OEChem warnings
oechem.OEThrow.SetLevel(oechem.OEErrorLevel_Error)
status = omega(mol)
# Turn OEChem warnings back on
oechem.OEThrow.SetLevel(error_level)
return status
def read_design_unit(filename):
"""Read an OpenEye design unit
:param filename: design unit filename (.oedu)
:return: a docking grid
"""
du = oechem.OEDesignUnit()
rfs = oechem.oeifstream()
if not rfs.open(filename):
oechem.OEThrow.Fatal("Unable to open %s for reading" % filename)
du = oechem.OEDesignUnit()
if not oechem.OEReadDesignUnit(rfs, du):
oechem.OEThrow.Fatal("Failed to read design unit")
if not du.HasReceptor():
oechem.OEThrow.Fatal("Design unit %s does not contain a receptor" % du.GetTitle())
dock_opts = oedocking.OEDockOptions()
dock = oedocking.OEDock(dock_opts)
dock.Initialize(du)
return dock
def test_fred_eval():
"""Test function for the Fred docking Evaluator
:return: None
"""
input_dict = {"design_unit_file": "data/2zdt_receptor.oedu"}
fred_eval = FredEvaluator(input_dict)
smi = "CCSc1ncc2c(=O)n(-c3c(C)nc4ccccn34)c(-c3[nH]nc(C)c3F)nc2n1"
mol = Chem.MolFromSmiles(smi)
score = fred_eval.evaluate(mol)
print(score)
def test_rocs_eval():
"""Test function for the ROCS evaluator
:return: None
"""
input_dict = {"query_molfile": "data/2chw_lig.sdf"}
rocs_eval = ROCSEvaluator(input_dict)
smi = "CCSc1ncc2c(=O)n(-c3c(C)nc4ccccn34)c(-c3[nH]nc(C)c3F)nc2n1"
mol = Chem.MolFromSmiles(smi)
combo_score = rocs_eval.evaluate(mol)
print(combo_score)
class MLClassifierEvaluator(Evaluator):
"""An evaluator class the calculates a score based on a trained ML model
"""
def __init__(self, input_dict):
self.cls = joblib.load(input_dict["model_filename"])
self.num_evaluations = 0
@property
def counter(self):
return self.num_evaluations
def evaluate(self, mol):
self.num_evaluations += 1
fp = uru.mol2morgan_fp(mol)
return self.cls.predict_proba([fp])[:,1][0]
def test_ml_classifier_eval():
"""Test function for the ML Classifier Evaluator
:return: None
"""
input_dict = {"model_filename": "mapk1_modl.pkl"}
ml_cls_eval = MLClassifierEvaluator(input_dict)
smi = "CCSc1ncc2c(=O)n(-c3c(C)nc4ccccn34)c(-c3[nH]nc(C)c3F)nc2n1"
mol = Chem.MolFromSmiles(smi)
score = ml_cls_eval.evaluate(mol)
print(score)
if __name__ == "__main__":
test_rocs_eval()