/* samie.c 
 *
 * SAMIE (Surface Atoms and Motifs IdentifiEr) is a file for 
 * identification of the surface and internal motifs in molecule
 * (given in the PDB format). 
 * First part of the program comprises received information about
 * surface and internal atoms in the molecule. In the second part 
 * of the program these atoms are further analysed. Finally, it is
 * decided whether given motif is on the surface of the molecule 
 * (or not).
 * ---------------------------------------------------------------
 */

#include "main.h"

void vypisHelp () {
      printf("\nDescription of all possible parametres:\n");
      printf("\nAll parametres below are mandatory:\n");
      printf("1) -i input_file\tspecification of input PDB file name\n");
      printf("2) -o output_file\tspecification of output PDB file name\n");
      printf("3) only one of the following options is permitted:\n");
      printf("   -NIN <level>\t\tNIN approach and boundary value for atom division\n");
      printf("   -UCSF\t\tUCSF approach\n");
      printf("   -SAS <file>\t\tSAS approach and path to file with pre-counted data\n");
      printf("4) -m <motif>\t\tspecify the searched motif\n");
      printf("\nAll parametres below are optional:\n");
      printf("1) -sc\t\tonly side chains are considered\n");
      printf("2) -q \tsilent mode (no text on standard output)\n");
      printf("\nProgram requires: input_file != output_file\n\n");
    exit(1);                                                   
}

/* ------------------------------------------------------------------------- */
/* all possible argv:                                                        */
/* -i input_file -o output_file -NIN level  -m motif [-sc] [-q]              */
/*                              -UCSF                                        */
/*                              -SAS file                                    */
/* Program requires: input_file != output_file, correct PDB file             */
int main(int argc, char *argv[]) {
/* ------------------------------------------------------------------------- */
 
  SEZNAM *p_sfirst=NULL;
  FILE *fr=NULL;
  char *soubor=NULL, *souborOUT=NULL, *soubor_vmd=NULL;
  int level=0, i=0, j=0, sidechain=0, q=0;
  double limit=0.01;
  int akce=0; /* (akce == 1) => NIN */
              /* (akce == 3) => UCSF */
              /* (akce == 4) => SAS */
  char motif[80]; /* Program is able to find max 80 character-long motif */
  motif[0]='\0';            
              

/* reading arguments */
   /* HELP menu */
  if ((argc==2)&&(strcmp(argv[1],"-h")==0)) {
      vypisHelp();
  }
  for (i=1; i < argc-1; i++) {
    if (strcmp(argv[i],"-i")==0) {
      soubor = argv[i+1];
    }
    if (strcmp(argv[i],"-o")==0) {
      souborOUT = argv[i+1];
    }
    if (strcmp(argv[i],"-m")==0) {
      strcpy(motif,argv[i+1]);
    }
    if (strcmp(argv[i],"-NIN")==0) {
      level = atoi(argv[i+1]);
      akce=1;
    }
    if (strcmp(argv[i],"-UCSF")==0) {
      akce=3;
    }
    if (strcmp(argv[i],"-SAS")==0) {
      soubor_vmd = argv[i+1];
      akce=4;
    }
    if (strcmp(argv[i],"-sc")==0) {
      sidechain=1;
    }
    if (strcmp(argv[i],"-q")==0) {
      q=1;
    }
 }
    if (strcmp(argv[argc-1],"-UCSF")==0) {
      akce =3;
    }
    if (strcmp(argv[argc-1],"-sc")==0) {
      sidechain=1;
    }
    if (strcmp(argv[argc-1],"-q")==0) {
      q=1;
    }

if (!q) {
 printf("SAMIE (Surface Atoms and Motifs IdentifiEr)\n");
 printf("program for identifying surface atoms and motifs\n");
 printf("------------------------------------------------\n");
}


  /* examinations of input */
  if (argc==1) {
      printf("Essential arguments are missing! For details see HELP:\n");
      vypisHelp();
  }
  if (soubor == NULL) {
    printf("Input PDB file is missing! For details see HELP:\n");
    vypisHelp();
  }
  if (souborOUT == NULL) {
    printf("Name of output file is missing! For details see HELP:\n");
    vypisHelp();
   }
  if (motif[0] == '\0') {
    printf("Specification of motif is missing! For details see HELP:\n");
    vypisHelp();
   }
  if (akce==0) {
    printf("Specification of approach is missing! For details see HELP:\n");
    vypisHelp();
  }
  if ((level == 0)&&(akce==1)) {
    printf("Boundary value for dividing of atoms is missing! For details see HELP:\n");
    vypisHelp();
  }
  if ((soubor_vmd == NULL)&&(akce==4)) {
    printf("Specification of file with pre-counted surfaces of atoms is missing!\n");
    printf("For details see HELP:\n");
    vypisHelp();
  }
  if ((soubor != NULL)&&(souborOUT != NULL)&&(strcmp(soubor, souborOUT)==0)) {
    printf("Having the same name for input and output file is not allowed.\n");
    printf("For details see HELP:\n");
    vypisHelp();
  }
       
  /* opening of file */
  fr = fopen (soubor, "r");
  if (fr == NULL) { 
     printf ("File '%s' not found.\n",soubor); 
     exit(1);
  } else {
         if (!q) { printf("Processed molecule: %s\n",soubor); }
  }
  fclose(fr);
  /* --- end of examinations of input --- */  
  
  /* --- start of program from given arguments --- */
  p_sfirst = nacti_PDB_do_LZS(soubor);

  /* NIN */
  if (akce==1) { 
    if (!q) {printf("Chosen access: NIN\n");}
    if (!q) {printf("Limit for atoms at the surface: #NIN < %d \n",level);}
    identify_surf_at_nina(p_sfirst,soubor);
    vypis_povrchove_atomy_nina_struktura(p_sfirst,level);
    if (!q) {printf("------------------------------------------------\n");}
  }

  /* UCSF */
  if (akce==3) {
    if (!q) {printf("Chosen access: UCSF\n");}
    identify_surf_at_ucsfa(p_sfirst, soubor);
    vypis_povrchove_atomy_ucsfa_struktura(p_sfirst);
    if (!q) {printf("------------------------------------------------\n");}

  }

  /* SAS */
  if (akce==4) {
    if (!q) {printf("Chosen access: SAS\n");}
    if (!q) {printf("Limit for atoms at the surface: |SAS| > %f \n",limit);}
    vypis_povrchove_atomy_sasa_struktura(p_sfirst,soubor_vmd,soubor,limit);
    if (!q) {printf("------------------------------------------------\n");}

  }
  
 /* motif detection */
 
   i=0;
   while (motif[i]=='-') {
         i++;
   }
   j=i;
   i=0;
   while (j<=strlen(motif)) {
         motif[i]=motif[j];
         i++; j++;
   }
   motif_detector(p_sfirst,motif,sidechain,q);
   vypis_motif_soubor(p_sfirst, soubor, souborOUT,q); 

  /* cleaning */
  zrus_seznam(p_sfirst);
  
  return 0;
}

