query.h
Go to the documentation of this file.
00001 /**********************************************************************
00002   query.h - OBQuery, OBQueryAtom & OBQueryBond classes.
00003 
00004   Copyright (C) 2010 by Tim Vandermeersch
00005 
00006   This file is part of the Open Babel project.
00007   For more information, see <http://openbabel.org/>
00008 
00009   This program is free software; you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation; either version 2 of the License, or
00012   (at your option) any later version.
00013 
00014   This program is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with this program; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00022   02110-1301, USA.
00023  **********************************************************************/
00024 #ifndef OB_QUERY_H
00025 #define OB_QUERY_H
00026 
00027 #include <openbabel/mol.h>
00028 #include <openbabel/tokenst.h>
00029 
00030 namespace OpenBabel {
00031 
00032   class OBQueryBond;
00033 
00036 
00052   class OBAPI OBQueryAtom
00053   {
00054     public:
00055       friend class OBQuery;
00056       friend class OBQueryBond;
00063       OBQueryAtom(int atomicNum = 6, bool isInRing = false, bool isAromatic = false) :
00064         m_atomicNum(atomicNum), m_isInRing(isInRing), m_isAromatic(isAromatic) {}
00065 
00066       virtual ~OBQueryAtom() {}
00067 
00072       unsigned int GetIndex() const
00073       {
00074         return m_index;
00075       }
00080       const std::vector<OBQueryBond*>& GetBonds() const
00081       {
00082         return m_bonds;
00083       }
00088       const std::vector<OBQueryAtom*>& GetNbrs() const
00089       {
00090         return m_nbrs;
00091       }
00099       virtual bool Matches(const OBAtom *atom) const
00100       {
00101         if (atom->GetAtomicNum() != m_atomicNum)
00102           return false;
00103         if (atom->IsAromatic() != m_isAromatic)
00104           return false;
00105         if (m_isInRing)
00106           if (!atom->IsInRing())
00107             return false;
00108         return true;
00109       }
00110     protected:
00111       unsigned int m_index;
00112       int m_atomicNum;
00113       bool m_isInRing, m_isAromatic;
00114       std::vector<OBQueryBond*> m_bonds;
00115       std::vector<OBQueryAtom*> m_nbrs;
00116   };
00117 
00134   class OBAPI OBQueryBond
00135   {
00136     public:
00137       friend class OBQuery;
00141       OBQueryBond(OBQueryAtom *begin, OBQueryAtom *end, int order = 1, bool aromatic = false) :
00142           m_begin(begin), m_end(end), m_order(order), m_aromatic(aromatic)
00143       {
00144         m_begin->m_bonds.push_back(this);
00145         m_end->m_bonds.push_back(this);
00146         m_begin->m_nbrs.push_back(m_end);
00147         m_end->m_nbrs.push_back(m_begin);
00148       }
00149 
00150       virtual ~OBQueryBond() {}
00151 
00155       unsigned int GetIndex() const
00156       {
00157         return m_index;
00158       }
00162       OBQueryAtom* GetBeginAtom() const { return m_begin; }
00166       OBQueryAtom* GetEndAtom() const { return m_end; }
00175       virtual bool Matches(const OBBond *bond) const
00176       {
00177         if (m_aromatic)
00178           return bond->IsAromatic();
00179         return bond->GetBondOrder() == m_order;
00180       }
00181     protected:
00182       unsigned int m_index;
00183       OBQueryAtom *m_begin, *m_end;
00184       int m_order;
00185       bool m_aromatic;
00186   };
00187 
00195   class OBAPI OBQuery
00196   {
00197     public:
00198       ~OBQuery()
00199       {
00200         std::for_each(m_atoms.begin(),m_atoms.end(), DeleteObject());
00201         std::for_each(m_bonds.begin(),m_bonds.end(), DeleteObject());
00202       }
00206       unsigned int NumAtoms() const
00207       {
00208         return m_atoms.size();
00209       }
00213       unsigned int NumBonds() const
00214       {
00215         return m_bonds.size();
00216       }
00220       const std::vector<OBQueryAtom*>& GetAtoms() const
00221       {
00222         return m_atoms;
00223       }
00227       const std::vector<OBQueryBond*>& GetBonds() const
00228       {
00229         return m_bonds;
00230       }
00235       OBQueryBond* GetBond(OBQueryAtom *begin, OBQueryAtom *end) const
00236       {
00237         for (unsigned int i = 0; i < begin->GetBonds().size(); ++i)
00238           if (begin->GetNbrs()[i] == end)
00239             return begin->GetBonds()[i];
00240         return 0;
00241       }
00245       void AddAtom(OBQueryAtom *atom)
00246       {
00247         atom->m_index = m_atoms.size();
00248         m_atoms.push_back(atom);
00249       }
00253       void AddBond(OBQueryBond *bond)
00254       {
00255         bond->m_index = m_bonds.size();
00256         m_bonds.push_back(bond);
00257       }
00258     protected:
00259       std::vector<OBQueryAtom*> m_atoms;
00260       std::vector<OBQueryBond*> m_bonds;
00261   };
00262 
00270   OBAPI OBQuery* CompileMoleculeQuery(OBMol *mol, const OBBitVec &mask = OBBitVec());
00271 
00279   OBAPI OBQuery* CompileSmilesQuery(const std::string &smiles, const OBBitVec &mask = OBBitVec());
00280 
00282 }
00283 
00284 #endif
00285 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines