Command Line Interface and Options

This chapter describes how Options classes that are derived from OEOptions can be configured with command line inputs passed in through argv.

The OEConfigureOpts function is a convenience function that takes an OEOptions derivative options class and the command line arguments argv, parses the command line arguments, and configures the option parameters with command line inputs. A OEInterface is generated internally from the OEOptions, which performs the command lines parsing.

Note that this only applies to Options classes that are derived from the OEOptions. There are a handful of Options classes that still does not derive from OEOptions, and hence cannot be configured with command line inputs directly. Command Line Interface Definitions is an alternative way to work with command line interfaces for such cases.

In addition to the convenience function OEConfigureOpts, two additional general purpose Options classes, OESimpleAppOptions, and OERefInputAppOptions, are provided that extends any other options classes to include necessary input/output parameters that are generally required to create an application.

Application with Input, output, and Options

The following example shows how to use the OESimpleAppOptions class to extend an Options class with an additional input and output file.

The specific example is taken from OEDocking, and uses the the OEMakeReceptor function to make a receptor in a design unit. In this example, the OEMakeReceptorOptions is extended with an input and output.

Listing 1: Application with input, output, and options (Making Receptor)

/* 
(C) 2022 Cadence Design Systems, Inc. (Cadence) 
All rights reserved.
TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
provided to current licensees or subscribers of Cadence products or
SaaS offerings (each a "Customer").
Customer is hereby permitted to use, copy, and modify the Sample Code,
subject to these terms. Cadence claims no rights to Customer's
modifications. Modification of Sample Code is at Customer's sole and
exclusive risk. Sample Code may require Customer to have a then
current license or subscription to the applicable Cadence offering.
THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
liable for any damages or liability in connection with the Sample Code
or its use.
*/
package openeye.examples.oedocking;

import openeye.oechem.*;
import openeye.oedocking.*;
import openeye.oebio.*;

public class MakeReceptor {

    public static void main(String[] args) {
        OEMakeReceptorOptions recOpts = new OEMakeReceptorOptions();
        OESimpleAppOptions opts = new OESimpleAppOptions(recOpts, "MakeReceptor", OEFileStringType.DU, OEFileStringType.DU);

        if (oechem.OEConfigureOpts(opts, args, false) == OEOptsConfigureStatus.Help)
            System.exit(0);

        recOpts.UpdateValues(opts);

        oeifstream ifs = new oeifstream(opts.GetInFile());
        oeofstream ofs = new oeofstream(opts.GetOutFile());
        OEDesignUnit du = new OEDesignUnit();
        while (oebio.OEReadDesignUnit(ifs, du))
            if (oedocking.OEMakeReceptor(du, recOpts))
                oebio.OEWriteDesignUnit(ofs, du);
            else
                oechem.OEThrow.Fatal(du.GetTitle() + " failed to make a receptor");

        ofs.close();
        ifs.close();
    }
}

Download code

MakeReceptor.java

Application with Reference system, Input, output, and Options

Besides an input and output, some applications require an additional reference system input. The OERefInputAppOptions class can be used to extend an options class with three additional parameters, an input, output, and a reference system.

The following code example is taken from OEDocking, and uses the OEDock object to perform docking. In this example, the OEDockOptions is extended with an input, output, and a receptor (i.e., the reference system).

Listing 2: Application with input, reference, output, and options (Docking Molecules)

/* 
(C) 2022 Cadence Design Systems, Inc. (Cadence) 
All rights reserved.
TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
provided to current licensees or subscribers of Cadence products or
SaaS offerings (each a "Customer").
Customer is hereby permitted to use, copy, and modify the Sample Code,
subject to these terms. Cadence claims no rights to Customer's
modifications. Modification of Sample Code is at Customer's sole and
exclusive risk. Sample Code may require Customer to have a then
current license or subscription to the applicable Cadence offering.
THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
liable for any damages or liability in connection with the Sample Code
or its use.
*/
package openeye.examples.oedocking;

