File: molchrg.cpp

package info (click to toggle)
openbabel 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 36,644 kB
  • ctags: 33,717
  • sloc: cpp: 242,528; ansic: 87,037; sh: 10,280; perl: 5,518; python: 5,156; pascal: 793; makefile: 747; cs: 392; xml: 97; ruby: 54; java: 23
file content (333 lines) | stat: -rw-r--r-- 9,120 bytes parent folder | download | duplicates (2)
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
/**********************************************************************
molchrg.cpp - Assign Gasteiger partial 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.sourceforge.net/>
 
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/molchrg.h>

using namespace std;
namespace OpenBabel
{

  /*! \class OBGastChrg molchrg.h <openbabel/molchrg.h>
    \brief Assigns Gasteiger partial charges

    The OBGastChrg class is responsible for the assignment of partial
    charges to a molecule according to the Gasteiger charge model (sigma). When
    the partial charge of an atom is requested and partial charges do not
    yet exist for the molecule the OBGastChrg class will automatically be
    called to assign charges for the molecule. If charges have been read
    in for a molecule the following code shows how to force the
    recalculation of partial charges:
    \code
    OBMol mol;

    mol.UnsetPartialChargesPerceived();
    FOR_ATOMS_IN_MOL(atom, mol)
    {
       cout << "atom number = " << atom->GetIdx();
       cout << " charge = " << atom->GetPartialCharge() << endl;
    }
    \endcode
    Formal charges are used as seed values of the initial charge of atoms,
    and the partial charge is propagated to neighboring atoms. For
    example, quaternary amines would have a +1 charge, and the effect of
    the positive charge would be felt by neighboring atoms according to
    the Gasteiger model (sigma).

    For more information, see:
    J. Gasteiger & M. Marsili, "A New Model for Calculating Atomic Charges in Molecules" Tetrahedron Lett., (1978) 3181-3184.
  */

  bool OBGastChrg::AssignPartialCharges(OBMol &mol)
  {
    //InitialPartialCharges(mol);
    obErrorLog.ThrowError(__FUNCTION__,
                          "Ran OpenBabel::AssignPartialCharges", obAuditMsg);

    // Annotate that partial charges come from Gasteiger
    OBPairData *dp = new OBPairData;
    dp->SetAttribute("PartialCharges");
    dp->SetValue("Gasteiger");
    dp->SetOrigin(perceived);
    mol.SetData(dp);

    OBAtom *atom;
    vector<OBAtom*>::iterator i;

    GSVResize(mol.NumAtoms()+1);
    double a,b,c;
    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
      {
        if (!GasteigerSigmaChi(atom,a,b,c))
          return(false);
        _gsv[atom->GetIdx()]->SetValues(a,b,c,atom->GetPartialCharge());
      }

    double alpha,charge,denom;
    unsigned j;
    int iter;
    OBBond *bond;
    OBAtom *src,*dst;
    vector<OBBond*>::iterator k;
    alpha = 1.0;
    for(iter = 0;iter < OB_GASTEIGER_ITERS;++iter)
      {
        alpha *= OB_GASTEIGER_DAMP;

        for( j=1;j < _gsv.size();++j)
          {
            charge = _gsv[j]->q;
            _gsv[j]->chi = (_gsv[j]->c*charge+_gsv[j]->b)*charge+_gsv[j]->a;
          }

        for (bond = mol.BeginBond(k);bond;bond = mol.NextBond(k))
          {
            src = bond->GetBeginAtom();
            dst = bond->GetEndAtom();

            if (_gsv[src->GetIdx()]->chi >= _gsv[dst->GetIdx()]->chi)
              {
                if (dst->IsHydrogen())
                  denom = double(OB_GASTEIGER_DENOM);
                else
                  denom = _gsv[dst->GetIdx()]->denom;
              }
            else
              {
                if (src->IsHydrogen())
                  denom = double(OB_GASTEIGER_DENOM);
                else
                  denom = _gsv[src->GetIdx()]->denom;
              }

            charge = (_gsv[src->GetIdx()]->chi - _gsv[dst->GetIdx()]->chi)/denom;
            _gsv[src->GetIdx()]->q -= alpha*charge;
            _gsv[dst->GetIdx()]->q += alpha*charge;
          }
      }

    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
      atom->SetPartialCharge(_gsv[atom->GetIdx()]->q);

    return(true);
  }

  //! Set initial partial charges in @p mol
  //! Carbonyl O => -0.5
  //! Phosphate O => -0.666
  //! Sulfate O => -0.5
  //! All other atoms are set to have their initial charge from their formal charge
  void OBGastChrg::InitialPartialCharges(OBMol &mol)
  {
    OBAtom *atom;
    vector<OBAtom*>::iterator i;

    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
      {
        if (atom->IsCarboxylOxygen())
          atom->SetPartialCharge(-0.500);
        else if (atom->IsPhosphateOxygen() &&
                 atom->GetHvyValence() == 1)
          atom->SetPartialCharge(-0.666);
        else if (atom->IsSulfateOxygen())
          atom->SetPartialCharge(-0.500);
        else
          atom->SetPartialCharge((double)atom->GetFormalCharge());
      }
  }

  bool OBGastChrg::GasteigerSigmaChi(OBAtom *atom,double &a,double &b,double &c )
  {
    int count;
    double val[3] = {0.0,0.0,0.0};

    switch(atom->GetAtomicNum())
      {
      case 1: //H
        val[0] = 0.37;
        val[1] = 7.17;
        val[2] = 12.85;
        break;
      case 6: //C
        if (atom->GetHyb() == 3)
          {
            val[0] = 0.68;
            val[1] = 7.98;
            val[2] = 19.04;
          }
        if (atom->GetHyb() == 2)
          {
            val[0] = 0.98;
            val[1] = 8.79;
            val[2] = 19.62;
          }
        if (atom->GetHyb() == 1)
          {
            val[0] = 1.67;
            val[1] = 10.39;
            val[2] = 20.57;
          }
        break;
      case 7: //N
        if (atom->GetHyb() == 3)
          {
            if (atom->GetValence() == 4 || atom->GetFormalCharge())
              {
                val[0] = 0.0;
                val[1] = 0.0;
                val[2] = 23.72;
              }
            else
              {
                val[0] = 2.08;
                val[1] = 11.54;
                val[2] = 23.72;
              }
          }

        if (atom->GetHyb() == 2)
          {
            if (EQ(atom->GetType(),"Npl") || EQ(atom->GetType(),"Nam"))
              {
                val[0] = 2.46;
                val[1] = 12.32;
                val[2] = 24.86;
              }
            else
              {
                val[0] = 2.57;
                val[1] = 12.87;
                val[2] = 24.87;
              }
          }

        if (atom->GetHyb() == 1)
          {
            val[0] = 3.71;
            val[1] = 15.68;
            val[2] = 27.11;
          }
        break;
      case 8: //O
        if (atom->GetHyb() == 3)
          {
            val[0] = 2.65;
            val[1] = 14.18;
            val[2] = 28.49;
          }
        if (atom->GetHyb() == 2)
          {
            val[0] = 3.75;
            val[1] = 17.07;
            val[2] = 31.33;
          }
        break;
      case 9: //F
        val[0] = 3.12;
        val[1] = 14.66;
        val[2] = 30.82;
        break;
      case 15: //P
        val[0] = 1.62;
        val[1] = 8.90;
        val[2] = 18.10;
        break;
      case 16: //S
        count = atom->CountFreeOxygens();
        if (count == 0 || count == 1)
          {
            val[0] = 2.39;
            val[1] = 10.14;
            val[2] = 20.65;
          }
        if (count > 1)
          {
            val[0] = 2.39;
            val[1] = 12.00;
            val[2] = 24.00;
          }
        /*S2? if (count == 0) {val[0] = 2.72;val[1] = 10.88;val[2] = 21.69;}*/
        break;
      case 17: //Cl
        val[0] = 2.66;
        val[1] = 11.00;
        val[2] = 22.04;
        break;
      case 35: //Br
        val[0] = 2.77;
        val[1] = 10.08;
        val[2] = 19.71;
        break;
      case 53: //I
        val[0] = 2.90;
        val[1] = 9.90;
        val[2] = 18.82;
        break;
      case 13: //Al
        val[0] = 1.06;
        val[1] = 5.47;
        val[2] = 11.65;
        break;
      }

    if (!IsNearZero(val[2]))
      {
        a = val[1];
        b = (val[2]-val[0])/2;
        c = (val[2]+val[0])/2 - val[1];
      }
    else
      return(false);

    return(true);
  }

  void OBGastChrg::GSVResize(int size)
  {
    vector <GasteigerState*>::iterator i;
    for (i = _gsv.begin();i != _gsv.end();++i)
      delete *i;
    _gsv.clear();

    for (int j = 0;j < size;++j)
      _gsv.push_back(new GasteigerState);
  }

  OBGastChrg::~OBGastChrg()
  {
    vector <GasteigerState*>::iterator i;
    for (i = _gsv.begin();i != _gsv.end();++i)
      delete *i;
  }

  GasteigerState::GasteigerState()
  {
    a = 0.0;
    b = 0.0;
    c = 0.0;
    denom = 0.0;
    chi = 0.0;
    q = 0.0;
  }

} // end namespace OpenBabel

//! \file molchrg.cpp
//! \brief Assign Gasteiger partial charges.