File: ConnectivityDescriptors.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 (346 lines) | stat: -rw-r--r-- 9,489 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
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
// $Id$
//
//  Copyright (C) 2012 Greg Landrum
//
//   @@ 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 <RDGeneral/Invariant.h>
#include <GraphMol/GraphMol.h>
#include <GraphMol/PeriodicTable.h>
#include <GraphMol/MolOps.h>
#include <GraphMol/Subgraphs/Subgraphs.h>
#include "MolDescriptors.h"
#include "ConnectivityDescriptors.h"

namespace RDKit {
namespace Descriptors {
namespace detail {
void hkDeltas(const ROMol &mol, std::vector<double> &deltas, bool force) {
  PRECONDITION(deltas.size() >= mol.getNumAtoms(), "bad vector size");
  if (!force && mol.hasProp(common_properties::_connectivityHKDeltas)) {
    mol.getProp(common_properties::_connectivityHKDeltas, deltas);
    return;
  }
  const PeriodicTable *tbl = PeriodicTable::getTable();
  ROMol::VERTEX_ITER atBegin, atEnd;
  boost::tie(atBegin, atEnd) = mol.getVertices();
  while (atBegin != atEnd) {
    const Atom* at = mol[*atBegin];
    unsigned int n = at->getAtomicNum();
    if (n <= 1) {
      deltas[at->getIdx()] = 0;
    } else if (n <= 10) {
      deltas[at->getIdx()] = tbl->getNouterElecs(n) - at->getTotalNumHs();
    } else {
      deltas[at->getIdx()] =
          double(tbl->getNouterElecs(n) - at->getTotalNumHs()) /
          (n - tbl->getNouterElecs(n) - 1);
    }
    if (deltas[at->getIdx()] != 0.0)
      deltas[at->getIdx()] = 1. / sqrt(deltas[at->getIdx()]);
    ++atBegin;
  }
  mol.setProp(common_properties::_connectivityHKDeltas, deltas, true);
}

void nVals(const ROMol &mol, std::vector<double> &nVs, bool force) {
  PRECONDITION(nVs.size() >= mol.getNumAtoms(), "bad vector size");
  if (!force && mol.hasProp(common_properties::_connectivityNVals)) {
    mol.getProp(common_properties::_connectivityNVals, nVs);
    return;
  }
  const PeriodicTable *tbl = PeriodicTable::getTable();
  ROMol::VERTEX_ITER atBegin, atEnd;
  boost::tie(atBegin, atEnd) = mol.getVertices();
  while (atBegin != atEnd) {
    const Atom* at = mol[*atBegin];
    double v = tbl->getNouterElecs(at->getAtomicNum()) - at->getTotalNumHs();
    if (v != 0.0) {
      v = 1. / sqrt(v);
    }
    nVs[at->getIdx()] = v;
    ++atBegin;
  }
  mol.setProp(common_properties::_connectivityNVals, nVs, true);
}

double getAlpha(const Atom &atom, bool &found) {
  double res = 0.0;
  found = false;
  switch (atom.getAtomicNum()) {
    case 1:
      res = 0.0;
      found = true;
      break;
    case 6:
      switch (atom.getHybridization()) {
        case Atom::SP:
          res = -0.22;
          found = true;
          break;
        case Atom::SP2:
          res = -0.13;
          found = true;
          break;
        default:
          res = 0.00;
          found = true;
      };
      break;
    case 7:
      switch (atom.getHybridization()) {
        case Atom::SP:
          res = -0.29;
          found = true;
          break;
        case Atom::SP2:
          res = -0.20;
          found = true;
          break;
        default:
          res = -0.04;
          found = true;
          break;
      };
      break;
    case 8:
      switch (atom.getHybridization()) {
        case Atom::SP2:
          res = -0.20;
          found = true;
          break;
        default:
          res = -0.04;
          found = true;
          break;
      };
      break;
    case 9:
      res = -0.07;
      found = true;
      break;
    case 15:
      switch (atom.getHybridization()) {
        case Atom::SP2:
          res = 0.30;
          found = true;
          break;
        default:
          res = 0.43;
          found = true;
          break;
      };
      break;
    case 16:
      switch (atom.getHybridization()) {
        case Atom::SP2:
          res = 0.22;
          found = true;
          break;
        default:
          res = 0.35;
          found = true;
          break;
      };
      break;
    case 17:
      res = 0.29;
      found = true;
      break;
    case 35:
      res = 0.48;
      found = true;
      break;
    case 53:
      res = 0.73;
      found = true;
      break;
    default:
      break;
  }
  return res;
}
}  // end of detail namespace

double calcChiNv(const ROMol &mol, unsigned int n, bool force) {
  std::vector<double> hkDs(mol.getNumAtoms());
  detail::hkDeltas(mol, hkDs, force);
  PATH_LIST ps = findAllPathsOfLengthN(mol, n + 1, false);
  double res = 0.0;
  BOOST_FOREACH (PATH_TYPE p, ps) {
    TEST_ASSERT(p.size() == n + 1);
    double accum = 1.0;
    for (unsigned int i = 0; i < n; ++i) {
      accum *= hkDs[p[i]];
    }
    // only push on the last element if this isn't a ring; this was github 463:
    if (p[n] != p[0]) {
      accum *= hkDs[p[n]];
    }
    res += accum;
  }
  return res;
}
double calcChiNn(const ROMol &mol, unsigned int n, bool force) {
  std::vector<double> nVs(mol.getNumAtoms());
  detail::nVals(mol, nVs, force);
  PATH_LIST ps = findAllPathsOfLengthN(mol, n + 1, false);
  double res = 0.0;
  BOOST_FOREACH (PATH_TYPE p, ps) {
    TEST_ASSERT(p.size() == n + 1);
    double accum = 1.0;
    for (unsigned int i = 0; i < n; ++i) {
      accum *= nVs[p[i]];
    }
    // only push on the last element if this isn't a ring; this was github 463:
    if (p[n] != p[0]) {
      accum *= nVs[p[n]];
    }

    res += accum;
  }
  return res;
}

double calcChi0v(const ROMol &mol, bool force) {
  std::vector<double> hkDs(mol.getNumAtoms());
  detail::hkDeltas(mol, hkDs, force);
  return std::accumulate(hkDs.begin(), hkDs.end(), 0.0);
};
double calcChi1v(const ROMol &mol, bool force) {
  std::vector<double> hkDs(mol.getNumAtoms());
  detail::hkDeltas(mol, hkDs, force);

  double res = 0.0;
  ROMol::EDGE_ITER firstB, lastB;
  boost::tie(firstB, lastB) = mol.getEdges();
  while (firstB != lastB) {
    const Bond* bond = mol[*firstB];
    res += hkDs[bond->getBeginAtomIdx()] * hkDs[bond->getEndAtomIdx()];
    ++firstB;
  }
  return res;
};
double calcChi2v(const ROMol &mol, bool force) {
  return calcChiNv(mol, 2, force);
};
double calcChi3v(const ROMol &mol, bool force) {
  return calcChiNv(mol, 3, force);
};
double calcChi4v(const ROMol &mol, bool force) {
  return calcChiNv(mol, 4, force);
};

double calcChi0n(const ROMol &mol, bool force) {
  std::vector<double> nVs(mol.getNumAtoms());
  detail::nVals(mol, nVs, force);
  return std::accumulate(nVs.begin(), nVs.end(), 0.0);
};
double calcChi1n(const ROMol &mol, bool force) {
  std::vector<double> nVs(mol.getNumAtoms());
  detail::nVals(mol, nVs, force);

  double res = 0.0;
  ROMol::EDGE_ITER firstB, lastB;
  boost::tie(firstB, lastB) = mol.getEdges();
  while (firstB != lastB) {
    const Bond* bond = mol[*firstB];
    res += nVs[bond->getBeginAtomIdx()] * nVs[bond->getEndAtomIdx()];
    ++firstB;
  }
  return res;
};
double calcChi2n(const ROMol &mol, bool force) {
  return calcChiNn(mol, 2, force);
};
double calcChi3n(const ROMol &mol, bool force) {
  return calcChiNn(mol, 3, force);
};
double calcChi4n(const ROMol &mol, bool force) {
  return calcChiNn(mol, 4, force);
};

double calcHallKierAlpha(const ROMol &mol, std::vector<double> *atomContribs) {
  PRECONDITION(!atomContribs || atomContribs->size() >= mol.getNumAtoms(),
               "bad atomContribs vector");
  const PeriodicTable *tbl = PeriodicTable::getTable();
  double alphaSum = 0.0;
  double rC = tbl->getRb0(6);
  ROMol::VERTEX_ITER atBegin, atEnd;
  boost::tie(atBegin, atEnd) = mol.getVertices();
  while (atBegin != atEnd) {
    const Atom* at = mol[*atBegin];
    ++atBegin;
    unsigned int n = at->getAtomicNum();
    if (!n) continue;
    bool found;
    double alpha = detail::getAlpha(*(at), found);
    if (!found) {
      double rA = tbl->getRb0(n);
      alpha = rA / rC - 1.0;
    }
    alphaSum += alpha;
    if (atomContribs) (*atomContribs)[at->getIdx()] = alpha;
  }
  return alphaSum;
};

namespace {
double kappa1Helper(double P1, double A, double alpha) {
  double denom = P1 + alpha;
  double kappa = 0.0;
  if (denom) {
    kappa = (A + alpha) * (A + alpha - 1) * (A + alpha - 1) / (denom * denom);
  }
  return kappa;
}
double kappa2Helper(double P2, double A, double alpha) {
  double denom = (P2 + alpha) * (P2 + alpha);
  double kappa = 0.0;
  if (denom) {
    kappa = (A + alpha - 1) * (A + alpha - 2) * (A + alpha - 2) / denom;
  }
  return kappa;
}
double kappa3Helper(double P3, int A, double alpha) {
  double denom = (P3 + alpha) * (P3 + alpha);
  double kappa = 0.0;
  if (denom) {
    if (A % 2) {
      kappa = (A + alpha - 1) * (A + alpha - 3) * (A + alpha - 3) / denom;
    } else {
      kappa = (A + alpha - 2) * (A + alpha - 3) * (A + alpha - 3) / denom;
    }
  }
  return kappa;
}
}  // end of anonymous namespace

double calcKappa1(const ROMol &mol) {
  double P1 = mol.getNumBonds();
  double A = mol.getNumHeavyAtoms();
  double alpha = calcHallKierAlpha(mol);
  double kappa = kappa1Helper(P1, A, alpha);
  return kappa;
}
double calcKappa2(const ROMol &mol) {
  PATH_LIST ps = findAllPathsOfLengthN(mol, 2);
  double P2 = ps.size();
  double A = mol.getNumHeavyAtoms();
  double alpha = calcHallKierAlpha(mol);
  double kappa = kappa2Helper(P2, A, alpha);
  return kappa;
}
double calcKappa3(const ROMol &mol) {
  double P3 = findAllPathsOfLengthN(mol, 3).size();
  int A = mol.getNumHeavyAtoms();
  double alpha = calcHallKierAlpha(mol);
  double kappa = kappa3Helper(P3, A, alpha);
  return kappa;
}
}  // end of namespace Descriptors
}