#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#define seqLength 1000

void usage();


void usage()
{
	printf("usage: simpleComp alignment cutoff\n");
	exit(1);
}

int main(int argc, char **argv)
{
	int i,j,k;
	if(argc<4) usage();
	char predFile[50],alnFile[50],para[5];
	strcpy(predFile,argv[2]);
	strcpy(alnFile,argv[1]);
	strcpy(para,argv[3]);
	int *cutoff5,*cutoff8,index5=1,index8=1;
//	cutoff5=ivector(1,seqLength);
//	cutoff8=ivector(1,seqLength);
	cutoff5 =new int [seqLength+1];
	cutoff8 =new int [seqLength+1];
	FILE *cutoffFile;
	char line[1000];
	char seps[]=" \n";
	char *token;
	if((cutoffFile=fopen(predFile,"r"))!=NULL){
		if(fgets(line,1000,cutoffFile)!=NULL){
			token=strtok(line,seps);
			while(token!=NULL){
				if(strcmp(token,"cutoff5:")!=0){
					cutoff5[index5++]=atoi(token);
				}
				token=strtok(NULL,seps);
			}
		}
		if(fgets(line,1000,cutoffFile)!=NULL){
			token=strtok(line,seps);
			while(token!=NULL){
				if(strcmp(token,"cutoff8:")!=0){
					cutoff8[index8++]=atoi(token);
				}
				token=strtok(NULL,seps);
			}
		}
	}
	int site[1000]; char aa[1000]; double value[1000];
	int index=0;
/*	ifstream in (alnFile,ios::in);
	if(!in) 
	{ 
		cout<<"cannot open file: "<<alnFile<<endl;
		exit(1);
	}
	while(!in.eof()){
		in>>site[index]>>aa[index]>>value[index];
		cout<<site[index]<<"  "<<aa[index]<<"  "<<value[index]<<endl;
		index++;
	}
	in.close();
	for(i=0;i<index;i++) {
		printf("%5d  %c  %8.3f\n",site[i],aa[i],value[i]);
	}
*/
	FILE *in;
	if((in=fopen(alnFile,"r"))==NULL) {printf("can not open file: %s",alnFile); exit(1);}
	else {
		while(fgets(line,1000,in)!=NULL){
			token=strtok(line,seps);
			token=strtok(NULL,seps);
			site[index]=atoi(token);
			token=strtok(NULL,seps);
			aa[index]=token[0];
//			value[index]=atof(token);
			index++;
		}
	}
	fclose(in);
	for(i=0;i<index;i++) {
//		printf("%5d  %c  %8.3f\n",site[i],aa[i],value[i]);
	}

	int hit5[seqLength+1],hit8[seqLength+1];

	int A5_5,A5_8;
	int A10_5,A10_8;
	int A15_5,A15_8;
	int A20_5,A20_8;
	A5_5=A5_8=A10_5=A10_8=A15_5=A15_8=A20_5=A20_8=0;
	for(i=0;i<=19;i++){
		for(j=1;j<index5;j++){
			if(site[i]==cutoff5[j]){
				if(i<5) { A5_5++; A10_5++; A15_5++; A20_5++;}
				else if(i<10) {A10_5++; A15_5++; A20_5++;}
				else if(i<15) { A15_5++; A20_5++;}
				else A20_5++;
			}
		}
		for(j=1;j<index8;j++){
			if(site[i]==cutoff8[j]){
				if(i<5) { A5_8++; A10_8++; A15_8++; A20_8++;}
				else if(i<10) {A10_8++; A15_8++; A20_8++;}
				else if(i<15) { A15_8++; A20_8++;}
				else A20_8++;
			}
		}
	}

	char conFile[50];
	strcpy(conFile,predFile);
	strcat(conFile,".comp");
	FILE *out; 
	out=fopen(conFile,"a+");
//	out=stdout;
	fprintf(out,"%c %c %c ",para[0],para[1],para[2]);
	fprintf(out,"%4d %4d %4d ",A5_5,A5_8,A5_8-A5_5);
	fprintf(out,"%4d %4d %4d ",A10_5,A10_8,A10_8-A10_5);
	fprintf(out,"%4d %4d %4d ",A15_5,A15_8,A15_8-A15_5);
	fprintf(out,"%4d %4d %4d\n",A20_5,A20_8,A20_8-A20_5);
	fclose(out);

}

