
static char const rcsid [] = "$Id: plor.c,v 1.1 1998/04/21 10:50:03 stojanov Exp $";


/*****************************************************************************/
/*                                                                           */
/* Program: plor (data reorganization of plain format landmarks files)       */
/*                                                                           */
/* Author: Nikola Stojanovic                                                 */
/*                                                                           */
/* Revision:    16 APR 97   Version 1.0                                      */
/*                                                                           */
/*                                                                           */
/*   Given a file in "plain" format, the program reorganizes and outputs     */
/* lines of data, either as default or with respect to the provided pattern  */
/* of output                                                                 */
/*                                                                         */
/*****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ntl.h"

/*****************************************************************************/
/*                                                                           */
/* Definitions section                                                       */
/*                                                                           */
/*****************************************************************************/


/*****************************************************************************/
/* Definitions of the constants of the program unit                          */
/*****************************************************************************/


#define DEFAULT_TEMPLATE            "set"


/*****************************************************************************/
/* Prototypes of locally used functions of the program unit                  */
/*****************************************************************************/


void plor_output_data (plain_ptr records, char *template);

/*****************************************************************************/
/*                                                                           */
/* Code section                                                              */
/*                                                                           */
/*****************************************************************************/


/*****************************************************************************/
/*                                                                           */
/* Procedure: main                                                           */
/*                                                                           */
/* "main" procedure of the program. Receives and analyses the command line   */
/*   parameters, sets the control variables of the program, checksir     */
/*   consistency and passes control to internal procedures which actually    */
/*   process the input data and output results; returns 0 if everything is   */
/*   OK, non-zero status in case of any errors                               */

int main (int argc, char **argv)
{
 char *input_file, *template; bool template_set; int arg_count, scan;
 plain_ptr records; errind report;
 
 input_file = NULL;
 template_set = FALSE; template = DEFAULT_TEMPLATE;
 
 if (argc < 2) {                       /* Display instructions and terminate */
 
  fprintf (stderr, "usage: %s <input_file>\n", argv [0]);
  fprintf (stderr, "            [-o <template(seltnbdfmarc)>]\n");
  exit (1);
 }
 else {                           /* Some parameters provided - process them */
 
  /* Proceed to extract the command line parameters and create the settings  */
  
  arg_count = 1; while (arg_count < argc) {
  
   if (argv [arg_count] [0] != '-') {       /* Not an "-" option - file name */
    if (input_file != NULL) {
     fprintf (stderr, "Input file already set.\n"); exit (1);
    }
    else {
     input_file = NTL0_strsave (argv [arg_count]); arg_count++;
    }
   }
   else if (!strcmp (argv [arg_count], "-o")) {    /* Template specification */
    arg_count++;
    if (template_set) {
     fprintf (stderr, "Output template already set.\n"); exit (1);
    }
    else if (arg_count == argc) {
     fprintf (stderr, "Missing template specification.\n"); exit (1);
    }
    else {
     template = NTL0_strsave (argv [arg_count]); template_set = TRUE;
     arg_count++;
    }
   }
   else {                                     /* Unknown command-line option */
    fprintf (stderr, "Illegal option (%s).\n", argv [arg_count]);
    fprintf (stderr, "usage: %s <input_file>\n", argv [0]);
    fprintf (stderr, "            [-o <template(seltnbdfmarc)>]\n");
    exit (1);
   }
  }
  /* Now check whether all necessary parameters have been provided           */

  if (input_file == NULL) {
   fprintf (stderr, "Must have a file to process.\n"); exit (1);
  }

  /* Check the legality of the provided template                             */

  if (template == NULL) {
   fprintf (stderr, "Must have a template to work with - internal error.\n");
   exit (1);
  }
  else {
   scan = 0; while (template [scan] != '\0') {
    if ((template [scan] != 's') &&            /* Range start output request */
        (template [scan] != 'e') &&              /* Range end output request */
        (template [scan] != 'l') &&           /* Range length output request */
        (template [scan] != 't') &&          /* Sequence text output request */
        (template [scan] != 'n') &&              /* Site name output request */
        (template [scan] != 'b') &&         /* Binding factor output request */
        (template [scan] != 'd') &&   /* Originating database output request */
        (template [scan] != 'f') &&       /* Originating file output request */
        (template [scan] != 'm') &&          /* Matching data output request */
        (template [scan] != 'a') &&                 /* Strand output request */
        (template [scan] != 'r') &&              /* Reference output request */
        (template [scan] != 'c')) {                  /* Count output request */
        
     fprintf (stderr, "Illegal specifier ('%c') in the output template.\n",
                      template [scan]);
     exit (1);
    }
    scan++;
   }
  }
  /* Now proceed to load the data from the specified plain format file       */
  
  if ((report = NTL2_Load_Plain (input_file, &records)) != NULL) {
   fprintf (stderr, "Can't open sequences file '%s' (%s).\n",
                    input_file, report -> message);
   exit (1);
  }
  else {                             /* File loading OK, output the contents */
   plor_output_data (records, template);
   exit (0);
  }
 }
}


/*****************************************************************************/
/*                                                                           */
/* Procedure: plor_output_data                                               */
/*                                                                           */

void plor_output_data (plain_ptr records, char *template)
{
 plain_ptr data_scan; int template_scan;

 for (data_scan = records; data_scan != NULL; data_scan = data_scan -> next) {
 
  template_scan = 0; while (template [template_scan] != '\0') {
  
   if (template [template_scan] == 's') {              /* Range start output */
    printf ("%ld", data_scan -> start);
   }
   else if (template [template_scan] == 'e') {           /* Range end output */
    printf ("%ld", data_scan -> stop);
   }
   else if (template [template_scan] == 'l') {        /* Range length output */
    if (data_scan -> text == NULL)
     printf ("%ld", (data_scan -> stop) - (data_scan -> start) + 1);
    else printf ("%d", (int) (strlen (data_scan -> text)));
   }
   else if (template [template_scan] == 't') {       /* Sequence text output */
    if (data_scan -> text == NULL) printf ("null");
    else printf ("%s", data_scan -> text);
   }
   else if (template [template_scan] == 'n') {           /* Site name output */
    if (data_scan -> site == NULL) printf ("null");
    else printf ("%s", data_scan -> site);
   }
   else if (template [template_scan] == 'b') {      /* Binding factor output */
    if (data_scan -> binding == NULL) printf ("null");
    else printf ("%s", data_scan -> binding);
   }
   else if (template [template_scan] == 'd') {            /* Database output */
    if (data_scan -> database == NULL) printf ("null");
    else printf ("%s", data_scan -> database);
   }
   else if (template [template_scan] == 'f') {    /* Originating file output */
    if (data_scan -> file == NULL) printf ("null");
    else printf ("%s", data_scan -> file);
   }
   else if (template [template_scan] == 'm') {       /* Matching data output */
    if (data_scan -> matched == NULL) printf ("null");
    else printf ("%s", data_scan -> matched);
   }
   else if (template [template_scan] == 'a') {              /* Strand output */
    printf ("%c", data_scan -> strand);
   }
   else if (template [template_scan] == 'r') {           /* Reference output */
    if (data_scan -> reference == NULL) printf ("null");
    else printf ("%s", data_scan -> reference);
   }
   else if (template [template_scan] == 'c') {               /* Count output */
    if (data_scan -> c_range == NULL) printf ("null");
    else printf ("%s", data_scan -> c_range);
   }
   template_scan++;
   if (template [template_scan] != '\0') printf (" "); else printf ("\n");
  }
 }
}


