Generated on Wed Apr 29 2015 11:51:40 for GGL-4.1.2 by doxygen 1.8.3.1
SMILESwriterOB.hh
Go to the documentation of this file.
1 #ifndef GGL_CHEM_SMILESWRITEROB_HH_
2 #define GGL_CHEM_SMILESWRITEROB_HH_
3 
4 
5 #include <sstream>
6 #include <iostream>
7 #include <memory>
8 
9 #include "ggl/chem/Molecule.hh"
10 #include "ggl/chem/MoleculeUtil.hh"
11 
12 #include <openbabel/babelconfig.h>
13 #include <openbabel/mol.h>
14 #include <openbabel/obconversion.h>
15 
16 
17 
18 namespace ggl {
19  namespace chem {
20 
21  /*! @brief Molecule to SMILES writer via OpenBabel
22  *
23  * Utility class to generate a canonical SMILES string from a molecule
24  * graph representation. It expects atom (node) and edge (bond) label
25  * following the Daylight's SMILES description. See
26  * ggl::chem::SMILES_grammar for further details.
27  *
28  * It utilizes the OpenBabel library (http://openbabel.org) for the
29  * conversion.
30  *
31  * @author Martin Mann (c) 2009 http://www.bioinf.uni-freiburg.de/~mmann/
32  *
33  */
35  {
36 
37  protected:
38 
39  //! the OpenBabel conversion object used
40  static std::auto_ptr<OpenBabel::OBConversion> converter;
41 
42  public:
44 
45  /*! Generates a canonical SMILES string of the given graph
46  * representation of a molecule.
47  *
48  * NOTE: THE FUNCTIONALITY IS INCOMPLETE, i.e. only a few atom and
49  * bond types are possible! See class description!
50  *
51  * @param mol the molecule graph to parse
52  * @return a canonical SMILES string representing the given molecule
53  */
54  static
55  std::string
56  getSMILES( const Molecule& mol)
57  {
58  // setup converter if not done yet
59  if (converter.get() == 0) {
60  // create converter
61  converter.reset( new OpenBabel::OBConversion(NULL,NULL));
62  // setup conversion scheme
63  converter->SetInAndOutFormats("CML","CAN");
64  }
65 
66  // get CML representation of the molecule
67  std::stringstream molCML, outSMILES;
68  MoleculeUtil::convertCML( mol, molCML );
69 
70  // convert from CML to canonical SMILES
71  converter->Convert( &molCML, &outSMILES );
72 
73  // trim whitespaces
74  std::string out = outSMILES.str();
75  size_t from = out.find_first_not_of(" \t\n");
76  assert(from != out.npos);
77  size_t to = out.find_last_not_of(" \t\n");
78  assert(to != out.npos);
79  // return conversion result
80  return out.substr(from, to-from+1);
81  }
82 
83 
84  };
85 
86  // create member
87  std::auto_ptr<OpenBabel::OBConversion>
88  SMILESwriterOB::converter = std::auto_ptr<OpenBabel::OBConversion>();
89 
90  } // namespace chem
91 } // namespace ggl
92 
93 
94 #endif /*GGL_CHEM_SMILESWRITEROB_HH_*/