File: GasteigerCharges.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (265 lines) | stat: -rw-r--r-- 8,262 bytes parent folder | download
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
// $Id$
//
//  Copyright (C) 2003-2011 Greg Landrum and Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include "GasteigerCharges.h"
#include <RDGeneral/types.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/ROMol.h>
#include <GraphMol/MolOps.h>
#include "GasteigerParams.h"

namespace Gasteiger {
using namespace RDKit;
/*! \brief split the formal charge across atoms of same type if we have a
 *conjugated system
 *
 *  This function is called before the charge equivalization iteration is
 *started for the
 *  Gasteiger charges. If any of the atom involved in conjugated system have
 *formal charges
 *  set on them, this charges is equally distributed across atoms of the same
 *type in that
 *  conjugated system. So for example the two nitrogens in the benzamidine
 *system start the iteration
 * with equal charges of 0.5
 */
void splitChargeConjugated(const ROMol &mol, DOUBLE_VECT &charges) {
  int aix;
  int natms = mol.getNumAtoms();
  INT_VECT marker;
  INT_VECT_CI mci;
  int aax, yax;
  double formal;
  const Atom *at, *aat, *yat;
  for (aix = 0; aix < natms; aix++) {
    at = mol.getAtomWithIdx(aix);
    formal = at->getFormalCharge();
    // std::cout << aix << " formal charges:" << formal << "\n";
    marker.resize(0);
    if ((fabs(formal) > EPS_DOUBLE) && (fabs(charges[aix]) < EPS_DOUBLE)) {
      marker.push_back(aix);
      ROMol::OEDGE_ITER bnd1, end1, bnd2, end2;
      boost::tie(bnd1, end1) = mol.getAtomBonds(at);
      while (bnd1 != end1) {
        if (mol[*bnd1]->getIsConjugated()) {
          aax = mol[*bnd1]->getOtherAtomIdx(aix);
          aat = mol.getAtomWithIdx(aax);
          boost::tie(bnd2, end2) = mol.getAtomBonds(aat);
          while (bnd2 != end2) {
            if ((*bnd1) != (*bnd2)) {
              if (mol[*bnd2]->getIsConjugated()) {
                yax = mol[*bnd2]->getOtherAtomIdx(aax);
                yat = mol.getAtomWithIdx(yax);
                if (at->getAtomicNum() == yat->getAtomicNum()) {
                  formal += yat->getFormalCharge();
                  marker.push_back(yax);
                }
              }
            }
            bnd2++;
          }
        }
        bnd1++;
      }

      for (mci = marker.begin(); mci != marker.end(); mci++) {
        charges[*mci] = (formal / marker.size());
      }
    }
  }
  /*
  for (aix = 0; aix < natms; aix++) {
    std::cout << "In splitter: " << " charges:" << charges[aix] << "\n";
    }*/
}
}  // end of namespace Gasteiger

