-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotocol.py
382 lines (327 loc) · 12.4 KB
/
protocol.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
#!/usr/bin/env python
import math, os, shutil, itertools, stk, time, concurrent.futures, string, os.path
import subprocess as sp
import pandas as pd
import numpy as np
import argparse
from rdkit import Chem
from rdkit.Chem import rdmolfiles, rdmolops, SanitizeMol, rdDistGeom, AllChem
from time import sleep, time
from utils import *
import sqlite3
import sql
import constants
class polyscreen:
def __init__(self, Id, monomers, style, length, solvent_params, threads):
self.pwd = os.getcwd()
self.Id = Id
self.monomers = monomers
self.style = style
self.length = int(length)
self.solvent_params = solvent_params
self.threads = int(threads)
def getPolymerWithConformer(self):
"""
Using STK, the polymer is created from a combination of n monomer smiles strings.
At the moment, bromine groups are used as to indicate where the monomers link, but
boronic acid group may be more appropriate as the former means we cannot include
monomers that contain genuine bromo groups.
"""
bbs = []
# Making the building blocks and hence the polymer from the monomer smiles strings.
for m in self.monomers:
bbs.append(
stk.BuildingBlock(smiles=m, functional_groups=[stk.BromoFactory()])
)
polymer = stk.ConstructedMolecule(
topology_graph=stk.polymer.Linear(
building_blocks=bbs,
repeating_unit=self.style,
num_repeating_units=self.length,
),
)
repeat = stk.ConstructedMolecule(
topology_graph=stk.polymer.Linear(
building_blocks=bbs,
repeating_unit=self.style,
num_repeating_units=1,
),
)
# Replacing the unused terminal bromines with H
H = Chem.MolFromSmarts("[H]")
Br = Chem.MolFromSmarts("[Br]")
repeat = repeat.with_canonical_atom_ordering().to_rdkit_mol()
molObj = polymer.with_canonical_atom_ordering().to_rdkit_mol()
molObj = Chem.rdmolops.ReplaceSubstructs(molObj, Br, H, replaceAll=True)[0]
repeat = Chem.rdmolops.ReplaceSubstructs(repeat, Br, H, replaceAll=True)[0]
molObj = rdmolops.RemoveHs(molObj)
repeat = rdmolops.RemoveHs(repeat)
rdmolops.SanitizeMol(molObj)
rdmolops.SanitizeMol(repeat)
self.smiles = Chem.MolToSmiles(molObj)
self.repeat_smiles = Chem.MolToSmiles(repeat)
molObj = Chem.AddHs(molObj)
# Setting parameters for ETKDG for the conformer search.
params = AllChem.ETKDGv3()
params.useSmallRingTorsions = False
params.ignoreSmoothingFailures = True
params.randomSeed = -1
params.numThreads = self.threads
params.useRandomCoords = True
confNum = 500
cids = Chem.rdDistGeom.EmbedMultipleConfs(molObj, confNum, params=params)
# Minimizing the conformer set, and hence saving the geometry of the lowest energy conformer
res = AllChem.MMFFOptimizeMoleculeConfs(molObj, numThreads=self.threads)
mmffenergies = []
for r in res:
mmffenergies.append(r[1])
lowest = mmffenergies.index(min(mmffenergies))
Chem.rdmolfiles.MolToXYZFile(
molObj, "{}.xyz".format(str(self.Id)), confId=cids[lowest]
)
def runCalculations(self):
"""
In order for VIP/VEA calculations to run, we have to use xTB 5.6.4SE and as such, must also
use FakeTime as old versions of xTB appear to be timelocked. The properties of each calculation
search for a specific string in the output stream, and finding the output values relative to
the lookup string.
"""
sid = str(self.Id)
spawn_xtb = ["faketime", "2018-1-1 00:00:00", "xtb"]
xyzfile = f"{str(self.Id)}.xyz"
# xTB OPTIMIZATION
command = spawn_xtb + [xyzfile, "-opt", self.solvent_params]
output = run(command)
with open(f"opt.out", "w+") as f:
f.write(output)
lines = output.splitlines()
E_lines = []
solv = []
for idx, line in enumerate(lines):
if "total E" in line:
E_lines.append(line)
if "Etot ala COSMO" in line:
solv.append(line)
E_line = E_lines[-1].split()
solv_line = solv[-1].split()
if len(self.solvent_params) > 0:
E_xtb, E_solv = float(E_line[-1]), float(solv_line[-1])
else:
E_xtb, E_solv = float(E_line[-1]), 0
shutil.copy("xtbopt.xyz", f"{str(self.Id)}-opt.xyz")
xyzfile = f"{self.Id}-opt.xyz"
# xTB VIP CALCULATION
command = spawn_xtb + [xyzfile, "-vip", self.solvent_params]
output = run(command)
with open("vip.out", "w+") as f:
f.write(output)
vip = (
float(output[output.find("delta SCC IP") :].split()[4])
+ constants.ELECTRODE
)
# xTB VEA CALCULATION
command = spawn_xtb + [xyzfile, "-vea", self.solvent_params]
output = run(command)
with open("vea.out", "w+") as f:
f.write(output)
vea = (
float(output[output.find("delta SCC EA") :].split()[4])
+ constants.ELECTRODE
)
# xTB WAVEFUNCTION CALCULATION (xtb4stda)
command = ["xtb4stda", xyzfile, self.solvent_params]
output = run(command)
# xTB EXCITATIONS CALCULATIONS
command = ["stda", "-xtb", "-e", "8"]
output = run(command)
with open("stda-calc.out", "w+") as f:
f.write(output)
lines = output.splitlines()
for idx, line in enumerate(lines):
if "excitation energies, transition moments" in line:
ogap = float(lines[idx + 2].split()[1])
fL = lines[idx + 2].split()[3]
# Determining which model to use for DFT scaling
dielectric_threshold = 40.0
solvent = self.solvent_params.split()[-1]
tSolvents = np.array(constants.SOLVENTS).T.tolist()
dielectric = float(tSolvents[1][tSolvents[0].index(solvent)])
if dielectric >= dielectric_threshold:
DiHiLo = "High_epsilon"
else:
DiHiLo = "Low_epsilon"
unified = True
if unified:
model = ["IP/EA", "IP/EA"]
else:
model = ["IP", "EA"]
# Processing results
vip_dft = convert(vip, constants.DFT_FACTOR[model[0]][DiHiLo])
vea_dft = convert(vea, constants.DFT_FACTOR[model[1]][DiHiLo])
ogap_dft = convert(ogap, constants.DFT_FACTOR["Gap"][DiHiLo])
egap = vip - vea
egap_dft = vip_dft - vea_dft
results = [
quotes(self.repeat_smiles),
E_xtb,
E_solv,
vip,
vea,
egap,
ogap,
vip_dft,
vea_dft,
egap_dft,
ogap_dft,
fL,
]
removeJunk()
return results
def getProps(
Id, monomers, style, length, solvent_params, threads, name, database, tableName
):
"""
This function allows for the parallelisation of the screening protocol using the
screening class, defined above. Each polymer is given its own directory wherein the
ETKDG and xTB geometries are saved, as well as the logs of the xTB calculations.
"""
start = int(time.time())
sid = str(Id)
log(f"Starting polymer {sid}")
if not os.path.exists(sid):
os.mkdir(sid)
os.chdir(sid)
ps = polyscreen(Id, monomers, style, length, solvent_params, threads)
ps.getPolymerWithConformer()
results = ps.runCalculations()
os.chdir("..")
end = int(time.time())
duration = (end - start) / 60
results.append(duration)
connection = sql.newConnection(database)
sql.updateData(connection, tableName, results, Id)
del connection
log(f"Done polymer {sid}")
return results
def getCombinations(monomers, n, exhaustive):
if exhaustive:
flat_monomers = [i for lst in monomers for i in lst]
combinations = list(itertools.combinations_with_replacement(flat_monomers, n))
else:
combinations = list(itertools.product(*monomers))
log(str(np.array(combinations)))
print(str(np.array(combinations)))
return combinations
def runScreen(
name,
monomer_list_raw,
style,
length,
solvent,
parallel,
database,
exhaustive=False,
forceNew=False,
):
"""
The screening is initialised, using concurrent.futures from within this function. Presently,
the number of concurrent jobs can be changed with 'in_parallel', but the total cores and
OMP_NUM_THREADS must be changed accordingly. OMP_NUM_THREADS is used as that is how xTB
reads how many threads are to be used.
"""
# Check solvent validity
solvents = np.array(constants.SOLVENTS).T.tolist()[0]
if solvent != None:
if solvent not in solvents:
raise Exception(
"Invalid solvent choice. Valid solvents:",
[i for i in solvents],
)
else:
solvent_params = f"-gbsa {solvent}"
else:
solvent_params = ""
# Conversion of monomers to canonical smiles
converted = []
for mlist in monomer_list_raw:
canonical = []
for m in mlist:
molObj = Chem.MolFromSmiles(m)
links = molObj.GetSubstructMatches(Chem.MolFromSmarts("Br"))
if len(links) == 2:
canonical.append(Chem.MolToSmiles(molObj, canonical=True))
else:
log("Input should have exactly 2 link identifier groups.")
converted.append(canonical)
monomer_list = converted
# Calculation and database setup/re-setup
tableName = name
connection = sql.newConnection(database)
tableExists = sql.SQLExistenceCheck(connection, tableName)
if forceNew and tableExists:
log(
f"forceNew set to True, deleting previous table {tableName} and previous screening directory."
)
sql.dropTable(connection, tableName)
os.rmdir(name)
log(f"Writing to {database}")
log(f"Table name: {tableName}")
n = len(set([char for char in style]))
threads = int(os.environ["OMP_NUM_THREADS"])
args = []
if tableExists == False:
sql.newTable(connection, tableName, style)
# Generate all possible combinations of monomers from the provided list.
combinations = getCombinations(monomer_list, n, exhaustive)
ids = np.arange(0, len(combinations), 1)
# If partially populated table already exists, find empty records, read combinations and get IDs
else:
df = pd.read_sql_query(
f"select * from {tableName} where DurationMins = 0.0", connection
)
columns = list(df.columns)
monomercols = [x for x in columns if x in list(string.ascii_uppercase)]
monomers = []
for col in monomercols:
monomers.append(df[col].tolist())
combinations = np.array(monomers).T.tolist()
ids = df["id"].to_list()
if len(combinations) == 0:
log("Quitting. Nothing to do.")
exit()
# Combining the arguments for getProps into iterables to be cycled with concurrent.futures.
chunksize = int(math.ceil(len(combinations) / (parallel)))
for idx, combination in zip(ids, combinations):
l = [
idx,
combination,
style,
length,
solvent_params,
threads,
name,
database,
tableName,
]
if tableExists == False:
empty_data = []
for c in combination:
empty_data.append(quotes(c))
empty_data.insert(0, idx)
empty_data.append(
[length, "''"] + list(np.zeros(len(constants.DATACOLS) - 1))
)
sql.insertData(connection, flatten_list(empty_data), tableName, style)
args.append(l)
pwd = os.getcwd()
if not os.path.exists(name):
os.mkdir(name)
os.chdir(name)
# Launching the parallelised screening protocol
log("Beginning parallelisation")
with concurrent.futures.ProcessPoolExecutor(max_workers=parallel) as executor:
x = executor.map(getProps, *zip(*args), chunksize=chunksize)
lst = list(x)
transpose = list(map(list, zip(*lst)))
os.chdir(pwd)
log("Done.")