forked from bdu91/group-contribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
molecule.py
310 lines (262 loc) · 9.74 KB
/
molecule.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
import openbabel
import types
import re
"""
This script is adapted from molecule.py in https://github.com/eladnoor/component-contribution.
"""
class OpenBabelError(Exception):
pass
class Molecule(object):
# for more rendering options visit:
# http://www.ggasoftware.com/opensource/indigo/api/options#rendering
_obElements = openbabel.OBElementTable()
_obSmarts = openbabel.OBSmartsPattern()
@staticmethod
def GetNumberOfElements():
return Molecule._obElements.GetNumberOfElements()
@staticmethod
def GetAllElements():
return [Molecule._obElements.GetSymbol(i) for i in
range(Molecule.GetNumberOfElements())]
@staticmethod
def GetSymbol(atomic_num):
return Molecule._obElements.GetSymbol(atomic_num)
@staticmethod
def GetAtomicNum(elem):
if type(elem) == types.UnicodeType:
elem = str(elem)
return Molecule._obElements.GetAtomicNum(elem)
@staticmethod
def VerifySmarts(smarts):
return Molecule._obSmarts.Init(smarts)
def __init__(self):
self.title = None
self.obmol = openbabel.OBMol()
self.smiles = None
self.inchi = None
def __str__(self):
return self.title or self.smiles or self.inchi or ""
def __len__(self):
return self.GetNumAtoms()
def Clone(self):
tmp = Molecule()
tmp.title = self.title
tmp.obmol = openbabel.OBMol(self.obmol)
tmp.smiles = self.smiles
tmp.inchi = self.inchi
return tmp
def SetTitle(self, title):
self.title = title
@staticmethod
def FromSmiles(smiles):
m = Molecule()
m.smiles = smiles
obConversion = openbabel.OBConversion()
obConversion.AddOption("w", obConversion.OUTOPTIONS)
obConversion.SetInFormat("smiles")
if not obConversion.ReadString(m.obmol, m.smiles):
raise OpenBabelError("Cannot read the SMILES string: " + smiles)
try:
m.UpdateInChI()
except OpenBabelError:
raise OpenBabelError("Failed to create Molecule from SMILES: " + smiles)
m.SetTitle(smiles)
return m
@staticmethod
def FromInChI(inchi):
m = Molecule()
m.inchi = inchi
obConversion = openbabel.OBConversion()
obConversion.AddOption("w", obConversion.OUTOPTIONS)
obConversion.SetInFormat("inchi")
obConversion.ReadString(m.obmol, m.inchi)
try:
m.UpdateSmiles()
except OpenBabelError:
raise OpenBabelError("Failed to create Molecule from InChI: " + inchi)
m.SetTitle(inchi)
return m
@staticmethod
def FromMol(mol):
m = Molecule()
obConversion = openbabel.OBConversion()
obConversion.AddOption("w", obConversion.OUTOPTIONS)
obConversion.SetInFormat("mol")
obConversion.ReadString(m.obmol, mol)
try:
m.UpdateInChI()
m.UpdateSmiles()
except OpenBabelError:
raise OpenBabelError("Failed to create Molecule from MOL file:\n" + mol)
m.SetTitle("")
return m
@staticmethod
def FromOBMol(obmol):
m = Molecule()
m.obmol = obmol
try:
m.UpdateInChI()
m.UpdateSmiles()
except OpenBabelError:
raise OpenBabelError("Failed to create Molecule from OBMol")
m.SetTitle("")
return m
@staticmethod
def _FromFormat(s, fmt='inchi'):
if fmt == 'smiles' or fmt == 'smi':
return Molecule.FromSmiles(s)
if fmt == 'inchi':
return Molecule.FromInChI(s)
if fmt == 'mol':
return Molecule.FromMol(s)
if fmt == 'obmol':
return Molecule.FromOBMol(s)
@staticmethod
def _ToFormat(obmol, fmt='inchi'):
obConversion = openbabel.OBConversion()
obConversion.AddOption("w", obConversion.OUTOPTIONS)
obConversion.SetOutFormat(fmt)
res = obConversion.WriteString(obmol)
if not res:
raise OpenBabelError("Cannot convert OBMol to %s" % fmt)
if fmt == 'smiles' or fmt == 'smi':
res = res.split()
if res == []:
raise OpenBabelError("Cannot convert OBMol to %s" % fmt)
else:
return res[0]
elif fmt == 'inchi':
return res.strip()
else:
return res
@staticmethod
def Smiles2InChI(smiles):
obConversion = openbabel.OBConversion()
obConversion.AddOption("w", obConversion.OUTOPTIONS)
obConversion.SetInAndOutFormats("smiles", "inchi")
obmol = openbabel.OBMol()
if not obConversion.ReadString(obmol, smiles):
raise OpenBabelError("Cannot read the SMILES string: " + smiles)
return obConversion.WriteString(obmol).strip()
@staticmethod
def InChI2Smiles(inchi):
obConversion = openbabel.OBConversion()
obConversion.AddOption("w", obConversion.OUTOPTIONS)
obConversion.SetInAndOutFormats("inchi", "smiles")
obmol = openbabel.OBMol()
if not obConversion.ReadString(obmol, inchi):
raise OpenBabelError("Cannot read the InChI string: " + inchi)
return obConversion.WriteString(obmol).split()[0]
def RemoveHydrogens(self):
self.obmol.DeleteHydrogens()
def RemoveAtoms(self, indices):
self.obmol.BeginModify()
for i in sorted(indices, reverse=True):
self.obmol.DeleteAtom(self.obmol.GetAtom(i+1))
self.obmol.EndModify()
self.smiles = None
self.inchi = None
def SetAtomicNum(self, index, new_atomic_num):
self.obmol.GetAtom(index+1).SetAtomicNum(new_atomic_num)
self.smiles = None
self.inchi = None
def ToOBMol(self):
return self.obmol
def ToFormat(self, fmt='inchi'):
return Molecule._ToFormat(self.obmol, fmt=fmt)
def ToMolfile(self):
return self.ToFormat('mol')
def UpdateInChI(self):
self.inchi = Molecule._ToFormat(self.obmol, 'inchi')
def ToInChI(self):
"""
Lazy storage of the InChI identifier (calculate once only when
asked for and store for later use).
"""
if not self.inchi:
self.UpdateInChI()
return self.inchi
def UpdateSmiles(self):
self.smiles = Molecule._ToFormat(self.obmol, 'smiles')
def ToSmiles(self):
"""
Lazy storage of the SMILES identifier (calculate once only when
asked for and store for later use).
"""
if not self.smiles:
self.UpdateSmiles()
return self.smiles
def GetFormula(self):
tokens = re.findall('InChI=1S?/([0-9A-Za-z\.]+)', self.ToInChI())
if len(tokens) == 1:
return tokens[0]
elif len(tokens) > 1:
raise ValueError('Bad InChI: ' + self.ToInChI())
else:
return ''
def GetExactMass(self):
return self.obmol.GetExactMass()
def GetAtomBagAndCharge(self):
inchi = self.ToInChI()
fixed_charge = 0
for s in re.findall('/q([0-9\+\-]+)', inchi):
fixed_charge += int(s)
fixed_protons = 0
for s in re.findall('/p([0-9\+\-]+)', inchi):
fixed_protons += int(s)
formula = self.GetFormula()
atom_bag = {}
for mol_formula_times in formula.split('.'):
for times, mol_formula in re.findall('^(\d+)?(\w+)', mol_formula_times):
if not times:
times = 1
else:
times = int(times)
for atom, count in re.findall("([A-Z][a-z]*)([0-9]*)", mol_formula):
if count == '':
count = 1
else:
count = int(count)
atom_bag[atom] = atom_bag.get(atom, 0) + count * times
if fixed_protons:
atom_bag['H'] = atom_bag.get('H', 0) + fixed_protons
fixed_charge += fixed_protons
return atom_bag, fixed_charge
def GetHydrogensAndCharge(self):
atom_bag, charge = self.GetAtomBagAndCharge()
return atom_bag.get('H', 0), charge
def GetNumElectrons(self):
"""Calculates the number of electrons in a given molecule."""
atom_bag, fixed_charge = self.GetAtomBagAndCharge()
n_protons = 0
for elem, count in atom_bag.iteritems():
n_protons += count * self._obElements.GetAtomicNum(elem)
return n_protons - fixed_charge
def GetNumAtoms(self):
return self.obmol.NumAtoms()
def GetAtoms(self):
return [self.obmol.GetAtom(i+1) for i in range(self.obmol.NumAtoms())]
def FindSmarts(self, smarts):
"""
Corrects the pyBel version of Smarts.findall() which returns results as tuples,
with 1-based indices even though Molecule.atoms is 0-based.
Args:
mol: the molecule to search in.
smarts_str: the SMARTS query to search for.
Returns:
The re-mapped list of SMARTS matches.
"""
Molecule._obSmarts.Init(smarts)
if Molecule._obSmarts.Match(self.obmol):
match_list = Molecule._obSmarts.GetMapList()
shift_left = lambda m: [(n - 1) for n in m]
return list(map(shift_left, match_list))
else:
return []
def GetAtomCharges(self):
"""
Returns:
A list of charges, according to the number of atoms
in the molecule
"""
return [atom.GetFormalCharge() for atom in self.GetAtoms()]