File: Lipinski.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 (511 lines) | stat: -rw-r--r-- 17,107 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//
//  Copyright (C) 2011-2013 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 <GraphMol/RDKitBase.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/Descriptors/MolDescriptors.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <RDGeneral/types.h>

#include <RDGeneral/BoostStartInclude.h>
#include <boost/dynamic_bitset.hpp>
#include <boost/foreach.hpp>
#include <boost/flyweight.hpp>
#include <boost/flyweight/key_value.hpp>
#include <boost/flyweight/no_tracking.hpp>
#include <RDGeneral/BoostEndInclude.h>

#include <vector>
#include <string>

namespace {
class ss_matcher {
 public:
  ss_matcher(const std::string &pattern) : m_pattern(pattern) {
    m_needCopies = (pattern.find_first_of("$") != std::string::npos);
    RDKit::RWMol *p = RDKit::SmartsToMol(pattern);
    m_matcher = p;
    POSTCONDITION(m_matcher, "no matcher");
  };
  const RDKit::ROMol *getMatcher() const { return m_matcher; };
  unsigned int countMatches(const RDKit::ROMol &mol) const {
    PRECONDITION(m_matcher, "no matcher");
    std::vector<RDKit::MatchVectType> matches;
    // This is an ugly one. Recursive queries aren't thread safe.
    // Unfortunately we have to take a performance hit here in order
    // to guarantee thread safety
    if (m_needCopies) {
      const RDKit::ROMol nm(*(m_matcher), true);
      RDKit::SubstructMatch(mol, nm, matches);
    } else {
      const RDKit::ROMol &nm = *m_matcher;
      RDKit::SubstructMatch(mol, nm, matches);
    }
    return matches.size();
  }
  ~ss_matcher() { delete m_matcher; };

 private:
  ss_matcher() : m_pattern(""), m_needCopies(false), m_matcher(nullptr){};
  std::string m_pattern;
  bool m_needCopies;
  const RDKit::ROMol *m_matcher;
};
}

typedef boost::flyweight<boost::flyweights::key_value<std::string, ss_matcher>,
                         boost::flyweights::no_tracking> pattern_flyweight;
#define SMARTSCOUNTFUNC(nm, pattern, vers)         \
  const std::string nm##Version = vers;            \
  unsigned int calc##nm(const RDKit::ROMol &mol) { \
    pattern_flyweight m(pattern);                  \
    return m.get().countMatches(mol);              \
  }                                                \
  extern int no_such_variable

