-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmcalc.py
382 lines (350 loc) · 12.5 KB
/
qmcalc.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""
Module for the QM calculation of the random PCMOLs.
"""
from __future__ import annotations
import os
import shutil
import subprocess
from .miscelleanous import bcolors, chdir, create_directory
def xtb_sp(name: str) -> str:
"""
Function to run xTB single point calculation.
Arguments:
name: CID of the molecule to be optimized
"""
error = ""
pgout = None
try:
pgout = subprocess.run(
["xtb", name],
check=True,
capture_output=True,
timeout=120,
)
with open("xtb.out", "w", encoding="UTF-8") as f:
f.write(pgout.stdout.decode("utf-8"))
with open("xtb.err", "w", encoding="UTF-8") as f:
f.write(pgout.stderr.decode("utf-8"))
except subprocess.TimeoutExpired as exc:
error = " " * 3 + f"Process timed out.\n{exc}"
print(error)
return error
except subprocess.CalledProcessError as exc:
print(" " * 3 + f"{bcolors.FAIL}Status : FAIL{bcolors.ENDC}", exc.returncode)
with open("xtb_error.out", "w", encoding="UTF-8") as f:
f.write(exc.output.decode("utf-8"))
error = f"{bcolors.WARNING}xTB optimization failed - skipping CID {name}.{bcolors.ENDC}"
print(error)
return error
return error
def xtb_opt(name: str) -> str:
"""
Function to run xTB optimization and convert the output to xyz format.
"""
error = ""
pgout = None
try:
pgout = subprocess.run(
["xtb", f"{name}.sdf", "--opt"],
check=True,
capture_output=True,
timeout=120,
)
with open("xtb.out", "w", encoding="UTF-8") as f:
f.write(pgout.stdout.decode("utf-8"))
with open("xtb.err", "w", encoding="UTF-8") as f:
f.write(pgout.stderr.decode("utf-8"))
except subprocess.TimeoutExpired as exc:
error = " " * 3 + f"Process timed out.\n{exc}"
print(error)
return error
except subprocess.CalledProcessError as exc:
print(" " * 3 + f"{bcolors.FAIL}Status : FAIL{bcolors.ENDC}", exc.returncode)
with open("xtb_error.out", "w", encoding="UTF-8") as f:
f.write(exc.output.decode("utf-8"))
error = f"{bcolors.WARNING}xTB optimization failed - skipping CID {name}.{bcolors.ENDC}"
print(error)
return error
# load fourth entry of a line with ":: total charge" of xtb.out into a variable
chrg = 0
with open("xtb.out", encoding="UTF-8") as f:
lines = f.readlines()
for line in lines:
if ":: total charge" in line:
chrg = round(float(line.split()[3]))
break
print(" " * 3 + f"Total charge: {chrg:6d}")
# write chrg to a file called .CHRG
with open(".CHRG", "w", encoding="UTF-8") as f:
f.write(str(chrg) + "\n")
try:
pgout = subprocess.run(
["mctc-convert", "xtbopt.sdf", "opt.xyz"],
check=True,
capture_output=True,
timeout=120,
)
except subprocess.TimeoutExpired as exc:
error = " " * 3 + f"Process timed out.\n{exc}"
print(error)
return error
except subprocess.CalledProcessError as exc:
print(" " * 3 + "Status : FAIL", exc.returncode, exc.output)
# write the error output to a file
with open("mctc-convert_error.err", "w", encoding="UTF-8") as f:
f.write(pgout.stderr.decode("utf-8"))
error = (
f"{bcolors.WARNING}mctc-convert failed - skipping CID {name}.{bcolors.ENDC}"
)
print(error)
return error
return error
def crest_sampling(
homedir: str, name: str, crestsettings: dict[str, int | float | str]
) -> dict[str, str | int | float | list[float]]:
"""
Function to run CREST sampling.
"""
error = ""
pgout = None
chdir(name)
direxist = create_directory("crest")
shutil.copy2(str(crestsettings["strucfile"]), "crest/")
# if exist, copy the .CHRG file to the crest directory
if os.path.exists(".CHRG"):
shutil.copy2(".CHRG", "crest/")
chdir("crest")
conformer_prop: dict[str, str | int | float | list[float]] = {}
# initialize conformer_prop with default values
conformer_prop["name"] = name
# obtain molecular charge from .CHRG
if os.path.exists(".CHRG"):
with open(".CHRG", encoding="UTF-8") as f:
lines = f.readlines()
conformer_prop["charge"] = int(lines[0].strip())
else:
conformer_prop["charge"] = 0
# obtain number of atoms from opt.xyz
with open(str(crestsettings["strucfile"]), encoding="UTF-8") as f:
lines = f.readlines()
conformer_prop["natoms"] = int(lines[0].strip())
conformer_prop["energies"] = []
conformer_prop["nconf"] = 0
error = ""
try:
pgout = subprocess.run(
[
"crest",
str(crestsettings["strucfile"]),
"--squick",
"--T",
str(crestsettings["nthreads"]),
"--mddump",
str(crestsettings["mddump"]),
"--mdlen",
str(crestsettings["mdlen"]),
],
check=True,
capture_output=True,
)
with open("crest.out", "w", encoding="UTF-8") as f:
f.write(pgout.stdout.decode("utf-8"))
with open("crest.err", "w", encoding="UTF-8") as f:
f.write(pgout.stderr.decode("utf-8"))
except subprocess.CalledProcessError as exc:
print(
f"{bcolors.FAIL}Status : FAIL for {name} with code {exc.returncode}{bcolors.ENDC}, ",
end="",
flush=True,
)
with open("crest_error.out", "w", encoding="UTF-8") as f:
f.write(exc.output.decode("utf-8"))
chdir(homedir)
return conformer_prop
# parse crest.out and get the number of conformers
# the relevant line is "number of unique conformers for further calc"
try:
with open("crest.out", encoding="UTF-8") as f:
lines = f.readlines()
for line in lines:
if "number of unique conformers for further calc" in line:
conformer_prop["nconf"] = int(line.split()[7])
break
except FileNotFoundError:
print(
f"{bcolors.FAIL}CREST conformer search failed - \
skipping CID {name}.{bcolors.ENDC}"
)
chdir(homedir)
return conformer_prop
try:
with open("crest.energies", encoding="UTF-8") as f:
lines = f.readlines()
if isinstance(conformer_prop["energies"], list):
for line in lines:
conformer_prop["energies"].append(float(line.split()[1]))
except FileNotFoundError:
print(
f"{bcolors.FAIL}CREST conformer search failed - \
skipping CID {name}.{bcolors.ENDC}"
)
conformer_prop["nconf"] = 0
chdir(homedir)
return conformer_prop
print(f"{name}, ", end="", flush=True)
chdir(homedir)
return conformer_prop
def crest_protonate(
homedir: str, name: str, crestsettings: dict[str, int | float | str]
) -> str:
"""
Function to run CREST sampling.
"""
error = ""
pgout = None
chdir(name)
crest_dir = "protonation"
direxist = create_directory(crest_dir)
shutil.copy2("opt.xyz", f"{crest_dir}/")
# if exist, copy the .CHRG file to the crest directory
init_charge = 0
if os.path.exists(".CHRG"):
shutil.copy2(".CHRG", f"{crest_dir}/")
with open(".CHRG", encoding="UTF-8") as f:
lines = f.readlines()
init_charge = int(lines[0].strip())
# obtain number of atoms from opt.xyz
nat = 0
with open("opt.xyz", encoding="UTF-8") as f:
lines = f.readlines()
nat = int(lines[0].strip())
chdir(crest_dir)
error = ""
try:
pgout = subprocess.run(
[
"crest",
"opt.xyz",
"--protonate",
"--T",
str(crestsettings["nthreads"]),
],
check=True,
capture_output=True,
)
with open("crest.out", "w", encoding="UTF-8") as f:
f.write(pgout.stdout.decode("utf-8"))
with open("crest.err", "w", encoding="UTF-8") as f:
f.write(pgout.stderr.decode("utf-8"))
except subprocess.CalledProcessError as exc:
print(
f"{bcolors.FAIL}Status : FAIL for {name} with code {exc.returncode}{bcolors.ENDC}, ",
end="",
flush=True,
)
with open("crest_error.out", "w", encoding="UTF-8") as f:
f.write(exc.output.decode("utf-8"))
chdir(homedir)
error = f"CREST protonation failed - skipping CID {name}."
return error
try:
with open("crest.out", encoding="UTF-8") as f:
lines = f.readlines()
for line in lines:
if "molecular fragmentation" in line:
error = (
f"CREST protonation failed - skipping CID {name}. "
+ "Molecule fragmented."
)
chdir(homedir)
return error
except FileNotFoundError:
error = (
f"CREST protonation failed - skipping CID {name}. "
+ "File 'crest.out' not found."
)
chdir(homedir)
return error
# read first nat+2 lines from protonated.xyz and write it to opt_proto.xyz
try:
with open("protonated.xyz", encoding="UTF-8") as f:
lines = f.readlines()
with open("opt_proto.xyz", "w", encoding="UTF-8") as g:
print(f"{nat+1}\n", file=g)
for i in range(2, nat + 3):
g.write(lines[i])
except FileNotFoundError:
error = (
f"CREST protonation failed - skipping CID {name}. "
+ "File 'protonated.xyz' not found."
)
chdir(homedir)
return error
# write new .CHRG file with initial charge + 1
with open(".CHRG", "w", encoding="UTF-8") as g:
g.write(str(init_charge + 1) + "\n")
shutil.copy2("opt_proto.xyz", f"{homedir}/{name}/")
shutil.copy2(".CHRG", f"{homedir}/{name}/")
chdir(homedir)
return error
def get_sdf(cid: str) -> str:
"""
Function to download a compound from PubChem in sdf format.
"""
error = ""
try:
pgout = subprocess.run(
["PubGrep", "--input", "cid", cid, "--output", "sdf"],
check=True,
capture_output=True,
timeout=30,
)
except subprocess.TimeoutExpired as exc:
print(f"Process timed out.\n{exc}")
if os.path.exists(f"{cid}.sdf"):
os.remove(f"{cid}.sdf")
error = f"ERROR - PubGrep timed out for CID {cid}."
return error
except subprocess.CalledProcessError as exc:
print("Status : FAIL", exc.returncode, exc.output)
if os.path.exists(f"{cid}.sdf"):
os.remove(f"{cid}.sdf")
error = f"ERROR - PubGrep failed for CID {cid}."
return error
# print the return code
if pgout.returncode != 0:
print("Return code:", pgout.returncode)
if (pgout.stderr.decode("utf-8") == "") or (
"normal termination" in pgout.stderr.decode("utf-8")
):
print(" " * 3 + f"Downloaded {cid} successfully.")
if not os.path.exists(f"{cid}.sdf"):
print(
" " * 3
+ f"{bcolors.WARNING}Warning: \
File {cid}.sdf not found even though it is allocated. Skipping...{bcolors.ENDC}"
)
error = f"ERROR - PubGrep failed for CID {cid}."
return error
else:
if "abnormal termination" in pgout.stderr.decode("utf-8"):
print(
" " * 3
+ f"""{bcolors.WARNING}xTB error in conversion process - \
skipping CID {cid}.{bcolors.ENDC}"""
)
error = f"ERROR - PubGrep xTB error in conversion process for CID {cid}."
return error
elif not "normal termination" in pgout.stderr.decode("utf-8"):
print(
" " * 3
+ f"{bcolors.WARNING}Unknown PubGrep/xTB conversion error - \
skipping CID {cid}.{bcolors.ENDC}"
)
error = f"Unknown PubGrep/xTB conversion error for CID {cid}."
else:
print(" " * 3 + f"Unknown PubGrep error for {cid:7d}.")
return error
if os.path.exists(f"{cid}.sdf"):
os.remove(f"{cid}.sdf")
return error