/* -*-C-*- */
/**********************************************************************
 * This source code is public domain, and may be freely distributed,  *
 * modified, and used for any purpose.  IT COMES WITH ABSOLUTELY NO   *
 * WARRANTY OF ANY KIND.					      *
 **********************************************************************/

/*+======================================================================
  | FILE:		trantest2.c
  | DESCRIPTION:
  |	Use this to debug transformations.  Prompts for a transform
  |       and molecules repeatedly.  Prints the resulting matches.
  +======================================================================
  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dt_smiles.h"
#include "dt_smarts.h"

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

/***************************************************************************
* FUNCTION: dump_errors
*
* RETURNS: (void)
***************************************************************************/

void dump_errors(void)
{
  dt_Handle errseq, errob;
  dt_String errstr;
  dt_Integer elen, first = TRUE;

  errseq = dt_errors(DX_ERR_WARN);
  while (NULL_OB != (errob = dt_next(errseq)))
    {
      errstr = dt_stringvalue(&elen, errob);
      if ((elen > 0) && (errstr != NULL))
	{
	  if (first) 
	    printf("\nError queue:\n");
	  first = FALSE;
	  printf("  %.*s\n", elen, errstr);
	}
      dt_dealloc(errob);
    }
  dt_dealloc(errseq);
  dt_errorclear();
  return;
}

/***************************************************************************
* FUNCTION: main
*
* RETURNS: (int)
***************************************************************************/

int main()
{
  dt_Integer count, rlen, plen, templen, counter;
  char *reactant, *product, *tempstr;
  dt_Handle mols, mol, som, seq, reaction, atoms, atom, tempmol, strob;
  dt_Handle trx, pattern, matches, sor, temprxn;
  char smirk_string[1000];
  char mol_string[1000];

  /*** Loop forever. ***/
  while(1)
    {
      /*** Get transform ***/

      printf("Enter Transformation (<cr> terminates):\n");
      if (!gets(smirk_string)) break;
      if (strlen(smirk_string) == 0) break;

      trx = dt_smirkin(strlen(smirk_string), smirk_string);
      dump_errors();
      
      /*** Good transform object ***/
      if (trx != NULL_OB)
	{
	  while(1)
	    {
	      /*** Get molecules. ***/
	      printf("Enter dot-separated molecules (<cr> terminates):\n");
	      if (!gets(mol_string)) break;
	      if (strlen(mol_string) == 0) break;

	      som = dt_smilin(strlen(mol_string), mol_string);
	      if (som == NULL_OB) break;

	      /*** Do the transformation. ***/
	      sor = dt_transform(trx, som, DX_FORWARD, FALSE); 
	      if (sor != NULL_OB)
		{
		  count = dt_count(sor, TYP_REACTION);
		  printf("Count of specific reactions found= %d\n\n", count);
		  while(NULL_OB != (reaction = dt_next(sor)))
		    {
		      product = dt_cansmiles(&plen, reaction, TRUE);
		      if (product != NULL) 
			printf("Absolute cansmiles: %.*s\n", plen, product);
		      else
			printf("ERROR: Absolute cansmiles is NULL.\n");

		      atoms = dt_canstream(reaction, TYP_ATOM, TRUE, FALSE);
		      while (NULL_OB != (atom = dt_next(atoms)))
			printf("Atom: %d, tmap: %d, torder: %d\n",
			       dt_uid(atom), dt_integer(atom, 4, "tmap"),
			       dt_integer(atom, 6, "torder"));
		      dt_dealloc(atoms);

		      product = dt_cansmiles(&plen, reaction, FALSE);
		      if (product != NULL) 
			printf("Unique cansmiles: %.*s\n", plen, product);
		      else
			printf("ERROR: Unique cansmiles is NULL.\n");
                      dt_dealloc(reaction);
		    }
		}
	      else 
		printf("Transform doesn't apply.\n");
	      dump_errors();
	      dt_dealloc(som);
	    }
	  dt_dealloc(trx);
	}
      else
	printf("Transform barfed: %s\n", smirk_string);
    }
  return(1);
}