/*
 * 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.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
//import java.util.TreeMap;

/**
 *
 * @author Rahul Semwal
 */
public class Utility 
{
    @SuppressWarnings("element-type-mismatch")
    public LinkedHashMap<String,KMER> getKmer(String currentSequence,int kmerLength,int totalSequence,Score s,double frequency[],int maxKmer,BinNode binhead,int binNumber) throws Exception
    {
        LinkedHashMap<String,KMER> CurrentBin = new LinkedHashMap<>();
        String kmer;
        for(int i=0;i<currentSequence.length();i++)
        {
            if((i+kmerLength)<=currentSequence.length())
            {
                kmer = currentSequence.substring(i, i+kmerLength);
                if(CurrentBin.containsKey(kmer))
                {
                    KMER k = CurrentBin.get(kmer);
                    k.updateIndex(i);
                }
                else
                {
                    CurrentBin.put(kmer, new KMER(i));
                }
            }
        }
        Manager manager = new Manager();
        Iterator it = CurrentBin.entrySet().iterator();
        FilterSequence fs = new FilterSequence();
        while (it.hasNext())
        {
            Map.Entry me = (Map.Entry)it.next();
            manager.ConnctionBuilder((String)me.getKey(), currentSequence, kmerLength, s, (KMER)me.getValue());   
            KMER k = CurrentBin.get(me.getKey());
            if(!k.valid)
            { 
                try 
                {
                    k.finalize();
                }catch (Throwable ex){}
                it.remove();
                System.gc();  
            }
           
            else
            {
                fs.SignificanceTest((String)me.getKey(),(KMER)me.getValue(), binNumber,kmerLength);
                if(k.getConnection().size() !=(k.getCoverage()-1))
                {
                    try 
                    {
                        k.finalize();
                    }catch (Throwable ex){}
                    it.remove();
                }
                else
                {
                    BinNode head=binhead;
                    while(head != null)
                    {
                        LinkedHashMap<String,KMER> node=head.getCurrentBin();
                        String ckmer=(String)me.getKey();
                        if(node.get(ckmer)!=null)
                        {   
                            k =(KMER)me.getValue();
                            LinkNode<Integer> CkmerIndex= k.getIndex().getHead();
                            TreeMap<Integer,LinkList<Integer>> Connection = k.getConnection();
                            if(Connection.containsKey(head.getBinNumber()))
                            {
                                LinkNode<Integer> tlist=Connection.get(head.getBinNumber()).getHead();//proceesing k-mer bin list
                                k=(KMER)node.get(ckmer);
                                LinkNode<Integer> Index=k.getIndex().getHead();// already exist k-mer index
                                TreeSet<Integer> tree = new TreeSet<>();
                                while(Index!=null)
                                {
                                    tree.add(Index.getValue());
                                    Index=Index.next;
                                }
                                
                                
                                
                                LinkList<Integer> tmp=null;
                                while(tlist!=null)
                                {
                                    if(!tree.contains(tlist.getValue()))
                                    {
                                        if(tmp==null)
                                            tmp=new LinkList<>();
                                        else
                                            tmp.addNode(tlist.getValue());
                                    }
                                    tlist=tlist.next;
                                }
                                
                                LinkList<Integer> temp =tmp;
                                if(tmp!=null)
                                {
                                    tmp=new LinkList<>();
                                    while(CkmerIndex!=null)
                                    {
                                        tmp.addNode(CkmerIndex.getValue());
                                        CkmerIndex=CkmerIndex.next;
                                    }
                                    k.AddNewIndex(binNumber, temp, tmp);
                                }  
                            }
                            try 
                            {
                                k=(KMER)me.getValue();
                                k.finalize();
                            }catch (Throwable ex){}
                            it.remove();
                            break;
                        }
                        head=head.next;        
                    }
                                
                }
                            
            }
                
        }
        manager=null;
        s=null;
        currentSequence=null;
        
       /* if(CurrentBin.size() > 0)
        {
            System.out.println("Bin number "+binNumber);
            it = CurrentBin.entrySet().iterator();
            while (it.hasNext())
            {
                Map.Entry me = (Map.Entry)it.next();
                KMER k = CurrentBin.get(me.getKey());
                TreeMap<Integer,LinkList<Integer>> con    = k.getConnection();
                Iterator tit = con.entrySet().iterator();
                System.out.println(me.getKey());
                 while(tit.hasNext())
                {
                    Map.Entry x = (Map.Entry)tit.next();
                    LinkList<Integer> a= (LinkList<Integer>) x.getValue();
                    LinkNode<Integer> head=a.getHead();
                    while(head!=null)
                    {
                        System.out.print(head.getValue()+" , ");head=head.next;
                    }
                    System.out.println("");
                    
                }
                System.out.println("**************************************************************");
                
            }
        }*/
        return (CurrentBin);
    }
}
    
