2.3 Creating a Molecule from SMILES

A common method of creating a molecule in OEChem is via the SMILES representation. SMILES notation is commonly used in chemical information systems, as it provides a convenient string representation of a molecule. An introduction to SMILES syntax is provided later in this manual. For examples, below we'll use the SMILES ``c1ccccc1'' which describes the molecule benzene.

A molecule can be created from a SMILES string using the function OEParseSmiles().

#include "oechem.h"

using namespace OEChem;

int main()
{
  OEMol mol;

  OEParseSmiles(mol,"c1ccccc1");
  return 0;
}

The OEParseSmiles function actually returns a boolean result indicating whether the input string was a valid SMILES string. It's good programming practice to check the return value and report an error message if anything went wrong. The following example uses C++'s iostream library to report the error.

#include "oechem.h"
#include <iostream>

using namespace OEChem;
using namespace std;

int main()
{
  OEMol mol;

  if (OEParseSmiles(mol,"c1ccccc1"))
  {
    // Do something with the molecule!
  }
  else cerr << "SMILES string was invalid!" << endl;
  return 0;
}

The molecule returned by OEParseSmiles preserves the aromaticity present in the input SMILES string, so for example, if benzene is expressed as ``c1ccccc1'' all atoms and bonds are marked as aromatic, but if expressed as a Kekulé form, ``C1=CC=CC=C1'' all atoms and bonds are kept aliphatic. A common task after creating a molecule from SMILES is to normalize its aromaticity with OEAssignAromaticFlags.

#include "oechem.h"
#include <iostream>

using namespace OEChem;
using namespace std;

int main()
{
  OEMol mol;

  if (OEParseSmiles(mol,"c1ccccc1"))
  {
    OEAssignAromaticFlags(mol);
    // Do something with the molecule!
  }
  else cerr << "SMILES string was invalid!" << endl;
  return 0;
}