File: rdGeneralizedSubstruct.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 (157 lines) | stat: -rw-r--r-- 5,406 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
//
//  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 <RDBoost/python.h>
#include <GraphMol/GraphMol.h>
#include <RDBoost/Wrap.h>

#include <GraphMol/GeneralizedSubstruct/XQMol.h>
#include <GraphMol/Wrap/substructmethods.h>

namespace python = boost::python;
using namespace RDKit;
using namespace RDKit::GeneralizedSubstruct;

namespace {

python::object XQMolToBinary(const ExtendedQueryMol &self) {
  std::string res;
  {
    NOGIL gil;
    res = self.toBinary();
  }
  python::object retval = python::object(
      python::handle<>(PyBytes_FromStringAndSize(res.c_str(), res.length())));
  return retval;
}

bool hasSubstructHelper(const ROMol &mol, const ExtendedQueryMol &query,
                        SubstructMatchParameters *iparams) {
  SubstructMatchParameters params;
  if (iparams) {
    params = *iparams;
  }
  bool res;
  {
    NOGIL gil;
    res = hasSubstructMatch(mol, query, params);
  }
  return res;
}

PyObject *getSubstructHelper(const ROMol &mol, const ExtendedQueryMol &query,
                             SubstructMatchParameters *iparams) {
  std::vector<MatchVectType> matches;
  SubstructMatchParameters params;
  if (iparams) {
    params = *iparams;
  }
  {
    NOGIL gil;
    params.maxMatches = 1;
    matches = SubstructMatch(mol, query, params);
  }
  if (matches.empty()) {
    matches.push_back(MatchVectType());
  }
  return convertMatches(matches[0]);
}
PyObject *getSubstructsHelper(const ROMol &mol, const ExtendedQueryMol &query,
                              SubstructMatchParameters *iparams) {
  std::vector<MatchVectType> matches;
  SubstructMatchParameters params;
  if (iparams) {
    params = *iparams;
  }
  {
    NOGIL gil;
    matches = SubstructMatch(mol, query, params);
  }

  PyObject *res = PyTuple_New(matches.size());
  for (auto idx = 0u; idx < matches.size(); idx++) {
    PyTuple_SetItem(res, idx, convertMatches(matches[idx]));
  }
  return res;
}

ExtendedQueryMol *createExtendedQueryMolHelper(
    const ROMol &mol, bool doEnumeration, bool doTautomers,
    bool adjustQueryProperties, MolOps::AdjustQueryParameters *ps) {
  MolOps::AdjustQueryParameters defaults;
  if (!ps) {
    ps = &defaults;
  }
  return new ExtendedQueryMol(createExtendedQueryMol(
      mol, doEnumeration, doTautomers, adjustQueryProperties, *ps));
}

}  // namespace

BOOST_PYTHON_MODULE(rdGeneralizedSubstruct) {
  python::scope().attr("__doc__") =
      "Module containing functions for generalized substructure searching";

  python::class_<ExtendedQueryMol, boost::noncopyable>(
      "ExtendedQueryMol",
      "Extended query molecule for use in generalized substructure searching.",
      python::init<const std::string &, bool>(
          (python::args("self"), python::args("text"),
           python::args("isJSON") = false),
          "constructor from either a binary string (from ToBinary()) or a JSON string."))
      .def("InitFromBinary", &ExtendedQueryMol::initFromBinary,
           python::args("self", "pkl"))
      .def("InitFromJSON", &ExtendedQueryMol::initFromJSON,
           python::args("self", "text"))
      .def("ToBinary", XQMolToBinary, python::args("self"))
      .def("ToJSON", &ExtendedQueryMol::toJSON, python::args("self"))
      .def("PatternFingerprintQuery",
           &ExtendedQueryMol::patternFingerprintQuery,
           (python::arg("self"), python::arg("fingerprintSize") = 2048));

  python::def(
      "MolHasSubstructMatch", &hasSubstructHelper,
      (python::arg("mol"), python::arg("query"),
       python::arg("params") = python::object()),
      "determines whether or not a molecule is a match to a generalized substructure query");
  python::def(
      "MolGetSubstructMatch", &getSubstructHelper,
      (python::arg("mol"), python::arg("query"),
       python::arg("params") = python::object()),
      "returns first match (if any) of a molecule to a generalized substructure query");
  python::def(
      "MolGetSubstructMatches", &getSubstructsHelper,
      (python::arg("mol"), python::arg("query"),
       python::arg("params") = python::object()),
      "returns all matches (if any) of a molecule to a generalized substructure query");

  python::def(
      "PatternFingerprintTarget", &patternFingerprintTargetMol,
      (python::arg("target"), python::arg("fingerprintSize") = 2048),
      "Creates a pattern fingerprint for a target molecule that is compatible with an extended query");

  python::def("CreateExtendedQueryMol", createExtendedQueryMolHelper,
              (python::arg("mol"), python::arg("doEnumeration") = true,
               python::arg("doTautomers") = true,
               python::arg("adjustQueryProperties") = false,
               python::arg("adjustQueryParameters") = python::object()),
              python::return_value_policy<python::manage_new_object>(),
              R"DOC(Creates an ExtendedQueryMol from the input molecule

  This takes a query molecule and, conceptually, performs the following steps to
  produce an ExtendedQueryMol:

    1. Enumerates features like Link Nodes and SRUs
    2. Converts everything into TautomerQueries
    3. Runs adjustQueryProperties()

  Each step is optional
)DOC");
}