/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package motifkmer;

/**
 *
 * @author rahul
 */
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.text.StyleConstants;
public class Binomial 
{
    double prob=0;
    public  void combinationUtil(double background [],char arr[], ArrayList data, int start,int end, int index, int r,int kmerLength)
    {
         if (index == r)
         {
             double a=1;
             for(int i=0;i<kmerLength;i++)
             {
                  if(data.contains(i))
                      a=a*background[((int)arr[i]-65)];
                  else
                      a=a*(1-background[((int)arr[i]-65)]);
             }
             this.prob+=a;
             return;
         }
         for (int i=start; i<=end && end-i+1 >= r-index; i++)
         {
             if(data.size() ==r)
                data.set(index, i);
             else
                  data.add(index,i);
             combinationUtil(background,arr, data, i+1, end, index+1, r,kmerLength);
         }
        
    }
    double [] setBackground(double []bg,Score score) throws Exception
    {
        double b[]=new double[26];
        String s="ARNDCQEGHILKMFPSTWYV";
        char ref;
        char check;
        for(int i=0;i<s.length();i++)
        {
            ref=s.charAt(i);
            for(int j=0;j<s.length();j++)
            {
                check=s.charAt(j);
                if(score.getCharScore(ref, check)>0)
                {
                    b[(int)ref-65]+=bg[(int)check-65];
                }
            }
        }
        return b;
    }
     public double getExpectation(double background[],String Kmer,int r,Score s)throws Exception
     {
         char a[] = new char[Kmer.length()];
         double bg[]=setBackground(background,s);
         for(int i=0;i<Kmer.length();i++)
              a[i]=Kmer.charAt(i);
         int n=a.length;
         ArrayList<Integer> data = new ArrayList<>();
         for(int i=r;i<=Kmer.length();i++)
         {
             combinationUtil(bg,a, data, 0, n-1, 0, i,Kmer.length());
             data.clear();
         }
         return(this.prob);
     }
}
