The previous example demonstrated reading and writing SMILES strings
from the command line. This requires the programmer to perform the I/O
explicitly. While this may be reasonable for SMILES strings that can
be read on a single line, it is unsuitable for more complex file
formats. To ease this task, OEChem provides the ``molstream''
abstraction. The classes oemolistream
and oemolostream
allow input and output of molecules from files or strings.
The first interface to stream I/O uses functions to read and write
molecules. This is provided by the functions OEReadMolecule and
OEWriteMolecule that both take a molstream and a OEMolBase as
arguments. As a high-level function OEReadMolecule calls mol.Clear()
automatically for each incoming molecule.
#!/usr/bin/env python # ch4-1.py from openeye.oechem import * ifs = oemolistream() ofs = oemolostream() mol = OEGraphMol() while (OEReadMolecule(ifs, mol) == 1): OEWriteMolecule(ofs, mol)
In this example, the script will read molecules from stdin
in SMILES
format and write them to stdout
in (absolute) SMILES format. Notice
that in this example, there's no need to call the Clear method
to reset the molecule, or OEAssignAromaticFlags to normalize
aromaticity. This is done automatically by the OEReadMolecule method.