File: QueryBond.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 (205 lines) | stat: -rw-r--r-- 5,804 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
// $Id$
//
//  Copyright (C) 2001-2006 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/QueryBond.h>

namespace RDKit {

QueryBond::QueryBond(BondType bT) : Bond(bT) {
  if (bT != Bond::UNSPECIFIED)
    dp_query = makeBondOrderEqualsQuery(bT);
  else
    dp_query = makeBondNullQuery();
};

QueryBond::~QueryBond() {
  delete dp_query;
  dp_query = nullptr;
};

QueryBond &QueryBond::operator=(const QueryBond &other) {
  // FIX: should we copy molecule ownership?  I don't think so.
  // FIX: how to deal with atom indices?
  dp_mol = nullptr;
  d_bondType = other.d_bondType;
  dp_query = other.dp_query->copy();
  dp_props = other.dp_props;
  return *this;
}

Bond *QueryBond::copy() const {
  auto *res = new QueryBond(*this);
  return res;
}

void QueryBond::setBondType(BondType bT) {
  // NOTE: calling this blows out any existing query
  d_bondType = bT;
  delete dp_query;
  dp_query = nullptr;

  dp_query = makeBondOrderEqualsQuery(bT);
}

void QueryBond::setBondDir(BondDir bD) {
  // NOTE: calling this blows out any existing query
  //
  //   Ignoring bond orders (which this implicitly does by blowing out
  //   any bond order query) is ok for organic molecules, where the
  //   only bonds assigned directions are single.  It'll fail in other
  //   situations, whatever those may be.
  //
  d_dirTag = bD;
#if 0  
  delete dp_query;
  dp_query = NULL;
  dp_query = makeBondDirEqualsQuery(bD);
#endif
}

void QueryBond::expandQuery(QUERYBOND_QUERY *what,
                            Queries::CompositeQueryType how,
                            bool maintainOrder) {
  QUERYBOND_QUERY *origQ = dp_query;
  std::string descrip;
  switch (how) {
    case Queries::COMPOSITE_AND:
      dp_query = new BOND_AND_QUERY;
      descrip = "BondAnd";
      break;
    case Queries::COMPOSITE_OR:
      dp_query = new BOND_OR_QUERY;
      descrip = "BondOr";
      break;
    case Queries::COMPOSITE_XOR:
      dp_query = new BOND_XOR_QUERY;
      descrip = "BondXor";
      break;
    default:
      UNDER_CONSTRUCTION("unrecognized combination query");
  }
  dp_query->setDescription(descrip);
  if (maintainOrder) {
    dp_query->addChild(QUERYBOND_QUERY::CHILD_TYPE(origQ));
    dp_query->addChild(QUERYBOND_QUERY::CHILD_TYPE(what));
  } else {
    dp_query->addChild(QUERYBOND_QUERY::CHILD_TYPE(what));
    dp_query->addChild(QUERYBOND_QUERY::CHILD_TYPE(origQ));
  }
}

namespace {
bool localMatch(BOND_EQUALS_QUERY const *q1, BOND_EQUALS_QUERY const *q2) {
  if (q1->getNegation() == q2->getNegation()) {
    return q1->getVal() == q2->getVal();
  } else {
    return q1->getVal() != q2->getVal();
  }
}

bool queriesMatch(QueryBond::QUERYBOND_QUERY const *q1,
                  QueryBond::QUERYBOND_QUERY const *q2) {
  PRECONDITION(q1, "no q1");
  PRECONDITION(q2, "no q2");

  static const unsigned int nQueries = 6;
  static std::string equalityQueries[nQueries] = {
      "BondRingSize", "BondMinRingSize", "BondOrder",
      "BondDir",      "BondInRing",      "BondInNRings"};

  bool res = false;
  std::string d1 = q1->getDescription();
  std::string d2 = q2->getDescription();
  if (d1 == "BondNull" || d2 == "BondNull") {
    res = true;
  } else if (d1 == "BondOr") {
    // FIX: handle negation on BondOr and BondAnd
    for (auto iter1 = q1->beginChildren(); iter1 != q1->endChildren();
         ++iter1) {
      if (d2 == "BondOr") {
        for (auto iter2 = q2->beginChildren(); iter2 != q2->endChildren();
             ++iter2) {
          if (queriesMatch(iter1->get(), iter2->get())) {
            res = true;
            break;
          }
        }
      } else {
        if (queriesMatch(iter1->get(), q2)) {
          res = true;
        }
      }
      if (res) break;
    }
  } else if (d1 == "BondAnd") {
    res = true;
    for (auto iter1 = q1->beginChildren(); iter1 != q1->endChildren();
         ++iter1) {
      bool matched = false;
      if (d2 == "BondAnd") {
        for (auto iter2 = q2->beginChildren(); iter2 != q2->endChildren();
             ++iter2) {
          if (queriesMatch(iter1->get(), iter2->get())) {
            matched = true;
            break;
          }
        }
      } else {
        matched = queriesMatch(iter1->get(), q2);
      }
      if (!matched) {
        res = false;
        break;
      }
    }
    // FIX : handle BondXOr
  } else if (d2 == "BondOr") {
    // FIX: handle negation on BondOr and BondAnd
    for (auto iter2 = q2->beginChildren(); iter2 != q2->endChildren();
         ++iter2) {
      if (queriesMatch(q1, iter2->get())) {
        res = true;
        break;
      }
    }
  } else if (d2 == "BondAnd") {
    res = true;
    for (auto iter2 = q2->beginChildren(); iter2 != q2->endChildren();
         ++iter2) {
      if (queriesMatch(q1, iter2->get())) {
        res = false;
        break;
      }
    }
  } else if (std::find(&equalityQueries[0], &equalityQueries[nQueries], d1) !=
             &equalityQueries[nQueries]) {
    res = localMatch(static_cast<BOND_EQUALS_QUERY const *>(q1),
                     static_cast<BOND_EQUALS_QUERY const *>(q2));
  }
  return res;
}
}  // end of local namespace

bool QueryBond::Match(Bond const *what) const {
  PRECONDITION(what, "bad query bond");
  PRECONDITION(dp_query, "no query set");
  return dp_query->Match(what);
}
bool QueryBond::QueryMatch(QueryBond const *what) const {
  PRECONDITION(what, "bad query bond");
  PRECONDITION(dp_query, "no query set");
  if (!what->hasQuery()) {
    return dp_query->Match(what);
  } else {
    return queriesMatch(dp_query, what->getQuery());
  }
}

}  // end o' namespace