-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshape_similarity.py
158 lines (129 loc) · 5.34 KB
/
shape_similarity.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
#!/usr/bin/env python
import argparse
import openeye
import time
from openeye.oechem import OESetSDData, OEGetSDDataPairs, OEGraphMol, OESuppressHydrogens, OEAddExplicitHydrogens, \
OEWriteMolecule, OEReadMolecule
# from openeye.oeomega import *
from openeye.oeshape import OEColorForceField, OEColorFFType_ExplicitMillsDean, OEColorOptions, OEOverlapPrep, \
OEOverlapResults, OERemoveColorAtoms, OEExactColorFunc, OEOverlapFunc
def parse_args():
"""Define and parse the arguments to the script."""
parser = argparse.ArgumentParser(
description='Execute Line of sight contact scripts.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter # To display default values in help message.
)
parser.add_argument(
'--template_ligand',
help='Path to template_ligand.',
default=''
)
parser.add_argument(
'--docked_ligand',
help='Path to docking_ligand.',
)
return parser.parse_args()
class Rescorer(object):
def __init__(self):
cff = OEColorForceField()
cff.Clear()
# cff.Init(OEColorFFType_ImplicitMillsDean)
cff.Init(OEColorFFType_ExplicitMillsDean)
self.prep = OEOverlapPrep()
self.prep.SetColorForceField(cff)
self.prep.SetUseHydrogens(True)
options = OEColorOptions()
options.SetColorForceField(cff)
self.func = OEOverlapFunc(OEExactColorFunc(options))
def is_similar(self, m, _diverse_confs):
"""
Make sure that conformation is sufficiently different from those already saved
:param m:
:param _diverse_confs:
:return:
"""
res = OEOverlapResults()
conf_is_similar = False
for diverse_conf in _diverse_confs:
self.func.SetupRef(diverse_conf)
self.func.Overlap(m, res)
print('TanimotCombo', res.GetTanimotoCombo())
if res.GetTanimotoCombo() > 1.9:
conf_is_similar = True
return conf_is_similar
def rescore_and_write(self, templates, dmols, nmax, ofs):
"""
molecules are rescored with ROCS to make sure the non-matched parts overlap as well as possible
:param templates:
:param dmols:
:param nmax:
:param ofs:
:return:
"""
# colorFunc = OEAnalyticColorFunc()
t1 = time.time()
print('start template scoring')
res = OEOverlapResults()
res_val = []
for i, dmol in enumerate(dmols):
dmol.SweepConfs()
self.prep.Prep(dmol)
confs = list(dmol.GetConfs())
for t in templates:
# --> templates already prepared with "prep.Prep(t) "
self.func.SetupRef(t)
for k, conf in enumerate(confs):
self.func.Overlap(conf, res)
res_val.append([i, t.GetTitle(), k, res.GetTanimotoCombo(), conf])
# print("SHAPE %f COLOR %f COMBO %f" %(res.GetTanimoto(), res.GetColorTanimoto(), res.GetTanimotoCombo())) # REMOVE
res_val = sorted(res_val, key=lambda x: x[3], reverse=True)
t2 = time.time()
print(f'finish: time {t2 - t1}')
diverse_confs = []
for j, lx in enumerate(res_val):
if j >= nmax:
break
if (lx[0], lx[2]) in diverse_confs:
continue
else:
diverse_confs.append((lx[0], lx[2]))
tmpmol = OEGraphMol(lx[4])
# re-generate hydrogen positions as they can be distorted in rare occasions
OESuppressHydrogens(tmpmol)
OEAddExplicitHydrogens(tmpmol)
# --> add all SD-tags from first molecule (are all the same anyway)
for dp in OEGetSDDataPairs(dmols[0]):
OESetSDData(tmpmol, dp.GetTag(), dp.GetValue())
OESetSDData(tmpmol, "TanimotoCombo", str(lx[3]))
OESetSDData(tmpmol, "Query", str(lx[1]))
OESetSDData(tmpmol, "Alignment_Type", "MCS")
# --> remove added pseudo atoms from ROCS calc.
OERemoveColorAtoms(tmpmol)
OEWriteMolecule(ofs, tmpmol)
return True
def shape_similarity(template_ligand_path, docked_ligand_path):
# shape similarity to template
oe_native_ligand_stream = openeye.oechem.oemolistream(template_ligand_path)
oe_native_ligand = openeye.oechem.OEGraphMol()
openeye.oechem.OEReadMolecule(oe_native_ligand_stream, oe_native_ligand)
oe_docked_ligand_stream = openeye.oechem.oemolistream(docked_ligand_path)
max_tanimoto_combo = 0
for oe_docked_ligand in oe_docked_ligand_stream.GetOEMols():
# oe_docked_ligand = openeye.oechem.OEGraphMol()
# openeye.oechem.OEReadMolecule(oe_docked_ligand_stream, oe_docked_ligand)
rescorer = Rescorer()
rescorer.prep.Prep(oe_native_ligand)
rescorer.func.SetupRef(oe_native_ligand)
res = openeye.oeshape.OEOverlapResults()
rescorer.prep.Prep(oe_docked_ligand)
rescorer.func.Overlap(oe_docked_ligand, res)
TanimotoCombo = res.GetTanimotoCombo()
if TanimotoCombo > max_tanimoto_combo:
max_tanimoto_combo = TanimotoCombo
print(max_tanimoto_combo)
return max_tanimoto_combo
def main():
args = parse_args()
shape_similarity(args.template_ligand, args.docked_ligand)
if __name__ == '__main__':
main()