import java.net.URL;
import openeye.oechem.*;
import openeye.oebio.*;
import openeye.oedocking.*;

public class DockMolecules {

    public static void main(String[] args) {

        OEDockOptions dockOpts = new OEDockOptions();
        
        OERefInputAppOptions opts = new OERefInputAppOptions(dockOpts, "DockMolecules", OEFileStringType.Mol3D,
            OEFileStringType.Mol3D, OEFileStringType.DU, "-receptor");

        if (oechem.OEConfigureOpts(opts, args, false) == OEOptsConfigureStatus.Help)
            System.exit(0);

        dockOpts.UpdateValues(opts);

        oemolistream ifs = new oemolistream();
        if(!ifs.open(opts.GetInFile()))
            oechem.OEThrow.Fatal("Unable to open " + opts.GetInFile() + " for reading");
        oemolostream ofs = new oemolostream();
        if(!ofs.open(opts.GetOutFile()))
            oechem.OEThrow.Fatal("Unable to open " + opts.GetOutFile() + " for writing");
        oeifstream rfs = new oeifstream();
        if (!rfs.open(opts.GetRefFile()))
            oechem.OEThrow.Fatal("Unable to open " + opts.GetRefFile() + " for reading");

        OEDesignUnit du = new OEDesignUnit();
        if (! oebio.OEReadDesignUnit(opts.GetRefFile(), du))
            oechem.OEThrow.Fatal("Failed to read design unit ");
        if (!du.HasReceptor())
            oechem.OEThrow.Fatal("Design unit " + du.GetTitle() + " does not contain a receptor");

        OEDock dock = new OEDock(dockOpts);
        dock.Initialize(du);

        OEMol mol = new OEMol();
        while (oechem.OEReadMolecule(ifs, mol)) {

            System.err.format("docking %s\n", mol.GetTitle());
            OEGraphMol dockedMol = new OEGraphMol();
            int retCode = dock.DockMultiConformerMolecule(dockedMol, mol);
            if (retCode != OEDockingReturnCode.Success) {
                oechem.OEThrow.Error("Docking failed with error code " + oedocking.OEDockingReturnCodeGetName(retCode));
            }
            String sdtag = oedocking.OEDockMethodGetName(dockOpts.GetScoreMethod());
            oedocking.OESetSDScore(dockedMol, dock, sdtag);
            dock.AnnotatePose(dockedMol);
            oechem.OEWriteMolecule(ofs, dockedMol);
        }
        ofs.close();
        ifs.close();
    }
}

Download code

DockMolecules.java

Application with Input and output only

Some applications do not have any additional options or parameters, and only require an input and output. The OESimpleAppOptions class has an overload that does not require an OEOptions input argument.

The following code example is taken from OEQuacpac, and uses the OEAssignCharges method to assign charges in an OEDesignUnit. This example only configures an input and output, and does not have any additional parameters to configure.

Adding custom parameters

Custom parameters can also be added to an application API when a built-in Options class does not contain all required parameters for the application.

The following code example is taken from OEDocking, and uses the OEPosit object to generate pose structures. In this example, the OEPositOptions is extended with an input, output, and receptor (i.e., the reference system). However, the number of poses to generate is not a built-in parameter into the OEPositOptions. This example shows how to add an additional parameter, for the number of poses, to be configured through the command line.

Listing 4: Adding custom parameters (POSIT with Multiple Receptors)

/* 
(C) 2022 Cadence Design Systems, Inc. (Cadence) 
All rights reserved.
TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
provided to current licensees or subscribers of Cadence products or
SaaS offerings (each a "Customer").
Customer is hereby permitted to use, copy, and modify the Sample Code,
subject to these terms. Cadence claims no rights to Customer's
modifications. Modification of Sample Code is at Customer's sole and
exclusive risk. Sample Code may require Customer to have a then
current license or subscription to the applicable Cadence offering.
THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
liable for any damages or liability in connection with the Sample Code
or its use.
*/
package openeye.examples.oedocking;

