/*
	file:	volume.c <<- main.c
	purpose:	contains main driver routines for volume program. 

	author: leigh willard 
	date:	21 July 1992	
	place:	University of Alberta / Department of Biochemistry
*/

#include "volume.h"
#define VOLFOR_PROGRAM    "volfor"  

/* globals */
int     type;
float   edge, rDelta;
void    read_parms_file();
FILE    *resfp, *get_out_file();
int	lastNum;
float	volumeTotal;
char	lastName[MAX_NAME_LEN];
char    inFname[MAX_FNAME_LEN];
char    volumeResultsFname[MAX_FNAME_LEN];

/* subroutines */

void set_volume_env( char *outfname);
void call_volume();
void calc_res_volume_totals(OUT_FEATURE_REC *res);
float calc_fracvol( float resVol, char *resName );

/*************************************************************/
/* main program
*/
void do_volume(outfname, ofresVol)
char outfname[MAX_FNAME_LEN];
OUT_FEATURE_REC *ofresVol;
{
	set_volume_env(outfname);
	call_volume();
	calc_res_volume_totals(ofresVol);

}	


/*************************************************************/
/* get flags, read parameters
*/
void set_volume_env(outfname)
char    *outfname;
{
int     i;
char    fortInFile[MAX_FNAME_LEN];
char    fortOutFile[MAX_FNAME_LEN];


        strcpy(fortInFile, outfname);

        strcat(fortInFile, ".area.atom");
	if (checkExist(fortInFile) != TRUE)
              show_err(TRUE, "Volume", "Input file does not exist");

        strcpy(inFname, fortInFile);

        strcpy(volumeResultsFname, outfname);  // copy pdbname.out to resultsfname 
        strcat(volumeResultsFname, ".volume"); // pdbname.out.volume 

        sprintf(fortOutFile, "%s.atom", volumeResultsFname);
        if( checkExist(fortOutFile) == TRUE )
                 unlink(fortOutFile);

        // set parms using define 
        edge = VOLUME_EDGE;
        type = VOLUME_CALC_TYPE;
        rDelta = VOLUME_RDELTA;

} 


/*************************************************************/
/* call routine (fortran) which calculates volume
*/
void call_volume()
{
char	shellstring[MAX_LINE_LEN];
char	tmp_input[MAX_FNAME_LEN], tmp_errFname[MAX_FNAME_LEN];
FILE	*tempfp;

	get_temp_file(tmp_input);
	tempfp = (FILE *)fopen(tmp_input, "w");
	fprintf(tempfp, "%s\n", inFname);
	fprintf(tempfp, "%s.atom\n", volumeResultsFname);   // pdbname.out.volume.atom
	fprintf(tempfp, "%s.err\n",  volumeResultsFname);
	fprintf(tempfp, "Y\n%f\n", edge);
	fprintf(tempfp, "Y\n%f\n", SRADIUS);
	fprintf(tempfp, "%d\n"   , type);
	fprintf(tempfp, "Y\n%f\n", rDelta);
	fclose(tempfp);

	
        // execute VOLUME.Fortran program
	sprintf (shellstring, "%s   %s   %s  %s",  VOLFOR_PROGRAM,   " <  " , tmp_input  ," > /dev/null" );
	prog_execute(shellstring);
	
        strcpy(tmp_errFname, volumeResultsFname);
        strcat(tmp_errFname, ".err");

        unlink(tmp_input);
        unlink(tmp_errFname);


}	/* call_volume */


/*************************************************************/
/* calculate total volume per residue 
*/
void calc_res_volume_totals(res)
OUT_FEATURE_REC *res;
{
char		fortfname[MAX_FNAME_LEN];
ATOM_REC	atom;
int		ret;
FILE		*fortfp;
int             i;


	/* open output file from fortran program volfor */
	sprintf(fortfname, "%s.atom", volumeResultsFname);
	fortfp = fopen(fortfname, "r");
	if (fortfp == NULL) {
		printf("ERROR: Can not open output %s\n", fortfname); 
		exit(-1);
	}

	i = 0;
	volumeTotal = 0.0;
	lastNum = -111;
	strcpy(lastName, "");

	while (1)  {
		ret = read_volume_atom(fortfp, &atom);
                if (ret == -1) break; 
		if (ret==4){

			if ( atom.resNum == lastNum ) {
				volumeTotal += atom.volume;   // volumeTotal is global variable 
                        } else {
				if (  lastNum !=-111 ) {
                                     res[i].resVol = volumeTotal;
                                     res[i].resFracVol = calc_fracvol( volumeTotal, lastName );

                                     i++;
                                }

				volumeTotal = atom.volume;
				strcpy(lastName, atom.resName);
				lastNum = atom.resNum;
			}
		}
	}

        res[i].resVol = volumeTotal;
        res[i].resFracVol = calc_fracvol( volumeTotal, lastName);

	fclose(fortfp);
//        unlink(fortfname);  // remove pdb.out.volume.atom
}	/* calc_res_totals */



/*********************************************************************
 * PURPOSE: calculate frac ASA 
 * INPUT:   a single residue
 */

float calc_fracvol(resVol, resName)
float resVol;
char resName[4];
{
        return( resVol/standardVol[res2num(resName)] );
}



