Using the methods outlined above, it is possible to allow the stream format to be controlled from the command line. OEChem's oemolstreams control the format by interpreting the input and output file names.
The following is a simple example of using command-line arguments to allow OEChem programs to support many file formats at run-time.
1 /**************************************************************
2 * Copyright 2005, OpenEye Scientific Software, Inc.
3 *************************************************************/
4 import openeye.oechem.*;
5
6 public class SimpleBabel {
7 public void processFile(oemolistream ifs, oemolostream ofs) {
8 OEGraphMol mol = new OEGraphMol();
9 while (oechem.OEReadMolecule(ifs, mol))
10 oechem.OEWriteMolecule(ofs, mol);
11 }
12 private void displayUsage() {
13 System.err.println("usage: SimpleBabel <infile> <outfile>");
14 System.exit(1);
15 }
16 public static void main(String argv[]) {
17 SimpleBabel app = new SimpleBabel();
18 if (argv.length != 2)
19 app.displayUsage();
20
21 oemolistream ifs = new oemolistream(argv[0]);
22 if (!ifs.IsValid())
23 System.err.println("Unable to open input stream");
24
25 oemolostream ofs = new oemolostream(argv[1]);
26 if (!ofs.IsValid())
27 System.err.println("Unable to open output stream");
28
29 app.processFile(ifs, ofs);
30 }
31 }
The example above allows a user to specify the input and output files and formats from the command line.
For instance:
prompt>java SimpleBabel file1.sdf file1.smi
will convert file1.sdf from MDL's SD format to SMILES format.
A first extension of this idea allows access to stdin and
stdout via the "-" filename.
For instance:
prompt>java SimpleBabel file2.mol2 -
This command will read file2.mol2 in MOL2 format and write the
molecules to stdout in SMILES, the default format.
Thus if you have another program GetFromDatabase which gets
molecules from a database and writes them in SMILES format, you can chain
it with any OEChem program. Using your operating system's redirection
commands (e.g. - Unix pipe "|" or redirect ">") you can move
molecules directly from GetFromDatabase to foo.py without a
temporary file.
prompt>GetFromDatabase | java SimpleBabel - file3.sdf
This command will take the SMILES format output from
GetFromDatabase, send it to SimpleBabel on stdin with the
default format of OEFormat.SMI and generate an SD format output file.
However, to make this concept of using stdin and stdout
for piping data really useful, one needs to be able to control the
format of stdin and stdout similarly to the way it would
be controlled for temporary files. To facilitate this, oemolstreams
interpret filenames which are ONLY format extensions to indicate
format control for stdin and stdout.
Now, using our program from listing
prompt>java SimpleBabel .smi .mol2
This command opens stdin with SMILES format and opens
stdout with MOL2 format.
Now we have complete format control of stdin and stdout
from the command line. If we have a program
GenerateStructures, which only writes MOL2 format and another
program GenerateData, which only reads SD format, we can use
them from the command line with any OEChem program which uses
command-line arguments for file specification.
prompt> GenerateStructures | java SimpleBabel .mol2 .sd | GenerateData
This command demonstrates how any OEChem program with command-line file specification can be used to pipe formatted input and output.