-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimizing.py
executable file
·188 lines (133 loc) · 5.08 KB
/
minimizing.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
'''
(c) Thomas Holder, Schrodinger Inc.
'''
from pymol import cmd, CmdException
def get_fixed_indices(selection, state, _self):
fixed_list = []
_self.iterate_state(state, selection,
'_append(flags & 0x8)',
space={'_append': fixed_list.append})
return [idx for (idx, fixed) in enumerate(fixed_list) if fixed]
def load_or_update(molstr, name, sele, state, _self):
update = not name
if update:
name = _self.get_unused_name('_minimized')
else:
_self.delete(name)
_self.load(molstr, name, 1, 'molstr', zoom=0)
try:
from psico.fitting import xfit
xfit(name, sele, 1, state, match='none', cycles=100, guide=0)
except Exception as e:
print('xfit failed, fallback to cmd.fit')
_self.fit(name, sele, 1, state, cycles=5, matchmaker=-1)
if update:
_self.update(sele, name, state, 1, matchmaker=0)
_self.delete(name)
def minimize_ob(selection='enabled', state=-1, ff='UFF', nsteps=500,
conv=0.0001, cutoff=0, cut_vdw=6.0, cut_elec=8.0,
name='', quiet=1, _self=cmd):
'''
DESCRIPTION
Emergy minimization with openbabel
Supports fixed atoms (flag fix)
ARGUMENTS
selection = str: atom selection
state = int: object state {default: -1}
ff = GAFF|MMFF94s|MMFF94|UFF|Ghemical: force field {default: UFF}
nsteps = int: number of steps {default: 500}
'''
import openbabel as ob
state = int(state)
sele = _self.get_unused_name('_sele')
_self.select(sele, selection, 0)
try:
ioformat = 'mol'
molstr = _self.get_str(ioformat, sele, state)
obconversion = ob.OBConversion()
obconversion.SetInAndOutFormats(ioformat, ioformat)
mol = ob.OBMol()
obconversion.ReadString(mol, molstr)
# add hydrogens
orig_ids = [a.GetId() for a in ob.OBMolAtomIter(mol)]
mol.AddHydrogens()
added_ids = set(a.GetId() for a in ob.OBMolAtomIter(mol)).difference(orig_ids)
consttrains = ob.OBFFConstraints()
consttrains.Setup(mol)
# atoms with "flag fix"
fixed_indices = get_fixed_indices(sele, state, _self)
for idx in fixed_indices:
consttrains.AddAtomConstraint(idx + 1)
# setup forcefield (one of: GAFF, MMFF94s, MMFF94, UFF, Ghemical)
ff = ob.OBForceField.FindForceField(ff)
ff.Setup(mol, consttrains)
if int(cutoff):
ff.EnableCutOff(True)
ff.SetVDWCutOff(float(cut_vdw))
ff.SetElectrostaticCutOff(float(cut_elec))
# run minimization
ff.SteepestDescent(int(nsteps) // 2, float(conv))
ff.ConjugateGradients(int(nsteps) // 2, float(conv))
ff.GetCoordinates(mol)
# remove previously added hydrogens
for hydro_id in added_ids:
mol.DeleteAtom(mol.GetAtomById(hydro_id))
molstr = obconversion.WriteString(mol)
load_or_update(molstr, name, sele, state, _self)
if not int(quiet):
print(' Energy: %8.2f %s' % (ff.Energy(), ff.GetUnit()))
finally:
_self.delete(sele)
def minimize_rdkit(selection='enabled', state=-1, ff='MMFF94', nsteps=200,
name='', quiet=1, _self=cmd):
'''
DESCRIPTION
Emergy minimization with RDKit
Supports fixed atoms (flag fix)
ARGUMENTS
selection = str: atom selection
state = int: object state {default: -1}
ff = MMFF94s|MMFF94|UFF: force field {default: MMFF94}
nsteps = int: number of steps {default: 200}
'''
from rdkit import Chem
from rdkit.Chem import AllChem
state = int(state)
sele = _self.get_unused_name('_sele')
_self.select(sele, selection, 0)
try:
molstr = _self.get_str('mol', sele, state)
mol = Chem.MolFromMolBlock(molstr, True, False)
if mol is None:
raise CmdException('Failed to load molecule into RDKit. '
'Please check bond orders and formal charges.')
# setup forcefield
if ff.startswith('MMFF'):
ff = AllChem.MMFFGetMoleculeForceField(mol,
AllChem.MMFFGetMoleculeProperties(mol, ff,
0 if int(quiet) else 1))
elif ff == 'UFF':
ff = AllChem.UFFGetMoleculeForceField(mol)
else:
raise CmdException('unknown forcefield: ' + ff)
if ff is None:
raise CmdException('forcefield setup failed')
# atoms with "flag fix"
for idx in get_fixed_indices(sele, state, _self):
ff.AddFixedPoint(idx)
# run minimization
if ff.Minimize(int(nsteps)) != 0:
print(" Warning: minimization did not converge")
molstr = Chem.MolToMolBlock(mol)
load_or_update(molstr, name, sele, state, _self)
if not int(quiet):
print(' Energy: %8.2f %s' % (ff.CalcEnergy(), 'kcal/mol'))
finally:
_self.delete(sele)
cmd.extend('minimize_ob', minimize_ob)
cmd.extend('minimize_rdkit', minimize_rdkit)
cmd.auto_arg[0].update([
('minimize_ob', cmd.auto_arg[0]['zoom']),
('minimize_rdkit', cmd.auto_arg[0]['zoom']),
])
# vi:expandtab:smarttab