namespace RDKit {
void computeGasteigerCharges(const ROMol *mol, int nIter,
                             bool throwOnParamFailure) {
  PRECONDITION(mol, "bad molecule");
  computeGasteigerCharges(*mol, nIter, throwOnParamFailure);
}
void computeGasteigerCharges(const ROMol &mol, int nIter,
                             bool throwOnParamFailure) {
  std::vector<double> chgs(mol.getNumAtoms());
  computeGasteigerCharges(mol, chgs, nIter, throwOnParamFailure);
}

/*! \brief compute the Gasteiger partial charges and return a new molecule with
 *the charges set
 *
 * Ref : J.Gasteiger, M. Marsili, "Iterative Equalization of Oribital
 *Electronegatiity
 *  A Rapid Access to Atomic Charges", Tetrahedron Vol 36 p3219 1980
 */
void computeGasteigerCharges(const ROMol &mol, std::vector<double> &charges,
                             int nIter, bool throwOnParamFailure) {
  PRECONDITION(charges.size() >= mol.getNumAtoms(), "bad array size");

  PeriodicTable *table = PeriodicTable::getTable();
  const GasteigerParams *params = GasteigerParams::getParams();

  double damp = DAMP;
  int natms = mol.getNumAtoms();
  // space for parameters for each atom in the molecule
  std::vector<DOUBLE_VECT> atmPs;
  atmPs.reserve(natms);

  std::fill(charges.begin(), charges.end(), 0.0);

  DOUBLE_VECT
  hChrg;  // total charge on the implicit hydrogen on each heavy atom
  hChrg.resize(natms, 0.0);

  DOUBLE_VECT ionX;
  ionX.resize(natms, 0.0);

  DOUBLE_VECT energ;
  energ.resize(natms, 0.0);

  ROMol::ADJ_ITER nbrIdx, endIdx;

  // deal with the conjugated system - distribute the formal charges on atoms of
  // same type in each
  // conjugated system
  Gasteiger::splitChargeConjugated(mol, charges);

  // now read in the parameters
  ROMol::ConstAtomIterator ai;

  for (ai = mol.beginAtoms(); ai != mol.endAtoms(); ai++) {
    std::string elem = table->getElementSymbol((*ai)->getAtomicNum());
    std::string mode;

    switch ((*ai)->getHybridization()) {
      case Atom::SP3:
        mode = "sp3";
        break;
      case Atom::SP2:
        mode = "sp2";
        break;
      case Atom::SP:
        mode = "sp";
        break;
      default:
        if ((*ai)->getAtomicNum() == 1) {
          // if it is hydrogen
          mode = "*";
        } else if ((*ai)->getAtomicNum() == 16) {
          // we have a sulfur atom with no hydribidation information
          // check how many oxygens we have on the sulfer
          boost::tie(nbrIdx, endIdx) = mol.getAtomNeighbors(*ai);
          int no = 0;
          while (nbrIdx != endIdx) {
            if (mol.getAtomWithIdx(*nbrIdx)->getAtomicNum() == 8) {
              no++;
            }
            nbrIdx++;
          }
          if (no == 2) {
            mode = "so2";
          } else if (no == 1) {
            mode = "so";
          } else {
            // some other sulfur state. Default to sp3
            mode = "sp3";
          }
        }
    }

    // if we get a unknown mode or element type the
    // following will will throw an exception
    atmPs.push_back(params->getParams(elem, mode, throwOnParamFailure));

    // set ionX paramters
    // if Hydrogen treat differently
    int idx = (*ai)->getIdx();
    if ((*ai)->getAtomicNum() == 1) {
      ionX[idx] = IONXH;
    } else {
      ionX[idx] = atmPs[idx][0] + atmPs[idx][1] + atmPs[idx][2];
    }
  }

  // do the iteration here
  int itx, aix, sgn, niHs;
  double enr, dq, dx, qHs, dqH;
  // parameters for hydrogen atoms (for case where the hydrogen are not in the
  // graph (implicit hydrogens)
  DOUBLE_VECT hParams;
  hParams = params->getParams("H", "*", throwOnParamFailure);

  /*
  int itmp;
  for (itmp = 0; itmp < 5; itmp++) {
    std::cout << " aq:" << charges[itmp] << "\n";
    }*/

  for (itx = 0; itx < nIter; itx++) {
    for (aix = 0; aix < natms; aix++) {
      // enr = p0 + charge*(p1 + p2*charge)
      enr = atmPs[aix][0] +
            charges[aix] * (atmPs[aix][1] + atmPs[aix][2] * charges[aix]);
      energ[aix] = enr;
    }

    for (aix = 0; aix < natms; aix++) {
      dq = 0.0;
      boost::tie(nbrIdx, endIdx) =
          mol.getAtomNeighbors(mol.getAtomWithIdx(aix));
      while (nbrIdx != endIdx) {
        dx = energ[*nbrIdx] - energ[aix];
        if (dx < 0.0) {
          sgn = 0;
        } else {
          sgn = 1;
        }
        dq += dx / ((sgn * (ionX[aix] - ionX[*nbrIdx])) + ionX[*nbrIdx]);
        nbrIdx++;
      }
      // now loop over the implicit hydrogens and get their contributions
      // since hydrogens don't connect to anything else, update their charges at
      // the same time
      niHs = mol.getAtomWithIdx(aix)->getTotalNumHs();
      if (niHs > 0) {
        qHs = hChrg[aix] / niHs;
        enr = hParams[0] + qHs * (hParams[1] + hParams[2] * qHs);
        dx = enr - energ[aix];
        if (dx < 0.0) {
          sgn = 0;
        } else {
          sgn = 1;
        }

        dqH = dx / ((sgn * (ionX[aix] - IONXH)) + IONXH);

        dq += (niHs * dqH);

        // adjust the charges on the hydrogens simultaneously (possible because
        // each of the
        // hydrogens have no other neighbors)
        hChrg[aix] -= (niHs * dqH * damp);
      }
      charges[aix] += (damp * dq);
    }

    damp *= DAMP_SCALE;
  }

  for (aix = 0; aix < natms; aix++) {
    mol.getAtomWithIdx(aix)->setProp(common_properties::_GasteigerCharge,
                                     charges[aix], true);
    // set the implicit hydrogen charges
    mol.getAtomWithIdx(aix)->setProp(common_properties::_GasteigerHCharge,
                                     hChrg[aix], true);
  }
}
}