//	This source and Enthropy quotioent method apply to GNU General Public License. 
//			Copyright (C) 2012  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.


//  			The 3-bit number generator
//			    written March , 2017
//     			to my daughter Maria Dzinleska


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

FILE 	*f1;
char	infn[256]="fasta__.txt";
int 	ba15[8][8][8][8][8][3];
int	ba15i=0;
int 	ba16[256][256];
char	sv[256];
//char 	sa[256]="CCGTGGCGTCGCGCAGGCTCGGCTTACCGAGGTCGGCCTCTGGGACGCGGTCAAGGATCGGCTCAGCGAT";
long	mc=0,unmc=0;

int p_1(char sa[256])
{

//000
//110
//011
//101

int i,j,k,l,m,n;
int mi,mj,mk,ml,mm;
int a,b,mx;
long lb;
char sa_[256];

for(i=0;i<256;++i){for(j=0;j<256;++j){ba16[i][j]=0;}}
for(i=0;i<=7;++i){for(j=0;j<=7;++j){for(k=0;k<=7;++k){for(l=0;l<=7;++l){for(m=0;m<=7;++m)
{
	ba15[i][j][k][l][m][0]=-1;
	ba15[i][j][k][l][m][1]=0;
	ba15[i][j][k][l][m][2]=0;
}}}}}
ba15i=0;

for(a=0;a<-1+strlen(sa);++a)
{
	lb=(sa[a]<<8)|(sa[1+a]<<0);
	i=(~((0xffff>>0)<<3))&(lb>>12);
	j=(~((0xffff>>0)<<3))&(lb>>9 );
	k=(~((0xffff>>0)<<3))&(lb>>6 );
	l=(~((0xffff>>0)<<3))&(lb>>3 );
	m=(~((0xffff>>0)<<3))&(lb>>0 );
	if(ba15[i][j][k][l][m][0]==-1){ba15[i][j][k][l][m][0]=ba15i;++ba15i;++ba15[i][j][k][l][m][1];}else{++ba15[i][j][k][l][m][1];}
}

//for(i=0;i<=7;++i){for(j=0;j<=7;++j){for(k=0;k<=7;++k){for(l=0;l<=7;++l){for(m=0;m<=7;++m)
//{
//	if(ba15[i][j][k][l][m][0]!=-1){printf("%d%d%d%d%d %d\n",i,j,k,l,m,ba15[i][j][k][l][m][1]);}
//}}}}}

mx=-1;
for(i=0;i<=7;++i){for(j=0;j<=7;++j){for(k=0;k<=7;++k){for(l=0;l<=7;++l){for(m=0;m<=7;++m)
{
	if(ba15[i][j][k][l][m][0]!=-1){if(mx==-1){mx=ba15[i][j][k][l][m][1];mi=i;mj=j;mk=k;ml=l;mm=m;}else{if(mx<ba15[i][j][k][l][m][1]){mx=ba15[i][j][k][l][m][1];mi=i;mj=j;mk=k;ml=l;mm=m;}}}
}}}}}

for(a=0;a<-1+strlen(sa);++a)
{
	lb=(sa[a]<<8)|(sa[1+a]<<0);
	i=(~((0xffff>>0)<<3))&(lb>>12);
	j=(~((0xffff>>0)<<3))&(lb>>9 );
	k=(~((0xffff>>0)<<3))&(lb>>6 );
	l=(~((0xffff>>0)<<3))&(lb>>3 );
	m=(~((0xffff>>0)<<3))&(lb>>0 );

	b=0;
	if(i==mi){++b;}
	if(j==mj){++b;}
	if(k==mk){++b;}
	if(l==ml){++b;}
	if(m==mm){++b;}
	if(b>=4){++ba16[sa[a]][sa[1+a]];/*printf("%c%c\n",sa[a],sa[1+a]);*/}	
}
for(a=0;a<strlen(sa);++a){sa_[a]=sa[a];}
for(a=0;a<-1+strlen(sa);++a)
{
	if(ba16[sa[a]][sa[1+a]]!=0){sa_[a]=sa[a];}else{sa_[a]='_';}//{printf("%c",sa[a]);}else{printf("_");}
}
for(a=0;a<strlen(sa);++a){if(sa_[a]=='_'){++mc;}else{++unmc;}/*printf("%c",sa_[a]);*/}//printf("\n");

return(0);

}

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

int	a,b;
int	d,e;
int	i,j,k,l,m,n;
int	fb;

	if(argc<1){return(0);}
	if(strlen(argv[1])==0){return(0);}strcpy(infn,argv[1]);

	printf("\n\tEnthropy quotioent\n");
	printf("\twritten by Dzinleski Jasenko Jun 2012, March , 2017\n\n");

	f1=fopen(infn,"rb");
	fb=getc(f1);
	while(!feof(f1))
	{
		for(i=0;i<256;++i){sv[i]='\0';}i=0;
		while((fb!=10)&&(fb!=13)){sv[i]=fb;++i;fb=getc(f1);}
		while((fb==10)||(fb==13)){fb=getc(f1);}
		p_1(sv);
	}
	fclose(f1);

    printf("\t%e\n",(100*((double)unmc/(double)mc)));

	return(0);

}