/* a_sas.c 
 *
 * writes results received from counting SASA
 * by the VMD program into the PDB file 
 * (SASA = Solvent-accessible surface area)
 * --------------------------------------------
 */

#include "main.h"

/* ------------------------------------------------------------------------- */
/* supposes that in != out (checked in higher function)*/
void vypis_povrchove_atomy_sasa_soubor(char vmd_file[], char in[], char out[], double level, int q){
/* ------------------------------------------------------------------------- */
  FILE *fw, *fr_pdb, *fr_vmd;
  char  radek_pdb[SLOUPCE], radek_vmd[SLOUPCE], outRadek[SLOUPCE], klicove_slovo[6];
  int uroven, i, counter=0;
  char *p_c, *p_c2;
  float value;

  fr_pdb = fopen(in,"r");
  if (fr_pdb == NULL) {
    printf("Opening input file %s did not succeed. Program will be terminated.\n", in);
    exit(1);
  }
  fr_vmd = fopen(vmd_file,"r");
  if (fr_vmd == NULL) {
    printf("Opening input file %s did not succeed. Program will be terminated.\n", vmd_file);
    exit(1);
  }
  fw = fopen(out,"w");
  if (fw == NULL) {
    printf("Creating output file did not succeed. Program will be terminated.\n");
    exit(1);
  }

  while (fgets(radek_pdb, SLOUPCE, fr_pdb) != NULL) {       
      for (i=0; i<SLOUPCE+1; i++) {outRadek[i]=radek_pdb[i];}
      uroven=0;
      strncpy(klicove_slovo,radek_pdb,6);
      klicove_slovo[6]='\0';
      p_c = strtok(klicove_slovo," ");
      p_c[strlen(p_c)]='\0';
      if (strcmp(p_c,"HETATM")==0) {uroven = 1;}
      if (strcmp(p_c,"ATOM")==0) {uroven = 2;}
      switch (uroven) {
         case 0: /* all except for HETATM a ATOM */
	       fprintf(fw,"%s",outRadek);
           break;
 
         case 1: /* HETATM */
         case 2: /* ATOM */
           fgets(radek_vmd, SLOUPCE, fr_vmd);
           p_c2 = strtok (radek_vmd," ");
           p_c2 = strtok (NULL," ");
           value = atof (p_c2);
    	   if (value > level) {
	         outRadek[17]='S';
	         outRadek[18]='U';
    	         outRadek[19]='R';
    	         counter++;
	       } else {
	         outRadek[17]='I';
                 outRadek[18]='N';
	         outRadek[19]='N';
	       }
	       fprintf(fw,"%s",outRadek);
    	   break;

         default : 
     	   /* opened file on this line does not begins with any of known keywords */
           fprintf(stderr,"\nOpened file %s do not have expected structure.\n", in);
    	   fclose(fr_pdb);
           exit(1);
      }

  }
  if (!q) {
    printf("\nResults:\n%d surface atoms identified.\n",counter);
    printf("Final PDB file: %s\n",out);
  }
  fclose(fr_pdb);
  fclose(fr_vmd);
  fclose(fw);
}
/* ------------------------------------------------------------------------- */
/* supposes that in != out (checked in higher function)*/
void vypis_povrchove_atomy_sasa_struktura(SEZNAM *p_sf, char vmd_file[], char in[], double level){
/* ------------------------------------------------------------------------- */
  SEZNAM *p_sa=NULL;
  FILE *fr_pdb, *fr_vmd;
  char  radek_pdb[SLOUPCE], radek_vmd[SLOUPCE], klicove_slovo[6];
  int uroven;
  char *p_c, *p_c2;
  float value;

  fr_pdb = fopen(in,"r");
  if (fr_pdb == NULL) {
    printf("Opening input file %s did not succeed. Program will be terminated.\n", in);
    exit(1);
  }
  fr_vmd = fopen(vmd_file,"r");
  if (fr_vmd == NULL) {
    printf("Opening input file %s did not succeed. Program will be terminated.\n", vmd_file);
    exit(1);
  }
  p_sa = p_sf;
  if (p_sa == NULL) {
    printf("An internal error appear during reading data about molecule.\n");
    printf("The program will be terminated.\n");
    exit(1);
  }

  while (fgets(radek_pdb, SLOUPCE, fr_pdb) != NULL) { 
      strncpy(klicove_slovo,radek_pdb,6);
      klicove_slovo[6]='\0';
      p_c = strtok(klicove_slovo," ");
      p_c[strlen(p_c)]='\0';

      uroven=0;
      if (strcmp(p_c,"HETATM")==0) {uroven = 1;}
      if (strcmp(p_c,"ATOM")==0) {uroven = 2;} 
      if ((strcmp(p_c,"ATOM")!=0)&&(strcmp(p_c,"HETATM")!=0)) {uroven = 0;} 
      switch (uroven) {
         case 0: /* all except for HETATM a ATOM */
           break;
 
         case 1: /* HETATM */
         case 2: /* ATOM */
           fgets(radek_vmd, SLOUPCE, fr_vmd);
           p_c2 = strtok (radek_vmd," ");
           p_c2 = strtok (NULL," ");
           value = atof (p_c2);
    	   if (value > level) { /* SUR */ p_sa->sur_atom = 1; }
    	                 else { /* INN */ p_sa->sur_atom = 0; }
           p_sa = p_sa->dalsi_seznam;
    	   break;

         default : 
     	   /* opened file on this line does not begin with any of known keywords */
           fprintf(stderr,"\nOpened file %s do not have expected structure.\n", in);
    	   fclose(fr_pdb);
           exit(1);
      }

  }

  fclose(fr_pdb);
  fclose(fr_vmd);
}
