OEDepict Examples

Generating 2D Coordinates

/****************************************************************************
* Copyright (C) 2004-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Generates 2D coordinates
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "depict.itf"

using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;
using namespace std;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigurePrepareDepictionOptions(itf, OEPrepareDepictionSetup::SuppressHydrogens |
                                          OEPrepareDepictionSetup::DepictOrientation);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open input file!");

  oemolostream ofs(".sdf");
  if (itf.Has<string>("-out"))
  {
    string oname = itf.Get<string>("-out");
    if (!ofs.open(oname.c_str()))
      OEThrow.Fatal("Cannot open output file!");
    if (!OEIs2DFormat(ofs.GetFormat()))
      OEThrow.Fatal("Invalid output format for 2D coordinates!");
  }

  if (itf.Has<string>("-ringdict"))
  {
    const string rdfname = itf.Get<string>("-ringdict");
    if (!OEInit2DRingDictionary(rdfname))
      OEThrow.Warning("Cannot use user-defined ring dictionary!");
  }

  OEPrepareDepictionOptions popts;
  OESetupPrepareDepictionOptions(popts, itf);
  popts.SetClearCoords(true);

  OEGraphMol mol;
  while (OEReadMolecule(ifs, mol))
  {
    OEPrepareDepiction(mol, popts);
    OEWriteMolecule(ofs, mol);
  }

  return 0;
}

Depicting Substructure Search Match

/****************************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Depict a molecule and highlight the substructure specified by
* the given SMARTS pattern
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "match2img.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureImageOptions(itf);
  OEConfigurePrepareDepictionOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);
  OEConfigureHighlightParams(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  const string oname = itf.Get<string>("-out");

  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredImageFile(ext))
    OEThrow.Fatal("Unknown image type!");

  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open input file!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");

  OEGraphMol mol;
  if (!OEReadMolecule(ifs, mol))
    OEThrow.Fatal("Cannot read input file!");

  const string smarts = itf.Get<string>("-smarts");
  OESubSearch ss;
  if (!ss.Init(smarts.c_str()))
    OEThrow.Fatal("Cannot parse smarts: %s !", smarts.c_str());

  OEPrepareDepictionOptions popts;
  OESetupPrepareDepictionOptions(popts, itf);

  OEPrepareDepiction(mol, popts);

  const double width  = OEGetImageWidth (itf);
  const double height = OEGetImageHeight(itf);
  OE2DMolDisplayOptions dopts(width, height, OEScale::AutoScale);
  OESetup2DMolDisplayOptions(dopts, itf);
  dopts.SetMargins(10.0);

  OE2DMolDisplay disp(mol, dopts);

  const unsigned int hstyle = OEGetHighlightStyle(itf);
  const OEColor      hcolor = OEGetHighlightColor(itf);

  OEPrepareSearch(mol, ss);

  const bool unique = true;
  for (OEIter<OEMatchBase> match = ss.Match(mol, unique); match; ++match)
    OEAddHighlighting(disp, hcolor, hstyle, match);

  OERenderMolecule(ofs, ext, disp);

  return 0;
}

Aligning Molecules Based on MCS

/****************************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Aligns the fit molecule(s) based on their maximum common substructure
* with the reference molecule.
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mcsalign2D.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char* argv[])
{
  OEInterface itf(InterfaceData);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string rname = itf.Get<string>("-ref");
  const string fname = itf.Get<string>("-fit");
  const string oname = itf.Get<string>("-out");

  oemolistream rifs;
  if (!rifs.open(rname.c_str()))
    OEThrow.Fatal("Cannot open reference molecule file!");

  OEGraphMol refmol;
  if (!OEReadMolecule(rifs, refmol))
    OEThrow.Fatal("Cannot read reference molecule!");

  OEPrepareDepiction(refmol);

  OEMCSSearch mcss(OEMCSType::Approximate);
  unsigned int atomexpr = OEExprOpts::DefaultAtoms;
  unsigned int bondexpr = OEExprOpts::DefaultBonds;
  mcss.Init(refmol, atomexpr, bondexpr);
  mcss.SetMCSFunc(OEMCSMaxBondsCompleteCycles());

  oemolistream fifs;
  if (!fifs.open(fname.c_str()))
    OEThrow.Fatal("Cannot open align molecule file!");

  oemolostream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");
  if (!OEIs2DFormat(ofs.GetFormat()))
    OEThrow.Fatal("Invalid output format for 2D coordinates!");

  OEWriteConstMolecule(ofs, refmol);

  OEGraphMol fitmol;
  while (OEReadMolecule(fifs, fitmol))
  {
    OEAlignmentResult alignres = OEPrepareAlignedDepiction(fitmol, mcss);
    if (alignres.IsValid())
    {
      OEThrow.Info("%s mcs size: %d", fitmol.GetTitle(), alignres.NumAtoms());
      OEWriteMolecule(ofs, fitmol);
    }
  }

  return 0;
}

Depicting MDL Query

/****************************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Converts an MDL query structure into an image file.
* The output file format depends on its file extension.
****************************************************************************/

