#include "area.h"

/*************************************************************/
/* initialise data 
*/
void load_cov_radius()
{

	/* set covalent radius */
	radius[COV][C4] = 0.77;
	radius[COV][C4H] = 0.77;
	radius[COV][C4HH] = 0.77;
	radius[COV][C4HHH] = 0.77;
	radius[COV][C3] = 0.77;
	radius[COV][C3H] = 0.77;
	radius[COV][C3HH] = 0.77;
	radius[COV][O1] = 0.66;	
	radius[COV][O2H] = 0.66;
	radius[COV][O1O2H] = 0.66;
	radius[COV][N4] = 0.70;
	radius[COV][N4H] = 0.70;
	radius[COV][N4HH] = 0.70;
	radius[COV][N4HHH] = 0.70;
	radius[COV][N3] = 0.70;
	radius[COV][N3H] = 0.70;
	radius[COV][N3HH] = 0.70;
	radius[COV][S2] = 1.04;
	radius[COV][S2H] = 1.04;
	radius[COV][P4] = 1.10;
	radius[COV][O1N3HH] = 0.68;
	radius[COV][Z2ION] = 0.74;
	radius[COV][FE] = 0.70;
	radius[COV][C4D] = 0.77;
	radius[COV][C4DD] = 0.77;
	radius[COV][C4DDD] = 0.77;

}	/* load_cov_radius */


/*************************************************************/
/* load in values for vanderwaals radius 
*/
void load_van_radius()
{

	if (radType == CHOTH_RAD) {
		radius[VAN][C4] = 0.00;
		radius[VAN][C4H] = 1.87;
		radius[VAN][C4HH] = 1.87;
		radius[VAN][C4HHH] = 1.87;
		radius[VAN][C3] = 1.76;
		radius[VAN][C3H] = 1.76;
		radius[VAN][C3HH] = 1.76;
		radius[VAN][O1] = 1.40;
		radius[VAN][O2H] = 1.40;
		radius[VAN][O1O2H] = 1.40;
		radius[VAN][N4] = 1.50;
		radius[VAN][N4H] = 1.50;
		radius[VAN][N4HH] = 1.50;
		radius[VAN][N4HHH] = 1.50;
		radius[VAN][N3] = 1.65;
		radius[VAN][N3H] = 1.65;
		radius[VAN][N3HH] = 1.65;
		radius[VAN][S2] = 1.85;
		radius[VAN][S2H] = 1.85;
		radius[VAN][P4] = 0.00;
		radius[VAN][O1N3HH] = 1.40;
		radius[VAN][Z2ION] = 1.70;
		radius[VAN][FE] = 1.70;
		radius[VAN][C4D] = 1.86;
		radius[VAN][C4DD] = 1.86;
		radius[VAN][C4DDD] = 1.86;
	}
	else if (radType == EIS_RAD) {
		radius[VAN][C4] = 0.00;
		radius[VAN][C4H] = 1.90;
		radius[VAN][C4HH] = 1.90;
		radius[VAN][C4HHH] = 1.90;
		radius[VAN][C3] = 1.90;
		radius[VAN][C3H] = 1.90;
		radius[VAN][C3HH] = 1.90;
		radius[VAN][O1] = 1.40;
		radius[VAN][O2H] = 1.40;
		radius[VAN][O1O2H] = 1.40;
		radius[VAN][N4] = 1.70;
		radius[VAN][N4H] = 1.70;
		radius[VAN][N4HH] = 1.70;
		radius[VAN][N4HHH] = 1.70;
		radius[VAN][N3] = 1.70;
		radius[VAN][N3H] = 1.70;
		radius[VAN][N3HH] = 1.70;
		radius[VAN][S2] = 1.80;
		radius[VAN][S2H] = 1.80;
		radius[VAN][P4] = 0.00;
		radius[VAN][O1N3HH] = 1.40;
		radius[VAN][Z2ION] = 1.70;
		radius[VAN][FE] = 1.70;
		radius[VAN][C4D] = 1.89;
		radius[VAN][C4DD] = 1.89;
		radius[VAN][C4DDD] = 1.89;
	}
	else if (radType == RICH_RAD) {
		radius[VAN][C4] = 0.00;
		radius[VAN][C4H] = 2.00;
		radius[VAN][C4HH] = 2.00;
		radius[VAN][C4HHH] = 2.00;
		radius[VAN][C3] = 1.70;
		radius[VAN][C3H] = 1.85;
		radius[VAN][C3HH] = 1.85;
		radius[VAN][O1] = 1.40;
		radius[VAN][O2H] = 1.60;
		radius[VAN][O1O2H] = 1.50;
		radius[VAN][N4] = 0.00;
		radius[VAN][N4H] = 2.00;
		radius[VAN][N4HH] = 2.00;
		radius[VAN][N4HHH] = 2.00;
		radius[VAN][N3] = 1.50;
		radius[VAN][N3H] = 1.70;
		radius[VAN][N3HH] = 1.80;
		radius[VAN][S2] = 1.85;
		radius[VAN][S2H] = 2.00;
		radius[VAN][P4] = 0.00;
		radius[VAN][O1N3HH] = 1.60;
		radius[VAN][Z2ION] = 0.74;
		radius[VAN][FE] = 1.70;
		radius[VAN][C4D] = 1.99;
		radius[VAN][C4DD] = 1.99;
		radius[VAN][C4DDD] = 1.99;
	}
	else if (radType == SHRAKE_RAD) {
		radius[VAN][C4] = 0.00;
		radius[VAN][C4H] = 2.00;
		radius[VAN][C4HH] = 2.00;
		radius[VAN][C4HHH] = 2.00;
		radius[VAN][C3] = 1.50;
		radius[VAN][C3H] = 1.85;
		radius[VAN][C3HH] = 1.85;
		radius[VAN][O1] = 1.40;
		radius[VAN][O2H] = 1.40;
		radius[VAN][O1O2H] = 1.40;
		radius[VAN][N4] = 1.50;
		radius[VAN][N4H] = 1.50;
		radius[VAN][N4HH] = 1.50;
		radius[VAN][N4HHH] = 1.50;
		radius[VAN][N3] = 1.50;
		radius[VAN][N3H] = 1.50;
		radius[VAN][N3HH] = 1.50;
		radius[VAN][S2] = 1.85;
		radius[VAN][S2H] = 1.85;
		radius[VAN][P4] = 0.00;
		radius[VAN][O1N3HH] = 1.40;
		radius[VAN][Z2ION] = 0.74;
		radius[VAN][FE] = 1.70;
		radius[VAN][C4D] = 1.99;
		radius[VAN][C4DD] = 1.99;
		radius[VAN][C4DDD] = 1.99;
	}
	else {
		printf("ERROR: illegal option for vanderwaals radius type\n");
		printf("       see vadar.parms file\n");
		exit(-1);
	}

}	/* load_van_radius */


