/*****************************************************************************
******************************************************************************
******************************************************************************
*****                                                                    *****
*****                        PROGRAM REPORT v1.1                         *****
*****    Copyright (c) 1990 by Joyce C. Miller.  All Rights Reserved.    *****
*****                                                                    *****
***** This  program takes pooled data files created by program RESTSITE, *****
***** and prints  their contents to  text files so that the user may see *****
***** what  is in them.  The  user must  supply the number of files, and *****
***** this number  may be entered  on the  command  line, or the program *****
***** will ask for it.  The pooled data files must be in order, starting *****
***** with "0.$$$" through "n-1.$$$".  There cannot be any missing files *****
***** (e.g., 0,1,3,4  will not work).  The program takes each data file, *****
***** and creates a text  file with the name  "#.REP",  where "#" is the *****
***** same  number as the number  of the pooled data file.  The date and *****
***** time, and the  OTU are  printed at the top of the output file, and *****
***** lines of text are printed.  Each  text line  is  for one  discrete *****
***** site, and  contains  the  probe, enzyme, cut site  size, number of *****
***** individuals, and the site, and how many times it occurred in those *****
***** individuals.                                                       *****
*****                                                                    *****
***** List of C functions used in this program:                          *****
*****                                                                    *****
*****     FUNCTION         LIBRARY          FUNCTION         LIBRARY     *****
*****     asctime          time.h           fclose           stdio.h     *****
*****     fopen            stdio.h          fprintf          stdio.h     *****
*****     fread            stdio.h          gets             stdio.h     *****
*****     localtime        time.h           printf           stdio.h     *****
*****     rewind           stdio.h          sscanf           stdio.h     *****
*****     strcat           string.h         strcpy           string.h    *****
*****     strlen           string.h         time             time.h      *****
*****                                                                    *****
******************************************************************************
******************************************************************************
*****************************************************************************/