namespace RDKit {
namespace Descriptors {

unsigned int calcLipinskiHBA(const ROMol &mol) {
  unsigned int res = 0;
  for (ROMol::ConstAtomIterator iter = mol.beginAtoms(); iter != mol.endAtoms();
       ++iter) {
    if ((*iter)->getAtomicNum() == 7 || (*iter)->getAtomicNum() == 8) ++res;
  }
  return res;
}

unsigned int calcLipinskiHBD(const ROMol &mol) {
  unsigned int res = 0;
  for (ROMol::ConstAtomIterator iter = mol.beginAtoms(); iter != mol.endAtoms();
       ++iter) {
    if (((*iter)->getAtomicNum() == 7 || (*iter)->getAtomicNum() == 8)) {
      res += (*iter)->getTotalNumHs(true);
    }
  }
  return res;
}


namespace {
#ifdef RDK_USE_STRICT_ROTOR_DEFINITION
const NumRotatableBondsOptions DefaultStrictDefinition = Strict;
#else
const NumRotatableBondsOptions DefaultStrictDefinition = NonStrict;
#endif
}

const std::string NumRotatableBondsVersion = "3.0.0";
unsigned int calcNumRotatableBonds(const ROMol &mol, NumRotatableBondsOptions strict) {
  if (strict == Default)
    strict = DefaultStrictDefinition;
  
  if (strict == NonStrict) {
    std::string pattern = "[!$(*#*)&!D1]-&!@[!$(*#*)&!D1]";
    pattern_flyweight m(pattern);
    return m.get().countMatches(mol);
  }
  else if (strict==Strict) {
    std::string strict_pattern =
        "[!$(*#*)&!D1&!$(C(F)(F)F)&!$(C(Cl)(Cl)Cl)&!$(C(Br)(Br)Br)&!$(C([CH3])("
        "[CH3])[CH3])&!$([CD3](=[N,O,S])-!@[#7,O,S!D1])&!$([#7,O,S!D1]-!@[CD3]="
        "[N,O,S])&!$([CD3](=[N+])-!@[#7!D1])&!$([#7!D1]-!@[CD3]=[N+])]-!@[!$(*#"
        "*)&!D1&!$(C(F)(F)F)&!$(C(Cl)(Cl)Cl)&!$(C(Br)(Br)Br)&!$(C([CH3])([CH3])"
        "[CH3])]";
    pattern_flyweight m(strict_pattern);
    return m.get().countMatches(mol);
  } else {
    // Major changes in definition relative to the original GPS calculator:
    //   Bonds linking ring systems:
    //     - Single bonds between aliphatic ring Cs are always rotatable. This
    //     means that the
    //       central bond in CC1CCCC(C)C1-C1C(C)CCCC1C is now considered
    //       rotatable; it was not
    //       before.
    //     - Heteroatoms in the linked rings no longer affect whether or not the
    //     linking bond
    //       is rotatable
    //     - the linking bond in systems like Cc1cccc(C)c1-c1c(C)cccc1 is now
    //     considered
    //       non-rotatable
    pattern_flyweight rotBonds_matcher("[!$([D1&!#1])]-!@[!$([D1&!#1])]");
    pattern_flyweight nonRingAmides_matcher("[C&!R](=O)NC");
    pattern_flyweight symRings_matcher(
        "[a;r6;$(a(-!@[a;r6])(a[!#1])a[!#1])]-!@[a;r6;$(a(-!@[a;r6])(a[!#1])a)]");
    pattern_flyweight terminalTripleBonds_matcher("C#[#6,#7]");

    std::vector<MatchVectType> matches;

    // initialize to the number of bonds matching the base pattern:
    int res = rotBonds_matcher.get().countMatches(mol);

    // remove symmetrical rings:
    res -= symRings_matcher.get().countMatches(mol);
    if (res < 0) res = 0;

    // remove triple bonds
    res -= terminalTripleBonds_matcher.get().countMatches(mol);
    if (res < 0) res = 0;

    // removing amides is more complex
    boost::dynamic_bitset<> atomsSeen(mol.getNumAtoms());
    SubstructMatch(mol, *(nonRingAmides_matcher.get().getMatcher()), matches);
    BOOST_FOREACH (const MatchVectType &iv, matches) {
      bool distinct = true;
      for (const auto &mIt : iv) {
        if (atomsSeen[mIt.second]) {
          distinct = false;
        }
        atomsSeen.set(mIt.second);
      }
      if (distinct && res > 0) --res;
    }

    if (res < 0) res = 0;
    return static_cast<unsigned int>(res);
  }
}

unsigned int calcNumRotatableBonds(const ROMol &mol, bool strict) {
  return calcNumRotatableBonds(mol,
                               (strict) ? Strict
                                        : NonStrict );
}

// SMARTSCOUNTFUNC(NumHBD,
// "[$([N;!H0;v3]),$([N;!H0;+1;v4]),$([O,S;H1;+0]),$([n;H1;+0])]","2.0.1" ) ;
SMARTSCOUNTFUNC(NumHBD, "[N&!H0&v3,N&!H0&+1&v4,O&H1&+0,S&H1&+0,n&H1&+0]",
                "2.0.1");
SMARTSCOUNTFUNC(NumHBA,
                "[$([O,S;H1;v2]-[!$(*=[O,N,P,S])]),$([O,S;H0;v2]),$([O,S;-]),$("
                "[N;v3;!$(N-*=!@[O,N,P,S])]),$([nH0,o,s;+0])]",
                "2.0.1");
SMARTSCOUNTFUNC(NumHeteroatoms, "[!#6;!#1]", "1.0.1");
SMARTSCOUNTFUNC(NumAmideBonds, "C(=[O;!R])N", "1.0.0");

const std::string NumRingsVersion = "1.0.1";
unsigned int calcNumRings(const ROMol &mol) {
  return mol.getRingInfo()->numRings();
}

const std::string FractionCSP3Version = "1.0.0";
double calcFractionCSP3(const ROMol &mol) {
  unsigned int nCSP3 = 0;
  unsigned int nC = 0;
  ROMol::VERTEX_ITER atBegin, atEnd;
  boost::tie(atBegin, atEnd) = mol.getVertices();
  while (atBegin != atEnd) {
    const Atom* at = mol[*atBegin];
    if (at->getAtomicNum() == 6) {
      ++nC;
      if (at->getTotalDegree() == 4) {
        ++nCSP3;
      }
    }
    ++atBegin;
  }
  if (!nC) return 0;
  return static_cast<double>(nCSP3) / nC;
}

const std::string NumHeterocyclesVersion = "1.0.0";
unsigned int calcNumHeterocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->atomRings()) {
    BOOST_FOREACH (int i, iv) {
      if (mol.getAtomWithIdx(i)->getAtomicNum() != 6) {
        ++res;
        break;
      }
    }
  }
  return res;
}
const std::string NumAromaticRingsVersion = "1.0.0";
unsigned int calcNumAromaticRings(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    ++res;
    BOOST_FOREACH (int i, iv) {
      if (!mol.getBondWithIdx(i)->getIsAromatic()) {
        --res;
        break;
      }
    }
  }
  return res;
}
const std::string NumSaturatedRingsVersion = "1.0.0";
unsigned int calcNumSaturatedRings(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    ++res;
    BOOST_FOREACH (int i, iv) {
      if (mol.getBondWithIdx(i)->getBondType() != Bond::SINGLE ||
          mol.getBondWithIdx(i)->getIsAromatic()) {
        --res;
        break;
      }
    }
  }
  return res;
}
const std::string NumAliphaticRingsVersion = "1.0.0";
unsigned int calcNumAliphaticRings(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    BOOST_FOREACH (int i, iv) {
      if (!mol.getBondWithIdx(i)->getIsAromatic()) {
        ++res;
        break;
      }
    }
  }
  return res;
}
const std::string NumAromaticHeterocyclesVersion = "1.0.0";
unsigned int calcNumAromaticHeterocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    bool countIt = false;
    BOOST_FOREACH (int i, iv) {
      if (!mol.getBondWithIdx(i)->getIsAromatic()) {
        countIt = false;
        break;
      }
      // we're checking each atom twice, which is kind of doofy, but this
      // function is hopefully not going to be a big time sink.
      if (!countIt &&
          (mol.getBondWithIdx(i)->getBeginAtom()->getAtomicNum() != 6 ||
           mol.getBondWithIdx(i)->getEndAtom()->getAtomicNum() != 6)) {
        countIt = true;
      }
    }
    if (countIt) ++res;
  }
  return res;
}
const std::string NumAromaticCarbocyclesVersion = "1.0.0";
unsigned int calcNumAromaticCarbocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    bool countIt = true;
    BOOST_FOREACH (int i, iv) {
      if (!mol.getBondWithIdx(i)->getIsAromatic()) {
        countIt = false;
        break;
      }
      // we're checking each atom twice, which is kind of doofy, but this
      // function is hopefully not going to be a big time sync.
      if (mol.getBondWithIdx(i)->getBeginAtom()->getAtomicNum() != 6 ||
          mol.getBondWithIdx(i)->getEndAtom()->getAtomicNum() != 6) {
        countIt = false;
        break;
      }
    }
    if (countIt) ++res;
  }
  return res;
}
const std::string NumAliphaticHeterocyclesVersion = "1.0.0";
unsigned int calcNumAliphaticHeterocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    bool hasAliph = false;
    bool hasHetero = false;
    BOOST_FOREACH (int i, iv) {
      if (!mol.getBondWithIdx(i)->getIsAromatic()) {
        hasAliph = true;
      }
      // we're checking each atom twice, which is kind of doofy, but this
      // function is hopefully not going to be a big time sink.
      if (!hasHetero &&
          (mol.getBondWithIdx(i)->getBeginAtom()->getAtomicNum() != 6 ||
           mol.getBondWithIdx(i)->getEndAtom()->getAtomicNum() != 6)) {
        hasHetero = true;
      }
    }
    if (hasHetero && hasAliph) ++res;
  }
  return res;
}
const std::string NumAliphaticCarbocyclesVersion = "1.0.0";
unsigned int calcNumAliphaticCarbocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    bool hasAliph = false;
    bool hasHetero = false;
    BOOST_FOREACH (int i, iv) {
      if (!mol.getBondWithIdx(i)->getIsAromatic()) {
        hasAliph = true;
      }
      // we're checking each atom twice, which is kind of doofy, but this
      // function is hopefully not going to be a big time sync.
      if (mol.getBondWithIdx(i)->getBeginAtom()->getAtomicNum() != 6 ||
          mol.getBondWithIdx(i)->getEndAtom()->getAtomicNum() != 6) {
        hasHetero = true;
        break;
      }
    }
    if (hasAliph && !hasHetero) ++res;
  }
  return res;
}
const std::string NumSaturatedHeterocyclesVersion = "1.0.0";
unsigned int calcNumSaturatedHeterocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    bool countIt = false;
    BOOST_FOREACH (int i, iv) {
      if (mol.getBondWithIdx(i)->getBondType() != Bond::SINGLE ||
          mol.getBondWithIdx(i)->getIsAromatic()) {
        countIt = false;
        break;
      }
      // we're checking each atom twice, which is kind of doofy, but this
      // function is hopefully not going to be a big time sync.
      if (!countIt &&
          (mol.getBondWithIdx(i)->getBeginAtom()->getAtomicNum() != 6 ||
           mol.getBondWithIdx(i)->getEndAtom()->getAtomicNum() != 6)) {
        countIt = true;
      }
    }
    if (countIt) ++res;
  }
  return res;
}
const std::string NumSaturatedCarbocyclesVersion = "1.0.0";
unsigned int calcNumSaturatedCarbocycles(const ROMol &mol) {
  unsigned int res = 0;
  BOOST_FOREACH (const INT_VECT &iv, mol.getRingInfo()->bondRings()) {
    bool countIt = true;
    BOOST_FOREACH (int i, iv) {
      if (mol.getBondWithIdx(i)->getBondType() != Bond::SINGLE ||
          mol.getBondWithIdx(i)->getIsAromatic()) {
        countIt = false;
        break;
      }
      // we're checking each atom twice, which is kind of doofy, but this
      // function is hopefully not going to be a big time sync.
      if (mol.getBondWithIdx(i)->getBeginAtom()->getAtomicNum() != 6 ||
          mol.getBondWithIdx(i)->getEndAtom()->getAtomicNum() != 6) {
        countIt = false;
        break;
      }
    }
    if (countIt) ++res;
  }
  return res;
}

