Converting Molecules to Names

Listing 1: Converting molecules to names

/***********************************************************************
Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
OpenEye Scientific Software, Inc.
***********************************************************************/
#include "openeye.h"

#include <stdlib.h>
#include <string>

#include "oeplatform.h"  
#include "oesystem.h"
#include "oechem.h"
#include "oeiupac.h"

#include "mol2nam_example.itf"

using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEIUPAC;
using namespace std;

int main(int argc,char *argv[])
{
  OESetMemPoolMode(OEMemPoolMode::SingleThreaded|
                   OEMemPoolMode::UnboundedCache);

  OEThrow.Info("Lexichem mol2nam example");
  OEThrow.Info("   Lexichem version: %s", OEIUPACGetRelease());

  OEInterface itf(InterfaceData,argc,argv);

  unsigned int language=OEGetIUPACLanguage(itf.Get<string>("-language"));
  unsigned int charset=OEGetIUPACCharSet(itf.Get<string>("-encoding"));
  const unsigned char *style=OEGetIUPACNamStyle(itf.Get<string>("-style"));

  oemolistream ifs(itf.Get<string>("-in"));
  if (!ifs)
    OEThrow.Fatal("Unable to open %s for reading",
                  itf.Get<string>("-in").c_str());

  oemolostream ofs;
  string outname="";
  if (itf.Has<string>("-out"))
  {
    outname=itf.Get<string>("-out");
    if (!ofs.open(outname))
      OEThrow.Fatal("Unable to open %s for writing", outname.c_str());
  }

  OEGraphMol mol;
  std::string tmp;
  while (OEReadMolecule(ifs, mol))
  {
    std::string name = OEIUPAC::OECreateIUPACName(mol,style);

    if (language)
      name = OEIUPAC::OEToLanguage(name.c_str(),language);
    if (itf.Get<bool>("-capitalize"))
      name = OEIUPAC::OECapitalizeName(name.c_str());

    switch (charset)
    {
    case OECharSet::ASCII:
      name = OEIUPAC::OEToASCII(name.c_str());
      break;
    case OECharSet::UTF8:
      name = OEIUPAC::OEToUTF8(name.c_str());
      break;
    case OECharSet::HTML:
      name = OEIUPAC::OEToHTML(name.c_str());
      break;
    case OECharSet::SJIS:
      name = OEIUPAC::OEToSJIS(name.c_str());
      break;
    case OECharSet::EUCJP:
      name = OEIUPAC::OEToEUCJP(name.c_str());
      break;
    }
   
    if (outname.size()>0)
    {
      if (itf.Has<string>("-delim"))
      {
        const char *title = mol.GetTitle();
        if(title && *title)
        {
          tmp = name;
          name = title;
          name.append(itf.Get<string>("-delim"));
          name.append(tmp);
        }
      }

      if (itf.Has<string>("-tag"))
        OESetSDData(mol,itf.Get<string>("-tag"),name);

      mol.SetTitle(name);
      OEWriteMolecule(ofs, mol);
    }
    else printf("%s\n",name.c_str());
  }

  return 0;
}

Converting Names to Molecules

Listing 2: Converting names to molecules

/***********************************************************************
Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
OpenEye Scientific Software, Inc.
***********************************************************************/
#include "openeye.h"

#include "oeplatform.h"
#include "oesystem.h"
#include "oechem.h"
#include "oeiupac.h"

#include "nam2mol_example.itf"

using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEIUPAC;
using namespace std;

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

int main(int argc, char *argv[])
{
  OESetMemPoolMode(OEMemPoolMode::SingleThreaded|OEMemPoolMode::UnboundedCache);

  OEThrow.Info("Lexichem nam2mol example");
  OEThrow.Info("   Lexichem version: %s", OEIUPACGetRelease());

  OEInterface itf(InterfaceData, argc, argv);

  oeifstream infile;
  string inname=itf.Get<string>("-in");
  if (inname=="-")
  {
    if (!infile.openfd(STDIN_FILENO, true)) // read from stdin
      OEThrow.Fatal("Unable to read from stdin");  
  }
  else
  {   
    if (!infile.open(inname))
      OEThrow.Fatal("Unable to open input file: %s\n", inname.c_str());
  }

  oemolostream outfile;
  if (!outfile.open(itf.Get<string>("-out")))
    OEThrow.Fatal("Unable to create output file: %s\n", 
                  itf.Get<string>("-out").c_str());

  unsigned int language = OEGetIUPACLanguage(itf.Get<string>("-language"));
   
  OEGraphMol mol;
  char buffer[8192];
  bool done;

  while (infile.getline(buffer,8192))
  {
    mol.Clear();

    // Speculatively reorder CAS permuted index names
    std::string str = OEReorderIndexName(buffer);
    if (str.empty()) str = buffer;

    if (language != OELanguage::AMERICAN)
    {
      str = OEFromUTF8(str.c_str());
      str = OELowerCaseName(str.c_str());
      str = OEFromLanguage(str.c_str(),language);
    }

    done = OEParseIUPACName(mol,str.c_str());

    if (!done && itf.Get<bool>("-empty"))
    {
      mol.Clear();
      done = true;
    }
    if (done)
    {
      if (itf.Has<string>("-tag"))
        OESetSDData(mol,itf.Get<string>("-tag"),buffer);
      mol.SetTitle(buffer);
      OEWriteMolecule(outfile,mol);
    }
  }

  return 0;
}