PageRenderTime 72ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/openbabel-2.3.1/src/query.cpp

#
C++ | 76 lines | 40 code | 10 blank | 26 comment | 5 complexity | 78f637ffdf9a73c08f5f25ba570d5a47 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. /**********************************************************************
  2. query.cpp - 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. #include <openbabel/query.h>
  20. #include <openbabel/obconversion.h>
  21. using namespace std;
  22. namespace OpenBabel {
  23. OBQuery* CompileMoleculeQuery(OBMol *mol, const OBBitVec &mask)
  24. {
  25. // set all atoms to 1 if the mask is empty
  26. OBBitVec mask2 = mask;
  27. if (!mask2.CountBits())
  28. for (unsigned int i = 0; i < mol->NumAtoms(); ++i)
  29. mask2.SetBitOn(i + 1);
  30. OBQuery *query = new OBQuery;
  31. unsigned int offset = 0;
  32. std::vector<unsigned int> indexes;
  33. FOR_ATOMS_OF_MOL (obatom, mol) {
  34. indexes.push_back(obatom->GetIndex() - offset);
  35. if (!mask2.BitIsSet(obatom->GetIndex() + 1)) {
  36. offset++;
  37. continue;
  38. }
  39. query->AddAtom(new OBQueryAtom(obatom->GetAtomicNum(), obatom->IsInRing(), obatom->IsAromatic()));
  40. }
  41. FOR_BONDS_OF_MOL (obbond, mol) {
  42. unsigned int beginIndex = obbond->GetBeginAtom()->GetIndex();
  43. unsigned int endIndex = obbond->GetEndAtom()->GetIndex();
  44. if (!mask2.BitIsSet(beginIndex + 1) || !mask2.BitIsSet(endIndex + 1))
  45. continue;
  46. query->AddBond(new OBQueryBond(query->GetAtoms()[indexes[beginIndex]], query->GetAtoms()[indexes[endIndex]],
  47. obbond->GetBondOrder(), obbond->IsAromatic()));
  48. }
  49. return query;
  50. }
  51. OBQuery* CompileSmilesQuery(const std::string &smiles, const OBBitVec &mask)
  52. {
  53. OBConversion conv;
  54. conv.SetInFormat("smi");
  55. OBMol mol;
  56. conv.ReadString(&mol, smiles);
  57. return CompileMoleculeQuery(&mol, mask);
  58. }
  59. }
  60. /// @file query.cpp
  61. /// @brief OBQuery, OBQueryAtom & OBQueryBond classes.