4.4 Molecule Input and Output with Files

In addition to stdin and stdout, OEChem's oemolstreams also support reading from files. To open a file, use the filename as a constructor argument or call the open method with the filename as an argument. For input (oemolistream) if the file doesn't exist, the open fails and returns 0 (false). For output (oemolostream) the output file is created if it didn't previously exist and is overwritten if it did. If no filename is passed as an argument to the constructor (or to the open method), an oemolistream will use stdin and an oemolostream will use stdout. Much like standard file I/O in Python, oemolstreams can be closed after use with the close method. When an oemolstream goes out of scope and is deleted by Python, it is automatically closed as well.

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

ifs = oemolistream()
ofs = oemolostream()

if (ifs.open("drugs.sdf") == 1):
    if (ofs.open("drugs.mol2") == 1):
        for mol in ifs.GetOEMols():
            print mol.NumConfs()
            OEWriteMolecule(ofs, mol)
    else:
        sys.stderr.write("Unable to open output file\n")
else:
    sys.stderr.write("Unable to open input file\n")

One convenient use of the open method of molstreams is that it sets the file format associated with the stream from the file extension of the filename used as the argument. The example above converts the file ``drugs.sdf'' in MDL SD format into the file, ``drugs.mol2'' in Tripos Mol2 format. This behavior can be overridden by calling SetFormat after the call to open but before the first molecule is read or written from/to the stream.