
/*
	file: util.c
	contains utility routines for area 
*/

#include "area.h"
#include "ctype.h"

char *read_pdb();
void radasn(ATOM_REC *, int);

/*************************************************************/
/* get rid of header on top of pdb file then write small
*  header to output file.
*/
void read_write_header(infp)
FILE *infp;
{
char	fld1[MAX_LINE_LEN], line[MAX_LINE_LEN], firstchar;
int		done;


	done = FALSE;
	firstchar = getc(infp);

	while ( (firstchar != EOF) && (!done) ) {

		switch (firstchar) { 

			/* SOURCE field  - print to output */
			case 'S': 	fscanf(infp, "%s", fld1);
					   	if ( 0 != strcmp(fld1, "OURCE") )
					 		break;
						fgets(line, MAX_LINE_LEN-1, infp);
						fprintf(resfp, "SOURCE: %s", line);
						break;

			/* HEADER field  - print to output */
			case 'H': 	fscanf(infp, "%s", fld1);
					   	if ( 0 != strcmp(fld1, "EADER") )
					 		break;
						fgets(line, MAX_LINE_LEN-1, infp);
						fprintf(resfp, "HEADER: %s", line);
						break;

			/* COMPND field  - print to output */
			case 'C': 	fscanf(infp, "%s", fld1);
					   	if ( 0 != strcmp(fld1, "OMPND") )
					 		break;
						fgets(line, MAX_LINE_LEN-1, infp);
						fprintf(resfp, "COMPOUND: %s", line);
						break;

			/* ATOM found, stop reading the header */ 
			case 'A' :  firstchar = getc(infp);
						if (firstchar == 'T')
							done = TRUE;
						else
							fgets(line, MAX_LINE_LEN-1, infp);
						break;

			default: fgets(line, MAX_LINE_LEN-1, infp);

		}
		if (!done)
			firstchar = getc(infp);
	}
	rewind(infp);

}	/* read_write_header */


/*************************************************************/
/* retrieve next atom from input file
*/
int read_atom(pdbfp, atom)
FILE		*pdbfp;
ATOM_REC 	*atom;
{
int  	ret1;
char    *ret3;


	/* get accessible area
	ret2 = get_atom(&atomtmp, accessfp, ACCESS_FLAG);
	*/

	/* get contact area */
	ret1 = get_atom(atom, contactfp, CONTACT_FLAG);

	/* get xyz coords from pdb file */
	ret3 = read_pdb(pdbfp, atom, 0);

	if (ret3 == NULL) 
		return(EOF);
	else
		return(ret1);

}	/* read_atom */


/*************************************************************/
/* retrieve next atom from input file
*/
int get_atom(atom, fp, flag)
int flag;
ATOM_REC	*atom;
FILE		*fp;   // result of access 
{
char	line[MAX_LINE_LEN];
int		ret;

	ret = fscanf(fp, "%*d%d%s%s", &atom->resNum, atom->resName, atom->atomName);

	if (flag == ACCESS_FLAG)
		ret = fscanf(fp, " %f", &atom->accessArea);
	else
		ret = fscanf(fp, " %f", &atom->contactArea);

	fgets(line, MAX_LINE_LEN-1, fp);

	return(ret);

}	/* get_atom */


/*************************************************************************/
/* convert pdb file to atom structure array 
   that which access can read (with radius values attached).
*/
int  convert_pdb_accessc(atomArr, maxObs)
ATOM_REC   *atomArr;
int        *maxObs;
{
int 	counter, numRes, currResNum;
int	    firstResNum, lastResNum;
char	*ret;
FILE	*pfp;

	pfp = fopen(pdbFname, "r");
	counter = 1;
    numRes = 0;
    currResNum = -9999;

	ret = read_pdb(pfp, &atomArr[counter], 0);  // read an ATOM line from pdb file
	lastResNum = firstResNum = atomArr[counter].resNum;
	while ( ( ret != NULL ) && (allChains || (atomArr[counter].resNum >= lastResNum)) ) {

		/* skip hydrogen atoms */
		if ( (atomArr[counter].atomName[0] != 'H') ) {
				if ( ! isdigit(atomArr[counter].atomName[0]) || atomArr[counter].atomName[1] != 'H') {
						/* calculate radius with Vanderwaals radius */ 
						radasn(&atomArr[counter], VAN);
				}
		}	
		lastResNum = atomArr[counter].resNum;

		if( currResNum != atomArr[counter].resNum ) {
			numRes++;
			currResNum = atomArr[counter].resNum;
		}
		counter++;
		ret = read_pdb(pfp, &atomArr[counter], 0);
	}

	fclose(pfp);

	/* reset counter in read_pdb */
	resNumAdd = 0;
    *maxObs = counter - 1;

    //   return (lastResNum-firstResNum + 1);
    return numRes;

}	/* convert_pdb */



/* !!! NO USE any more:
*  convert pdb file to that which access can read (with radius
*  values attached).
*/
int  convert_pdb(tmp_output_addradii)
char *tmp_output_addradii;
{
int 		counter;
int		firstResNum, lastResNum;
char		*ret;
FILE		*pfp, *outfp;
ATOM_REC	atom;

	pfp = fopen(pdbFname, "r");
	outfp = fopen(tmp_output_addradii, "w");
	counter = 0;

	ret = read_pdb(pfp, &atom, 0);
	lastResNum = firstResNum = atom.resNum;
	while ( ( ret != NULL ) && (allChains || (atom.resNum >= lastResNum)) ) {

		/* skip hydrogen atoms */
		if (atom.atomName[0] != 'H') {

			/* calculate radius */ 
			radasn(&atom, VAN);

			/* print one atom's results out */
			if (atom.rad[VAN] != -1)
				fprintf(outfp,
				"%7d %-4s%-4s%5d   %10.5f%10.5f%10.5f    %8.4f\n",
				counter++, atom.atomName, atom.resName, atom.resNum,
				atom.x, atom.y, atom.z, atom.rad[VAN]);
					// String format updated to conform with the Fortran number format
					// expected by the program 'access'. David Arndt, Nov 23, 2007.
					// Format:
					// (1X,I1,6X,2A4,I5,3X,3F10.5,4X,F8.4)
		}	
		lastResNum = atom.resNum;
		ret = read_pdb(pfp, &atom, 0);
	}

	fclose(pfp);
	fclose(outfp);

	/* reset counter in read_pdb */
	resNumAdd = 0;
 
        return (lastResNum-firstResNum + 1);

}	/* convert_pdb */


/*********************************************************************
* PURPOSE:      to close files etc.
*/
void vadar_finish(flag)
int flag;
{

/*
        if (outfp[FPSTATS] != NULL)
                print_end(outfp[FPSTATS]);

    fclose(outfp[0]);
    fclose(outfp[1]);
    fclose(outfp[2]);
    fclose(outfp[3]);

#ifndef DEBUG   
    unlink(vol_in);
    unlink(phipsi_in);
    unlink(def2_in);
    unlink(area_in);
#endif
*/

/*
        if (flag) {
                unlink(areaAtomName);
                unlink(volAtomName);
                unlink(logFname);
                exit(-1);
        }   
*/

}   /* vadar_finish */

