/*
 * 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;

//import java.math.RoundingMode;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
//import java.text.DecimalFormat;
/**
 *
 * @author rahul
 */
public class FilterSequence 
{
        static
        {
            System.loadLibrary("NativeC");
        }
        
        public void SignificanceTest(String ckmer,KMER CurrentKmer,int len,int BinNumber) throws Exception//, int BinNumber,int len) throws Exception
        {
           TreeMap<Integer,LinkList<Integer>> Connection= CurrentKmer.getConnection();
           TreeMap<Integer,LinkList<Integer>> Score=CurrentKmer.getScore();
           TreeMap<Integer, Integer> probability= CurrentKmer.prob;
           Iterator prob= probability.entrySet().iterator();
           
               int weight=0;
               int low=probability.firstKey();
               int high =probability.lastKey();
               double pro[] = new double[high-low+1];
              
               {
               
               int i=0;
               int plow=low;
               while(prob.hasNext())
               {
                    Map.Entry me = (Map.Entry)prob.next();
                    weight+=(int)me.getValue();
                    if(plow==(int)me.getKey())
                        pro[i++]=(int)me.getValue()*1.0D;
                    else
                    {
                        for(;plow<(int)me.getKey();plow++)
                            pro[i++]=0.0D;
                        pro[i++]=(int)me.getValue()*1.0D;
                    }
                    plow+=1;
                }               
                for(i=0;i<pro.length;i++)
                    pro[i]/=weight;
                
                double var[]= new FilterSequence().CalculateVar(low,high,pro);
                pro=null;
                double lambda = var[0];
                double K=var[1];
                var=null;
                
                Iterator con= Connection.entrySet().iterator();
                Iterator scor= Score.entrySet().iterator();
                while(con.hasNext())
                {
                    Map.Entry c = (Map.Entry)con.next();
                    Map.Entry s = (Map.Entry)scor.next();
                    LinkNode<Integer> chead=((LinkList<Integer>)c.getValue()).getHead();
                    LinkNode<Integer> shead=((LinkList<Integer>)s.getValue()).getHead();
                    LinkList<Integer> node = new LinkList<>();
                    LinkNode<Integer> tnode;
                    LinkNode<Integer> snode;
                    while(chead!=null && shead!=null )
                    {
                        double p_value = 1- Math.exp(-K*len*len*Math.exp(-lambda*shead.getValue()));
                        if(p_value<="<PROVIDE CUTOFF VALUE>")
                        {
                            node.addNode(chead.getValue());
                        }
                        tnode=chead;
                        snode=shead;
                        chead=chead.next;
                        shead=shead.next;
                        snode.next=null;
                        snode=null;
                        tnode.next=null;
                        tnode=null;
                    }
                    if(node.getHead()==null)
                    {
                        CurrentKmer.finalize();
                        break;
                    }
                    else
                    {
                        c.setValue(node);
                    }
                    
                }
                
           }
          
           
        }
        
    
        public native double[] CalculateVar(int low, int high,double pr[]);
}