const std::string NumSpiroAtomsVersion = "1.0.0";
unsigned int calcNumSpiroAtoms(const ROMol &mol,
                               std::vector<unsigned int> *atoms) {
  if (!mol.getRingInfo() || !mol.getRingInfo()->isInitialized()) {
    MolOps::findSSSR(mol);
  }
  const RingInfo *rInfo = mol.getRingInfo();
  std::vector<unsigned int> lAtoms;
  if (!atoms) atoms = &lAtoms;

  for (unsigned int i = 0; i < rInfo->atomRings().size(); ++i) {
    const INT_VECT &ri = rInfo->atomRings()[i];
    for (unsigned int j = i + 1; j < rInfo->atomRings().size(); ++j) {
      const INT_VECT &rj = rInfo->atomRings()[j];
      // EFF: using intersect here does more work and memory allocation than is
      // required
      INT_VECT inter;
      Intersect(ri, rj, inter);
      if (inter.size() == 1) {
        if (std::find(atoms->begin(), atoms->end(), inter[0]) == atoms->end()) {
          atoms->push_back(inter[0]);
        }
      }
    }
  }
  return atoms->size();
}

const std::string NumBridgeheadAtomsVersion = "1.0.0";
unsigned int calcNumBridgeheadAtoms(const ROMol &mol,
                                    std::vector<unsigned int> *atoms) {
  if (!mol.getRingInfo() || !mol.getRingInfo()->isInitialized()) {
    MolOps::findSSSR(mol);
  }
  const RingInfo *rInfo = mol.getRingInfo();
  std::vector<unsigned int> lAtoms;
  if (!atoms) atoms = &lAtoms;

  for (unsigned int i = 0; i < rInfo->bondRings().size(); ++i) {
    const INT_VECT &ri = rInfo->bondRings()[i];
    for (unsigned int j = i + 1; j < rInfo->bondRings().size(); ++j) {
      const INT_VECT &rj = rInfo->bondRings()[j];
      // EFF: using intersect here does more work and memory allocation than is
      // required
      INT_VECT inter;
      Intersect(ri, rj, inter);
      if (inter.size() > 1) {
        INT_VECT atomCounts(mol.getNumAtoms(), 0);
        BOOST_FOREACH (int ii, inter) {
          atomCounts[mol.getBondWithIdx(ii)->getBeginAtomIdx()] += 1;
          atomCounts[mol.getBondWithIdx(ii)->getEndAtomIdx()] += 1;
        }
        for (unsigned int ti = 0; ti < atomCounts.size(); ++ti) {
          if (atomCounts[ti] == 1) {
            if (std::find(atoms->begin(), atoms->end(), ti) == atoms->end()) {
              atoms->push_back(ti);
            }
          }
        }
      }
    }
  }
  return atoms->size();
}