/************************************************************************************
*  from VADAR main.tables.c
*/


/*...................global variables...................*/
float	aaAve[NUMAA][NUMATMS], standardAsa[NUMAA], sidestandAsa[NUMAA], standardVol[NUMAA];


/*************************************************************/
/* load table of molecular weights 
*/

/* 
void load_mol_weights()
{
char tableName[MAX_FNAME_LEN];
char idx[MAX_LINE_LEN], line[MAX_LINE_LEN], c;
float value;
FILE *fp;
int ret;

	sprintf(tableName, "%s/tables/mol.weights", lib_abspath);
	fp = fopen(tableName, "r");
	if (fp == NULL) {
		printf("ERROR: cannot find %s\n", tableName);
		exit(-1);
	}

	// skip over header in table 
	c = fgetc(fp);
	while (c == '#') {
		fgets(line, MAX_LINE_LEN-1, fp);
		c = fgetc(fp);
	}
	ungetc(c, fp);

	// read table 
	ret = fscanf(fp, "%s %f", idx, &value);
	while (ret != EOF) {
		molWeightTable[res2num(idx)] = value;
		ret = fscanf(fp, "%s %f", idx, &value);
	}

}	
*/


/*************************************************************/
/* load table of average volumes 
*/
void load_ave_vol( char *lib_abspath )
{
char tableName[MAX_FNAME_LEN];
char idx[MAX_LINE_LEN], line[MAX_LINE_LEN], c;
float value;
FILE *fp;
int ret;

	if (radType == SHRAKE_RAD)
		sprintf(tableName, "%s/tables/vol.shrake", lib_abspath);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/vol.rich", lib_abspath);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/vol.eisenberg", lib_abspath);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/vol.chothia", lib_abspath);
	else {
		printf("ERROR: illegal vanderwaals radius for AREA\n");
		printf("       see vadar.parms file\n");
		vadar_finish(TRUE);
	}

	fp = fopen(tableName, "r");
	if (fp == NULL) {
		printf("ERROR: cannot find %s\n", tableName);
		exit(-1);
	}

	/* skip over header in table */
	c = fgetc(fp);
	while (c == '#') {
		fgets(line, MAX_LINE_LEN-1, fp);
		c = fgetc(fp);
	}
	ungetc(c, fp);

	/* read table */
	ret = fscanf(fp, "%s %f", idx, &value);
	while (ret != EOF) {
		standardVol[res2num(idx)] = value;
		ret = fscanf(fp, "%s %f", idx, &value);
	}

}	/* load_ave_vol */


