-
Notifications
You must be signed in to change notification settings - Fork 3
/
correct_sdf.py
executable file
·179 lines (113 loc) · 3.84 KB
/
correct_sdf.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
#!/usr/bin/env python2
import sys
import copy
import pybel
import openbabel
CHAR2NUM = dict()
CHAR2NUM["S"] = 1
CHAR2NUM["D"] = 2
CHAR2NUM["T"] = 3
def get_bonds_nbo(filename):
f = open(filename, "r")
lines = f.readlines()
f.close()
start_line = -1
end_line = -1
for i, line in enumerate(lines):
if "$CHOOSE" in line:
start_line = i
if "$END" in line:
end_line = i
mode = "lone"
bonds = []
for line in lines[start_line+1:end_line]:
if mode == "end":
break
if mode == "bond":
tokens = line.split()
if tokens[0] == "BOND":
tokens = tokens[1:]
if tokens[-1] == "END":
tokens = tokens[:-1]
# print len(tokens), tokens
for n in range(len(tokens) // 3):
bonds.append(tokens[n*3:n*3+3])
if "END" in line:
mode = "end"
if mode == "lone":
if "END" in line:
mode = "bond"
charges = [0 for i in range(10000)]
start_lines = []
end_lines = []
for i, line in enumerate(lines):
if "------ Lewis --------------------------------------" in line:
start_lines.append(i)
if "------ non-Lewis ----------------------------------" in line:
end_lines.append(i)
# """
# 1234567890123456789012345
# 137. BD ( 1) C 1- C 2
# """
for i in range(len(start_lines)):
start_line = start_lines[i]
end_line = end_lines[i]
for line in lines[start_line+1:end_line]:
# print line
tokens = line.split()
if (len(tokens) < 2):
continue
elif tokens[1] == "CR":
a = int(tokens[5]) - 1
charges[a] += 2
elif tokens[1] == "LP":
a = int(tokens[5]) - 1
charges[a] += 2
elif tokens[1] == "BD":
# print "-" + line[16:19] + "-"
# a = int(tokens[5][:-1]) - 1
a = int(line[16:19]) - 1
charges[a] += 1
# b = int(tokens[7]) - 1
b = int(line[22:25]) - 1
charges[b] += 1
total_charge = None
for i, line in enumerate(lines):
if "* Total *" in line:
tokens = line.split()
total_charge = int(float(tokens[3]))
return bonds, charges, total_charge
def delete_bonds_from_mol(mol):
#Delete All bonds
for i in range(10000):
try:
bo = mol.OBMol.GetBondById(i).GetBO()
bond = mol.OBMol.GetBondById(i)
mol.OBMol.DeleteBond(bond)
except AttributeError:
break
return mol
def add_bonds_to_mol(mol, bonds):
for i, bond in enumerate(bonds):
mol.OBMol.AddBond(int(bond[1]), int(bond[2]), CHAR2NUM[bond[0]])
return mol
if __name__ == "__main__":
nbo_filename = sys.argv[1]
sdf_filename = sys.argv[2]
bonds, charges, total_charge = get_bonds_nbo(nbo_filename)
mol = pybel.readfile("sdf", sdf_filename).next()
mol = delete_bonds_from_mol(mol)
mol_corrected = add_bonds_to_mol(mol, bonds)
total_charge_check = 0
for i, atom in enumerate(mol):
nuc = atom.OBAtom.GetAtomicNum()
formal_charge = nuc - charges[i]
total_charge_check += formal_charge
atom.OBAtom.SetFormalCharge(formal_charge)
atom.OBAtom.SetSpinMultiplicity(0)
# print nuc, charges[i], atom.formalcharge
# print total_charge, total_charge_check
assert (total_charge == total_charge_check)
mol.OBMol.SetTotalCharge(total_charge)
outfilename = sdf_filename.rstrip(".sdf") + "_corrected.sdf"
mol.write("sdf", outfilename, overwrite=True)