#include "main.h"


/*...................global variables...................*/
float	aaAve[NUMAA][NUMATMS],
		sidestandAsa[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_PATH);
	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_mol_weights */


/*************************************************************/
/* load table of average volumes 
*/
void load_ave_vol()
{
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_PATH);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/vol.rich", LIB_PATH);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/vol.eisenberg", LIB_PATH);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/vol.chothia", LIB_PATH);
	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 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_PATH);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/area.rich", LIB_PATH);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/area.eisenberg", LIB_PATH);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/area.chothia", LIB_PATH);
	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 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_PATH);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/area_aa.rich", LIB_PATH);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/area_aa.eisenberg", LIB_PATH);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/area_aa.chothia", LIB_PATH);
	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 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_PATH);
	else if (radType == RICH_RAD)
		sprintf(tableName, "%s/tables/area_side.rich", LIB_PATH);
	else if (radType == EIS_RAD)
		sprintf(tableName, "%s/tables/area_side.eisenberg", LIB_PATH);
	else if (radType == CHOTH_RAD)
		sprintf(tableName, "%s/tables/area_side.chothia", LIB_PATH);
	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 */
