// This program writes the output to the file "output1"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <math.h>

#include "String.h"
#include "pph.h"

void write_format11(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	char string2[MAXSIZE];
	
	int i,j,no,row,column,digit;
	
	FILE *fp,*fp1,*fp2,*fp3,*fp4;
	
	//strcpy(outname,prefix);
	//strncat(outname,".out",4);
	char *outname = "output1";
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	fp1 = fopen(filename,"r");
	fp2 = fopen(inname,"r");
	fp3 = fopen("title","r");
	fp4 = fopen(hname,"r");
	
	fscanf(fp2,"%d%d",&row,&column);
	fscanf(fp4,"%d%d",&row,&column);
	
	fprintf(fp,"Output format:\ninput haplotype\ninput haplotype\n\ngenotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
				
	while(fscanf(fp1,"%s",string1)!=EOF) {
		fscanf(fp3,"%d",&no);
		fscanf(fp1,"%s",string2);
		
		for(i=0;i<no;i++) {
			fprintf(fp,"%s\n",string1);
			fprintf(fp,"%s\n\n",string2);
				
			for(j=0;j<column;j++) {
				fscanf(fp2,"%d",&digit);
				fprintf(fp,"%d",digit);
			}
			fprintf(fp,"\n\n");
			
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				fprintf(fp,"%d",digit);
			}
			fprintf(fp,"\n");
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				fprintf(fp,"%d",digit);
			}
			fprintf(fp,"\n");
			
			for(j=0;j<NEWLINE;j++) 
				fprintf(fp,"-");
				
			fprintf(fp,"\n");
		}
	}
	
	fclose(fp);
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	fclose(fp4);
}

bool check_line1(char *line) {
	int len = strlen(line);
	int i;
	for(i=0;i<len;i++) {
		if(line[i]!=' ' || line[i]!='\n')
			return true;
	}
	return false;
}

void write_format12(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	char string2[MAXSIZE];
	char title[100];
	int i,j,no,row,column,digit;
	int which = 0;
	FILE *fp,*fp2,*fp3,*fp4;
	
	//strcpy(outname,prefix);
	//strncat(outname,".out",4);
	char *outname = "output1";
	
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	//fp1 = fopen(filename,"r");
	fp2 = fopen(inname,"r");
	fp3 = fopen("title","r");
	fp4 = fopen(hname,"r");
	
	ifstream infile(filename);
	fscanf(fp2,"%d%d",&row,&column);
	fscanf(fp4,"%d%d",&row,&column);
	
	fprintf(fp,"Output format:\nlabel\ninput haplotype\ninput haplotype\n\ngenotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
	
	while(infile) {
	//while(fscanf(fp1,"%s",title)!=EOF) {
		
		if(which==0) {
			infile.getline(title,MAXSIZE);
			if(!check_line1(title)) {
				which = 0;
				continue;
			}
			which = 1;
		}
		
		if(which==1) {	
			infile.getline(string1,MAXSIZE);
			if(!check_line1(string1)) {
				which = 1;
				continue;
			}
			which = 2;
		}
		
		if(which==2) {	
			infile.getline(string2,MAXSIZE);
			if(!check_line1(string2)) {
				which = 2;
				continue;
			}
			which = 0;
		}
			
		fscanf(fp3,"%d",&no);
		//fscanf(fp1,"%s%s",string1,string2);
		
		for(i=0;i<no;i++) {
			fprintf(fp,"%s\n",title);
			fprintf(fp,"%s\n",string1);
			fprintf(fp,"%s\n\n",string2);
				
			for(j=0;j<column;j++) {
				fscanf(fp2,"%d",&digit);
				fprintf(fp,"%d",digit);
			}
			fprintf(fp,"\n\n");
			
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				fprintf(fp,"%d",digit);
			}
			fprintf(fp,"\n");
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				fprintf(fp,"%d",digit);
			}
			fprintf(fp,"\n");
			
			for(j=0;j<NEWLINE;j++) 
				fprintf(fp,"-");
				
			fprintf(fp,"\n");
		}
	}
	
	fclose(fp);
	//fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	fclose(fp4);
}

