PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/openbabel-2.3.1/include/openbabel/query.h

#
C Header | 287 lines | 124 code | 16 blank | 147 comment | 11 complexity | 28dab525a05a7d22e8b39e6f3b617259 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. /**********************************************************************
  2. query.h - OBQuery, OBQueryAtom & OBQueryBond classes.
  3. Copyright (C) 2010 by Tim Vandermeersch
  4. This file is part of the Open Babel project.
  5. For more information, see <http://openbabel.org/>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301, USA.
  18. **********************************************************************/
  19. #ifndef OB_QUERY_H
  20. #define OB_QUERY_H
  21. #include <openbabel/mol.h>
  22. #include <openbabel/tokenst.h>
  23. namespace OpenBabel {
  24. class OBQueryBond;
  25. ///@addtogroup substructure Substructure Searching
  26. ///@{
  27. /**
  28. * @class OBQueryAtom query.h <openbabel/query.h>
  29. * @brief Atom in an OBQuery
  30. *
  31. * The OBQueryAtom class defines an interface for query atoms. The class provides
  32. * some general methods and properties to access the topology information. The Matches
  33. * method can be reimplemented in subclasses to get custom matching behavior.
  34. *
  35. * The default Matches implementation only checks the atomic number.
  36. *
  37. * See @ref substructure for more information.
  38. *
  39. * @sa OBQuery OBQueryBond OBIsomorphismMapper
  40. * @since version 2.3
  41. */
  42. class OBAPI OBQueryAtom
  43. {
  44. public:
  45. friend class OBQuery;
  46. friend class OBQueryBond;
  47. /**
  48. * Constructor.
  49. * @param atomicNum The atomic number for this query atom.
  50. * @param isInRing Specify wether the query atom is in a ring. Default is false.
  51. * @param isAromatic Specify wether the query atom is aromatic. Default is false.
  52. */
  53. OBQueryAtom(int atomicNum = 6, bool isInRing = false, bool isAromatic = false) :
  54. m_atomicNum(atomicNum), m_isInRing(isInRing), m_isAromatic(isAromatic) {}
  55. virtual ~OBQueryAtom() {}
  56. /**
  57. * Get the index for this query atom. Atoms are indexed starting from 0.
  58. * This method is used by OBIsomorphismMapper implementations.
  59. */
  60. unsigned int GetIndex() const
  61. {
  62. return m_index;
  63. }
  64. /**
  65. * Get the query bonds for this atom.
  66. * This method is used by OBIsomorphismMapper implementations.
  67. */
  68. const std::vector<OBQueryBond*>& GetBonds() const
  69. {
  70. return m_bonds;
  71. }
  72. /**
  73. * Get the neighbor query atoms.
  74. * This method is used by OBIsomorphismMapper implementations.
  75. */
  76. const std::vector<OBQueryAtom*>& GetNbrs() const
  77. {
  78. return m_nbrs;
  79. }
  80. /**
  81. * This is the match method to verify if an OBQueryAtom and OBAtom class match.
  82. * The default implementation only checks if the atomic numbers match. Reimplement
  83. * this method in a subclass for more advances matching.
  84. * This method is used by OBIsomorphismMapper implementations.
  85. * @param atom The OBAtom object to compare this OBQueryAtom with.
  86. */
  87. virtual bool Matches(const OBAtom *atom) const
  88. {
  89. if (atom->GetAtomicNum() != m_atomicNum)
  90. return false;
  91. if (atom->IsAromatic() != m_isAromatic)
  92. return false;
  93. if (m_isInRing)
  94. if (!atom->IsInRing())
  95. return false;
  96. return true;
  97. }
  98. protected:
  99. unsigned int m_index;
  100. int m_atomicNum;
  101. bool m_isInRing, m_isAromatic;
  102. std::vector<OBQueryBond*> m_bonds;
  103. std::vector<OBQueryAtom*> m_nbrs;
  104. };
  105. /**
  106. * @class OBQueryBond query.h <openbabel/query.h>
  107. * @brief Bond in an OBQuery
  108. *
  109. * The OBQueryBond class defines an interface for query bonds. The class provides
  110. * some general methods and properties to access the topology information. The Matches
  111. * method can be reimplemented in subclasses to get custom matching behavior.
  112. *
  113. * The default Matches implementation only checks if the bonds are both aromatic,
  114. * otherwise the bond orders are compared.
  115. *
  116. * See @ref substructure for more information.
  117. *
  118. * @sa OBQuery OBQueryAtom OBIsomorphismMapper
  119. * @since version 2.3
  120. */
  121. class OBAPI OBQueryBond
  122. {
  123. public:
  124. friend class OBQuery;
  125. /**
  126. * Constructor.
  127. */
  128. OBQueryBond(OBQueryAtom *begin, OBQueryAtom *end, int order = 1, bool aromatic = false) :
  129. m_begin(begin), m_end(end), m_order(order), m_aromatic(aromatic)
  130. {
  131. m_begin->m_bonds.push_back(this);
  132. m_end->m_bonds.push_back(this);
  133. m_begin->m_nbrs.push_back(m_end);
  134. m_end->m_nbrs.push_back(m_begin);
  135. }
  136. virtual ~OBQueryBond() {}
  137. /**
  138. * Get the index for this query bonds. Query bonds are indexed starting from 0.
  139. */
  140. unsigned int GetIndex() const
  141. {
  142. return m_index;
  143. }
  144. /**
  145. * Get the begin atom.
  146. */
  147. OBQueryAtom* GetBeginAtom() const { return m_begin; }
  148. /**
  149. * Get the end atom.
  150. */
  151. OBQueryAtom* GetEndAtom() const { return m_end; }
  152. /**
  153. * This is the match method to verify if an OBQueryBond and OBBond class match.
  154. * The default implementation checks if both bonds are aromatic and compares the
  155. * bond orders otherwise. Reimplement this method in a subclass for more
  156. * advances matching.
  157. * This method is used by OBIsomorphismMapper implementations.
  158. * @param bond The OBBond object to compare this OBQueryBond with.
  159. */
  160. virtual bool Matches(const OBBond *bond) const
  161. {
  162. if (m_aromatic)
  163. return bond->IsAromatic();
  164. return bond->GetBondOrder() == m_order;
  165. }
  166. protected:
  167. unsigned int m_index;
  168. OBQueryAtom *m_begin, *m_end;
  169. int m_order;
  170. bool m_aromatic;
  171. };
  172. /**
  173. * @class OBQuery query.h <openbabel/query.h>
  174. * @brief A substructure query
  175. *
  176. * See @ref substructure for more information.
  177. * @since version 2.3
  178. */
  179. class OBAPI OBQuery
  180. {
  181. public:
  182. ~OBQuery()
  183. {
  184. std::for_each(m_atoms.begin(),m_atoms.end(), DeleteObject());
  185. std::for_each(m_bonds.begin(),m_bonds.end(), DeleteObject());
  186. }
  187. /**
  188. * @return The number of atoms in the query.
  189. */
  190. unsigned int NumAtoms() const
  191. {
  192. return m_atoms.size();
  193. }
  194. /**
  195. * @return The number of bonds in the query.
  196. */
  197. unsigned int NumBonds() const
  198. {
  199. return m_bonds.size();
  200. }
  201. /**
  202. * @return std::vector with pointers to the query atoms.
  203. */
  204. const std::vector<OBQueryAtom*>& GetAtoms() const
  205. {
  206. return m_atoms;
  207. }
  208. /**
  209. * @return std::vector with pointers to the query bonds.
  210. */
  211. const std::vector<OBQueryBond*>& GetBonds() const
  212. {
  213. return m_bonds;
  214. }
  215. /**
  216. * @return The query bond between @p begin and @p end. If there is no
  217. * bond between @p begin and @p end, this function returns 0.
  218. */
  219. OBQueryBond* GetBond(OBQueryAtom *begin, OBQueryAtom *end) const
  220. {
  221. for (unsigned int i = 0; i < begin->GetBonds().size(); ++i)
  222. if (begin->GetNbrs()[i] == end)
  223. return begin->GetBonds()[i];
  224. return 0;
  225. }
  226. /**
  227. * Add a query atom to the query. This function steals the pointer.
  228. */
  229. void AddAtom(OBQueryAtom *atom)
  230. {
  231. atom->m_index = m_atoms.size();
  232. m_atoms.push_back(atom);
  233. }
  234. /**
  235. * Add a query atom to the query. This function steals the pointer.
  236. */
  237. void AddBond(OBQueryBond *bond)
  238. {
  239. bond->m_index = m_bonds.size();
  240. m_bonds.push_back(bond);
  241. }
  242. protected:
  243. std::vector<OBQueryAtom*> m_atoms;
  244. std::vector<OBQueryBond*> m_bonds;
  245. };
  246. /**
  247. * Create an OBQuery object from an OBMol object.
  248. * @param mol The query molecule.
  249. * @param mask The mask specifying the atoms to use. Indexed from 1 (i.e. OBAtom::GetIdx()).
  250. * @return A pointer to an OBQuery object for the smiles string. This pointer should be deleted.
  251. * @since version 2.3
  252. */
  253. OBAPI OBQuery* CompileMoleculeQuery(OBMol *mol, const OBBitVec &mask = OBBitVec());
  254. /**
  255. * Create an OBQuery object from a smiles string.
  256. * @param smiles The query smiles string.
  257. * @param mask The mask specifying the atoms to use. Indexed from 1 (i.e. OBAtom::GetIdx()).
  258. * @return A pointer to an OBQuery object for the smiles string. This pointer should be deleted.
  259. * @since version 2.3
  260. */
  261. OBAPI OBQuery* CompileSmilesQuery(const std::string &smiles, const OBBitVec &mask = OBBitVec());
  262. ///@}
  263. }
  264. #endif
  265. /// @file query.h
  266. /// @brief OBQuery, OBQueryAtom & OBQueryBond classes.