Open Babel  3.0
query.h
Go to the documentation of this file.
1 /**********************************************************************
2  query.h - OBQuery, OBQueryAtom & OBQueryBond classes.
3 
4  Copyright (C) 2010 by Tim Vandermeersch
5 
6  This file is part of the Open Babel project.
7  For more information, see <http://openbabel.org/>
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  02110-1301, USA.
23  **********************************************************************/
24 #ifndef OB_QUERY_H
25 #define OB_QUERY_H
26 
27 #include <openbabel/bond.h> // TODO: Move OBBond code out of this header
28 #include <openbabel/bitvec.h>
29 #include <openbabel/tokenst.h>
30 
31 namespace OpenBabel {
32 
33  class OBQueryBond;
34  class OBMol;
35 
38 
54  class OBAPI OBQueryAtom
55  {
56  public:
57  friend class OBQuery;
58  friend class OBQueryBond;
65  OBQueryAtom(int atomicNum = 6, bool isInRing = false, bool isAromatic = false) :
66  m_atomicNum(atomicNum), m_isInRing(isInRing), m_isAromatic(isAromatic) {}
67 
68  virtual ~OBQueryAtom() {}
69 
74  unsigned int GetIndex() const
75  {
76  return m_index;
77  }
82  const std::vector<OBQueryBond*>& GetBonds() const
83  {
84  return m_bonds;
85  }
90  const std::vector<OBQueryAtom*>& GetNbrs() const
91  {
92  return m_nbrs;
93  }
101  virtual bool Matches(const OBAtom *atom) const
102  {
103  if (atom->GetAtomicNum() != m_atomicNum)
104  return false;
105  if (atom->IsAromatic() != m_isAromatic)
106  return false;
107  if (m_isInRing)
108  if (!atom->IsInRing())
109  return false;
110  return true;
111  }
112  protected:
113  unsigned int m_index;
114  unsigned int m_atomicNum;
115  bool m_isInRing, m_isAromatic;
116  std::vector<OBQueryBond*> m_bonds;
117  std::vector<OBQueryAtom*> m_nbrs;
118  };
119 
136  class OBAPI OBQueryBond
137  {
138  public:
139  friend class OBQuery;
143  OBQueryBond(OBQueryAtom *begin, OBQueryAtom *end, int order = 1, bool aromatic = false) :
144  m_begin(begin), m_end(end), m_order(order), m_aromatic(aromatic)
145  {
146  m_begin->m_bonds.push_back(this);
147  m_end->m_bonds.push_back(this);
148  m_begin->m_nbrs.push_back(m_end);
149  m_end->m_nbrs.push_back(m_begin);
150  }
151 
152  virtual ~OBQueryBond() {}
153 
157  unsigned int GetIndex() const
158  {
159  return m_index;
160  }
164  OBQueryAtom* GetBeginAtom() const { return m_begin; }
168  OBQueryAtom* GetEndAtom() const { return m_end; }
177  virtual bool Matches(const OBBond *bond) const
178  {
179  if (m_aromatic)
180  return bond->IsAromatic();
181  return bond->GetBondOrder() == m_order;
182  }
183  protected:
184  unsigned int m_index;
185  OBQueryAtom *m_begin, *m_end;
186  unsigned int m_order;
188  };
189 
197  class OBAPI OBQuery
198  {
199  public:
200  ~OBQuery();
204  unsigned int NumAtoms() const
205  {
206  return m_atoms.size();
207  }
211  unsigned int NumBonds() const
212  {
213  return m_bonds.size();
214  }
218  const std::vector<OBQueryAtom*>& GetAtoms() const
219  {
220  return m_atoms;
221  }
225  const std::vector<OBQueryBond*>& GetBonds() const
226  {
227  return m_bonds;
228  }
234  {
235  for (unsigned int i = 0; i < begin->GetBonds().size(); ++i)
236  if (begin->GetNbrs()[i] == end)
237  return begin->GetBonds()[i];
238  return 0;
239  }
243  void AddAtom(OBQueryAtom *atom)
244  {
245  atom->m_index = m_atoms.size();
246  m_atoms.push_back(atom);
247  }
251  void AddBond(OBQueryBond *bond)
252  {
253  bond->m_index = m_bonds.size();
254  m_bonds.push_back(bond);
255  }
256  protected:
257  std::vector<OBQueryAtom*> m_atoms;
258  std::vector<OBQueryBond*> m_bonds;
259  };
260 
268  OBAPI OBQuery* CompileMoleculeQuery(OBMol *mol, const OBBitVec &mask = OBBitVec());
269 
277  OBAPI OBQuery* CompileSmilesQuery(const std::string &smiles, const OBBitVec &mask = OBBitVec());
278 
280 }
281 
282 #endif
283 
virtual ~OBQueryAtom()
Definition: query.h:68
OBQueryAtom * GetEndAtom() const
Definition: query.h:168
A substructure query.
Definition: query.h:197
Handle bonds.
OBQueryBond * GetBond(OBQueryAtom *begin, OBQueryAtom *end) const
Definition: query.h:233
unsigned int m_atomicNum
Definition: query.h:114
Atom in an OBQuery.
Definition: query.h:54
const std::vector< OBQueryAtom * > & GetAtoms() const
Definition: query.h:218
bool IsAromatic() const
Definition: atom.cpp:773
bool IsAromatic() const
Definition: bond.cpp:458
OBQueryAtom(int atomicNum=6, bool isInRing=false, bool isAromatic=false)
Definition: query.h:65
Bond class.
Definition: bond.h:58
Molecule Class.
Definition: mol.h:118
unsigned int m_index
Definition: query.h:113
std::vector< OBQueryBond * > m_bonds
Definition: query.h:116
void AddAtom(OBQueryAtom *atom)
Definition: query.h:243
A speed-optimized vector of bits.
Definition: bitvec.h:57
const std::vector< OBQueryBond * > & GetBonds() const
Definition: query.h:82
virtual ~OBQueryBond()
Definition: query.h:152
OBQueryBond(OBQueryAtom *begin, OBQueryAtom *end, int order=1, bool aromatic=false)
Definition: query.h:143
std::vector< OBQueryAtom * > m_atoms
Definition: query.h:257
unsigned int NumAtoms() const
Definition: query.h:204
bool IsInRing() const
Definition: atom.cpp:785
const std::vector< OBQueryBond * > & GetBonds() const
Definition: query.h:225
virtual bool Matches(const OBBond *bond) const
Definition: query.h:177
OBQuery * CompileMoleculeQuery(OBMol *mol, const OBBitVec &mask=OBBitVec())
Definition: query.cpp:43
unsigned int GetAtomicNum() const
Definition: atom.h:176
std::vector< OBQueryAtom * > m_nbrs
Definition: query.h:117
void AddBond(OBQueryBond *bond)
Definition: query.h:251
OBQueryAtom * m_end
Definition: query.h:185
bool m_isInRing
Definition: query.h:115
std::vector< OBQueryBond * > m_bonds
Definition: query.h:258
const std::vector< OBQueryAtom * > & GetNbrs() const
Definition: query.h:90
unsigned int GetBondOrder() const
Definition: bond.h:157
bool m_aromatic
Definition: query.h:187
unsigned int GetIndex() const
Definition: query.h:74
Bond in an OBQuery.
Definition: query.h:136
unsigned int NumBonds() const
Definition: query.h:211
unsigned int GetIndex() const
Definition: query.h:157
unsigned int m_order
Definition: query.h:186
Fast and efficient bitstring class.
virtual bool Matches(const OBAtom *atom) const
Definition: query.h:101
unsigned int m_index
Definition: query.h:184
OBQueryAtom * GetBeginAtom() const
Definition: query.h:164
OBQuery * CompileSmilesQuery(const std::string &smiles, const OBBitVec &mask=OBBitVec())
Definition: query.cpp:75
Tokenize strings, open data files.
Global namespace for all Open Babel code.
Definition: alias.h:22
Atom class.
Definition: atom.h:71