void write_format13(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	
	int j,row,column,digit;
	
	FILE *fp,*fp1,*fp2,*fp4;
	
	//strcpy(outname,prefix);
	//strncat(outname,".out",4);
	char *outname = "output1";
	
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	fp1 = fopen(filename,"r");
	fp2 = fopen(inname,"r");
	
	fp4 = fopen(hname,"r");
	
	fscanf(fp2,"%d%d",&row,&column);
	fscanf(fp4,"%d%d",&row,&column);
	
	fprintf(fp,"Output format:\ninput genotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
		
	while(fscanf(fp1,"%s",string1)!=EOF) {
		fprintf(fp,"%s\n\n",string1);
				
			
		for(j=0;j<column;j++) {
			fscanf(fp4,"%d",&digit);
			fprintf(fp,"%d",digit);
		}
		fprintf(fp,"\n");
		for(j=0;j<column;j++) {
			fscanf(fp4,"%d",&digit);
			fprintf(fp,"%d",digit);
		}
		fprintf(fp,"\n");
			
		for(j=0;j<NEWLINE;j++) 
			fprintf(fp,"-");
				
		fprintf(fp,"\n");
	}
	
	fclose(fp);
	fclose(fp1);
	fclose(fp2);
	fclose(fp4);
}

void write_format14(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	char title[100];
	int j,row,column,digit,erow;
	int which = 0 ;
	FILE *fp,*fp2,*fp4;
	
	//strcpy(outname,prefix);
	//strncat(outname,".out",4);
	char *outname = "output1";
	
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	//fp1 = fopen(filename,"r");
	fp2 = fopen(inname,"r");
	fp4 = fopen(hname,"r");
	ifstream infile(filename);
	
	fscanf(fp2,"%d%d",&erow,&column);
	fscanf(fp4,"%d%d",&row,&column);
	
	fprintf(fp,"Output format:\nlabel\ninput genotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
	
	//infile.getline(title,MAXSIZE);
	//while(!check_line(title))
	//	infile.getline(title,MAXSIZE);
	//printf("line is %s\n",title);
	
	//for(i=0;i<erow;i++) {
	//while(fscanf(fp1,"%s",title)!=EOF) {
	while(infile) {	
		if(which==0) {
			infile.getline(title,MAXSIZE);
			if(!check_line1(title)) {
				which = 0;
				continue;
			}
			which = 1;
		}
		
		if(which==1) {
			infile.getline(string1,MAXSIZE);
			if(!check_line1(string1)) {
				which = 1;
				continue;
			}
			which = 0;
		}
			
		//fscanf(fp1,"%s",string1);
		
		fprintf(fp,"%s\n",title);
		fprintf(fp,"%s\n\n",string1);
			
		for(j=0;j<column;j++) {
			fscanf(fp4,"%d",&digit);
			fprintf(fp,"%d",digit);
		}
		fprintf(fp,"\n");
		for(j=0;j<column;j++) {
			fscanf(fp4,"%d",&digit);
			fprintf(fp,"%d",digit);
		}
		fprintf(fp,"\n");
			
		for(j=0;j<NEWLINE;j++) 
			fprintf(fp,"-");
				
		fprintf(fp,"\n");
	}
	
	fclose(fp);
	//fclose(fp1);
	fclose(fp2);
	fclose(fp4);
}

void write_format15(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	char string2[MAXSIZE];
	char *first,*second;
	int i,j,no,row,column,digit;
	char nucli,newline;
	
	FILE *fp,*fp1,*fp2,*fp3,*fp4;
	
	char *outname = "output1";
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	fp1 = fopen(filename,"r");
	fp2 = fopen(inname,"r");
	fp3 = fopen("title","r");
	fp4 = fopen(hname,"r");
	
	fprintf(fp,"Output format:\ninput haplotype\ninput haplotype\n\ngenotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
	
	fscanf(fp2,"%d%d",&row,&column);
	fscanf(fp4,"%d%d",&row,&column);
	
	first = new char[column];
	second = new char[column];
	
	for(i=0;i<column;i++) {
		fscanf(fp3,"%c%c%c",&first[i],&second[i],&newline);
	}
		
	while(fscanf(fp1,"%s",string1)!=EOF) {
		fscanf(fp3,"%d",&no);
		fscanf(fp1,"%s",string2);
		
		for(i=0;i<no;i++) {
			fprintf(fp,"%s\n",string1);
			fprintf(fp,"%s\n\n",string2);
				
			for(j=0;j<column;j++) {
				if(string1[j]==string2[j])
					nucli = string1[j];
				else
					nucli = 'X';
				fprintf(fp,"%c",nucli);
			}
			fprintf(fp,"\n\n");
			
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				if(digit==0)
					nucli = first[j];
				else
					nucli = second[j];
					
				fprintf(fp,"%c",nucli);
			}
			fprintf(fp,"\n");
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				if(digit==0)
					nucli = first[j];
				else
					nucli = second[j];
				fprintf(fp,"%c",nucli);
			}
			fprintf(fp,"\n");
			
			for(j=0;j<NEWLINE;j++) 
				fprintf(fp,"-");
				
			fprintf(fp,"\n");
		}
	}
	
	fclose(fp);
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	fclose(fp4);
}

void write_format16(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	char string2[MAXSIZE];
	char title[100];
	char *first,*second;
	int i,j,no,row,column,digit;
	char nucli,newline;
	int which = 0;
	FILE *fp,*fp2,*fp3,*fp4;
	
	char *outname = "output1";
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	//fp1 = fopen(filename,"r");
	ifstream infile(filename);
	fp2 = fopen(inname,"r");
	fp3 = fopen("title","r");
	fp4 = fopen(hname,"r");
	
	fprintf(fp,"Output format:\nlabel\ninput haplotype\ninput haplotype\n\ngenotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
	
	fscanf(fp2,"%d%d",&row,&column);
	fscanf(fp4,"%d%d",&row,&column);
	
	first = new char[column];
	second = new char[column];
	
	for(i=0;i<column;i++) {
		fscanf(fp3,"%c%c%c",&first[i],&second[i],&newline);
	}
	
	while(infile) {	
		if(which==0) {
			infile.getline(title,MAXSIZE);
			if(!check_line1(title)) {
				which = 0;
				continue;
			}
			which = 1;
		}
		
		if(which==1) {	
			infile.getline(string1,MAXSIZE);
			if(!check_line1(string1)) {
				which = 1;
				continue;
			}
			which = 2;
		}
		
		if(which==2) {	
			infile.getline(string2,MAXSIZE);
			if(!check_line1(string2)) {
				which = 2;
				continue;
			}
			which = 0;
		}
		
		
		fscanf(fp3,"%d",&no);
		
		for(i=0;i<no;i++) {
			fprintf(fp,"%s\n",title);
			fprintf(fp,"%s\n",string1);
			fprintf(fp,"%s\n\n",string2);
				
			for(j=0;j<column;j++) {
				if(string1[j]==string2[j])
					nucli = string1[j];
				else
					nucli = 'X';
				fprintf(fp,"%c",nucli);
			}
			fprintf(fp,"\n\n");
			
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				if(digit==0)
					nucli = first[j];
				else
					nucli = second[j];
					
				fprintf(fp,"%c",nucli);
			}
			fprintf(fp,"\n");
			for(j=0;j<column;j++) {
				fscanf(fp4,"%d",&digit);
				if(digit==0)
					nucli = first[j];
				else
					nucli = second[j];
				fprintf(fp,"%c",nucli);
			}
			fprintf(fp,"\n");
			
			for(j=0;j<NEWLINE;j++) 
				fprintf(fp,"-");
				
			fprintf(fp,"\n");
		}
	}
	
	fclose(fp);
	//fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	fclose(fp4);
}

void write_format17(char *prefix, char *filename) {
	char inname[100];
	char hname[100];
	char string1[MAXSIZE];
	char string2[MAXSIZE];
	char string3[MAXSIZE];
	
	int j,row,column,digit;
	
	FILE *fp,*fp1,*fp4;
	
	//strcpy(outname,prefix);
	//strncat(outname,".out",4);
	char *outname = "output1";
	
	strcpy(inname,prefix);
	strncat(inname,".1",2);
	
	strcpy(hname,prefix);
	strncat(hname,".1co",4);
	
	fp = fopen(outname,"w");
	fp1 = fopen("hapsav.1","r");
	fp4 = fopen(hname,"r");
	
	fprintf(fp,"Output format:\ninput haplotype\ninput haplotype\n\ngenotype\n\noutput haplotype\noutput haplotype\n");
	for(j=0;j<16;j++) 
		fprintf(fp,"-");	
	fprintf(fp,"\n");
	
	fscanf(fp1,"%d%d%d",&row,&column,&column);
	fscanf(fp4,"%d%d",&row,&column);
		
	while(fscanf(fp1,"%s",string1)!=EOF) {
		fscanf(fp1,"%s",string2);
		fscanf(fp1,"%s",string3);
		
		fprintf(fp,"%s\n",string2);
		fprintf(fp,"%s\n\n",string3);
		fprintf(fp,"%s\n\n",string1);
				
		for(j=0;j<column;j++) {
			fscanf(fp4,"%d",&digit);
			fprintf(fp,"%d",digit);
		}
		fprintf(fp,"\n");
		for(j=0;j<column;j++) {
			fscanf(fp4,"%d",&digit);
			fprintf(fp,"%d",digit);
		}
		fprintf(fp,"\n");	
		for(j=0;j<NEWLINE;j++) 
			fprintf(fp,"-");
				
		fprintf(fp,"\n");	
	}
	
	fclose(fp);
	fclose(fp1);
	fclose(fp4);
}

void write_tree1(char *prefix,double is) {
	char treefile[100],temp[1000];
	FILE *fp1,*fp2;
	
	char *outfile = "output1";
	
	strcpy(treefile,prefix);
	strncat(treefile,".1ct",4);
	
	fp1 = fopen(outfile,"a");
	fp2 = fopen(treefile,"r");
	fprintf(fp1,"The tree is:\n");
	
	while(fscanf(fp2,"%s",temp)!=EOF) {
		fprintf(fp1,"%s ",temp);	
	}
	
	fprintf(fp1,"\n\n");
	
	double result = pow(2.0,is);
	
	if(is>=30)
		fprintf(fp1,"The tree is not unique and there are 2^%d possibilities",(int)is);
	else if(is!=0)
		fprintf(fp1,"The tree is not unique and there are %d possibilities\n",(int)result);
	else
		fprintf(fp1,"The tree is unique\n");
			
	fclose(fp1);
	fclose(fp2);
}

int parse_output1(int format) {
	char output1[10*MAXSIZE],output2[MAXSIZE],output3[MAXSIZE],output4[MAXSIZE],output5[MAXSIZE],space[MAXSIZE];
	FILE *fp;
	int len,times,i,j,header,alen;
	fp = fopen("tempout","w");
	ifstream infile("output1");
	
	if(format==1 || format==7 || format==5)
		header = 9;
	else if(format==2 || format==6)
		header = 10;
	else if(format==3)
		header = 6;
	else if(format==4)
		header = 7;
					
	for(i=0;i<header;i++) {
		infile.getline(output1,MAXSIZE);
		fprintf(fp,"%s\n",output1);
	}
	
	while(infile) {
		if(format==2 || format==4 || format==6) {
			infile.getline(output1,MAXSIZE); // label
			if(strncmp(output1,"The",3)==0)
				break;
			fprintf(fp,"%s\n",output1);
		}
		
			if(format!=3 && format!=4) {
				infile.getline(output1,MAXSIZE);
				if(strncmp(output1,"The",3)==0)
					break;
				infile.getline(output2,MAXSIZE);
				infile.getline(space,MAXSIZE);
			}		
			infile.getline(output3,MAXSIZE);
			
			if(format==3)
				if(strncmp(output3,"The",3)==0)
					break;
						
			infile.getline(space,MAXSIZE);
			infile.getline(output4,MAXSIZE);
			infile.getline(output5,MAXSIZE);
			
			len = strlen(output3);
			alen = len;
			times = len/NEWLINE;
		
			for(i=0;i<times;i++) {
				fprintf(fp,"length: %d ~ %d\n",i*NEWLINE+1,i*NEWLINE+NEWLINE);
				
				if(format!=3 && format!=4) {
					for(j=i*NEWLINE;j<i*NEWLINE+NEWLINE;j++)
						fprintf(fp,"%c",output1[j]);
					fprintf(fp,"\n");
				
					for(j=i*NEWLINE;j<i*NEWLINE+NEWLINE;j++)
						fprintf(fp,"%c",output2[j]);
					fprintf(fp,"\n\n");
				}
					
				for(j=i*NEWLINE;j<i*NEWLINE+NEWLINE;j++)
					fprintf(fp,"%c",output3[j]);
				fprintf(fp,"\n\n");
				
				for(j=i*NEWLINE;j<i*NEWLINE+NEWLINE;j++)
					fprintf(fp,"%c",output4[j]);
				fprintf(fp,"\n");
				
				for(j=i*NEWLINE;j<i*NEWLINE+NEWLINE;j++)
					fprintf(fp,"%c",output5[j]);
				fprintf(fp,"\n\n");
			}
			
			if(NEWLINE*times<len) {
				if(len>NEWLINE)
					fprintf(fp,"length: %d ~ %d\n",NEWLINE*times+1,len);
					
				if(format!=3 && format!=4) {
					for(j=NEWLINE*times;j<len;j++) 
						fprintf(fp,"%c",output1[j]);
					fprintf(fp,"\n");
			
					for(j=NEWLINE*times;j<len;j++) 
						fprintf(fp,"%c",output2[j]);
					fprintf(fp,"\n\n");
				}
				
				for(j=NEWLINE*times;j<len;j++) 
					fprintf(fp,"%c",output3[j]);
				fprintf(fp,"\n\n");
			
				for(j=NEWLINE*times;j<len;j++) 
					fprintf(fp,"%c",output4[j]);
				fprintf(fp,"\n");
			
				for(j=NEWLINE*times;j<len;j++) 
					fprintf(fp,"%c",output5[j]);
				fprintf(fp,"\n");
			}
		
			infile.getline(output1,MAXSIZE);
			fprintf(fp,"%s\n",output1);
		
	}
	
	if(format==3) {
		for(i=0;i<20;i++)
			output1[i] = output3[i];
	}
		
	// print tree
	while(infile) {
		len = strlen(output1);
		times = len/NEWLINE;
		
		for(i=0;i<times;i++) {
			for(j=i*NEWLINE;j<i*NEWLINE+NEWLINE;j++)
				fprintf(fp,"%c",output1[j]);
			fprintf(fp,"\n");
		}
		for(j=NEWLINE*times;j<len;j++) 
			fprintf(fp,"%c",output1[j]);
		fprintf(fp,"\n");
				
		infile.getline(output1,10*MAXSIZE);	
	}
	
	fclose(fp);
	infile.close();
	system("cp tempout output1");
	system("rm tempout");
	return alen;
}