namespace {
bool hasStereoAssigned(const ROMol &mol) {
  return mol.hasProp(common_properties::_StereochemDone);
}
}
const std::string NumAtomStereoCentersVersion = "1.0.0";
unsigned int numAtomStereoCenters(const ROMol &mol) {
  if(!hasStereoAssigned(mol))
    throw ValueErrorException("numStereoCenters called without stereo being assigned");
  
  unsigned int res=0;
  for (ROMol::ConstAtomIterator atom = mol.beginAtoms(); atom != mol.endAtoms();
       ++atom) {
    if ((*atom)->hasProp(common_properties::_ChiralityPossible)) {
      res++;
    }
  }
  return res;
}

const std::string NumUnspecifiedAtomStereoCentersVersion = "1.0.0";
unsigned int numUnspecifiedAtomStereoCenters(const ROMol &mol) {
  if(!hasStereoAssigned(mol))
    throw ValueErrorException("numUnspecifiedStereoCenters called without stereo being assigned");

  unsigned int res=0;
  for (ROMol::ConstAtomIterator atom = mol.beginAtoms(); atom != mol.endAtoms();
       ++atom) {
    if ((*atom)->hasProp(common_properties::_ChiralityPossible) &&
        (*atom)->getChiralTag() == Atom::CHI_UNSPECIFIED) {
      res++;
    }
  }
  return res;
}

}  // end of namespace Descriptors
}  // end of namespace RDKit