/* 
(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 <oeshape.h>

#include "hermite-tanimoto.itf"

using namespace std;
using namespace OESystem;
using namespace OEChem;
using namespace OEShape;


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

  const unsigned int NPolyMax = oeCast(unsigned int, itf.Get<int>("-NPolyMax"));

  const string ifrefname = itf.Get<string>("-inputreffile");
  const string iffitname = itf.Get<string>("-inputfitfile");
  const string ofname = itf.Get<string>("-outputfile");

  oemolistream ifsref;
  oemolistream ifsfit;
  oemolostream ofs;

  if (!ifsref.open(ifrefname))
    OEThrow.Fatal("Unable to open %s for reading", ifrefname.c_str());

  if (!ifsfit.open(iffitname))
    OEThrow.Fatal("Unable to open %s for reading", iffitname.c_str());

  OEMol refmol;
  OEMol fitmol;

  if (!OEReadMolecule(ifsref, refmol))
    OEThrow.Fatal("Unable to read molecule in %s", ifrefname.c_str());

  if (!ofs.open(ofname))
    OEThrow.Fatal("Unable to open %s for writing", ofname.c_str());

  OEOverlapPrep prep;
  prep.SetAssignColor(false);
  prep.Prep(refmol);

  OEHermiteOptions hermiteoptions;
  hermiteoptions.SetNPolyMax(NPolyMax);
  hermiteoptions.SetUseOptimalLambdas(true);

  OEHermiteOverlayOptions options;
  options.SetHermiteOptions(hermiteoptions);
  OEHermiteOverlay overlay(options);
  overlay.SetupRef(refmol);

  while (OEReadMolecule(ifsfit, fitmol))
  {
    prep.Prep(fitmol);
    OEBestOverlayScore score;
    overlay.BestOverlay(score, fitmol);
    cout << " Hermite Tanimoto = " << score.GetTanimoto() << endl;

    OEGraphMol outmol(*fitmol.GetConf(OEHasConfIdx(score.GetFitConfIdx())));
    OESetSDData(outmol, "HermiteTanimoto_" + OENumberToString(NPolyMax), OENumberToString(score.GetTanimoto()));
    score.Transform(outmol);
    OERemoveColorAtoms(outmol);
    OEWriteMolecule(ofs, outmol);
  }

  return 0;
}