#include <openeye.h>

#include <string>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mdlquery2img.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureImageOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  const string oname = itf.Get<string>("-out");

  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredImageFile(ext))
    OEThrow.Fatal("Unknown image type!");

  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open input file!");

  if (ifs.GetFormat() != OEFormat::MDL)
    OEThrow.Fatal("Input file is not an MDL query file!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");

  OEGraphMol mol;
  if (!OEReadMDLQueryFile(ifs, mol))
    OEThrow.Fatal("Cannot read mdl query input file!");

  const bool clearcoords = false;
  const bool suppressH = false;
  OEPrepareDepiction(mol, clearcoords, suppressH);

  const double width  = OEGetImageWidth (itf);
  const double height = OEGetImageHeight(itf);
  OE2DMolDisplayOptions opts(width, height, OEScale::AutoScale);
  OESetup2DMolDisplayOptions(opts, itf);

  OE2DMolDisplay disp(mol, opts);
  OERenderMolecule(ofs, ext, disp);

  return 0;
}

Depicting MDL Reaction

/****************************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Converts an MDL reaction into an image file.
* The output file format depends on its file extension.
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mdlreaction2img.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureImageOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  const string oname = itf.Get<string>("-out");

  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredImageFile(ext))
    OEThrow.Fatal("Unknown image type!");

  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open input file!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");

  OEGraphMol mol;
  if (!OEReadMDLReactionQueryFile(ifs, mol))
    OEThrow.Fatal("Cannot read mdl reaction!");

  const bool suppressH   = false;
  const bool clearcoords = false;
  OEPrepareDepiction(mol, clearcoords, suppressH);

  const double width  = OEGetImageWidth (itf);
  const double height = OEGetImageHeight(itf);
  OE2DMolDisplayOptions opts(width, height, OEScale::AutoScale);
  OESetup2DMolDisplayOptions(opts, itf);

  OE2DMolDisplay disp(mol, opts);
  OERenderMolecule(ofs, ext, disp);

  return 0;
}

Depicting a Single Molecule

/****************************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Converts a molecule structure into an image file.
* The output file format depends on its file extension.
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mol2img.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureImageOptions(itf);
  OEConfigurePrepareDepictionOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  const string oname = itf.Get<string>("-out");

  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredImageFile(ext))
    OEThrow.Fatal("Unknown image type!");

  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open input file!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");

  OEGraphMol mol;
  if (!OEReadMolecule(ifs, mol))
    OEThrow.Fatal("Cannot read input file!");

  if (itf.Has<string>("-ringdict"))
  {
    const string rdfname = itf.Get<string>("-ringdict");
    if (!OEInit2DRingDictionary(rdfname))
      OEThrow.Warning("Cannot use user-defined ring dictionary!");
  }

  OEPrepareDepictionOptions popts;
  OESetupPrepareDepictionOptions(popts, itf);

  OEPrepareDepiction(mol, popts);

  const double width  = OEGetImageWidth (itf);
  const double height = OEGetImageHeight(itf);
  OE2DMolDisplayOptions dopts(width, height, OEScale::AutoScale);
  OESetup2DMolDisplayOptions(dopts, itf);

  OE2DMolDisplay disp(mol, dopts);
  OERenderMolecule(ofs, ext, disp);

  return 0;
}

Depicting Molecules in Multi-Page

/***********************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
************************************************************************
* Converts molecules into a multi-page PDF document.
***********************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mols2pdf.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;


int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureReportOptions(itf);
  OEConfigurePrepareDepictionOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open %s input file!", iname.c_str());

  const string oname = itf.Get<string>("-out");
  const string ext = OESystem::OEFileExtension(oname, "");
  if (ext != "pdf")
    OEThrow.Fatal("Output must be PDF format!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open %s output file!", oname.c_str());

  if (itf.Has<string>("-ringdict"))
  {
    const string rdfname = itf.Get<string>("-ringdict");
    if (!OEInit2DRingDictionary(rdfname))
      OEThrow.Warning("Cannot use user-defined ring dictionary!");
  }

  OEReportOptions ropts;
  OESetupReportOptions(ropts, itf);
  ropts.SetFooterHeight(25.0);
  OEReport report(ropts);

  OEPrepareDepictionOptions popts;
  OESetupPrepareDepictionOptions(popts, itf);

  OE2DMolDisplayOptions dopts;
  OESetup2DMolDisplayOptions(dopts, itf);
  dopts.SetDimensions(report.GetCellWidth(), report.GetCellHeight(), OEScale::AutoScale);

  OEGraphMol mol;
  while (OEReadMolecule(ifs, mol))
  {
    OEImageBase* cell = report.NewCell();
    OEPrepareDepiction(mol, popts);
    OE2DMolDisplay disp(mol, dopts);
    OERenderMolecule(*cell, disp);
  }

  unsigned int pagenum = 1;
  OEFont font(OEFontFamily::Default, OEFontStyle::Bold, 12, OEAlignment::Center, OEBlack);
  for (OEIter<OEImageBase> fi = report.GetFooters(); fi; ++fi, ++pagenum)
  {
    ostringstream stream;
    stream << "Page " << pagenum  << " of " << report.NumPages();
    OEDrawTextToCenter(*fi, stream.str(), font);
  }

  OEWriteReport(ofs, ext, report);

  return 0;
}

Depicting Molecules In a Grid

/****************************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Converts molecules into an image with grid layout.
* The output file format depends on its file extension.
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mols2img.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureImageWidth (itf, 800.0);
  OEConfigureImageHeight(itf, 800.0);
  OEConfigureImageGridParams(itf);
  OEConfigurePrepareDepictionOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string oname = itf.Get<string>("-out");
  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredImageFile(ext))
    OEThrow.Fatal("Unknown image type!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");

  const double width  = OEGetImageWidth (itf);
  const double height = OEGetImageHeight(itf);
  OEImage image(width, height);

  const unsigned int rows = OEGetImageGridNumRows(itf);
  const unsigned int cols = OEGetImageGridNumColumns(itf);
  OEImageGrid grid(image, rows, cols);

  OEPrepareDepictionOptions popts;
  OESetupPrepareDepictionOptions(popts, itf);

  OE2DMolDisplayOptions dopts;
  OESetup2DMolDisplayOptions(dopts, itf);
  dopts.SetDimensions(grid.GetCellWidth(), grid.GetCellHeight(), OEScale::AutoScale);

  OEIter<OEImageBase> celliter = grid.GetCells();
  for (unsigned int i = 0; itf.Has<string>("-in", i); ++i)
  {
    string iname = itf.Get<string>("-in", i);
    oemolistream ifs;
    if (!ifs.open(iname))
    {
      OEThrow.Warning("Cannot open %s input file!", iname.c_str());
      continue;
    }

    OEGraphMol mol;
    while (OEReadMolecule(ifs, mol) && (bool)celliter)
    {
      OEPrepareDepiction(mol, popts);
      OE2DMolDisplay disp(mol, dopts);
      OERenderMolecule(celliter, disp);
      ++celliter;
    }
  }

  OEWriteImage(ofs, ext, image);

  return 0;
}

Generating 2D Ring Dictionary Report

/****************************************************************************
 Copyright (C) 2015 OpenEye Scientific Software, Inc.
*****************************************************************************
* Converts a ring dictionary into a multi-page PDF document.
****************************************************************************/

