3.1 Using OEChem oemolstreams

The previous example demonstrated reading and writing SMILES strings from a file. This required the programmer to perform the file I/O explicitly. Whilst this may be reasonable for SMILES strings that may be read via STL's getline, it isn't suitable for more complex file formats. To ease this task, OEChem provides the oemolstream abstraction. The classes oemolistream and oemolostream allow input and output of molecules using C++'s « and » operators, respectively.

 1 #include "openeye.h"
 2 #include "oechem.h"
 3
 4 using namespace OEChem;
 5
 6 int main()
 7 {
 8   oemolistream ims;
 9   oemolostream oms;
10   OEGraphMol mol;
11
12   while (ims >> mol)
13     oms << mol;
14   return 0;
15 }

Listing:3.1 Simple oemolstream example

In this example, the program will read molecules from cin (stdin) in SMILES format and write them to cout (stdout) in (absolute) SMILES format. Notice that in this example, there's no need to call the mol.Clear method to reset the molecule, OEAssignAromaticFlags to normalize aromaticity, or if(mol) to test the validity of the molecule. This is done automatically for you by the » operator.

Some C++ programmers prefer--and C, Python and Java programmers are required--to use a functional interface rather than use the « and » operators for file I/O. These are provided by the functions OEReadMolecule and OEWriteMolecule that both take an oemolstream and OEMolBase as arguments. Identically, OEReadMolecule calls mol.Clear and perceives appropriate properties for each connection table, and skips any invalid molecules automatically.

 1 #include "openeye.h"
 2 #include "oechem.h"
 3
 4 using namespace OEChem;
 5
 6 int main()
 7 {
 8   oemolistream ims;
 9   oemolostream oms;
10   OEGraphMol mol;
11
12   while (OEReadMolecule(ims,mol))
13     OEWriteMolecule(oms,mol);
14   return 0;
15 }

Listing:3.2 Functional interface to oemolstreams