//	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 	ba24[8][8][8][8][8][8][8][8][2];
int		ba24i=0;
int 	ba24_[256][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,o,p;
int mi,mj,mk,ml,mm,mn,mo,mp;
int a,b,mx;
long lb;
char sa_[256];

for(i=0;i<256;++i){for(j=0;j<256;++j){for(k=0;k<256;++k){ba24_[i][j][k]=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){for(n=0;n<=7;++n){for(o=0;o<=7;++o){for(p=0;p<=7;++p)
{
	ba24[i][j][k][l][m][n][o][p][0]=-1;
	ba24[i][j][k][l][m][n][o][p][1]=0;
}}}}}}}}
ba24i=0;

for(a=0;a<-1-1+strlen(sa);++a)
{
	lb=(sa[a]<<16)|(sa[1+a]<<8)|(sa[1+1+a]<<0);
	i=(~((0xffffff>>0)<<3))&(lb>>21);
	j=(~((0xffffff>>0)<<3))&(lb>>18);
	k=(~((0xffffff>>0)<<3))&(lb>>15);
	l=(~((0xffffff>>0)<<3))&(lb>>12);
	m=(~((0xffffff>>0)<<3))&(lb>>9 );
	n=(~((0xffffff>>0)<<3))&(lb>>6 );
	o=(~((0xffffff>>0)<<3))&(lb>>3 );
	p=(~((0xffffff>>0)<<3))&(lb>>0 );	
	if(ba24[i][j][k][l][m][n][o][p][0]==-1){ba24[i][j][k][l][m][n][o][p][0]=ba24i;++ba24i;++ba24[i][j][k][l][m][n][o][p][1];}else{++ba24[i][j][k][l][m][n][o][p][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){for(n=0;n<=7;++n){for(o=0;o<=7;++o){for(p=0;p<=7;++p)
//{
//	if(ba24[i][j][k][l][m][n][o][p][0]!=-1){printf("%d%d%d%d%d %d\n",i,j,k,l,m,ba24[i][j][k][l][m][n][o][p][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){for(n=0;n<=7;++n){for(o=0;o<=7;++o){for(p=0;p<=7;++p)
{
	if(ba24[i][j][k][l][m][n][o][p][0]!=-1){if(mx==-1){mx=ba24[i][j][k][l][m][n][o][p][1];mi=i;mj=j;mk=k;ml=l;mm=m;mn=n;mo=o;mp=p;}else{if(mx<ba24[i][j][k][l][m][n][o][p][1]){mx=ba24[i][j][k][l][m][n][o][p][1];mi=i;mj=j;mk=k;ml=l;mm=m;mn=n;mo=o;mp=p;}}}
}}}}}}}}

for(a=0;a<-1-1+strlen(sa);++a)
{
	lb=(sa[a]<<16)|(sa[1+a]<<8)|(sa[1+1+a]<<0);
	i=(~((0xffffff>>0)<<3))&(lb>>21);
	j=(~((0xffffff>>0)<<3))&(lb>>18);
	k=(~((0xffffff>>0)<<3))&(lb>>15);
	l=(~((0xffffff>>0)<<3))&(lb>>12);
	m=(~((0xffffff>>0)<<3))&(lb>>9 );
	n=(~((0xffffff>>0)<<3))&(lb>>6 );
	o=(~((0xffffff>>0)<<3))&(lb>>3 );
	p=(~((0xffffff>>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(n==mn){++b;}
	if(o==mo){++b;}
	if(p==mp){++b;}	
	if(b>=6){++ba24_[sa[a]][sa[1+a]][sa[1+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-1+strlen(sa);++a)
{
	if(ba24_[sa[a]][sa[1+a]][sa[1+1+a]]!=0){sa_[a]=sa[a];sa_[1+a]=sa[1+a];}else{sa_[a]='_';sa_[1+a]='_';}
}
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);

}