-
-
Notifications
You must be signed in to change notification settings - Fork 403
/
phmodel.cpp
426 lines (361 loc) · 12.5 KB
/
phmodel.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**********************************************************************
phmodel.cpp - Read pH rules and assign charges.
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2005 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 <openbabel/mol.h>
#include <openbabel/atom.h>
#include <openbabel/bond.h>
#include <openbabel/typer.h>
#include <openbabel/obiter.h>
#include <openbabel/oberror.h>
#include <openbabel/phmodel.h>
#include <openbabel/obfunctions.h>
#include <cstdlib>
// private data header with default parameters
#include "phmodeldata.h"
#ifdef WIN32
#pragma warning (disable : 4786)
#endif
using namespace std;
namespace OpenBabel
{
// Global OBPhModel for assigning formal charges and hydrogen addition rules
THREAD_LOCAL OBPhModel phmodel;
OBPhModel::OBPhModel()
{
_init = false;
_dir = BABEL_DATADIR;
_envvar = "BABEL_DATADIR";
_filename = "phmodel.txt";
_subdir = "data";
_dataptr = PhModelData;
}
OBPhModel::~OBPhModel()
{
vector<OBChemTsfm*>::iterator k;
for (k = _vtsfm.begin();k != _vtsfm.end();++k)
delete *k;
vector<pair<OBSmartsPattern*,vector<double> > >::iterator m;
for (m = _vschrg.begin();m != _vschrg.end();++m)
delete m->first;
}
void OBPhModel::ParseLine(const char *buffer)
{
vector<string> vs;
OBSmartsPattern *sp;
if (buffer[0] == '#')
return;
if (EQn(buffer,"TRANSFORM",7))
{
tokenize(vs,buffer);
if (vs.size() < 5)
{
obErrorLog.ThrowError(__FUNCTION__, " Could not parse line in phmodel table from phmodel.txt", obInfo);
return;
}
OBChemTsfm *tsfm = new OBChemTsfm;
if (!tsfm->Init(vs[1],vs[3]))
{
delete tsfm;
tsfm = nullptr;
obErrorLog.ThrowError(__FUNCTION__, " Could not parse line in phmodel table from phmodel.txt", obInfo);
return;
}
_vtsfm.push_back(tsfm);
_vpKa.push_back(atof(vs[4].c_str()));
}
else if (EQn(buffer,"SEEDCHARGE",10))
{
tokenize(vs,buffer);
if (vs.size() < 2)
{
obErrorLog.ThrowError(__FUNCTION__, " Could not parse line in phmodel table from phmodel.txt", obInfo);
return;
}
sp = new OBSmartsPattern;
if (!sp->Init(vs[1]) || (vs.size()-2) != sp->NumAtoms())
{
delete sp;
sp = nullptr;
obErrorLog.ThrowError(__FUNCTION__, " Could not parse line in phmodel table from phmodel.txt", obInfo);
return;
}
vector<double> vf;
vector<string>::iterator i;
for (i = vs.begin()+2;i != vs.end();++i)
vf.push_back(atof((char*)i->c_str()));
_vschrg.push_back(pair<OBSmartsPattern*,vector<double> > (sp,vf));
}
}
void OBPhModel::AssignSeedPartialCharge(OBMol &mol)
{
if (!_init)
Init();
mol.SetPartialChargesPerceived();
if (!mol.AutomaticPartialCharge())
return;
vector<pair<OBSmartsPattern*,vector<double> > >::iterator i;
for (i = _vschrg.begin(); i != _vschrg.end(); ++i) {
std::vector<std::vector<int> > mlist;
if (i->first->Match(mol, mlist, OBSmartsPattern::AllUnique))
{
unsigned int k;
vector<vector<int> >::iterator j;
for (j = mlist.begin(); j != mlist.end(); ++j)
for (k = 0; k < j->size(); ++k)
mol.GetAtom((*j)[k])->SetPartialCharge(i->second[k]);
}
}
}
void OBPhModel::CorrectForPH(OBMol &mol, double pH)
{
if (!_init)
Init();
if (mol.IsCorrectedForPH())
return;
if (mol.GetDimension() > 0 && !mol.AutomaticFormalCharge())
return;
bool hasChainsPerceived = mol.HasChainsPerceived();
mol.SetCorrectedForPH();
obErrorLog.ThrowError(__FUNCTION__,
"Ran OpenBabel::CorrectForPH", obAuditMsg);
mol.DeleteHydrogens();
for (unsigned int i = 0; i < _vtsfm.size(); ++i) {
if (_vpKa[i] > 1E+9) {
// always apply when pKa is > 1e+9
_vtsfm[i]->Apply(mol);
} else {
// 10^(pKa - pH) = [HA] / [A-]
//
// > 1 : [HA] > [A-]
// < 1 : [HA] < [A-]
if (_vtsfm[i]->IsAcid()) {
//cout << "IsAcid == " << _vtsfm[i]->IsAcid() << endl;
//cout << "pKa == " << _vpKa[i] << endl;
//cout << "pow(10, _vpKa[i] - pH) == " << pow(10, _vpKa[i] - pH) << endl;
if (pow(10, _vpKa[i] - pH) < 1.0) {
//cout << "APPLY!!" << endl;
_vtsfm[i]->Apply(mol);
}
}
// 10^(pKa - pH) = [BH+] / [B:]
//
// > 1 : [BH+] > [B:]
// < 1 : [BH+] < [B:]
if (_vtsfm[i]->IsBase()) {
//cout << "IsBase == " << _vtsfm[i]->IsBase() << endl;
//cout << "pKa == " << _vpKa[i] << endl;
//cout << "pow(10, _vpKa[i] - pH) == " << pow(10, _vpKa[i] - pH) << endl;
if (pow(10, _vpKa[i] - pH) > 1.0) {
//cout << "APPLY!!" << endl;
_vtsfm[i]->Apply(mol);
}
}
}
}
if (hasChainsPerceived)
{
mol.SetChainsPerceived();
}
}
// Portions of this documentation adapted from the JOELib docs, written by
// Joerg Wegner
/** \class OBChemTsfm phmodel.h <openbabel/phmodel.h>
\brief SMARTS based structural modification (chemical transformation)
Transformation of chemical structures can be used for pH value correction
(i.e. via OBPhModel and OBMol::CorrectForPH()). The OBChemTsfm class
defines SMARTS based TRANSFORM patterns to delete atoms, change atom types,
atom formal charges, and bond types.
For storing and converting chemical reaction files, use the OBReaction class.
**/
bool OBChemTsfm::Init(string &bgn,string &end)
{
if (!_bgn.Init(bgn))
return(false);
if (!end.empty())
if (!_end.Init(end))
return(false);
//find atoms to be deleted
unsigned int i,j;
int vb;
bool found;
for (i = 0;i < _bgn.NumAtoms();++i)
if ((vb = _bgn.GetVectorBinding(i)))
{
found = false;
for (j = 0;j < _end.NumAtoms();++j)
if (vb == _end.GetVectorBinding(j))
{
found = true;
break;
}
if (!found)
_vadel.push_back(i);
}
//find elements to be changed
int ele;
for (i = 0;i < _bgn.NumAtoms();++i)
// Allow single-atom transformations without vector bindings
if ((vb = _bgn.GetVectorBinding(i)) || _bgn.NumAtoms() == 1)
{
ele = _bgn.GetAtomicNum(i);
for (j = 0;j < _end.NumAtoms();++j)
if (vb == _end.GetVectorBinding(j))
if (ele != _end.GetAtomicNum(j))
{
_vele.push_back(pair<int,int> (i,_end.GetAtomicNum(j)));
break;
}
}
//find charges to modify
int chrg;
for (i = 0;i < _bgn.NumAtoms();++i)
// Allow single-atom transformations without vector bindings
// PR#2802980.
if ((vb = _bgn.GetVectorBinding(i)) || _bgn.NumAtoms() == 1)
{
chrg = _bgn.GetCharge(i);
for (j = 0;j < _end.NumAtoms();++j)
if (vb == _end.GetVectorBinding(j))
if (chrg != _end.GetCharge(j))
_vchrg.push_back(pair<int,int> (i,_end.GetCharge(j)));
}
//find bonds to be modified
//find bonds to be modified
int bsrc,bdst,bord,bvb1,bvb2;
int esrc,edst,eord,evb1,evb2;
for (i = 0;i < _bgn.NumBonds();++i)
{
_bgn.GetBond(bsrc,bdst,bord,i);
bvb1 = _bgn.GetVectorBinding(bsrc);
bvb2 = _bgn.GetVectorBinding(bdst);
if (!bvb1 || !bvb2)
continue;
for (j = 0;j < _end.NumBonds();++j)
{
_end.GetBond(esrc,edst,eord,j);
evb1 = _end.GetVectorBinding(esrc);
evb2 = _end.GetVectorBinding(edst);
if ((bvb1 == evb1 && bvb2 == evb2) || (bvb1 == evb2 && bvb2 == evb1))
{
if (bord == eord)
break; //nothing to modify if bond orders identical
_vbond.push_back(pair<pair<int,int>,int> (pair<int,int> (bsrc,bdst),eord));
break;
}
}
}
//make sure there is some kind of transform to do here
if (_vadel.empty() && _vchrg.empty() && _vbond.empty() && _vele.empty())
return(false);
return(true);
}
bool OBChemTsfm::Apply(OBMol &mol)
{
if (!_bgn.Match(mol))
return(false);
mol.BeginModify();
vector<vector<int> > mlist = _bgn.GetUMapList();
obErrorLog.ThrowError(__FUNCTION__,
"Ran OpenBabel::OBChemTransform", obAuditMsg);
if (!_vchrg.empty()) //modify charges
{
vector<vector<int> >::iterator i;
vector<pair<int,int> >::iterator j;
for (i = mlist.begin();i != mlist.end();++i)
for (j = _vchrg.begin();j != _vchrg.end();++j)
if (j->first < (signed)i->size()) { //goof proofing
OBAtom *atom = mol.GetAtom((*i)[j->first]);
int old_charge = atom->GetFormalCharge();
if(j->second != old_charge) {
atom->SetFormalCharge(j->second);
OBAtomAssignTypicalImplicitHydrogens(atom); //update with new charge info
}
}
}
if (!_vbond.empty()) //modify bond orders
{
OBBond *bond;
vector<vector<int> >::iterator i;
vector<pair<pair<int,int>,int> >::iterator j;
for (i = mlist.begin();i != mlist.end();++i)
for (j = _vbond.begin();j != _vbond.end();++j)
{
bond = mol.GetBond((*i)[j->first.first],(*i)[j->first.second]);
if (!bond)
{
obErrorLog.ThrowError(__FUNCTION__, "unable to find bond", obDebug);
continue;
}
unsigned int old_bond_order = bond->GetBondOrder();
bond->SetBondOrder(j->second);
for (int k = 0; k < 2; ++k) {
OBAtom* atom = k == 0 ? bond->GetBeginAtom() : bond->GetEndAtom();
int new_hcount = atom->GetImplicitHCount() - (j->second - old_bond_order);
if (new_hcount < 0)
new_hcount = 0;
atom->SetImplicitHCount(new_hcount);
}
}
}
if (!_vadel.empty() || !_vele.empty()) //delete atoms and change elements
{
vector<int>::iterator j;
vector<vector<int> >::iterator i;
if (!_vele.empty())
{
vector<pair<int,int> >::iterator k;
for (i = mlist.begin();i != mlist.end();++i)
for (k = _vele.begin();k != _vele.end();++k)
mol.GetAtom((*i)[k->first])->SetAtomicNum(k->second);
}
//make sure same atom isn't deleted twice
vector<bool> vda;
vector<OBAtom*> vdel;
vda.resize(mol.NumAtoms()+1,false);
for (i = mlist.begin();i != mlist.end();++i)
for (j = _vadel.begin();j != _vadel.end();++j)
if (!vda[(*i)[*j]])
{
vda[(*i)[*j]] = true;
vdel.push_back(mol.GetAtom((*i)[*j]));
}
vector<OBAtom*>::iterator k;
for (k = vdel.begin();k != vdel.end();++k)
mol.DeleteAtom((OBAtom*)*k);
}
mol.EndModify();
return(true);
}
bool OBChemTsfm::IsAcid()
{
if (_bgn.NumAtoms() > _end.NumAtoms()) // O=CO[#1:1] >> O=CO
return true;
for (unsigned int i = 0; i < _end.NumAtoms(); ++i) {
if (_end.GetCharge(i) < 0)
return true;
}
return false;
}
bool OBChemTsfm::IsBase()
{
for (unsigned int i = 0; i < _end.NumAtoms(); ++i) {
if (_end.GetCharge(i) > 0)
return true;
}
return false;
}
} //namespace OpenBabel
//! \file phmodel.cpp
//! \brief Read pH rules and assign charges.