File: Queries.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 (269 lines) | stat: -rw-r--r-- 12,848 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
//
//  Copyright (C) 2003-2017 Rational Discovery LLC and 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.
//
#define NO_IMPORT_ARRAY
#include <RDBoost/python.h>
#include <string>

#include <GraphMol/RDKitBase.h>
#include <GraphMol/RDKitQueries.h>
#include <RDGeneral/types.h>

namespace python = boost::python;
namespace RDKit {

/*
  NOTE: it looks like there is a typo in the below code
  ATOM_GREATER_QUERY is intentionally being used for the LessQueryAtom
  and ATOM_LESS_QUERY for GreaterQueryAtom in the python API.
  The C++ API is internally consistent and logical, but having
  AtomNumLessQueryAtom(6) return atoms where 6 is less than their atomic
  number feels backwards in Python.

 */
#define QAFUNC1(_funcname_, _func_, _typ_)                                 \
  QueryAtom *_funcname_##EqualsQueryAtom(_typ_ val, bool negate) {         \
    QueryAtom *res = new QueryAtom();                                      \
    res->setQuery(_func_(val));                                            \
    if (negate) res->getQuery()->setNegation(true);                        \
    return res;                                                            \
  }                                                                        \
  QueryAtom *_funcname_##LessQueryAtom(_typ_ val, bool negate) {           \
    QueryAtom *res = new QueryAtom();                                      \
    res->setQuery(                                                         \
        _func_<ATOM_GREATER_QUERY>(val, std::string(#_funcname_ "Less"))); \
    if (negate) res->getQuery()->setNegation(true);                        \
    return res;                                                            \
  }                                                                        \
  QueryAtom *_funcname_##GreaterQueryAtom(_typ_ val, bool negate) {        \
    QueryAtom *res = new QueryAtom();                                      \
    res->setQuery(                                                         \
        _func_<ATOM_LESS_QUERY>(val, std::string(#_funcname_ "Greater"))); \
    if (negate) res->getQuery()->setNegation(true);                        \
    return res;                                                            \
  }

#define QAFUNC2(_funcname_, _func_, _typ_)          \
  QueryAtom *_funcname_(bool negate) {              \
    QueryAtom *res = new QueryAtom();               \
    res->setQuery(_func_());                        \
    if (negate) res->getQuery()->setNegation(true); \
    return res;                                     \
  }
QAFUNC1(AtomNum, makeAtomNumQuery, int);
QAFUNC1(ExplicitValence, makeAtomExplicitValenceQuery, int);
QAFUNC1(TotalValence, makeAtomTotalValenceQuery, int);
QAFUNC1(ExplicitDegree, makeAtomExplicitDegreeQuery, int);
QAFUNC1(TotalDegree, makeAtomTotalDegreeQuery, int);

QAFUNC1(HCount, makeAtomHCountQuery, int);
QAFUNC1(Mass, makeAtomMassQuery, int);
QAFUNC1(Isotope, makeAtomIsotopeQuery, int);
QAFUNC1(FormalCharge, makeAtomFormalChargeQuery, int);

QAFUNC1(Hybridization, makeAtomHybridizationQuery, int);
QAFUNC1(InNRings, makeAtomInNRingsQuery, int);
QAFUNC1(MinRingSize, makeAtomMinRingSizeQuery, int);
QAFUNC1(RingBondCount, makeAtomRingBondCountQuery, int);
QAFUNC1(NumRadicalElectrons, makeAtomNumRadicalElectronsQuery, int);
QAFUNC1(NumHeteroatomNeighbors, makeAtomNumHeteroatomNbrsQuery, int);
QAFUNC1(NumAliphaticHeteroatomNeighbors,
        makeAtomNumAliphaticHeteroatomNbrsQuery, int);

QAFUNC2(IsUnsaturatedQueryAtom, makeAtomUnsaturatedQuery, int);
QAFUNC2(IsAromaticQueryAtom, makeAtomAromaticQuery, int);
QAFUNC2(IsAliphaticQueryAtom, makeAtomAliphaticQuery, int);
QAFUNC2(IsInRingQueryAtom, makeAtomInRingQuery, int);
QAFUNC2(HasChiralTagQueryAtom, makeAtomHasChiralTagQuery, int);
QAFUNC2(MissingChiralTagQueryAtom, makeAtomMissingChiralTagQuery, int);

QueryAtom *HasPropQueryAtom(const std::string &propname, bool negate) {
  auto *res = new QueryAtom();
  res->setQuery(makeHasPropQuery<Atom>(propname));
  if (negate) res->getQuery()->setNegation(true);
  return res;
}

QueryBond *HasPropQueryBond(const std::string &propname, bool negate) {
  auto *res = new QueryBond();
  res->setQuery(makeHasPropQuery<Bond>(propname));
  if (negate) res->getQuery()->setNegation(true);
  return res;
}

template <class Ob, class Ret, class T>
Ret *PropQuery(const std::string &propname, const T &v, bool negate) {
  auto *res = new Ret();
  res->setQuery(makePropQuery<Ob, T>(propname, v));
  if (negate) res->getQuery()->setNegation(true);
  return res;
}

template <class Ob, class Ret, class T>
Ret *PropQueryWithTol(const std::string &propname, const T &v, bool negate,
                      const T &tol = T()) {
  auto *res = new Ret();
  res->setQuery(makePropQuery<Ob, T>(propname, v, tol));
  if (negate) res->getQuery()->setNegation(true);
  return res;
}

struct queries_wrapper {
  static void wrap() {
#define QADEF1(_funcname_)                                                     \
  python::def(#_funcname_ "EqualsQueryAtom", _funcname_##EqualsQueryAtom,      \
              (python::arg("val"), python::arg("negate") = false),             \
              "Returns a QueryAtom that matches atoms where " #_funcname_      \
              " is equal to the target value.",                                \
              python::return_value_policy<python::manage_new_object>());       \
  python::def(                                                                 \
      #_funcname_ "LessQueryAtom", _funcname_##LessQueryAtom,                  \
      (python::arg("val"), python::arg("negate") = false),                     \
      "Returns a QueryAtom that matches atoms where " #_funcname_              \
      " is less than the target value.\n"                                      \
      "NOTE: the direction of comparison is reversed relative to the C++ API", \
      python::return_value_policy<python::manage_new_object>());               \
  python::def(                                                                 \
      #_funcname_ "GreaterQueryAtom", _funcname_##GreaterQueryAtom,            \
      (python::arg("val"), python::arg("negate") = false),                     \
      "Returns a QueryAtom that matches atoms where " #_funcname_              \
      " is equal to the target value.\n"                                       \
      "NOTE: the direction of comparison is reversed relative to the C++ API", \
      python::return_value_policy<python::manage_new_object>());
#define QADEF2(_funcname_)                                               \
  python::def(#_funcname_ "QueryAtom", _funcname_##QueryAtom,            \
              (python::arg("negate") = false),                           \
              "Returns a QueryAtom that matches atoms when " #_funcname_ \
              " is True.",                                               \
              python::return_value_policy<python::manage_new_object>());

    QADEF1(AtomNum);
    QADEF1(ExplicitValence);
    QADEF1(TotalValence);
    QADEF1(ExplicitDegree);
    QADEF1(TotalDegree);
    QADEF1(HCount);
    QADEF1(Mass);
    QADEF1(Isotope);
    QADEF1(FormalCharge);
    QADEF1(Hybridization);
    QADEF1(InNRings);
    QADEF1(MinRingSize);
    QADEF1(RingBondCount);
    QADEF1(NumRadicalElectrons)
    QADEF1(NumHeteroatomNeighbors)
    QADEF1(NumAliphaticHeteroatomNeighbors)

    QADEF2(IsUnsaturated);
    QADEF2(IsAromatic);
    QADEF2(IsAliphatic);
    QADEF2(IsInRing);
    QADEF2(HasChiralTag);
    QADEF2(MissingChiralTag);

    python::def("HasPropQueryAtom", HasPropQueryAtom,
                (python::arg("propname"), python::arg("negate") = false),
                "Returns a QueryAtom that matches when the propery 'propname' "
                "exists in the atom.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasPropQueryBond", HasPropQueryBond,
                (python::arg("propname"), python::arg("negate") = false),
                "Returns a QueryBond that matches when the propery 'propname' "
                "exists in the bond.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasIntPropWithValueQueryAtom",
                PropQueryWithTol<Atom, QueryAtom, int>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false, python::arg("tolerance") = 0),
                "Returns a QueryAtom that matches when the propery 'propname' "
                "has the specified int value.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasBoolPropWithValueQueryAtom",
                PropQuery<Atom, QueryAtom, bool>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false),
                "Returns a QueryAtom that matches when the propery 'propname' "
                "has the specified boolean"
                " value.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasStringPropWithValueQueryAtom",
                PropQuery<Atom, QueryAtom, std::string>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false),
                "Returns a QueryAtom that matches when the propery 'propname' "
                "has the specified string "
                "value.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasDoublePropWithValueQueryAtom",
                PropQueryWithTol<Atom, QueryAtom, double>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false, python::arg("tolerance") = 0.0),
                "Returns a QueryAtom that matches when the propery 'propname' "
                "has the specified "
                "value +- tolerance",
                python::return_value_policy<python::manage_new_object>());

    /////////////////////////////////////////////////////////////////////////////////////
    //  Bond Queries
    python::def("HasPropQueryBond", HasPropQueryBond,
                (python::arg("propname"), python::arg("negate") = false),
                "Returns a QueryBond that matches when the propery 'propname' "
                "exists in the bond.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasPropQueryBond", HasPropQueryBond,
                (python::arg("propname"), python::arg("negate") = false),
                "Returns a QueryBond that matches when the propery 'propname' "
                "exists in the bond.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasIntPropWithValueQueryBond",
                PropQueryWithTol<Bond, QueryBond, int>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false, python::arg("tolerance") = 0),
                "Returns a QueryBond that matches when the propery 'propname' "
                "has the specified int value.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasBoolPropWithValueQueryBond",
                PropQuery<Bond, QueryBond, bool>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false),
                "Returns a QueryBond that matches when the propery 'propname' "
                "has the specified boolean"
                " value.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasStringPropWithValueQueryBond",
                PropQuery<Bond, QueryBond, std::string>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false),
                "Returns a QueryBond that matches when the propery 'propname' "
                "has the specified string "
                "value.",
                python::return_value_policy<python::manage_new_object>());

    python::def("HasDoublePropWithValueQueryBond",
                PropQueryWithTol<Bond, QueryBond, double>,
                (python::arg("propname"), python::arg("val"),
                 python::arg("negate") = false, python::arg("tolerance") = 0.0),
                "Returns a QueryBond that matches when the propery 'propname' "
                "has the specified "
                "value +- tolerance",
                python::return_value_policy<python::manage_new_object>());
  };
};
}  // end of namespace

void wrap_queries() { RDKit::queries_wrapper::wrap(); }