3.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 we'll use the SMILES ``c1ccccc1'' which describes the benzene molecule.

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

from openeye.oechem import *

# create a new molecule
mol = OEGraphMol()

# convert the string into a molecule
OEParseSmiles(mol, "c1ccccc1")

The OEParseSmiles function actually returns true or false (1 or 0) indicating whether the input string was a valid SMILES string. It is good programming practice to check the return value and report an error message if anything went wrong. The following example shows adding a check on the return status of OEParseSmiles and prints an error message to sys.stderr.

from openeye.oechem import *
import sys

# create a new molecule
mol = OEGraphMol()

if (OEParseSmiles(mol, "c1ccccc1") == 1):
    # do something interesting with mol

else:
    sys.stderr.write("SMILES string was invalid!\n")