import java.net.URL;
import java.util.Iterator;
import openeye.oechem.*;
import openeye.oebio.*;
import openeye.oedocking.*;

public class GenerateMultiplePose {

    public static class MyOptions extends OEPositOptions {

        private OEParameter _param1;

        public MyOptions() {
            super();
            OEIntParameter param1 = new OEIntParameter("-numPoses", 1);
            param1.AddLegalRange("1", "20");
            param1.SetBrief("Number of poses to generate");
            _param1 = AddParameter(param1);
        }

        public int GetNumPoses() {
            if (_param1.GetHasValue())
                return (Integer.parseInt(_param1.GetStringValue()));
            return (Integer.parseInt(_param1.GetStringDefault()));
        }

    }

    public static void main(String[] args) {

        MyOptions positOpts = new MyOptions();
        OERefInputAppOptions opts = new OERefInputAppOptions(positOpts, "PoseMolecules", OEFileStringType.Mol3D,
                OEFileStringType.DU, OEFileStringType.DU, "-receptor");

        if (oechem.OEConfigureOpts(opts, args, false) == OEOptsConfigureStatus.Help)
            System.exit(0);

        positOpts.UpdateValues(opts);

        oemolistream ifs = new oemolistream();
        if (!ifs.open(opts.GetInFile()))
            oechem.OEThrow.Fatal("Unable to open " + opts.GetInFile() + " for reading");

        oeifstream rfs = new oeifstream();
        if (!rfs.open(opts.GetRefFile()))
            oechem.OEThrow.Fatal("Unable to open " + opts.GetRefFile() + " for reading");

        oeofstream ofs = new oeofstream();
        if (!ofs.open(opts.GetOutFile()))
            oechem.OEThrow.Fatal("Unable to open " + opts.GetOutFile() + " for writing");

        OEPosit poser = new OEPosit(positOpts);

        OEDesignUnit du = new OEDesignUnit();
        int count = 0;
        while (oebio.OEReadDesignUnit(rfs, du)) {
            if (!du.HasReceptor())
                oechem.OEThrow.Fatal("Design unit " + du.GetTitle() + " does not contain a receptor");
            poser.AddReceptor(du);
            count++;
        }
        if (count == 0)
            oechem.OEThrow.Fatal("Input design unit does not contain any receptor");

        oechem.OEThrow.Info("Number of conformers: " + positOpts.GetNumPoses());
        oechem.OEThrow.Info("Best receptor pose flag: " + positOpts.GetBestReceptorPoseOnly());

        OEMol mcmol = new OEMol();
        while (oechem.OEReadMolecule(ifs, mcmol)) {
            oechem.OEThrow.Info("posing " + mcmol.GetTitle());
            OEPositResults results = new OEPositResults();

            int returnCode = poser.Dock(results, mcmol, positOpts.GetNumPoses());

            if (returnCode == OEDockingReturnCode.Success) {
                for (OESinglePoseResult result : results.GetSinglePoseResults()) {
                    OEDesignUnit posedDU = new OEDesignUnit(result.GetDesignUnit());
                    posedDU.SetDoubleData(poser.GetName(), result.GetProbability());
                    oechem.OEThrow.Info(
                            "Receptor used: " + posedDU.GetTitle() + " pose probability: " + result.GetProbability());
                    oebio.OEWriteDesignUnit(ofs, posedDU);
                }

            } else {
                String errMsg = oedocking.OEDockingReturnCodeGetName(returnCode);
                oechem.OEThrow.Warning(mcmol.GetTitle() + " : " + errMsg);
            }
        }

        ofs.close();
        ifs.close();
        rfs.close();
    }
}

Defining new options class

Options classes, derived from OEOptions, can also be defined at the script level, and wrapped with the OESimpleAppOptions or OERefInputAppOptions, to build applications.

The following code example is taken from OEFF, and optimizes molecule conformations using a force field. In this example, a new Options class is defined to accept command line input for the force field.