-
-
Notifications
You must be signed in to change notification settings - Fork 403
/
bondtyper.cpp
344 lines (280 loc) · 10.2 KB
/
bondtyper.cpp
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
/**********************************************************************
bondtyper.cpp - Bond typer to perceive connectivity and bond orders/types.
Copyright (C) 2003-2006 by Geoffrey R. Hutchison
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#include <openbabel/babelconfig.h>
#include <cstdlib>
#include <openbabel/mol.h>
#include <openbabel/atom.h>
#include <openbabel/bond.h>
#include <openbabel/oberror.h>
#include <openbabel/bondtyper.h>
#include <openbabel/elements.h>
// data header with default parameters
#include "bondtyp.h"
using namespace std;
namespace OpenBabel
{
extern OBMessageHandler obErrorLog;
//! Global OBBondTyper for perception of bond order assignment.
#if __cplusplus >= 201103L
thread_local //this is required for correct multi-threading
#endif
OBBondTyper bondtyper;
/*! \class OBBondTyper bondtyper.h <openbabel/bondtyper.cpp>
\brief Assigns bond types for file formats without bond information
The OBBondTyper class is designed to read in a list of bond typing
rules and apply them to molecules. It is called from the
OBMol::PerceiveBondOrders() method.
*/
OBBondTyper::OBBondTyper()
{
_init = false;
_dir = BABEL_DATADIR;
_envvar = "BABEL_DATADIR";
_filename = "bondtyp.txt";
_subdir = "data";
_dataptr = BondTypeData;
}
void OBBondTyper::ParseLine(const char *buffer)
{
vector<string> vs;
vector<int> bovector;
OBSmartsPattern *sp;
if (buffer[0] != '#')
{
tokenize(vs,buffer);
// Make sure we actually have a SMARTS pattern plus at least one triple
// and make sure we have the correct number of integers
if (vs.size() < 4)
return; // just ignore empty (or short lines)
else if (vs.size() >= 4 && (vs.size() % 3 != 1))
{
stringstream errorMsg;
errorMsg << " Error in OBBondTyper. Pattern is incorrect, found "
<< vs.size() << " tokens." << endl;
errorMsg << " Buffer is: " << buffer << endl;
obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obInfo);
return;
}
sp = new OBSmartsPattern;
if (sp->Init(vs[0]))
{
for (unsigned int i = 1; i < vs.size() ; ++i)
{
bovector.push_back( atoi((char *)vs[i].c_str()) );
}
_fgbonds.push_back(pair<OBSmartsPattern*,vector<int> >
(sp, bovector));
}
else
{
delete sp;
sp = nullptr;
}
}
}
OBBondTyper::~OBBondTyper()
{
vector<pair<OBSmartsPattern*, vector<int> > >::iterator i;
for (i = _fgbonds.begin();i != _fgbonds.end();++i)
{
delete i->first;
i->first = nullptr;
}
}
void OBBondTyper::AssignFunctionalGroupBonds(OBMol &mol)
{
if (!_init)
Init();
OBSmartsPattern *currentPattern;
OBBond *b1, *b2;
OBAtom *a1,*a2, *a3, *a4;
double angle, angle1, angle2, dist1, dist2;
vector<int> assignments;
vector<vector<int> > mlist;
vector<vector<int> >::iterator matches, l;
vector<pair<OBSmartsPattern*, vector<int> > >::iterator i;
unsigned int j;
// Loop through for all the functional groups and assign bond orders
for (i = _fgbonds.begin();i != _fgbonds.end();++i)
{
currentPattern = i->first;
assignments = i->second;
if (currentPattern && currentPattern->Match(mol))
{
mlist = currentPattern->GetUMapList();
for (matches = mlist.begin(); matches != mlist.end(); ++matches)
{
// Now loop through the bonds to assign from _fgbonds
for (j = 0; j < assignments.size(); j += 3)
{
// along the assignments vector: atomID1 atomID2 bondOrder
a1 = mol.GetAtom((*matches)[ assignments[j] ]);
a2 = mol.GetAtom((*matches)[ assignments[j+1 ] ]);
if (!a1 || !a2) continue;
b1 = a1->GetBond(a2);
if (!b1) continue;
b1->SetBondOrder(assignments[j+2]);
} // bond order assignments
} // each match
} // current pattern matches
} // for(functional groups)
// FG with distance and/or bond criteria
// Carbonyl oxygen C=O (O must be neutral)
OBSmartsPattern carbo; carbo.Init("[#8D1;!-][#6](*)(*)");
if (carbo.Match(mol))
{
mlist = carbo.GetUMapList();
for (l = mlist.begin(); l != mlist.end(); ++l)
{
a1 = mol.GetAtom((*l)[0]);
a2 = mol.GetAtom((*l)[1]);
angle = a2->AverageBondAngle();
dist1 = a1->GetDistance(a2);
// carbonyl geometries ?
if (angle > 115 && angle < 150 && dist1 < 1.28) {
if ( !a1->HasDoubleBond() ) {// no double bond already assigned
b1 = a1->GetBond(a2);
if (!b1 ) continue;
b1->SetBondOrder(2);
}
}
}
} // Carbonyl oxygen
// thione C=S
OBSmartsPattern thione; thione.Init("[#16D1][#6](*)(*)");
if (thione.Match(mol))
{
mlist = thione.GetUMapList();
for (l = mlist.begin(); l != mlist.end(); ++l)
{
a1 = mol.GetAtom((*l)[0]);
a2 = mol.GetAtom((*l)[1]);
angle = a2->AverageBondAngle();
dist1 = a1->GetDistance(a2);
// thione geometries ?
if (angle > 115 && angle < 150 && dist1 < 1.72) {
if ( !a1->HasDoubleBond() ) {// no double bond already assigned
b1 = a1->GetBond(a2);
if (!b1 ) continue;
b1->SetBondOrder(2);
}
}
}
} // thione
// Imidazole [nD3]1c=[nD3]c=c1
OBSmartsPattern imidazolium; imidazolium.Init("[#7D3][#6][#7D3][#6][#6]");
if (imidazolium.Match(mol))
{
mlist = imidazolium.GetUMapList();
for (l = mlist.begin(); l != mlist.end(); ++l)
{
a1 = mol.GetAtom((*l)[1]);
a2 = mol.GetAtom((*l)[2]);
a2->SetFormalCharge(1);
angle1 = a2->AverageBondAngle();
dist1 = a1->GetDistance(a2);
if (angle1 > 115 && angle < 150 && dist1 < 1.72) {
if ( !a1->HasDoubleBond() ) {// no double bond already assigned
b1 = a1->GetBond(a2);
if (!b1 ) continue;
b1->SetBondOrder(2);
}
}
a3 = mol.GetAtom((*l)[3]);
a4 = mol.GetAtom((*l)[4]);
angle2 = a3->AverageBondAngle();
dist2 = a3->GetDistance(a4);
// imidazolium geometries ?
if (angle2 > 115 && angle2 < 150 && dist2 < 1.72) {
if ( !a3->HasDoubleBond() ) {// no double bond already assigned
b2 = a3->GetBond(a4);
if (!b2 ) continue;
b2->SetBondOrder(2);
}
}
}
} // imidazolium
// Isocyanate N=C=O or Isothiocyanate
bool dist1OK;
OBSmartsPattern isocyanate; isocyanate.Init("[#8,#16;D1][#6D2][#7D2]");
if (isocyanate.Match(mol))
{
mlist = isocyanate.GetUMapList();
for (l = mlist.begin(); l != mlist.end(); ++l)
{
a1 = mol.GetAtom((*l)[0]);
a2 = mol.GetAtom((*l)[1]);
a3 = mol.GetAtom((*l)[2]);
angle = a2->AverageBondAngle();
dist1 = a1->GetDistance(a2);
dist2 = a2->GetDistance(a3);
// isocyanate geometry or Isothiocyanate geometry ?
if (a1->GetAtomicNum() == OBElements::Oxygen)
dist1OK = dist1 < 1.28;
else
dist1OK = dist1 < 1.72;
if (angle > 150 && dist1OK && dist2 < 1.34) {
b1 = a1->GetBond(a2);
b2 = a2->GetBond(a3);
if (!b1 || !b2) continue;
b1->SetBondOrder(2);
b2->SetBondOrder(2);
}
}
} // Isocyanate
// oxime C=S
OBSmartsPattern oxime; oxime.Init("[#6D3][#7D2][#8D2]");
if (oxime.Match(mol))
{
mlist = oxime.GetUMapList();
for (l = mlist.begin(); l != mlist.end(); ++l)
{
a1 = mol.GetAtom((*l)[0]);
a2 = mol.GetAtom((*l)[1]);
angle = a2->AverageBondAngle();
dist1 = a1->GetDistance(a2);
// thione geometries ?
if (angle > 110 && angle < 150 && dist1 < 1.4) {
if ( !a1->HasDoubleBond() ) {// no double bond already assigned
b1 = a1->GetBond(a2);
if (!b1 ) continue;
b1->SetBondOrder(2);
}
}
}
} // oxime
// oxido-n+ (e.g., pyridine-N-oxide)
OBSmartsPattern oxidopyr; oxidopyr.Init("[#8D1][#7D3r6]");
if (oxidopyr.Match(mol))
{
mlist = oxidopyr.GetUMapList();
for (l = mlist.begin(); l != mlist.end(); ++l)
{
a1 = mol.GetAtom((*l)[0]);
a2 = mol.GetAtom((*l)[1]);
angle = a2->AverageBondAngle();
dist1 = a1->GetDistance(a2);
if (angle > 110 && angle < 150 && dist1 < 1.35) {
a1->SetFormalCharge(-1); // oxygen
a2->SetFormalCharge(+1); // nitrogen
}
}
}
}
} //namespace OpenBabel;
//! \file bondtyper.cpp
//! \brief Bond typer to perceive connectivity and bond orders/types.
//! \todo Needs to add aromatic ring bond order assignment.
//! Eventually need to migrate OBMol::PerceiveBondOrders(),
//! OBMol::ConnectTheDots(), and possibly some of the Kekulize routines