//	This source and Redundancy quotient method apply to GNU General Public License. 
//			Copyright (C) 2017  Jasenko Dzinleski 

//	This source applies to the GNU General Public License as
//	published by the Free Software Foundation 
//	and can not be used, copied, sold, redistributed or 
//	used in any other way but only by written permission by Jasenko Dzinleski . 
//	Copyright (C) from 2001 - 2012 and later by Jasenko Dzinleski 

//	This program is distributed in the hope that it will be useful, but
//	WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//	General Public License for more details. 

//	You should have received a copy of the GNU General Public License along
//	with this program; if not, write to the Free Software Foundation, Inc.,
//	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

//
//			Redundancy quotient
//			written by Dzinleski Jasenko April , 2017
//

#include 	<stdio.h>
#include 	<stdlib.h>
#include 	<string.h>
#include 	<ctype.h>
#include 	<math.h>

FILE 	*f1,*f2;
char	infn[256]="fasta__.txt";
char	outfn[256];
char	soutfn[256];
char	infn_[256];

char	sv[256];
char	sva[1024][256];int svai=0;
char	svl[256];
char	svl_[1024];

//--------------------------------------------------------------
	#include 	<unistd.h>
	#include 	<wait.h>

	int p_1_(char *ar[],char ofn[256])
	{
		int 	outf[2];
		int	status;
		pipe(outf);pid_t pid=fork();if(pid==0){while((status=execvp(ar[0],ar))<0){while(wait(&status)!= pid);}}
		return(0);
	}
//--------------------------------------------------------------
double p_2(char inf[256])
{
	int 	i,j,k,fbyte;
	int	mcc=0,ncc=0,mc=0,nc=0;
	char	sb[2][256];
	double	dmc=0;
	int	dmcc=1;

	for(i=0;i<256;++i){sb[0][i]='\0';}
	f1=fopen(inf,"rb");if(f1==NULL){return(0);} 
	fbyte=getc(f1);j=0;while(!feof(f1)&&fbyte!=13&&fbyte!=10){sb[0][j]=fbyte;++j;fbyte=getc(f1);}
	while(!feof(f1)&&(fbyte==13||fbyte==10)){fbyte=getc(f1);}
	while(!feof(f1))
	{
		for(i=0;i<256;++i){sb[1][i]='\0';}
		j=0;k=0;while(!feof(f1)&&fbyte!=13&&fbyte!=10){sb[1][j]=fbyte;if(sb[0][j]==sb[1][j]){++k;}++j;fbyte=getc(f1);}
		while(!feof(f1)&&(fbyte==13||fbyte==10)){fbyte=getc(f1);}
		if(strlen(sb[1])!=k){if(nc){++nc;}else{++nc;++nc;}for(i=0;i<256;++i){sb[0][i]=sb[1][i];}}else{ncc+=nc;nc=0;}
		
	}
	fclose(f1);
	for(i=0;i<256;++i){sb[0][i]='\0';}
	f1=fopen(inf,"rb");if(f1==NULL){return(0);} 
	fbyte=getc(f1);j=0;while(!feof(f1)&&fbyte!=13&&fbyte!=10){sb[0][j]=fbyte;++j;fbyte=getc(f1);}
	while(!feof(f1)&&(fbyte==13||fbyte==10)){fbyte=getc(f1);}
	while(!feof(f1))
	{
		for(i=0;i<256;++i){sb[1][i]='\0';}
		j=0;k=0;while(!feof(f1)&&fbyte!=13&&fbyte!=10){sb[1][j]=fbyte;if(sb[0][j]==sb[1][j]){++k;}++j;fbyte=getc(f1);}
		while(!feof(f1)&&(fbyte==13||fbyte==10)){fbyte=getc(f1);}
		if(strlen(sb[1])==k){if(mc){++mc;}else{++mc;++mc;}}else{for(i=0;i<256;++i){sb[0][i]=sb[1][i];}mcc+=mc;mc=0;}
		
	}
	fclose(f1);
	//printf("%d\t%d\t",mcc,ncc);
	//if(ncc&&mcc){printf("%e\n",(double)mcc/(double)ncc);}else{printf("%e\n",0);}
	if(ncc&&mcc){return((double)mcc/(double)ncc);}
	return(0);
}

