File: XQMol.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 (204 lines) | stat: -rw-r--r-- 6,992 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
//
//  Copyright (c) 2023, Greg Landrum and other RDKit contributors
//
//   @@ 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 <cstdint>
#include <variant>
#include <sstream>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/MolBundle.h>
#include <GraphMol/MolEnumerator/MolEnumerator.h>

#include "XQMol.h"

#include "GraphMol/Fingerprints/Fingerprints.h"

namespace RDKit {
namespace GeneralizedSubstruct {

ExtendedQueryMol::ExtendedQueryMol(const std::string &text, bool isJSON) {
  if (!isJSON) {
    initFromBinary(text);
  } else {
    initFromJSON(text);
  }
}

void ExtendedQueryMol::initFromOther(const ExtendedQueryMol &other) {
  if (std::holds_alternative<ExtendedQueryMol::RWMol_T>(other.xqmol)) {
    xqmol = std::make_unique<RWMol>(
        *std::get<ExtendedQueryMol::RWMol_T>(other.xqmol));
  } else if (std::holds_alternative<ExtendedQueryMol::MolBundle_T>(
                 other.xqmol)) {
    xqmol = std::make_unique<MolBundle>(
        *std::get<ExtendedQueryMol::MolBundle_T>(other.xqmol));
  } else if (std::holds_alternative<ExtendedQueryMol::TautomerQuery_T>(
                 other.xqmol)) {
    xqmol = std::make_unique<TautomerQuery>(
        *std::get<ExtendedQueryMol::TautomerQuery_T>(other.xqmol));
  } else if (std::holds_alternative<ExtendedQueryMol::TautomerBundle_T>(
                 other.xqmol)) {
    auto tb = std::make_unique<std::vector<std::unique_ptr<TautomerQuery>>>();
    for (const auto &tqp :
         *std::get<ExtendedQueryMol::TautomerBundle_T>(other.xqmol)) {
      tb->emplace_back(std::make_unique<TautomerQuery>(*tqp));
    }
    xqmol = std::move(tb);
  }
}

std::unique_ptr<ExplicitBitVect> ExtendedQueryMol::patternFingerprintQuery(
    unsigned fpSize) const {
  if (std::holds_alternative<RWMol_T>(xqmol)) {
    const auto raw = PatternFingerprintMol(*std::get<RWMol_T>(xqmol), fpSize,
                                           nullptr, nullptr, true);
    std::unique_ptr<ExplicitBitVect> ptr(raw);
    return ptr;
  }
  if (std::holds_alternative<MolBundle_T>(xqmol)) {
    const auto raw = PatternFingerprintMol(*std::get<MolBundle_T>(xqmol),
                                           fpSize, nullptr, true);
    std::unique_ptr<ExplicitBitVect> ptr(raw);
    return ptr;
  }
  if (std::holds_alternative<TautomerQuery_T>(xqmol)) {
    const auto raw =
        std::get<TautomerQuery_T>(xqmol)->patternFingerprintTemplate(fpSize);
    std::unique_ptr<ExplicitBitVect> ptr(raw);
    return ptr;
  }
  if (std::holds_alternative<TautomerBundle_T>(xqmol)) {
    const auto &tautomerBundle = std::get<TautomerBundle_T>(xqmol);
    ExplicitBitVect *res = nullptr;
    for (const auto &tautomer : *tautomerBundle) {
      const auto molfp = tautomer->patternFingerprintTemplate(fpSize);
      if (!res) {
        res = molfp;
      } else {
        *res &= *molfp;
        delete molfp;
      }
    }
    std::unique_ptr<ExplicitBitVect> ptr(res);
    return ptr;
  }

  throw std::invalid_argument("Unknown extended query molecule type");
}

std::vector<MatchVectType> SubstructMatch(
    const ROMol &mol, const ExtendedQueryMol &query,
    const SubstructMatchParameters &params) {
  std::vector<MatchVectType> res;
  if (std::holds_alternative<ExtendedQueryMol::RWMol_T>(query.xqmol)) {
    res = RDKit::SubstructMatch(
        mol, *std::get<ExtendedQueryMol::RWMol_T>(query.xqmol), params);
#ifdef RDK_USE_BOOST_SERIALIZATION
  } else if (std::holds_alternative<ExtendedQueryMol::MolBundle_T>(
                 query.xqmol)) {
    res = RDKit::SubstructMatch(
        mol, *std::get<ExtendedQueryMol::MolBundle_T>(query.xqmol), params);
  } else if (std::holds_alternative<ExtendedQueryMol::TautomerQuery_T>(
                 query.xqmol)) {
    res = std::get<ExtendedQueryMol::TautomerQuery_T>(query.xqmol)
              ->substructOf(mol, params);
  } else if (std::holds_alternative<ExtendedQueryMol::TautomerBundle_T>(
                 query.xqmol)) {
    const auto &vect =
        std::get<ExtendedQueryMol::TautomerBundle_T>(query.xqmol);
    for (const auto &tq : *vect) {
      res = tq->substructOf(mol, params);
      if (!res.empty()) {
        break;
      }
    }
#endif
  } else {
    UNDER_CONSTRUCTION("unrecognized type in ExtendedQueryMol");
  }
  return res;
}

ExtendedQueryMol createExtendedQueryMol(const RWMol &mol, bool doEnumeration,
                                        bool doTautomers,
                                        bool adjustQueryProperties,
                                        MolOps::AdjustQueryParameters params) {
  MolBundle bndl;
  if (doEnumeration) {
    bndl = MolEnumerator::enumerate(mol);
  }
  if (bndl.empty()) {
    // nothing enumerated, just add the input molecule
    bndl.addMol(boost::shared_ptr<ROMol>(new ROMol(mol)));
  }

  if (bndl.size() == 1) {
    const ROMol *lmol = nullptr;
    std::unique_ptr<ROMol> holder;
    if (adjustQueryProperties) {
      holder.reset(MolOps::adjustQueryProperties(*bndl.getMol(0), &params));
      lmol = holder.get();
    } else {
      lmol = bndl.getMol(0).get();
    }
    if (doTautomers) {
      auto tq = std::unique_ptr<TautomerQuery>(TautomerQuery::fromMol(*lmol));
      if (tq->getTautomers().size() == 1) {
        // no tautomers, just one molecule, return the molecule:
        return {std::make_unique<RWMol>(*lmol)};
      } else {
        // return the tautomers
        return {std::move(tq)};
      }
    } else {
      return {std::make_unique<RWMol>(*lmol)};
    }
  } else {
    MolBundle lbndl;
    for (auto &bmol : bndl.getMols()) {
      if (adjustQueryProperties) {
        boost::shared_ptr<ROMol> lmol(
            MolOps::adjustQueryProperties(*bmol, &params));
        lbndl.addMol(lmol);
      } else {
        lbndl.addMol(bmol);
      }
    }
    bool hadTautomers = false;
    auto tautomerBundle =
        std::make_unique<std::vector<std::unique_ptr<TautomerQuery>>>(0);
    if (doTautomers) {
      for (const auto &bmol : lbndl.getMols()) {
        auto tq = std::unique_ptr<TautomerQuery>(TautomerQuery::fromMol(*bmol));
        if (tq->getTautomers().size() > 1) {
          hadTautomers = true;
        }
        tautomerBundle->emplace_back(std::move(tq));
      }
    }
    if (!hadTautomers) {
      // no tautomers, just return the bundle
      return {std::make_unique<MolBundle>(lbndl)};
    } else {
      // return the tautomer bundle
      return {std::move(tautomerBundle)};
    }
  }
}

std::unique_ptr<ExplicitBitVect> patternFingerprintTargetMol(const ROMol &mol,
                                                             unsigned fpSize) {
  const auto raw = PatternFingerprintMol(mol, fpSize, nullptr, nullptr, true);
  std::unique_ptr<ExplicitBitVect> ptr(raw);
  return ptr;
}
}  // namespace GeneralizedSubstruct
}  // namespace RDKit