/*****************************************************************************
******************************************************************************
******************************************************************************
*****                                                                    *****
*****                       PROGRAM REMOVEFILE.C                         *****
*****        A program for removing an OTU from a pooled data set.       *****
*****    Copyright (c) 1990 by Joyce C. Miller.  All Rights Reserved.    *****
*****                                                                    *****
***** This  program  will  remove a  pooled  data  file  created by  the *****
***** RESTSITE program and its corresponding OTU from the data set.  The *****
***** OTU  is  removed  from the  file "00.$$$", its pooled data file is *****
***** deleted, and all other pooled data files are renumbered to fill in *****
***** the gap in the series.                                             *****
*****                                                                    *****
***** List of C functions used in this program:                          *****
*****                                                                    *****
*****     FUNCTION         LIBRARY          FUNCTION         LIBRARY     *****
*****     atoi             stdlib.h         fclose           stdio.h     *****
*****     fread            stdio.h          fwrite           stdio.h     *****
*****     printf           stdio.h          rewind           stdio.h     *****
*****     strcat           string.h         strcpy           string.h    *****
*****     strlen           string.h         strncpy          string.h    *****
*****     system           stdlib.h                                      *****
*****                                                                    *****
******************************************************************************
******************************************************************************
*****************************************************************************/

/*****************************************************************************
**                             INCLUDE FILES                                **
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rstypes.h>         /* file with type definitions for this program */
#include <rserrors.h>                           /* file with error messages */
#include <rsfuncs.h>                   /* file with commonly-used functions */
/*****************************************************************************
**                            SYMBOLIC CONSTANTS                            **
*****************************************************************************/
#define VNUM 1.0                          /* version number of this program */
#define YEAR 1990                                         /* copyright year */
/*****************************************************************************
**                           FUNCTION PROTOTYPES                            **
*****************************************************************************/
void removefile(char flname[]);          /* removes file & renumbers others */
/*****************************************************************************
******************************************************************************
******************************************************************************
******                                                                  ******
******                          MAIN PROGRAM                            ******
******                                                                  ******
******************************************************************************
******************************************************************************
******************************************************************************
**                                                                          **
** The  main  part of  this  program  prints out  the program  name  and  a **
** copyright message, then reads the command line.  From  the command line, **
** the  program  reads in the name  of  the  pooled data file that is to be **
** removed from the  data set.  It  then  calls on  FUNCTION REMOVEFILE  to **
** delete it.                                                               **
**                                                                          **
** Functions called:                                                        **
** removefile     --  removes an OTU and its pooled data file from the data **
**                    set.                                                  **
*****************************************************************************/
void main(int argc, char *argv[])
{
  printf("\r\nProgram REMFILE v%3.1f\r\n",VNUM);       /* copyright message */
  printf("A program for removing one pooled data file from the data set.\r\n");
  printf("Copyright (c) %d by Joyce C. Miller.  ",YEAR);
  printf("All Rights Reserved.\r\n");

  if (argc < 2) {              /* error if not enough items on command line */
    printf("\a\r\nError 101:  A pooled data file must be specified.  ");
    printf("The proper format of\r\n            the command line should be:");
    printf("\r\n\n            REMFILE FILENAME\r\n\n");
    printf("            All of these items are necessary.\r\n");
    exit(0);
  }
  else removefile(argv[1]);          /* remove file and alter file "00.$$$" */
}                                                    /* END OF MAIN PROGRAM */
/*****************************************************************************
**                                                                          **
**                          FUNCTION REMOVEFILE                             **
**                                                                          **
**   This  function is  called from FUNCTION MAIN.  It does two things: (1) **
** it alters the file "00.$$$" to  reflect the  new  number of taxa and the **
** taxa names,  and  (2) it  re-numbers  all of the  pooled  data  files to **
** reflect the fact that one of the taxa has been eliminated.               **
**   To change "00.$$$", it receives the name of the file to be deleted. It **
** then opens the file  "00.$$$", which  holds all of the memory lists, and **
** creates an empty file "00.BK!".  Then it reads the number of taxa out of **
** 00.$$$, subtracts one, then  writes it to "00.BK!".  It  then copies the **
** number of treatments and  r-classes to  "00.BK!".  The list of  OTUs  is **
** then  copied, except  for the OTU that is to be deleted.  All others are **
** copied  as they  appear.  After  the  OTUlist is copied to "00.BK!", the **
** treatment list  and  r-class  list are  copied as is.  "00.$$$" is  then **
** deleted, and "00.BK!" is renamed to "00.$$$".                            **
**   After this is done, the program renumbers  the pooled  data files.  It **
** starts  at  FILENAME, and  deletes  it.  It  then  takes the  next file, **
** renames it to the  previous file's  name, then renames the  next file to **
** the previous file name, etc., until all of  the files have been renamed. **
** It then returns to FUNCTION MAIN.                                        **
**                                                                          **
** Functions called:                                                        **
** opnbrdfl       --  opens a binary file for reading.                      **
** opnbwtfl       --  opens a binary file for writing.                      **
** rserror311     --  error message if error reading datafile.              **
** rserror321     --  error message if error writing to datafile.           **
*****************************************************************************/
void removefile(char flname[])
{
  FILE *fp1, *fp2;                    /* file pointer for 00.$$$ and 00.BK! */
  char flnow[FILELEN];                              /* name of current file */
  char flprev[FILELEN];                            /* name of previous file */
  char *fnow;                            /* pointer to name of current file */
  char *fprev;                          /* pointer to name of previous file */
  char comm1[100];                     /* string for comunicating to system */
  char comm2[100];                     /* string for comunicating to system */
  char *c1, *c2;                              /* pointer to comm1 and comm2 */
  int ntx = 0;                                            /* number of taxa */
  int ntr = 0;                                      /* number of treatments */
  int nrv = 0;                                       /* number of r-classes */
  otuname on;                                                /* name of OTU */
  treatment trt;                                             /* a treatment */
  float rv;                                                   /* an r-class */
  int n = 0;                                 /* flnow's slot in the OTULIST */
  int i;                                           /* loop control variable */

  printf("Deleting file %s and altering file 00.$$$ \r\n",flname);
  printf("to reflect the loss of one OTU.\r\n\n");       /* message to user */

  strcpy(comm1,"DEL ");               /* build beginning of system commands */
  strcpy(comm2,"RENAME ");

  fp1 = opnbrdfl(fp1,"00.$$$");                  /* open 00.$$$ for reading */
  fp2 = opnbwtfl(fp2,"00.BK!");                  /* open 00.BK! for writing */

  /********** REWRITE FILE "00.$$$", WHICH HOLDS THE MEMORY LISTS ***********/
  /* read in number of taxa, treatments, and r-classes */
  if (fread(&ntx,sizeof(int),1,fp1) == NULL) rserror311("00.$$$");
  if (fread(&ntr,sizeof(int),1,fp1) == NULL) rserror311("00.$$$");
  if (fread(&nrv,sizeof(int),1,fp1) == NULL) rserror311("00.$$$");
  ntx--;                                    /* reduce number of taxa by one */

  /* write amended number of taxa, treatments, and r-classes */
  if (fwrite(&ntx,sizeof(int),1,fp2) == NULL) rserror321("00.$$$");
  if (fwrite(&ntr,sizeof(int),1,fp2) == NULL) rserror321("00.$$$");
  if (fwrite(&nrv,sizeof(int),1,fp2) == NULL) rserror321("00.$$$");
  ntx++;                                         /* set number of taxa back */

  flnow[0] = '\0';                 /* GET SLOT NUMBER IN OTULIST OF FILE #1 */
  strncpy(flnow,flname,strlen(flname)-4);  /* copy number part of file name */
  flnow[strlen(flname)-4] = '\0';                       /* terminate string */
  n = atoi(flnow);                              /* convert it to an integer */
  strcpy(flnow,flname);

  for (i=0; i<ntx; ++i) {               /* go through the original OTU list */
    if (fread(on,sizeof(otuname),1,fp1) == NULL) rserror311("00.$$$");
    if (i != n)    /* copy all other OTU names except the one to be deleted */
      if (fwrite(on,sizeof(otuname),1,fp2) == NULL) rserror321("00.$$$");
  }
  for (i=0; i<ntr; ++i) {             /* write list of treatments to 00.BK! */
    if (fread(&trt,sizeof(treatment),1,fp1) == NULL) rserror311("00.$$$");
    if (fwrite(&trt,sizeof(treatment),1,fp2) == NULL) rserror321("00.$$$");
  }
  for (i=0; i<nrv; ++i) {              /* write list of r-classes to 00.BK! */
    if (fread(&rv,sizeof(float),1,fp1) == NULL) rserror311("00.$$$");
    if (fwrite(&rv,sizeof(float),1,fp2) == NULL) rserror321("00.$$$");
  }
  rewind(fp1);                      /* rewind file pointers and close files */
  rewind(fp2);
  fclose(fp1);
  fclose(fp2);
  system("DEL 00.$$$");                     /* delete old version of 00.$$$ */
  system("RENAME 00.BK! 00.$$$");             /* rename new one to old name */

  /****************** RENAME ALL OF THE POOLED DATA FILES *******************/
  c1 = strcat(comm1,flname);            /* create command line "DEL FLNAME" */
  system(comm1);                                             /* delete file */

  /* change all other OTU names & pooled files, starting at deleted file */
  for (i=n+1; i<ntx; ++i) {                /* go through the remaining OTUs */
    strcpy(flprev,flnow);        /* file from last run is now previous file */
    flnow[0] = '\0';                                    /* initialize flnow */
    fnow = inttoalph(i,flnow);           /* convert i to a character string */
    strcpy(flnow,fnow);                                   /* put into flnow */
    fnow = strcat(flnow,".$$$");                   /* add on file extension */
    strcpy(comm1,comm2);                                       /* "RENAME " */
    c1 = strcat(comm1,flnow);                             /* "RENAME FLNOW" */
    c1 = strcat(comm1," ");                              /* "RENAME FLNOW " */
    c1 = strcat(comm1,flprev);                     /* "RENAME FLNOW FLPREV" */
    system(comm1);                                                 /* do it */
  }                          /* have now renamed all of the remaining files */
}                                             /* END OF FUNCTION REMOVEFILE */
/****************************************************************************/

