-
Notifications
You must be signed in to change notification settings - Fork 0
/
cts_pkasolver.py
396 lines (303 loc) · 9.36 KB
/
cts_pkasolver.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import logging
import pandas as pd
from rdkit import Chem
from os import path
import sys
import pkasolver
from pkasolver.query import calculate_microstate_pka_values
# Loads trained model:
base_path=path.dirname(pkasolver.__file__)
class CTSPkasolver:
"""
Function calls to pkasolver used by CTS.
"""
def __init__(self):
self.pka_dec= 2
self.step=0.2 # step size for charts
self.minph=0
self.maxph=14
def format_chart_data(self, chart_data):
"""
Formats cts-pkasolver chart_data into something the
speciation output page is expecting.
Reformats this:
{
"x": x,
"a0": a0,
"a1": a1,
"a2": a2,
"a3": a3
}
Into this:
{
"microspecies1": [[x[0], a0[0]], [x[1], a0[1]], ...],
"microspecies2": [[x[0], a1[0]], ...]
}
"""
results_obj = {}
# chart_data = results_obj.get("chart_data", {})
for key in chart_data:
if key != "x":
result = [[chart_data["x"][i], (100.0 * chart_data[key][i])] for i in range(len(chart_data[key]))]
new_key = "microspecies{}".format(int(key[1]) + 1)
results_obj[new_key] = result
return results_obj
def run_pka_solver(self, smiles):
"""
Generator function that returns pKa values and more.
"""
mol=Chem.MolFromSmiles(smiles) # creates mol object from smiles
protonation_states = calculate_microstate_pka_values(mol) # performs internal calculations and stores as object
sites=len(protonation_states) # gets the number of ionization sites
lst=[]
depSmi=[]
proSmi=[]
idx=[]
for j in range(len(protonation_states)):
state=protonation_states[j]
depSmi.append(Chem.MolToSmiles(state.deprotonated_mol))
proSmi.append(Chem.MolToSmiles(state.protonated_mol))
idx.append(state.reaction_center_idx)
lst.append(round(state.pka,2)) # get pka values for all sites for a given molecule store in a list
yield sites, lst, proSmi, depSmi, idx
def get_mono_plot(self, pka_list, proSmi, depSmi):
pka = pka_list[0]
x = []
a0 = []
a1 = []
ph = self.minph
while ph <= self.maxph:
ph += self.step
x.append(ph)
a0.append(round((1/(1+10**(ph-pka))),self.pka_dec))
a1.append(1-(round((1/(1+10**(ph-pka))),self.pka_dec))) # or round((10**(ph-pka))/(1+10**(ph-pka)),self.pka_dec)
chart_data = {
"x": x,
"a0": a0,
"a1": a1
}
# Makes a list of smiles in order
smiles=[proSmi[0]]+depSmi
# Makes a dictionary where the keys are the a_index (ex.0= a0, 1=a1, etc.) and values are the microspecies smiles strings
microspecies=dict(list(enumerate(smiles)))
return chart_data, microspecies
def get_di_plot(self, pka_list, proSmi, depSmi):
pka1 = pka_list[0]
pka2 = pka_list[1]
x = []
a0 = []
a1 = []
a2 = []
ph = self.minph
while ph <= self.maxph:
ph += self.step
x.append(ph)
ka1 = 10**(ph-pka1)
ka2 = 10**(ph-pka2)
E = ((1+ka1) + (ka1*ka2))
a0.append(round(((1**2)/E),self.pka_dec))
a1.append(round(((1*ka1)/E),self.pka_dec))
a2.append(round(((ka1*ka2)/E),self.pka_dec))
chart_data = {
"x": x,
"a0": a0,
"a1": a1,
"a2": a2
}
# Makes a list of smiles in order
smiles=[proSmi[0]] + depSmi
# Makes a dictionary where the keys are the a_index (ex.0= a0, 1=a1, etc.) and values are the microspecies smiles strings
microspecies=dict(list(enumerate(smiles)))
return chart_data, microspecies
def get_tri_plot(self, pka_list, proSmi, depSmi):
pka1 = pka_list[0]
pka2 = pka_list[1]
pka3 = pka_list[2]
x = []
a0 = []
a1 = []
a2 = []
a3 = []
ph = self.minph
while ph <= self.maxph:
ph += self.step
x.append(ph)
ka1 = 10**(ph-pka1)
ka2 = 10**(ph-pka2)
ka3 = 10**(ph-pka3)
D = ((1+ka1)+(ka1*ka2)+(ka1*ka2*ka3))
a0.append(round(((1**2)/D),self.pka_dec))
a1.append(round(((1*ka1)/D),self.pka_dec))
a2.append(round(((ka1*ka2)/D),self.pka_dec))
a3.append(round(((ka1*ka2*ka3)/D),self.pka_dec))
chart_data = {
"x": x,
"a0": a0,
"a1": a1,
"a2": a2,
"a3": a3
}
# Makes a list of smiles in order
smiles = [proSmi[0]] + depSmi
# Makes a dictionary where the keys are the a_index (ex.0= a0, 1=a1, etc.) and values are the microspecies smiles strings
microspecies=dict(list(enumerate(smiles)))
return chart_data, microspecies
def get_multi_plot_orig(self, pka_sites, pka_list, proSmi, depSmi):
df = pd.DataFrame()
x = []
a0 = []
ax = []
ph = self.minph
while ph <= self.maxph:
ph += self.step
x.append(ph)
count = 0
D = 1
numTerms=[]
for i in range(0,(pka_sites-1)):
n1 = 10**(ph-pka_list[i])
n2 = 10**(ph-pka_list[1+i])
#if there is only one term, return D+n1 (should not happend)
if pka_sites == 1:
D += n1
else:
while count < pka_sites:
#get the numerator term and save to list
N = n1
numTerms.append(N)
#caluclate denominator
D += n1
#calculate next term
nth = n1 *n2
#update values
n1 = nth
n2 = 10**(ph-pka_list[i+2])
count += 1
# Calculates ionization fraction for each numTerm
# a0.append(round(((1**2)/D), self.pka_dec))
a = []
for t in numTerms:
a.append(round((t/D), self.pka_dec))
ax.append(a)
df['pH'] = x
df['ax'] = ax
# df['a0'] = a0
# Separate out ionization fractions into their own columns (based on ka)
points = pd.DataFrame(df.ax.tolist()).add_prefix('a')
# Combine pka columns with rest of data
data = pd.concat([df,points],axis=1)
# Makes a list of smiles in order
smiles = [proSmi[0]]+depSmi
# Makes a dictionary where the keys are the a_index (ex.0= a0, 1=a1, etc.) and values are the microspecies smiles strings
microspecies = dict(list(enumerate(smiles)))
chart_data = {}
# Creates chart data for speciation workflow:
for i in data.columns[2:]:
chart_data[i] = data[i].tolist()
chart_data['x'] = data['pH'].tolist()
return chart_data, microspecies
def get_multi_plot(self, pka_sites,pka_list,proSmi,depSmi):
df=pd.DataFrame()
x=[]
a0=[]
ax=[]
ph=self.minph
while ph<=self.maxph:
ph+=self.step
x.append(ph)
count=0
D=1
numTerms=[]
for i in range(0,(pka_sites-1)):
n1=10**(ph-pka_list[i])
n2=10**(ph-pka_list[1+i])
#if there is only one term, return D+n1 (should not happend)
if pka_sites ==1:
D+=n1
else:
while count < pka_sites:
#get the numerator term and save to list
N=n1
numTerms.append(N)
#caluclate denominator
D+=n1
#calculate next term
nth=n1 *n2
#update values
n1=nth
n2=10**(ph-pka_list[i+2])
count+=1
#calculate ionization fraction for each numTerm
a0.append(round(((1**2)/D),self.pka_dec))
a=[]
for t in numTerms:
a.append(round((t/D),self.pka_dec))
ax.append(a)
df['pH']=x
df['ax']=ax
df['a0']=a0
#make list of column names
col_names=[]
for i in range(1,len(df.ax[0])+1):
name='a'+str(i)
col_names.append(name)
#separate out ionization fractions into their own columns (based on ka)
points=pd.DataFrame(df.ax.tolist())
#assign columns appropriate name
points.columns=col_names
#combine pka columns with rest of data
data=pd.concat([df,points],axis=1)
#make a list of smiles in order
smiles=[proSmi[0]]+depSmi
#make a dictionary where the keys are the a_index (ex.0= a0, 1=a1, etc.) and values are the microspecies smiles strings
microspecies=dict(list(enumerate(smiles)))
# Creates chart data for speciation workflow:
chart_data = {}
for i in data.columns[2:]:
chart_data[i] = data[i].tolist()
chart_data['x'] = data['pH'].tolist()
return chart_data, microspecies
def main(self, parent, data_type=None):
"""
Main function for returning pkas and/or microspecies chart data.
Examples: ['CC(O)=O','CC(C)C(N)C(O)=O','C(O)1=CC=C(N)C=C1','NC(CCS)C(O)=O','NC(CC1=CN=CN1)C(O)=O']
"""
# Calculates pka for input chemical:
data = self.run_pka_solver(parent)
# Returns number of pka sites(n), list of pka values(p),
# and lists of protonated (pro) and deprotonated (dep) microspecies smiles:
for n, p, ps, ds, i in data:
pkaSites = n # num of sites
pka_list = p # list of pka values
pro = ps # deprotonated_mol
dep = ds # protonated_mol
idx = i
# pka_dict = dict(zip(pka_list, idx)) # dict of atom index and pkas
pka_dict = dict(zip(idx, pka_list)) # dict of atom index and pkas
# logging.warning("SOLVER DICT: {}".format(pka_dict))
# Option to just return pKa list:
if data_type == "pka":
return pka_list
chart_data, species = None, None
if pkaSites == 1:
# print("Calling get_mono_plot.")
chart_data, species = self.get_mono_plot(p, pro, dep)
elif pkaSites == 2:
# print("Calling get_di_plot.")
chart_data, species = self.get_di_plot(p, pro, dep)
elif pkaSites == 3:
# print("Calling get_tri_plot.")
chart_data, species = self.get_tri_plot(p, pro, dep)
elif pkaSites > 3:
# print("Calling get_multi_plot.")
chart_data, species = self.get_multi_plot(n, p, pro, dep)
reformatted_chart_data = self.format_chart_data(chart_data)
return reformatted_chart_data, species, pka_list, pka_dict
if __name__ == "__main__":
# test_smiles = "CC(=O)OC1=CC=CC=C1C(O)=O"
test_smiles = "NC(CC1=CN=CN1)C(O)=O "
if len(sys.argv) > 1:
test_smiles = sys.argv[1]
cts_pkasolver = CTSPkasolver()
chart_data, species, pka_list = cts_pkasolver.main(test_smiles)
print("Chart Data: {}\nSpecies: {}\nPka List: {}".format(chart_data, species, pka_list))