File: PatternFingerprints.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (376 lines) | stat: -rw-r--r-- 12,438 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
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
//
//  Copyright (C) 2013-2020 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 <GraphMol/RDKitBase.h>
#include <GraphMol/MolBundle.h>
#include <GraphMol/QueryOps.h>
#include <DataStructs/ExplicitBitVect.h>
#include <DataStructs/BitOps.h>
#include "Fingerprints.h"
#include <GraphMol/Subgraphs/Subgraphs.h>
#include <GraphMol/Subgraphs/SubgraphUtils.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <RDGeneral/Invariant.h>
#include <boost/random.hpp>
#include <climits>
#include <cstdint>
#include <RDGeneral/hash/hash.hpp>
#include <RDGeneral/types.h>
#include <algorithm>
#include <boost/dynamic_bitset.hpp>

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

// #define VERBOSE_FINGERPRINTING 1

namespace {
class ss_matcher {
 public:
  ss_matcher() {};
  ss_matcher(const std::string &pattern) {
    RDKit::RWMol *p = RDKit::SmartsToMol(pattern);
    TEST_ASSERT(p);
    m_matcher.reset(p);
  };

  // const RDKit::ROMOL_SPTR &getMatcher() const { return m_matcher; };
  const RDKit::ROMol *getMatcher() const { return m_matcher.get(); };

 private:
  RDKit::ROMOL_SPTR m_matcher;
};
}  // namespace

