-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
245 lines (195 loc) · 5.9 KB
/
demo.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
#import dependancies
import pickle
import pandas as pd
from rdkit import Chem
import torch
from os import path
import matplotlib.pyplot as plt
#import pkasolver
import pkasolver
from pkasolver.query import QueryModel
from pkasolver.ml_architecture import GINPairV1
from pkasolver.query import draw_pka_map
from pkasolver.query import calculate_microstate_pka_values, draw_pka_reactions
from IPython.display import display
from IPython.display import HTML
#load trained model
base_path=path.dirname(pkasolver.__file__)
###Functions
def RunPkasolver(x): #takes input of smile
mol=Chem.MolFromSmiles(x)
protonation_states = calculate_microstate_pka_values(mol) #performs internal calculations and stores as object
sites=len(protonation_states) #get the number of ionization sites
lst=[]
depSmi=[]
proSmi=[]
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))
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
def GetMonoPlot(pka_list,minph,maxph,step,proSmi,depSmi):
pka=pka_list[0]
x=[]
a0=[]
a1=[]
ph=minph
while ph<=maxph:
ph+=step
x.append(ph)
a0.append(round((1/(1+10**(ph-pka))),pka_dec))
a1.append(1-(round((1/(1+10**(ph-pka))),pka_dec))) # or round((10**(ph-pka))/(1+10**(ph-pka)),pka_dec)
plt.plot(x,a0,color='red',label='a0')
plt.plot(x,a1, color='green',label='a1')
plt.legend()
#make a list of smiles in order
smiles=[pro[0]]+dep
print("GetMonoPlot smiles: {}".format(smiles))
#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)))
print("GetMonoPlot microspecies: {}".format(microspecies))
print("GetMonoPlot a0: {}".format(a0))
print("GetMonoPlot a1: {}".format(a1))
return plt,microspecies
def GetDiPlot(pka_list,minph,maxph,step,proSmi,depSmi):
pka1=pka_list[0]
pka2=pka_list[1]
x=[]
a0=[]
a1=[]
a2=[]
ph=minph
while ph<=maxph:
ph+=step
x.append(ph)
ka1=10**(ph-pka1)
ka2=10**(ph-pka2)
E=((1+ka1)+(ka1*ka2))
a0.append(round(((1**2)/E),pka_dec))
a1.append(round(((1*ka1)/E),pka_dec))
a2.append(round(((ka1*ka2)/E),pka_dec))
plt.plot(x,a0,color='red',label='a0')
plt.plot(x,a1, color='green',label='a1')
plt.plot(x,a2,color='blue',label='a2')
plt.legend()
#make a list of smiles in order
smiles=[pro[0]]+dep
#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)))
return plt,microspecies
def GetTriPlot(pka_list,minph,maxph,step,proSmi,depSmi):
pka1=pka_list[0]
pka2=pka_list[1]
pka3=pka_list[2]
x=[]
a0=[]
a1=[]
a2=[]
a3=[]
ph=minph
while ph<=maxph:
ph+=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),pka_dec))
a1.append(round(((1*ka1)/D),pka_dec))
a2.append(round(((ka1*ka2)/D),pka_dec))
a3.append(round(((ka1*ka2*ka3)/D),pka_dec))
plt.plot(x,a0,color='red',label='a0')
plt.plot(x,a1, color='green',label='a1')
plt.plot(x,a2,color='blue',label='a2')
plt.plot(x,a3,color='yellow',label='a3')
#make a list of smiles in order
smiles=[pro[0]]+dep
#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)))
return plt,microspecies
def GetMultiPlot(pka_sites,pka_list,minph,maxph,step,proSmi,depSmi):
df=pd.DataFrame()
x=[]
a0=[]
ax=[]
ph=minph
while ph<=maxph:
ph+=step
x.append(ph)
count=0
D=1
numTerms=[]
for i in range(0,(pka_sites-1)):
n1=10**(ph-pka_lst[i])
n2=10**(ph-pka_lst[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_lst[i+2])
count+=1
#calculate ionization fraction for each numTerm
a0.append(round(((1**2)/D),pka_dec))
a=[]
for t in numTerms:
a.append(round((t/D),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)
#make a list of smiles in order
smiles=[pro[0]]+dep
#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)))
#plot
for i in data.columns[2:]:
x=data['pH']
y=data[i]
plt.plot(x,y,lable=i)
plt.legend()
return plt, microspecies
### DEMO
## user input
pka_dec= 2
step=0.2
minph=int(0)
maxph=int(14)
# parent='CC(=O)OC1=CC=CC=C1C(O)=O'
parent = 'OC(=O)CC(O)(CC(O)=O)C(O)=O'
#calculating pka for input chemical
data=RunPkasolver(parent)
#function return 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 in data:
pkaSites=n
pka_list=p
pro=ps
dep=ds
print("pkaSites: {}\npka_list: {}\nprotonated sites: {}\ndeprotonated: {}".format(pkaSites, pka_list, pro, dep))
#determine which set of equations to use based on number of pka sites
if pkaSites == 1:
#user monoprotic function
graph,species=GetMonoPlot(p,minph,maxph,step,pro,dep)
elif pkaSites == 2:
#use diprotic funcion
graph,species=GetDiPlot(p,minph,maxph,step,pro,dep)
elif pkaSites == 3:
#use triprotic function
graph,species=GetTriPlot(p,minph,maxph,step,pro,dep)
elif pkaSites > 3:
#use generalized function
graph,species=GetMultiPlot(n,p,minph,maxph,step,pro,dep)