/* 
(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.
*/
#include "openeye.h"
#include "oesystem.h"
#include "oechem.h"
#include "oebio.h"
#include "oedocking.h"

using namespace OESystem;
using namespace OEChem;
using namespace OEBio;
using namespace OEDocking;
using namespace std;

int main(int argc, char** argv)
{

  OEDockOptions dockOpts;
  OERefInputAppOptions opts(dockOpts, "DockMolecules", OEFileStringType::Mol3D,
      OEFileStringType::Mol3D, OEFileStringType::DU, "-receptor");

  if (OEConfigureOpts(opts, argc, argv, false) == OEOptsConfigureStatus::Help)
    return 1;

  dockOpts.UpdateValues(opts);

  oemolistream imstr(opts.GetInFile());
  oemolostream omstr(opts.GetOutFile());

  OEDesignUnit du;
  if (!OEReadDesignUnit(opts.GetRefFile(), du))
    OEThrow.Fatal("Unable to read du");

  if (!du.HasReceptor())
      OEThrow.Fatal("Design Unit " + du.GetTitle() + " does not contain a receptor");

  //@ <SNIPPET-DOCK-MOLECULES-SETUP>
  OEDock dock(dockOpts);
  //@ </SNIPPET-DOCK-MOLECULES-SETUP>
  //@ <SNIPPET-DOCK-MOLECULES-INITIALIZE>
  dock.Initialize(du);
  //@ </SNIPPET-DOCK-MOLECULES-INITIALIZE>

  OEMol mcmol;
  while (OEReadMolecule(imstr, mcmol))
  {
    OEGraphMol dockedMol;
    //@ <SNIPPET-DOCK-MOLECULES-DOCK>
    unsigned int retCode = dock.DockMultiConformerMolecule(dockedMol, mcmol);
    if (retCode != OEDockingReturnCode::Success)
      OEThrow.Fatal("Docking Failed with error code " + OEDockingReturnCodeGetName(retCode));
    //@ </SNIPPET-DOCK-MOLECULES-DOCK>
    string sdtag = OEDockMethodGetName(dockOpts.GetScoreMethod());
    //@ <SNIPPET-DOCK-MOLECULES-ASSIGN-SCORE>
    OESetSDScore(dockedMol, dock, sdtag);
    //@ </SNIPPET-DOCK-MOLECULES-ASSIGN-SCORE>
    //@ <SNIPPET-DOCK-MOLECULES-ANNOTATE>
    dock.AnnotatePose(dockedMol);
    //@ </SNIPPET-DOCK-MOLECULES-ANNOTATE>
    OEWriteMolecule(omstr, dockedMol);
  }
  return 0;
}
