Converting Molecules to Names

Converts a file of chemical structures (specified by -in option) into chemical names (-out option), in a choice of language (-language option), encodings (-encoding option) and styles (-style option).

Listing 1: Converting molecules to names

/*******************************************************************************
 * Copyright 2004-2013 OpenEye Scientific Software, Inc.
 ******************************************************************************/
package openeye.examples.oeiupac;

import java.net.URL;

import openeye.oechem.*;
import openeye.oeiupac.*;

public class Mol2Nam_example {
    static { 
        oechem.OEThrow.SetStrict(true);
    }

    private void Mol2Nam(OEInterface itf) {
        oemolistream ifs = new oemolistream();
        if (!ifs.open(itf.GetString("-in")))
            oechem.OEThrow.Fatal("Unable to open input file: "+itf.GetString("-in"));

        oemolostream ofs = new oemolostream();
        String outname = null;
        if (itf.HasString("-out")) {
            outname = itf.GetString("-out");
            if (!ofs.open(outname))
                oechem.OEThrow.Fatal("Unable to open output file: "+outname);
        }

        int language = oeiupac.OEGetIUPACLanguage(itf.GetString("-language"));
        int charset  = oeiupac.OEGetIUPACCharSet(itf.GetString("-encoding"));
        short [] style = oeiupac.OEGetIUPACNamStyle(itf.GetString("-style"));

        OEGraphMol mol = new OEGraphMol();
        while (oechem.OEReadMolecule(ifs, mol)) {
            String name = oeiupac.OECreateIUPACName(mol, style);

            if (language > 0)
                name = oeiupac.OEToLanguage(name, language);
            if (itf.GetBool("-capitalize"))
                name = oeiupac.OECapitalizeName(name);

            if (charset == OECharSet.ASCII)
                name = oeiupac.OEToASCII(name);
            else if (charset == OECharSet.UTF8)
                name = oeiupac.OEToUTF8(name);
            else if (charset == OECharSet.HTML)
                name = oeiupac.OEToHTML(name);
            else if (charset == OECharSet.SJIS)
                name = oeiupac.OEToSJIS(name);
            else if (charset == OECharSet.EUCJP)
                name = oeiupac.OEToEUCJP(name);

            if (outname != null) {
                if (itf.HasString("-delim"))
                    name = mol.GetTitle() + itf.GetString("-delim") + name;
                if (itf.HasString("-tag"))
                    oechem.OESetSDData(mol, itf.GetString("-tag"), name);
                mol.SetTitle(name);
                oechem.OEWriteMolecule(ofs, mol);
            } else {
                System.out.printf("%s\n", name);
            }
        }
        ifs.close();
        ofs.close();
    }

    public static void main(String argv[]) {
        Mol2Nam_example app = new Mol2Nam_example();
        URL fileURL = app.getClass().getResource("Mol2Nam_example.txt");
        try {
            OEInterface itf = new OEInterface(fileURL, "Mol2Nam_example", argv);
            app.Mol2Nam(itf);
        } catch (java.io.IOException e) {
            System.err.println("Unable to open interface file");
        }
    }
}

Converting Names to Molecules

Converts a file of chemical names (specified by the -in option) of a specific language (-language option) into a file of chemical structures (specified by the -out option).

Listing 2: Converting names to molecules

/*******************************************************************************
 * Copyright 2004-2013 OpenEye Scientific Software, Inc.
 ******************************************************************************/
package openeye.examples.oeiupac;

import java.net.URL;

import openeye.oechem.*;
import openeye.oeiupac.*;

public class Nam2Mol_example {
    static { 
        oechem.OEThrow.SetStrict(true);
    }

    private void Nam2Mol(OEInterface itf) {
        oeifstream ifs = new oeifstream();
        if (!ifs.open(itf.GetString("-in")))
            oechem.OEThrow.Fatal("Unable to open input file: "+itf.GetString("-in"));

        oemolostream ofs = new oemolostream();
        if (!ofs.open(itf.GetString("-out")))
            oechem.OEThrow.Fatal("Unable to open output file: "+itf.GetString("-out"));

        int language = oeiupac.OEGetIUPACLanguage(itf.GetString("-language"));
        int charset = oeiupac.OEGetIUPACCharSet(itf.GetString("-charset"));

        OEGraphMol mol = new OEGraphMol();
        StringBuffer name = new StringBuffer();
        while (ifs.getline(name)) {
            mol.Clear();

            // Speculatively reorder CAS permuted index names
            String str = oeiupac.OEReorderIndexName(name.toString());
            if (str.length() == 0)
                str=name.toString();

            if (charset == OECharSet.HTML)
                str = oeiupac.OEFromHTML(str);
            else if (charset == OECharSet.UTF8)
                str = oeiupac.OEFromUTF8(str);

            str = oeiupac.OELowerCaseName(str);

            if (language != OELanguage.AMERICAN)
                str = oeiupac.OEFromLanguage(str,language);

            boolean done = oeiupac.OEParseIUPACName(mol,str);

            if (!done && itf.GetBool("-empty")) {
                mol.Clear();
                done = true;
            }

            if (done) {
                if (itf.HasString("-tag"))
                    oechem.OESetSDData(mol, itf.GetString("-tag"),name.toString());
                mol.SetTitle(name.toString());
                oechem.OEWriteMolecule(ofs,mol);
            }
        }
        ifs.close();
        ofs.close();
    }

