-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpka_calculator.py
157 lines (124 loc) · 5.39 KB
/
pka_calculator.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
import os
import shutil
from rdkit import Chem
verbose = 0
dry_run = 0
class pKaCalculator:
def __init__(self, molecule, charge, spin):
self.molecule = molecule
self.charge = charge
self.spin = spin
def single_point(self, xyz_dir, foldername, charge, spin):
if not os.path.exists(foldername):
os.makedirs(foldername)
shutil.copyfile(
xyz_dir + "/" + self.molecule + ".xyz", foldername + self.molecule + ".xyz"
)
os.chdir(foldername)
if dry_run != 1:
# os.system("echo $OMP_NUM_THREADS")
os.system(
f"xtb {self.molecule}.xyz --gfn2 --chrg {charge} --uhf {spin} --alpb water > {self.molecule}.out 2>> {self.molecule}.out"
)
mol_file = [f for f in os.listdir(".") if f.endswith(".mol")][-1]
start_mol = Chem.MolFromMolFile(
mol_file, sanitize=False, removeHs=False, strictParsing=False
)
start_smiles = Chem.MolToSmiles(start_mol)
os.chdir("../../")
return start_smiles
def optimization(self, xyz_dir, foldername, charge, spin):
if not os.path.exists(foldername):
os.makedirs(foldername)
shutil.copyfile(
xyz_dir + "/" + self.molecule + ".xyz", foldername + self.molecule + ".xyz"
)
os.chdir(foldername)
if dry_run != 1:
os.system(
f"xtb {self.molecule}.xyz --gfn2 --chrg {charge} --uhf {spin} --alpb water --ohess > {self.molecule}.out 2>> {self.molecule}.out"
)
mol_file = [f for f in os.listdir(".") if f.endswith(".mol")][-1]
end_mol = Chem.MolFromMolFile(
mol_file, sanitize=False, removeHs=False, strictParsing=False
)
end_smiles = Chem.MolToSmiles(end_mol)
for line in open(self.molecule + ".out", "r"):
if "TOTAL FREE ENERGY" in line:
energy = float(line.split()[-3])
os.chdir("../../")
return end_smiles, energy
def deprotonate(self, xyz_dir, foldername, charge, spin):
foldername = "deprotonate" + "/" + self.molecule + "/"
if not os.path.exists("deprotonate/xyz_files"):
os.makedirs("deprotonate/xyz_files")
if not os.path.exists(foldername):
os.makedirs(foldername)
shutil.copyfile(
xyz_dir + "/" + self.molecule + ".xyz", foldername + self.molecule + ".xyz"
)
os.chdir(foldername)
if dry_run != 1:
os.system(
f"xtb {self.molecule}.xyz --gfn2 --chrg {charge} --uhf {spin} --alpb water --ohess > {self.molecule}_opt.out 2>> {self.molecule}_opt.out"
)
os.system(
f"crest xtbopt.xyz --gfn2 --chrg {charge} --uhf {spin} --alpb water -deprotonate > {self.molecule}_deprot.out 2>> {self.molecule}_deprot.out"
)
shutil.copyfile("deprotonated.xyz", "../xyz_files/" + self.molecule + ".xyz")
os.chdir("../../")
def compare_smiles(self, start_smiles, end_smiles, mol_type):
with open("warnings.out", "a") as out:
if verbose == 1:
out.write(
f"\nMolecule: {self.molecule}\n {mol_type} Start SMILES: {start_smiles}\nEnd SMILES: {end_smiles}\n"
)
if start_smiles != end_smiles:
out.write(
f"WARNING! Topology for molecule {self.molecule} {mol_type} has changed!\n"
)
if "." in end_smiles:
out.write(
f"WARNING!!! Molecule {self.molecule} {mol_type} has undergone dissociation!!!\n"
)
def calculate_pka(self):
# radical cation starting single point
start_smiles = self.single_point(
"./xyz_files",
"xtb_sp/" + self.molecule + "/",
self.charge + 1,
self.spin + 1,
)
# radical cation optimization
end_smiles, protonated_energy = self.optimization(
"./xyz_files",
"xtb_opt/" + self.molecule + "/",
self.charge + 1,
self.spin + 1,
)
self.compare_smiles(start_smiles, end_smiles, "radical cation")
# optimizing and deprotonating neutral molecule
self.deprotonate(
"./xyz_files", "deprotonate/" + self.molecule + "/", self.charge, self.spin
)
# neutral radical single point
start_smiles = self.single_point(
"./deprotonate/xyz_files",
"xtb_sp_deprot/" + self.molecule + "/",
self.charge,
self.spin + 1,
)
# neutral radical optimization
end_smiles, deprotonated_energy = self.optimization(
"./deprotonate/xyz_files",
"xtb_opt_deprot/" + self.molecule + "/",
self.charge,
self.spin + 1,
)
self.compare_smiles(start_smiles, end_smiles, "neutral radical")
pka_correction = 164.22 # kcal/mol
pka = -(
(protonated_energy - deprotonated_energy) * 627.5 + 270.29 - pka_correction
) / (2.303 * 1.98720425864083 / 1000 * 298.15)
# print(f'pKa for molecule {self.molecule} radical cation = {pka}')
return self.molecule, pka