import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class dna
{ 
   int howmany = 10000;
   int themax = 100000;
   String gen11  = "BRAJYO BRA ALHYM AO HJMYM VAO HARE"; 
   int seed = 0;
   byte[] dna = getBytes("ltr/chr7.ltr");
   int dostart = 0;
   int doend = 14;
   int cntfound = 0;
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   dna bnk = new dna();
   bnk.start2(args);
}
//___________________________________________________________________________
void start2(String[] args)
{
   if (args.length > 0) if (args[0].equals("mix")) gen11 = mix(gen11);
   long t1 = System.nanoTime();
   String useit = gen11.replace(" ","");
   int[] fndval = new int[26];
   for (int i = 1; i <= 25; i++)
   {
      String n = Integer.toString(i);
      if (i == 23) n = "X";
      if (i == 24) n = "Y";
      if (i == 25) n = "MT";
      dna = getBytes("ltr/chr" + n + ".ltr");
      int fnd = start(useit, args, i);
      fndval[i] = fnd;
   }
   System.out.println("==============================");
   int total = 0;
   int[] fnd2val = new int[howmany];
   for (int i = 0; i < howmany; i++)
   {
      seed = i;
      dna = mixit(dna);
      int fnd2 = start(useit, args, i);
      fnd2val[i] = fnd2;
   }
   System.out.println("===============================");
   total = 0;
   int total2 = 0;
   long total3 = 0;
   int min = themax;
   System.out.println("------- chromosomes out of 23 below 50000 ---------");
   int cnt = 0;
   for (int i = 1; i < fndval.length; i++)
   {
      if (fndval[i] < 50000) 
      {
         cnt++;
         System.out.println(cnt + " " + i + " " + fndval[i]);
      }
   }
   
   int cnt2 = 0;
   System.out.println("--------- random out of " + howmany + " below 50000 ----------");
   for (int j = 0; j < fnd2val.length; j++)
   {
      if (fnd2val[j] < 50000) 
      {
         cnt2++;
         System.out.println(cnt2 + " " + j + " " + fnd2val[j]);
      }
   }   
   System.out.println("--------------------------------");
   double perc1 = 100.0 * (double)cnt / (double)23.0;
   double perc2 = 100.0 * (double)cnt2 / (double)howmany;
   System.out.println(perc1 + "% compared to " + perc2 + "%");
   double perc3 = ((double)howmany / (double)cnt2) / (23.0 / (double)cnt);
   System.out.println((int)perc3);
   long t2 = System.nanoTime();
   System.out.println("seconds = " + ((t2-t1)/1e9));
}
//___________________________________________________________________________
int start(String look, String[] args, int ii)
{
   int num1 = 14;
   int num2 = themax;
   int which = 0;
   byte[] gen11cmp = look.substring(dostart,doend).getBytes();
   byte[] bb = dna; 
   boolean found = false;
   for (int i = 0; i < bb.length - gen11cmp.length; i++)
   {
      for (int j = 0; j < gen11cmp.length; j++)
      {
         found = true;
         for (int k = j+1; k < gen11cmp.length; k++)
         {
            if (gen11cmp[j] == gen11cmp[k])
            {
               if (bb[i + j] == bb[i + k])
               {
               }
               else
               {
                  found = false;
                  break;
               }
            }
            else
            {
               if (bb[i + j] == bb[i + k])
               {
                  found = false;
                  break;
               }
               else
               {
               }
            }
         }
         if (!found)
         {
            break;
         }
      }
      if (found)
      {
         cntfound++;
         System.out.println(ii + " " + cntfound + " " + i);
         for (int m = 0; m < gen11cmp.length; m++)
         {
            System.out.print((char)gen11cmp[m] + " ");
         }
         System.out.println("");
         for (int m = 0; m < gen11cmp.length; m++)
         {
            System.out.print((char)bb[i+m] + " ");
         }
         System.out.println("");
         return(i);
      }
   }
   return(themax);
}
//___________________________________________________________________________
byte[] mixit(byte[] str)
{
   String out = new String(str);
   out = mix(out);
   return(out.getBytes());
}
//___________________________________________________________________________
String mix(String str)
{
   byte[] bc = str.getBytes();
   Random rand = new Random(seed);
   for (int i = 0; i < themax; i++)
   {
      int rnd1 = rand.nextInt(bc.length);
      int rnd2 = rand.nextInt(bc.length);
      byte bd = bc[rnd1];
      bc[rnd1] = bc[rnd2];
      bc[rnd2] = bd;
   }
   String out = new String(bc);
   return(out);
}
//___________________________________________________________________________
byte[] getBytes(String name) 
{
   byte[] b = new byte[0];
  try
  {
   File file = new File(name);
   b = new byte[(int) file.length()];
   FileInputStream fileInputStream = new FileInputStream(file);
   fileInputStream.read(b);
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(b);
}
}//__________________________________________________________________________