    public static void main(String argv[]) {
        Nam2Mol_example app = new Nam2Mol_example();
        URL fileURL = app.getClass().getResource("Nam2Mol_example.txt");
        try {
            OEInterface itf = new OEInterface(fileURL, "Nam2Mol_example", argv);
            app.Nam2Mol(itf);
        } catch (java.io.IOException e) {
            System.err.println("Unable to open interface file");
        }
    }
}

Translating Names Between Languages

Translates a file of chemical names (specified by the -in option) in a specific language (-from option) into a file of names (specified by the -out option) in another language (-to option).

Listing 3: Translate names between languages

/****************************************************************************
 Copyright 2004-2015 OpenEye Scientific Software, Inc.

 Translates between languages.  Internally LexichemTK uses American
 English so it will convert to/from that as an intermediate
 representation.

 By default the program inputs/outputs the internal LexichemTK
 character set representation.  Optionally one can convert the
 input or output to alternate encodings, eg: HTML or UTF8.

 ****************************************************************************/

package openeye.examples.oeiupac;

import java.net.URL;
import java.io.PrintStream;

import openeye.oechem.*;
import openeye.oeiupac.*;

public class Translate_example {
    static {
        oechem.OEThrow.SetStrict(true);
    }

    private void Translate(OEInterface itf) {
        oeifstream ifs = new oeifstream();
        if (!ifs.open(itf.GetString("-in")))
            oechem.OEThrow.Fatal("Unable to open input file: "+itf.GetString("-in"));

        PrintStream out = System.out;
        if (itf.HasString("-o")) {
            try {
                out = new PrintStream(itf.GetString("-o"));
            }
            catch(java.io.IOException e) {
                oechem.OEThrow.Fatal("Unable to open " + itf.GetString("-o") +
                                      " for writing");
            }
        }

        int from_language =
              oeiupac.OEGetIUPACLanguage(itf.GetString("-from_language"));
        int to_language =
              oeiupac.OEGetIUPACLanguage(itf.GetString("-to_language"));

        int from_charset =
              oeiupac.OEGetIUPACCharSet(itf.GetString("-from_charset"));
        int to_charset =
              oeiupac.OEGetIUPACCharSet(itf.GetString("-to_charset"));

        StringBuffer nbuf = new StringBuffer();
        while (ifs.getline(nbuf)) {
            String name = nbuf.toString();

            // Convert from charset to internal representation
            if (from_charset == OECharSet.UTF8)
                name = oeiupac.OEFromUTF8(name);
            else if (from_charset == OECharSet.HTML)
                name = oeiupac.OEFromHTML(name);

            // Translation functions operate on lowercase names
            name = oeiupac.OELowerCaseName(name);

            if (from_language != OELanguage.AMERICAN)
                name = oeiupac.OEFromLanguage(name, from_language);

            // At this point the name is American English in the
            // LexichemTK default internal character set representation.

            // Convert to output language
            if (to_language != OELanguage.AMERICAN)
                name = oeiupac.OEToLanguage(name, to_language);

            // Convert to output charset
            if (to_charset == OECharSet.ASCII)
                name = oeiupac.OEToASCII(name);
            else if (to_charset == OECharSet.UTF8)
                name = oeiupac.OEToUTF8(name);
            else if (to_charset == OECharSet.HTML)
                name = oeiupac.OEToHTML(name);
            else if (to_charset == OECharSet.SJIS)
                name = oeiupac.OEToSJIS(name);
            else if (to_charset == OECharSet.EUCJP)
                name = oeiupac.OEToEUCJP(name);

            out.println(name);
        }
        ifs.close();
    }

    public static void main(String argv[]) {
        Translate_example app = new Translate_example();
        URL fileURL = app.getClass().getResource("Translate_example.txt");
        try {
            OEInterface itf = new OEInterface(fileURL, "Translate_example",
                                              argv);
            app.Translate(itf);
        } catch (java.io.IOException e) {
            System.err.println("Unable to open interface file");
        }
    }
}