namespace RDKit {
const char *pqs[] = {
    "[*]~[*]", "[*]~[*]~[*]", "[R]~1~[R]~[R]~1",
    //"[*]~[*]~[*]~[*]",
    "[*]~[*](~[*])~[*]",
    //"[*]~[R]~1[R]~[R]~1",
    "[R]~1[R]~[R]~[R]~1",
    //"[*]~[*]~[*]~[*]~[*]",
    "[*]~[*]~[*](~[*])~[*]",
    //"[*]~[R]~1[R]~[R]~1~[*]",
    "[R]~1~[R]~[R]~[R]~[R]~1", "[R]~1~[R]~[R]~[R]~[R]~[R]~1",
    //"[R2]~[R1]~[R2]", Github #151: can't have ring counts in an SSS pattern
    //"[R2]~[R1]~[R1]~[R2]",  Github #151: can't have ring counts in an SSS
    // pattern
    "[R](@[R])(@[R])~[R]~[R](@[R])(@[R])",
    "[R](@[R])(@[R])~[R]@[R]~[R](@[R])(@[R])",

    //"[*]!@[R]~[R]!@[*]",  Github #151: can't have !@ in an SSS pattern
    //"[*]!@[R]~[R]~[R]!@[*]", Github #151: can't have !@ in an SSS pattern
    "[*]~[R](@[R])@[R](@[R])~[*]", "[*]~[R](@[R])@[R]@[R](@[R])~[*]",
    "[*]",  // single atom fragment
    ""};
typedef boost::flyweight<boost::flyweights::key_value<std::string, ss_matcher>,
                         boost::flyweights::no_tracking>
    pattern_flyweight;

namespace detail {
void getAtomNumbers(const Atom *a, std::vector<int> &atomNums) {
  atomNums.clear();
  if (!a->hasQuery()) {
    atomNums.push_back(a->getAtomicNum());
    return;
  }
  // negated things are always complex:
  if (a->getQuery()->getNegation()) {
    return;
  }
  std::string descr = a->getQuery()->getDescription();
  if (descr == "AtomAtomicNum") {
    atomNums.push_back(
        static_cast<ATOM_EQUALS_QUERY *>(a->getQuery())->getVal());
  } else if (descr == "AtomXor") {
    return;
  } else if (descr == "AtomAnd") {
    auto childIt = a->getQuery()->beginChildren();
    if ((*childIt)->getDescription() == "AtomAtomicNum" &&
        ((*(childIt + 1))->getDescription() == "AtomIsAliphatic" ||
         (*(childIt + 1))->getDescription() == "AtomIsAromatic") &&
        (childIt + 2) == a->getQuery()->endChildren()) {
      atomNums.push_back(
          static_cast<ATOM_EQUALS_QUERY *>((*childIt).get())->getVal());
      return;
    }
  } else if (descr == "AtomOr") {
    auto childIt = a->getQuery()->beginChildren();
    while (childIt != a->getQuery()->endChildren()) {
      if ((*childIt)->getDescription() == "AtomAtomicNum") {
        atomNums.push_back(
            static_cast<ATOM_EQUALS_QUERY *>((*childIt).get())->getVal());
      } else if ((*childIt)->getDescription() == "AtomAnd") {
        auto childIt2 = (*childIt)->beginChildren();
        if ((*childIt2)->getDescription() == "AtomAtomicNum" &&
            ((*(childIt2 + 1))->getDescription() == "AtomIsAliphatic" ||
             (*(childIt2 + 1))->getDescription() == "AtomIsAromatic") &&
            (childIt2 + 2) == (*childIt)->endChildren()) {
          atomNums.push_back(
              static_cast<ATOM_EQUALS_QUERY *>((*childIt2).get())->getVal());
        } else {
          atomNums.clear();
          return;
        }
      } else {
        atomNums.clear();
        return;
      }
      ++childIt;
    }
  }
  return;
}
}  // namespace detail

namespace {

bool isPatternComplexQuery(const Bond *b) {
  if (!b->hasQuery()) {
    return false;
  }
  // negated things are always complex:
  if (b->getQuery()->getNegation()) {
    return true;
  }
  std::string descr = b->getQuery()->getDescription();
  // std::cerr<<"   !!!!!! "<<b->getIdx()<<"
  // "<<b->getBeginAtomIdx()<<"-"<<b->getEndAtomIdx()<<" "<<descr<<std::endl;
  return descr != "BondOrder";
}

bool isTautomerBondQuery(const Bond *b) {
  // assumes we have already tested true for isPatternComplexQuery
  auto description = b->getQuery()->getDescription();
  return description == "SingleOrDoubleOrAromaticBond" ||
         description == "SingleOrAromaticBond";
}

void updatePatternFingerprint(const ROMol &mol, ExplicitBitVect &fp,
                              unsigned int fpSize,
                              std::vector<unsigned int> *atomCounts,
                              ExplicitBitVect *setOnlyBits,
                              bool tautomericFingerprint) {
  PRECONDITION(fpSize != 0, "fpSize==0");
  PRECONDITION(!atomCounts || atomCounts->size() >= mol.getNumAtoms(),
               "bad atomCounts size");
  PRECONDITION(!setOnlyBits || setOnlyBits->getNumBits() == fpSize,
               "bad setOnlyBits size");

  std::vector<const ROMol *> patts;
  patts.reserve(10);
  unsigned int idx = 0;
  while (1) {
    std::string pq = pqs[idx];
    if (pq == "") {
      break;
    }
    ++idx;
    const ROMol *matcher = pattern_flyweight(pq).get().getMatcher();
    CHECK_INVARIANT(matcher, "bad smarts");
    patts.push_back(matcher);
  }

  if (!mol.getRingInfo()->isFindFastOrBetter()) {
    MolOps::fastFindRings(mol);
  }

  boost::dynamic_bitset<> isQueryAtom(mol.getNumAtoms()),
      isQueryBond(mol.getNumBonds()), isTautomerBond(mol.getNumBonds());
  for (const auto at : mol.atoms()) {
    // isComplexQuery() no longer considers "AtomNull" to be complex, but for
    // the purposes of the pattern FP, it definitely needs to be treated as a
    // query feature.
    if (at->hasQuery() && (at->getQuery()->getDescription() == "AtomNull" ||
                           isComplexQuery(at))) {
      isQueryAtom.set(at->getIdx());
    }
  }

  for (const auto bond : mol.bonds()) {
    if (isPatternComplexQuery(bond)) {
      isQueryBond.set(bond->getIdx());
      if (tautomericFingerprint && isTautomerBondQuery(bond)) {
        isTautomerBond.set(bond->getIdx());
      }
    }
  }

  unsigned int pIdx = 0;
  for (const auto patt : patts) {
    ++pIdx;
    std::vector<MatchVectType> matches;
    // uniquify matches?
    //   time for 10K molecules w/ uniquify: 5.24s
    //   time for 10K molecules w/o uniquify: 4.87s

    SubstructMatchParameters params;
    params.uniquify = false;
    // raise maxMatches really high. This was the cause for github #2614.
    // if we end up with more matches than this, we're completely hosed: :-)
    params.maxMatches = 100000000;
    matches = SubstructMatch(mol, *patt, params);

    std::uint32_t mIdx = pIdx + patt->getNumAtoms() + patt->getNumBonds();
    for (const auto &mv : matches) {
#ifdef VERBOSE_FINGERPRINTING
      std::cerr << "\nPatt: " << pIdx << " | ";
#endif
      // collect bits counting the number of occurrences of the pattern:
      gboost::hash_combine(mIdx, 0xBEEF);
      fp.setBit(mIdx % fpSize);
#ifdef VERBOSE_FINGERPRINTING
      std::cerr << "count: " << mIdx % fpSize << " | ";
#endif

      bool isQuery = false;
      std::uint32_t bitId = pIdx;
      std::vector<unsigned int> amap(mv.size(), 0);
      for (const auto &p : mv) {
#ifdef VERBOSE_FINGERPRINTING
        std::cerr << p.second << " ";
#endif
        if (isQueryAtom[p.second]) {
          isQuery = true;
#ifdef VERBOSE_FINGERPRINTING
          std::cerr << "atom query.";
#endif
          break;
        }
        gboost::hash_combine(bitId,
                             mol.getAtomWithIdx(p.second)->getAtomicNum());
        amap[p.first] = p.second;
      }
      if (isQuery) {
        continue;
      }
      auto tautomerBitId = bitId;
      auto tautomerQuery = false;
      ROMol::EDGE_ITER firstB, lastB;
      boost::tie(firstB, lastB) = patt->getEdges();
#ifdef VERBOSE_FINGERPRINTING
      std::cerr << " bs:|| ";
#endif
      while (!isQuery && firstB != lastB) {
        const Bond *pbond = (*patt)[*firstB];
        ++firstB;
        const Bond *mbond = mol.getBondBetweenAtoms(
            amap[pbond->getBeginAtomIdx()], amap[pbond->getEndAtomIdx()]);
        const auto bondIdx = mbond->getIdx();

        if (isQueryBond[bondIdx]) {
          isQuery = true;
          if (isTautomerBond[bondIdx]) {
            isQuery = false;
            tautomerQuery = true;
#ifdef VERBOSE_FINGERPRINTING
            std::cerr << "tautomer query: " << mbond->getIdx();
#endif
          }
          if (isQuery) {
#ifdef VERBOSE_FINGERPRINTING
            std::cerr << "bond query: " << mbond->getIdx();
#endif
            break;
          }
        }

        if (tautomericFingerprint) {
          if (isTautomerBond[bondIdx] || mbond->getIsAromatic() ||
              mbond->getBondType() == Bond::SINGLE ||
              mbond->getBondType() == Bond::DOUBLE ||
              mbond->getBondType() == Bond::AROMATIC) {
            gboost::hash_combine(tautomerBitId, -1);
#ifdef VERBOSE_FINGERPRINTING
            std::cerr << "T ";
#endif
          }
        }

        if (!tautomerQuery) {
          if (!mbond->getIsAromatic()) {
            gboost::hash_combine(bitId, (std::uint32_t)mbond->getBondType());
#ifdef VERBOSE_FINGERPRINTING
            std::cerr << mbond->getBondType() << " ";
#endif
          } else {
            gboost::hash_combine(bitId, (std::uint32_t)Bond::AROMATIC);
#ifdef VERBOSE_FINGERPRINTING
            std::cerr << Bond::AROMATIC << " ";
#endif
          }
        }
      }

      if (!isQuery) {
        if (!tautomerQuery) {
#ifdef VERBOSE_FINGERPRINTING
          std::cerr << " set: " << bitId << " " << bitId % fpSize;
#endif
          fp.setBit(bitId % fpSize);
        }
        if (tautomericFingerprint) {
#ifdef VERBOSE_FINGERPRINTING
          std::cerr << " tset: " << tautomerBitId << " "
                    << tautomerBitId % fpSize;
#endif
          fp.setBit(tautomerBitId % fpSize);
        }
      }
    }
  }
}

}  // namespace

// caller owns the result, it must be deleted
ExplicitBitVect *PatternFingerprintMol(const ROMol &mol, unsigned int fpSize,
                                       std::vector<unsigned int> *atomCounts,
                                       ExplicitBitVect *setOnlyBits,
                                       bool tautomericFingerprint) {
  PRECONDITION(fpSize != 0, "fpSize==0");
  PRECONDITION(!atomCounts || atomCounts->size() >= mol.getNumAtoms(),
               "bad atomCounts size");
  PRECONDITION(!setOnlyBits || setOnlyBits->getNumBits() == fpSize,
               "bad setOnlyBits size");
  auto *res = new ExplicitBitVect(fpSize);
  updatePatternFingerprint(mol, *res, fpSize, atomCounts, setOnlyBits,
                           tautomericFingerprint);
  return res;
}

// caller owns the result, it must be deleted
ExplicitBitVect *PatternFingerprintMol(const MolBundle &bundle,
                                       unsigned int fpSize,
                                       ExplicitBitVect *setOnlyBits,
                                       bool tautomericFingerprint) {
  PRECONDITION(fpSize != 0, "fpSize==0");
  PRECONDITION(!setOnlyBits || setOnlyBits->getNumBits() == fpSize,
               "bad setOnlyBits size");
  ExplicitBitVect *res = nullptr;
  for (const auto &molp : bundle.getMols()) {
    ExplicitBitVect molfp(fpSize);
    updatePatternFingerprint(*molp, molfp, fpSize, nullptr, setOnlyBits,
                             tautomericFingerprint);
    if (!res) {
      res = new ExplicitBitVect(molfp);
    } else {
      (*res) &= molfp;
    }
  }
  return res;
}

}  // namespace RDKit