/*************************************************************/
/* load table of average areas 
*/
void load_ave_area( char *lib_abspath)
{
char tableName[MAX_FNAME_LEN];
char idx[MAX_LINE_LEN], line[MAX_LINE_LEN], c;
float value;
FILE *fp;
int ret;

	if (radType == SHRAKE_RAD)
		sprintf(tableName, "%s/tables/area.shrake", lib_abspath);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/area.rich", lib_abspath);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/area.eisenberg", lib_abspath);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/area.chothia", lib_abspath);
	else {
		printf("ERROR: illegal vanderwaals radius for AREA\n");
		printf("       see vadar.parms file\n");
		vadar_finish(TRUE);
	}

	fp = fopen(tableName, "r");
	if (fp == NULL) {
		printf("ERROR: cannot find %s\n", tableName);
		exit(-1);
	}

	/* skip over header in table */
	c = fgetc(fp);
	while (c == '#') {
		fgets(line, MAX_LINE_LEN-1, fp);
		c = fgetc(fp);
	}
	ungetc(c, fp);

	/* read table */
	ret = fscanf(fp, "%s %f", idx, &value);
	while (ret != EOF) {
		standardAsa[res2num(idx)] = value;
		ret = fscanf(fp, "%s %f", idx, &value);
	}
	
}	/* load_ave_area */


/*************************************************************/
/* load table of average exposed areas, broken down by amino
*  acid type.
*/
void load_aa_ave( char *lib_abspath )
{
char tableName[MAX_FNAME_LEN], line[MAX_LINE_LEN], c;
char idx1[MAX_LINE_LEN], idx2[MAX_LINE_LEN];
float value;
FILE *fp;
int ret;

	if (radType == SHRAKE_RAD)
		sprintf(tableName, "%s/tables/area_aa.shrake", lib_abspath);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/area_aa.rich", lib_abspath);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/area_aa.eisenberg", lib_abspath);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/area_aa.chothia", lib_abspath);
	else {
		printf("ERROR: illegal vanderwaals radius for AREA\n");
		printf("       see vadar.parms file\n");
		vadar_finish(TRUE);
	}

	fp = fopen(tableName, "r");
	if (fp == NULL) {
		printf("ERROR: cannot find %s\n", tableName);
		exit(-1);
	}

	/* skip over header in table */
	c = fgetc(fp);
	while (c == '#') {
		fgets(line, MAX_LINE_LEN-1, fp);
		c = fgetc(fp);
	}
	ungetc(c, fp);

	/* read table */
	ret = fscanf(fp, "%s %s %f", idx1, idx2, &value);
	while (ret != EOF) {
		aaAve[res2num(idx1)][atm2num(idx2)] = value;
		ret = fscanf(fp, "%s %s %f", idx1, idx2, &value);
	}
	
}	/* load_aa_area */



/*************************************************************/
/* load side chain table table of average areas 
*  these values come from the values in the load_aa_ave()
*  routine by ignoring the N, C, and O values.
*/
void load_side_ave_area( char *lib_abspath )
{
char tableName[MAX_FNAME_LEN];
char idx[MAX_LINE_LEN], line[MAX_LINE_LEN], c;
float value;
FILE *fp;
int ret;

	if (radType == SHRAKE_RAD)
		sprintf(tableName, "%s/tables/area_side.shrake", lib_abspath);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/area_side.rich", lib_abspath);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/area_side.eisenberg", lib_abspath);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/area_side.chothia", lib_abspath);
	else {
		printf("ERROR: illegal vanderwaals radius for AREA\n");
		printf("       see vadar.parms file\n");
		vadar_finish(TRUE);
	}

	fp = fopen(tableName, "r");
	if (fp == NULL) {
		printf("ERROR: cannot find %s\n", tableName);
		exit(-1);
	}

	/* skip over header in table */
	c = fgetc(fp);
	while (c == '#') {
		fgets(line, MAX_LINE_LEN-1, fp);
		c = fgetc(fp);
	}
	ungetc(c, fp);

	/* read table */
	ret = fscanf(fp, "%s %f", idx, &value);
	while (ret != EOF) {
		sidestandAsa[res2num(idx)] = value;
		ret = fscanf(fp, "%s %f", idx, &value);
	}
	
}	/* load_side_ave_area */

/*************************************************************/
/* initialise data 
*/
void load_tables(char *libabspath)
// void load_tables()
{

       // char lib_abspath[] = "/local/scratch/beomsoo/shiftx2-ver101/modules/resmf/lib";
       char *lib_abspath = libabspath;

       // add 2011.06.06
       if (*lib_abspath == NULL) { 
            // printf("lib_abspath is NULL\n");
            strcpy(lib_abspath, "./lib");   
            // printf("Now lib_abspath is %s\n", lib_abspath);
       } else {
            // printf("lib_abspath is %s\n", lib_abspath);
       }

        // printf("Load Tables in Refmf: %s \n", lib_abspath);

	load_cov_radius();
	load_van_radius();

        // for fracASA, sideFracASA from main.tables.c
	load_ave_area(lib_abspath);
	load_aa_ave(lib_abspath);
	load_side_ave_area(lib_abspath);
	load_ave_vol(lib_abspath);

}	/* load_tables */


