-
Notifications
You must be signed in to change notification settings - Fork 104
/
MacroModel.py
executable file
·331 lines (274 loc) · 11.6 KB
/
MacroModel.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
320
321
322
323
324
325
326
327
328
329
330
331
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 13:15:47 2015
@author: ke291
Contains all of the MacroModel specific code for input generation, calculation
execution and output interpretation. Called by PyDP4.py.
"""
import os
import shutil
import sys
import subprocess
import shutil
import time
import re
def SetupMacroModel(settings):
if settings.SCHRODINGER == '':
SchrodEnv = os.getenv('SCHRODINGER')
if SchrodEnv != None:
settings.SCHRODINGER = SchrodEnv
else:
if os.path.exists('/usr/local/shared/schrodinger/current'):
settings.SCHRODINGER = '/usr/local/shared/schrodinger/current'
else:
print('Could not find Schrodinger folder, please fill in PyDP4.py Settings with the path.')
MacroModelInputs = []
for f in settings.InputFiles:
if settings.Rot5Cycle is True:
if not os.path.exists(f + 'rot.sdf'):
import FiveConf
# Generate the flipped fivemembered ring,
# result is in '*rot.sdf' file
FiveConf.main(f + '.sdf', settings)
scriptdir = getScriptPath()
cwd = os.getcwd()
# Convert input structure to mae file
if os.name == 'nt':
convinp = '"' + settings.SCHRODINGER + '/utilities/sdconvert" -isd '
else:
convinp = settings.SCHRODINGER + '/utilities/sdconvert -isd '
if (f[-4:] == '.sdf'):
if not os.path.exists(f[:-4] + '.mae'):
outp = subprocess.check_output(convinp + f + ' -omae ' + f[:-4] +
'.mae', shell=True)
MacroModelInputs.append(f[:-4] + '.mae')
else:
if not os.path.exists(f + '.mae'):
outp = subprocess.check_output(convinp + f + '.sdf -omae ' + f +
'.mae', shell=True)
MacroModelInputs.append(f + '.mae')
# Copy default com file to directory
shutil.copyfile(settings.ScriptDir + '/default.com', cwd + '/' + f + '.com')
# Change input and output file names in com file
comf = open(f + '.com', 'r+')
com = comf.readlines()
com[0] = f + '.mae\n'
com[1] = f + '-out.mae\n'
# Change the molecular mechanics step count in the com file
cycles = (str(settings.MMstepcount)).rjust(6)
temp = list(com[7])
temp[7:13] = list(cycles)
com[7] = "".join(temp)
comf.truncate(0)
comf.seek(0)
comf.writelines(com)
# Change the forcefield in the com file
if (settings.ForceField).lower() == "opls":
temp = list(com[3])
temp[11:13] = list('14')
com[3] = "".join(temp)
comf.truncate(0)
comf.seek(0)
comf.writelines(com)
comf.close()
if settings.Rot5Cycle is True:
# Convert input structure to mae file
if os.name == 'nt':
convinp = '"' + settings.SCHRODINGER + '/utilities/sdconvert" -isd '
else:
convinp = settings.SCHRODINGER + '/utilities/sdconvert -isd '
outp = subprocess.check_output(convinp + f + 'rot.sdf -omae ' + f +
'rot.mae', shell=True)
MacroModelInputs.append(f + 'rot.mae')
# Copy default com file to directory
shutil.copyfile(settings.ScriptDir + '/default.com', cwd + '/' + f +
'rot.com')
# Change input and output file names in com file
comf = open(f + 'rot.com', 'r+')
com = comf.readlines()
com[0] = f + 'rot.mae\n'
com[1] = f + 'rot-out.mae\n'
# Change the molecular mechanics step count in the com file
cycles = (str(settings.MMstepcount)).rjust(6)
temp = list(com[7])
temp[7:13] = list(cycles)
com[7] = "".join(temp)
comf.truncate(0)
comf.seek(0)
comf.writelines(com)
comf.close()
print("Macromodel input for " + f + " prepared.")
return MacroModelInputs
def RunMacroModel(MacroModelInputs, settings):
# not args, but MacroModelInputs, numDS removed
# Run Macromodel conformation search for all diastereomeric inputs
MacroModelBaseNames = [x[:-4] for x in MacroModelInputs]
MacroModelOutputs = []
NCompleted = 0
SchrodingerNotInstalled = False
# Check if MacroModel is installed in the provided path, but if it's missing, complain,
# but don't quit quite yet, the conformation search data might already be there
if shutil.which(os.path.join(settings.SCHRODINGER, 'bmin')) is None:
print('MacroModel.py, RunMacroModel:\n Could not find MacroModel executable at ' +
os.path.join(settings.SCHRODINGER, 'bmin'))
SchrodingerNotInstalled = True
if os.name == 'nt':
MMPrefix = '"' + settings.SCHRODINGER + '\\bmin" '
else:
MMPrefix = settings.SCHRODINGER + "/bmin "
for isomer in MacroModelBaseNames:
if not os.path.exists(isomer + '.log'):
# if we need to run conformational search AND MacroModel is not installed,
# no choice but to quit
if SchrodingerNotInstalled is True:
quit()
print(MMPrefix + isomer)
outp = subprocess.check_output(MMPrefix + isomer, shell=True)
else:
if IsMMCompleted(isomer + '.log'):
print("Valid " + isomer + ".log exists, skipping...")
NCompleted = NCompleted + 1
MacroModelOutputs.append(isomer + '.log')
else:
print("Incomplete " + isomer + ".log exists, consider deleting it. Skipping...")
continue
time.sleep(60)
while (not IsMMCompleted(isomer + '.log')):
time.sleep(30)
NCompleted = NCompleted + 1
MacroModelOutputs.append(isomer + '.log')
print("Macromodel job " + str(NCompleted) + " of " + str(len(MacroModelBaseNames)) + " completed.")
return MacroModelOutputs
def ReadConformers(MacroModelOutputs, Isomers, settings):
MatchingOutput = ''
for iso in Isomers:
for outp in MacroModelOutputs:
if (outp[:-4] == iso.BaseName) and IsMMCompleted(outp):
print(outp + ' is matching conformational search output for ' + iso.BaseName)
atoms, conformers, charge, AbsEs = ReadMacromodel(iso.BaseName, settings)
iso.Atoms = atoms
iso.Conformers = conformers
iso.MMCharge = charge
iso.MMEnergies = AbsEs
return Isomers
def ReadMacromodel(MMoutp, settings):
conformers = []
conformer = -1
AbsEs = []
ConfAbsEs = []
atoms = []
charge = 0
MaeInps = []
MaeFile = open(MMoutp + '-out.mae', 'r')
MaeInp = MaeFile.readlines()
MaeFile.close()
AbsEOffsets = []
# find conformer description blocks
blocks = []
DataIndexes = []
for i in range(len(MaeInp)):
if 'f_m_ct' in MaeInp[i]:
blocks.append(i)
if 'p_m_ct' in MaeInp[i]:
blocks.append(i)
# find absolute energy offsets
for block in blocks:
for i in range(block, len(MaeInp)):
if 'mmod_Potential_Energy' in MaeInp[i]:
AbsEOffsets.append(i - block)
break
# Get absolute energies for conformers
for i in range(len(blocks)):
for line in range(blocks[i], len(MaeInp)):
if ':::' in MaeInp[line]:
AbsEs.append(float(MaeInp[line + AbsEOffsets[i]]))
break
# Pick only the conformers in the energy window
MinE = min(AbsEs)
# find geometry descriptions for each block
for i in range(len(blocks)):
for line in (MaeInp[blocks[i]:]):
if 'm_atom' in line:
blocks[i] = blocks[i] + MaeInp[blocks[i]:].index(line)
break
# find start of atom coordinates for each block
for i in range(len(blocks)):
if (AbsEs[i] < MinE + settings.MaxCutoffEnergy):
# Save the locations of atom number, xyz and charge
DataIndex = [0, 0, 0, 0, 0]
for offset, line in enumerate(MaeInp[blocks[i]:]):
if 'i_m_mmod_type' in line:
DataIndex[0] = offset - 1
if 'r_m_x_coord' in line:
DataIndex[1] = offset - 1
if 'r_m_y_coord' in line:
DataIndex[2] = offset - 1
if 'r_m_z_coord' in line:
DataIndex[3] = offset - 1
if 'r_m_charge1' in line:
DataIndex[4] = offset - 1
if ':::' in line:
blocks[i] = blocks[i] + MaeInp[blocks[i]:].index(line)
break
DataIndexes.append(DataIndex)
else:
break
# Read the atom numbers and coordinates
for i, block in enumerate(blocks):
if (AbsEs[i] < MinE + settings.MaxCutoffEnergy):
conformers.append([])
ConfAbsEs.append(AbsEs[i])
conformer = conformer + 1
index = block + 1
atom = 0
while not ':::' in MaeInp[index]:
# Replace quoted fields with x
line = (re.sub(r"\".*?\"", "x", MaeInp[index],
flags=re.DOTALL)).split(' ')
line = [word for word in line[:-1] if word != '']
conformers[conformer].append([])
if conformer == 0:
atoms.append(GetMacromodelSymbol(int(line[DataIndexes[i][0]])))
conformers[0][atom].append(line[DataIndexes[i][1]]) # add X
conformers[0][atom].append(line[DataIndexes[i][2]]) # add Y
conformers[0][atom].append(line[DataIndexes[i][3]]) # add Z
charge = charge + float(line[DataIndexes[i][4]])
else:
conformers[conformer][atom].append(line[DataIndexes[i][1]]) # add X
conformers[conformer][atom].append(line[DataIndexes[i][2]]) # add Y
conformers[conformer][atom].append(line[DataIndexes[i][3]]) # add Z
index = index + 1 # Move to next line
atom = atom + 1 # Move to next atom
else:
break
return atoms, conformers, int(charge), ConfAbsEs
def GetMacromodelSymbol(atomType):
Lookup = ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',
'C', 'C', 'C', 'O', 'O', 'O', ' ', 'O', ' ', 'O',
'O', ' ', 'O', 'N', 'N', 'N', ' ', ' ', ' ', ' ',
'N', 'N', ' ', ' ', ' ', ' ', ' ', 'N', 'N', 'N',
'H', 'H', 'H', 'H', 'H', ' ', ' ', 'H', 'S', ' ',
'S', 'S', 'P', 'B', 'B', 'F', 'Cl', 'Br', 'I', 'Si',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'S',
'S', 'Cl', 'B', 'F', ' ', ' ', ' ', ' ', 'S', 'S',
' ', ' ', 'S', 'S']
if Lookup[atomType - 1] == ' ':
print('Unknown atom type')
return Lookup[atomType - 1]
def getScriptPath():
return os.path.dirname(os.path.realpath(sys.argv[0]))
def IsMMCompleted(f):
Gfile = open(f, 'r')
outp = Gfile.readlines()
Gfile.close()
if os.name == 'nt':
i = -2
else:
i = -3
if "normal termination" in outp[i]:
return True
else:
return False