#include <openeye.h>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "ringdict2pdf.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char* argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureReportOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string ifname = itf.Get<string>("-ringdict");
  const string ofname = itf.Get<string>("-out");

  if (!OEIsValid2DRingDictionary(ifname))
    OEThrow.Fatal("Invalid ring dictionary file!");

  OE2DRingDictionary ringdict(ifname);

  OEReportOptions ropts;
  OESetupReportOptions(ropts, itf);

  OEWrite2DRingDictionaryReport(ofname, ringdict, ropts);

  return 0;
}

Depicting MDL Query Substructure Search Hits

/***********************************************************************
* Copyright (C) 2011-2015 OpenEye Scientific Software, Inc.
************************************************************************
* Performs substructure search on a molecule using a MDL query and
* generates an image file depicting one match per molecule.
* The output file format depends on its file extension.
***********************************************************************/

#include <openeye.h>

#include <string>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "viewmdlsearch.itf"

using namespace std;
using namespace OEChem;
using namespace OESystem;
using namespace OEPlatform;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureReportOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);
  OEConfigureHighlightParams(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    return 1;

  const string qname = itf.Get<string>("-query");
  const string tname = itf.Get<string>("-target");
  const string oname = itf.Get<string>("-out");

  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredMultiPageImageFile(ext))
    OEThrow.Fatal("Unknown multipage image type!");

  oemolistream qfile;
  if (!qfile.open(qname))
    OEThrow.Fatal("Cannot open mdl query file!");
  if (qfile.GetFormat() != OEFormat::MDL && qfile.GetFormat() != OEFormat::SDF)
    OEThrow.Fatal("Query file has to be an MDL file!");

  oemolistream ifs;
  if (!ifs.open(tname))
    OEThrow.Fatal("Cannot open target input file!");

  OEGraphMol depictquery;
  if (!OEReadMDLQueryFile(qfile, depictquery))
    OEThrow.Fatal("Cannot read query molecule!");
  OEPrepareDepiction(depictquery);

  const unsigned int queryopts = OEMDLQueryOpts::Default | OEMDLQueryOpts::SuppressExplicitH;
  OEQMol qmol;
  OEBuildMDLQueryExpressions(qmol, depictquery, queryopts);

  OESubSearch  ss;
  if (!ss.Init(qmol))
    OEThrow.Fatal("Cannot initialize substructure search!");

  const unsigned hstyle = OEGetHighlightStyle(itf);
  const OEColor  hcolor = OEGetHighlightColor(itf);

  const bool align  = itf.Get<bool>("-align");

  OEReportOptions ropts;
  OESetupReportOptions(ropts, itf);
  ropts.SetHeaderHeight(140.0);
  OEReport report(ropts);

  OE2DMolDisplayOptions dopts;
  OESetup2DMolDisplayOptions(dopts, itf);
  dopts.SetDimensions(report.GetCellWidth(), report.GetCellHeight(), OEScale::AutoScale);

  OEGraphMol mol;
  while (OEReadMolecule(ifs, mol))
  {
    OEPrepareSearch(mol, ss);
    const bool unique = true;
    OEIter<OEMatchBase> miter = ss.Match(mol, unique);
    if (!(bool)miter)
      continue; // no match

    if (align)
      OEPrepareAlignedDepiction(mol, ss.GetPattern(), miter);
    else
      OEPrepareDepiction(mol);

    OEImageBase* cell = report.NewCell();
    OE2DMolDisplay disp(mol, dopts);
    OEAddHighlighting(disp, hcolor, hstyle, miter);
    OERenderMolecule(*cell, disp);
    OEDrawBorder(*cell, OELightGreyPen);
  }

  // render query structure in each header
  dopts.SetDimensions(report.GetHeaderWidth(), report.GetHeaderHeight(), OEScale::AutoScale);
  OE2DMolDisplay disp(depictquery, dopts);
  for (OEIter<OEImageBase> hi = report.GetHeaders(); hi; ++hi)
  {
    OERenderMolecule(*hi, disp);
    OEDrawBorder(*hi, OELightGreyPen);
  }

  OEWriteReport(oname, report);
  return 0;
}