4.2 Reading Molecules with a Generator Method

The preferred way to read molecules in OEChem is to use the generator methods provided by oemolistreams. These methods provide syntax similar to using a for x in y loop to iterate over the elements of a list.

#!/usr/bin/env python
# ch4-2.py
from openeye.oechem import *

ifs = oemolistream()
ofs = oemolostream()

for mol in ifs.GetOEGraphMols():
    OEWriteMolecule(ofs, mol)

Note that using this syntax there is no need to create a molecule object. A single molecule object is created by the generator method (GetOEGraphMols) and is re-used on each loop iteration. As such, this syntax should not be used to put molecules into a list or other persistent container. If you need to create a molecule object that is persistent and can be used after the loop completes, there are a couple of alternatives.

Probably the most efficient is to change the looping criteria slightly and create a new molecule object each time through the loop. This first example tests the state of the input stream to determine when the loop is finished.

#!/usr/bin/env python
# ch4-3.py
from openeye.oechem import *

ifs = oemolistream()
ofs = oemolostream()

# create an empty list
mollist = []

# loop over input
while ifs.IsValid():
    mol = OEGraphMol()
    OEReadMolecule(ifs, mol)
    mollist.append(mol)

for mol in mollist:
    OEWriteMolecule(ofs, mol)

Alternatively, you can use iterator-like syntax and then construct a new molecule object from the current one. The OEGraphMol constructor can be used, except this time we use the ``mol'' as an argument to the function, creating a new molecule from our temporary one. In the next example, each time through the loop, a new molecule is created and stored in a Python list. Then iteration over the list is used to write the molecules back out.

from openeye.oechem import *

ifs = oemolistream()
ofs = oemolostream()

# create an empty list
mollist=[]

# loop over input
for mol in ifs.GetOEGraphMols():
    newmol = OEGraphMol(mol)            # create a new molecule
    mollist.append(newmol)              # append to list

for mol in mollist:
    OEWriteMolecule(ofs, mol)

Note for C++ Users: In the C++ theory manual, this same syntax is in the section describing reading molecules with iterators. The Python generator methods are a new functionality introduced in Python 2.2. In OEChem they are used to provide the C++ iterator functionality without requiring the Python user to explicitly create an iterator object.