/*****************************************************************************
**                             INCLUDE FILES                                **
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <rstypes.h>             /* file with most TYPDEFs for this program */
#include <rserrors.h>                           /* file with error messages */
#include <rsfuncs.h>         /* a file with some functions for this program */
/*****************************************************************************
**                            SYMBOLIC CONSTANTS                            **
*****************************************************************************/
#define VNUM 1.1                          /* version number of this program */
#define YEAR 1990                                 /* current copyright year */
/*****************************************************************************
**                          FUNCTION PROTOTYPES                             **
*****************************************************************************/
int  setup(int argc, char *argv[1]);                  /* set up the program */
int  getlimit();                  /* gets number of pooled data files to do */
FILE *fileheader(FILE *fpw, char flnmr[], char otu[]);   /* column headings */
/*****************************************************************************
******************************************************************************
******************************************************************************
******                                                                  ******
******                          MAIN PROGRAM                            ******
******                                                                  ******
******************************************************************************
******************************************************************************
**                                                                          **
** The main part of this  program starts by calling  FUNCTION  SETUP, which **
** prints a copyright message to the user, and asks for the number of files **
** to be done, if  this  was not entered by the user.  It then checks to be **
** sure  that this number is  not greater than  the maximum  number of OTUs ** 
** allowed by program  RESTSITE.  Once this is done, the  program  gets the **
** first  "#.$$$"  file, opens it, and  gets the  OTU name.  Then  FUNCTION **
** FILEHEADER is called, which sets up the output ("#.REP") file for #.$$$. **
** Then the program  goes through each  pooldat data record, and prints the **
** probe,  the enzyme,  the r-value, the number of individuals examined for **
** that  probe/enzyme combination (PEC), the fragment or site, and how many **
** times (in how many individuals)  that fragment/site was found.  When all **
** of the  PECs  for a  particular  probe  are  done,  the  total number of **
** fragments/sites for that probe are printed.  When all PECs are done, the **
** total number of probes and  the total number of sites are printed.  Then **
** the "#.$$$" and "#.REP" files are closed, and the program goes on to the **
** next "#.$$$" file.                                                       **
**                                                                          **
** Functions called:                                                        **
** setup          --  prints  program  name,  copyright  message, and  gets **
**                    number of files to do.                                **
** fileheader     --  prepares output file:  prints date  and time, name of **
**                    the input  ("#.$$$") and output  ("#.REP") files, and **
**                    column headings.                                      **
** rserror311     --  error message if error reading datafile.              **
*****************************************************************************/
void main(int argc, char *argv[])
{
  int limit = 0;                                /* integer version of "lim" */
  FILE *fpr, *fpw;          /* file pointers -- #1 for input, #2 for output */
  char flnmr[20];                                        /* input file name */
  char flnmw[20];                                       /* output file name */
  char *fr,*fw;                    /* pointers to input & output file names */
  pooldat p;                             /* temporary pooled data structure */
  int i,k,l;                                      /* loop control variables */
  char pb1[PRBLEN];                                        /* name of probe */
  int j = 0;                                         /* current site number */
  int J = 0;           /* number of sites within a probe across all enzymes */
  int NUMP = 0;               /* total number of probes within the $$$ file */
  int NUMF = 0;          /* total number of frags/sites within the $$$ file */

  limit = setup(argc,argv);
  for (i=0; i<limit; ++i) {                                 /* do each file */

    flnmr[0] = '\0';                   /* get name for input  file from "i" */
    flnmw[0] = '\0';                   /* get name for output file from "i" */
    fr = inttoalph(i,flnmr);
    strcpy(flnmr,fr);
    strcpy(flnmw,fr);
    fr = strcat(flnmr,".$$$");
    fw = strcat(flnmw,".REP");
    fpr = opnbrdfl(fpr,flnmr);              /* open input  file for reading */
    fpw = opntwtfl(fpw,flnmw);              /* open output file for writing */

    /* set up REP file -- print the otu name, and file header */
    if (fread(&p,sizeof(pooldat),1,fpr) == NULL) rserror311(flnmr);
    rewind(fpr);                                  /* go back to top of file */
    fpw = fileheader(fpw,flnmr,p.otu);          /* print header to REP file */

    /* initialize grand totals */
    pb1[0] = '\0';                             /* initialize the probe name */
    NUMP = 0;                      /* initialize the total number of probes */
    NUMF = 0;                 /* initialize the total number of frags/sites */
    J = 0;   /* init total # of frags/sites w/in a probe across all enzymes */

    printf("Printing contents of file %s to file %s.\n\r",flnmr,flnmw);
    while (fread(&p,sizeof(pooldat),1,fpr) != NULL) {        /* do each PEC */
      if (J == 0) strcpy(pb1,p.prb);                  /* get new probe name */
      if (strcmp(pb1,p.prb) != NULL) {  /* if we're done with current probe */
        fprintf(fpw,"Probe %s:  Number of different sites = %d\n",pb1,J);
        NUMF += J;            /* add # sites for old probe to total # sites */
        NUMP++;                     /* increment the total number of probes */
        J = 0;                       /* initialize # sites within new probe */
        strcpy(pb1,p.prb);                         /* get name of new probe */
      }
      j = 0;               /* initialize number of sites/frags for this PEC */
      while ((j<MAXFS) && (p.fs[j].f != -5)) {              /* do each site */
        /* print out: probe, enzyme, r, number of individuals, the site or
           fragment size, and the number of times it occurred */
        fprintf(fpw,"%s",p.prb);                             /* print probe */
        k = strlen(p.prb);                                     /* get width */
        while (k++ < PRBLEN) fprintf(fpw," ");       /* pad out with spaces */
        fprintf(fpw,"%s",p.enz);                            /* print enzyme */
        k = strlen(p.enz);                                     /* get width */
        while (k++ < ENZLEN) fprintf(fpw," ");       /* pad out with spaces */
        fprintf(fpw,"%3.1f",p.rv);                       /* print site size */
        fprintf(fpw,"%3d  ",p.ni);           /* print number of individuals */
        fprintf(fpw,"%8.2f  ",p.fs[j].f);                     /* print site */
        fprintf(fpw,"%3d\n",p.fs[j].no);     /* print # times site occurred */
        j++;                                  /* increment site/frag number */
      }                                              /* done with all sites */
      fprintf(fpw,"%s found %d sites\n",p.enz,j);
      J += j;        /* add # sites for this PEC to running total for probe */
    }                                              /* done with all records */
    if (feof(fpr)) {              /* if end of file, print out grand totals */
      NUMF += J;          /* add # sites for current probe to total # sites */
      NUMP++;                       /* increment the total number of probes */
      fprintf(fpw,"Probe %s:  Number of different sites = %d\n",pb1,J);
      fprintf(fpw,"Total number of probes = %d  ",NUMP);
      fprintf(fpw,"Total number of sites = %d\n",NUMF);
    }
    rewind(fpr);                                     /* close up input file */
    fclose(fpr);
    rewind(fpw);                                    /* close up output file */
    fclose(fpw);
  }                                                  /* done with all files */
}                                                    /* END OF MAIN PROGRAM */
/*****************************************************************************
**                                                                          **
**                              FUNCTION SETUP                              **
**                                                                          **
** This function is  called from FUNCTION MAIN, and prints the program name **
** and copyright message to the screen, along with a warning about previous **
** versions of the program.  It  then gets  the number of files to be done. **
**                                                                          **
** Functions called:                                                        **
** getlimit       --  gets the  number of files to be done, if this was not **
**                    entered by the user.                                  **
** rserror216     --  error  message if the number of  files to be  done is **
**                    greater that  the  maximum  number of OTUs allowed by **
**                    PROGRAM RESTSITE.                                     **
*****************************************************************************/
int setup(int argc, char *argv[1])
{
  char lim[20];       /* number from command line = number of files to read */
  int limit = 0;      /* number from command line = number of files to read */

  printf("\r\nProgram REPORT v%3.1f\r\n",VNUM);          /* message to user */
  printf("A program for printing the contents of pooled data files.\r\n");
  printf("Copyright (c) %4.0d by Joyce C. Miller.  ",YEAR);
  printf("All Rights Reserved.\r\n\n");
  printf("Warning: Printing out contents of pooled data files ('.$$$' ");
  printf("files) made\n         with RESTSITE v1.0  may result in  errors ");
  printf("in the total  number\n         of sites found per probe.  Only ");
  printf("'.$$$'  files made by RESTSITE\n         v1.1 can be assured to ");
  printf("give correct results.\r\n\n");

  if (argc < 2) limit = getlimit();    /* if number of files wasn't entered */
  if (argc == 2) strcpy(lim,argv[1]);                          /* if it was */
  sscanf(argv[1],"%d",&limit);     /* convert # of files from string to int */
  if (limit > MAXOTU) rserror216(limit);          /* check if # is too high */
  return(limit);
}                                                  /* END OF FUNCTION SETUP */
/*****************************************************************************
**                                                                          **
**                            FUNCTION GETLIMIT                             **
**                                                                          **
** This function is called from FUNCTION MAIN, and prompts the user for the **
** number of files to do, if the user did not enter it on the command line. **
**                                                                          **
** Functions called:                                                        **
** rserror216     --  error  message if the number of  files to be  done is **
**                    greater that  the  maximum  number of OTUs allowed by **
**                    PROGRAM RESTSITE.                                     **
*****************************************************************************/
int getlimit()
{
  char answer[10];                   /* temporary string to hold user input */
  char *a;                                     /* pointer for GETS function */
  int k;                              /* "answer" in converted integer form */

  printf("\r\nHow many pooled data files are there?  ");          /* prompt */
  a = gets(answer);                                 /* get number from user */

  sscanf(answer,"%d",&k);                    /* extract integer from string */
  if (k > MAXOTU) rserror216(k);              /* if it's larger than MAXOTU */
  else return(k);                           /* return KEY to FUNCTION CHECK */
}                                               /* END OF FUNCTION GETLIMIT */
/*****************************************************************************
**                                                                          **
**                           FUNCTION FILEHEADER                            **
**                                                                          **
** This  function is  called  from  FUNCTION MAIN, and  prints the  version **
** number, date, time to the top of the output file.                        **
** Functions called:  none                                                  **
*****************************************************************************/
FILE *fileheader(FILE *fpw, char flnmr[], char otu[])
{
  struct tm *nowtime;        /* time structures needed to print date & time */
  time_t aclock;                             /* at beginning of output file */

  time(&aclock);                                          /* get clock time */
  nowtime = localtime(&aclock);                      /* extract data & time */
  fprintf(fpw,"%s",asctime(nowtime));        /* print at top of output file */
  fprintf(fpw,"Program REPORT v%3.1f\r\n",VNUM);     /* message to user */
  fprintf(fpw,"A program for printing the contents of pooled data files.\r\n");
  fprintf(fpw,"Copyright (c) %4.0d by Joyce C. Miller.  ",YEAR);
  fprintf(fpw,"All Rights Reserved.\r\n\n");
  fprintf(fpw,"This file, %s, contains pooled data on OTU %s\n",flnmr,otu);
  fprintf(fpw,"PROBE    ENZYME    R   #I   SITE    #T\n");
  return(fpw);
}                                             /* END OF FUNCTION FILEHEADER */
/****************************************************************************/