int main(int argc,char *argv[])
{

int	a,b;
int	d,e;
int	i,j,k,l=1,lc=1200,m,n;
int	fb;
char	ssl[256];
char	outfn_[256]="out_d";
double  sqa[4096][20];

	while(l<=lc)
	{

		for(j=0;j<256;++j){ssl[j]='\0';outfn[j]='\0';}
		if(l<=9)
		{
		ssl[strlen(ssl)]=(char)(48+l);
		}else{
		if(l<=99)
		{
		ssl[strlen(ssl)]=(char)(48+((int)l/(int)10));
		ssl[strlen(ssl)]=(char)(48+(int)l-(int)10*((int)l/(int)10));
		}else{
		if(l<=999)
		{
		ssl[strlen(ssl)]=(char)(48+((int)l/(int)100));
		ssl[strlen(ssl)]=(char)(48+((int)l-(int)100*((int)l/(int)100))/(int)10);
		ssl[strlen(ssl)]=(char)(48+((int)l-(int)100*((int)l/(int)100))-(int)10*(((int)l-(int)100*((int)l/(int)100))/(int)10));
		}else{
		ssl[strlen(ssl)]=(char)(48+((int)l/(int)1000));
		ssl[strlen(ssl)]=(char)(48+((int)l-(int)1000*((int)l/(int)1000))/(int)100);
		ssl[strlen(ssl)]=(char)(48+((((int)l-(int)1000*((int)l/(int)1000))-(int)100*(((int)l-(int)1000*((int)l/(int)1000))/(int)100))/10));
		ssl[strlen(ssl)]=(char)(48+((int)l-(int)1000*((int)l/(int)1000))-(int)100*(((int)l-(int)1000*((int)l/(int)1000))/(int)100)
		-(int)10*((((int)l-(int)1000*((int)l/(int)1000))-(int)100*(((int)l-(int)1000*((int)l/(int)1000))/(int)100))/10));		
		}
		}
		}
		strcat(outfn,outfn_);strcat(outfn,ssl);
        	outfn[strlen(outfn)]='.';outfn[strlen(outfn)]='t';outfn[strlen(outfn)]='x';outfn[strlen(outfn)]='t';
		strcpy(infn_,outfn);
		for(i=6;i<=20;++i)
		{
            		for(j=0;j<256;++j){ssl[j]='\0';outfn[j]='\0';soutfn[j]='\0';}
            		if(i<=9){ssl[strlen(ssl)]=(char)(48+i);}else{
				ssl[strlen(ssl)]=(char)(48+((int)i/(int)10));ssl[strlen(ssl)]=(char)(48+i-(int)10*((int)i/(int)10));
			}
			j=0;while(j<-4+strlen(infn_)){outfn[strlen(outfn)]=infn_[j];++j;}
            		outfn[strlen(outfn)]='_';outfn[strlen(outfn)]='l';
			strcat(outfn,ssl);
            		outfn[strlen(outfn)]='.';outfn[strlen(outfn)]='t';outfn[strlen(outfn)]='x';outfn[strlen(outfn)]='t';
			soutfn[strlen(soutfn)]='-';soutfn[strlen(soutfn)]='o';soutfn[strlen(soutfn)]='s';strcat(soutfn,outfn);
			//printf("%s %s\n",outfn,soutfn);
			char *cm[]={"sort",outfn,soutfn,NULL};
			//p_1_(cm,"out.txt");
			
			for(j=0;j<256;++j){soutfn[j]='\0';}soutfn[strlen(soutfn)]='s';strcat(soutfn,outfn);
			//printf("%s\t%s\t",outfn,soutfn);
			sqa[-1+l][-6+i]=p_2(soutfn);
			
		}
		++l;
	}
	for(i=0;i<1200;++i){for(j=0;j<15;++j){printf("%e\t",sqa[i][j]);}printf("\n");}
	return(0);
}