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

class MyOptions : public OEDocking::OEPositOptions
{
public:
  MyOptions() : OEDocking::OEPositOptions()
  {
    OESystem::OEUIntParameter param1("-numPoses", 1);
    param1.AddLegalRange("1", "20");
    param1.SetBrief("Number of poses to generate");
    m_param1 = AddParameter(param1);
  }

  unsigned int GetNumPoses() const
  {
    unsigned int numConfs = 1;
    if (m_param1->GetHasValue())
      OESystem::OEStringToNumber(m_param1->GetStringValue(), numConfs);
    else
      OESystem::OEStringToNumber(m_param1->GetStringDefault(), numConfs);
    return numConfs;
  }

private:
  OESystem::OEParameter *m_param1;
};

int main(int argc, char **argv)
{
  MyOptions positOpts;
  OEChem::OERefInputAppOptions opts(positOpts, "PoseMolecules", OEChem::OEFileStringType::Mol3D,
                                    OEChem::OEFileStringType::DU, OEChem::OEFileStringType::DU, "-receptor");
  if (OESystem::OEConfigureOpts(opts, argc, argv, false) == OESystem::OEOptsConfigureStatus::Help)
    return 0;
  positOpts.UpdateValues(opts);

  OEChem::oemolistream ifs;
  if (!ifs.open(opts.GetInFile()))
    OESystem::OEThrow.Fatal("Unable to open %s for reading", opts.GetInFile().c_str());

  OEPlatform::oeifstream rfs;
  if (!rfs.open(opts.GetRefFile()))
    OESystem::OEThrow.Fatal("Unable to open %s for reading", opts.GetRefFile().c_str());

  OEPlatform::oeofstream ofs;
  if (!ofs.open(opts.GetOutFile()))
    OESystem::OEThrow.Fatal("Unable to open %s for writing", opts.GetOutFile().c_str());

  OEDocking::OEPosit poser(positOpts);

  OEBio::OEDesignUnit du;
  unsigned int count = 0;
  while (OEBio::OEReadDesignUnit(rfs, du))
  {
    if (!du.HasReceptor())
      OESystem::OEThrow.Fatal("Design Unit " + du.GetTitle() + " does not contain a receptor");
    poser.AddReceptor(du);
    count++;
  }
  if (count == 0)
    OESystem::OEThrow.Fatal("Receptor input does not contain any design unit");

  OESystem::OEThrow.Info("Number of conformers: %d", positOpts.GetNumPoses());
  OESystem::OEThrow.Info("Best receptor pose flag: %s", positOpts.GetBestReceptorPoseOnly() ? "true" : "false");

  OEChem::OEMol mcmol;
  while (OEChem::OEReadMolecule(ifs, mcmol))
  {
    OESystem::OEThrow.Info("posing %s", mcmol.GetTitle());
    OEDocking::OEPositResults results;
    unsigned int returnCode = poser.Dock(results, mcmol, positOpts.GetNumPoses());

    if (returnCode == OEDocking::OEDockingReturnCode::Success)
    {
      for (OESystem::OEIter<OEDocking::OESinglePoseResult> result = results.GetSinglePoseResults(); result; ++result)
      {
        OEBio::OEDesignUnit posedDU = result->GetDesignUnit();
        posedDU.SetDoubleData(poser.GetName().c_str(), result->GetProbability());
        OESystem::OEThrow.Info("Receptor used: %s pose probability: %f", posedDU.GetTitle().c_str(),
                               result->GetProbability());
        OEBio::OEWriteDesignUnit(ofs, posedDU);
      }
    }
    else
    {
      std::string errMsg = OEDocking::OEDockingReturnCodeGetName(returnCode);
      OESystem::OEThrow.Warning("%s: %s", mcmol.GetTitle(), errMsg.c_str());
    }
  }

  return 0;
}
