/*
 * 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.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Rahul Semwal
 */
public class Score 
{
   @SuppressWarnings("FieldMayBeFinal")
   private List<String> header;
   @SuppressWarnings("FieldMayBeFinal")
   private HashMap<String, List<String>> matrix;
   public Score() throws Exception
   {
       File file = new File("/home/rahul/NetBeansProjects/MotifKmer/Files/blosum62.txt");
       Scanner sc = new Scanner(file);
       String line = sc.nextLine();
       line =line.substring(3);
       matrix = new HashMap<>();
       String s[] = line.split("\\s+");
       header = Arrays.asList(s); 
       String key;
       while (sc.hasNextLine())
       {
           line = sc.nextLine();
           key = line.substring(0,1);
           if(line.substring(2, 3).equals(" "))
               line=line.substring(3);
           else
               line=line.substring(2);
           s=line.split("\\s+");
           matrix.put(key,Arrays.asList(s));
           
       }
               
   }
   
   
   
   public double[] getScore(String ckmer,String tkmer,KMER kmerptr)
   {
      boolean update = true;        
      int s=0;      
      int ar[]=new int[ckmer.length()];
      double score[] = new double[2];
      score[0]=score[1]=0;
      for(int i=0;i<ckmer.length();i++)
      {
           int index = header.indexOf(String.valueOf(ckmer.charAt(i)));
           String v=  (String)matrix.get(String.valueOf(tkmer.charAt(i))).get(index);
           s=Integer.parseInt(v);
           score[0]+=s;
           score[1]+=1.0D;
           ar[i]=s;
       }
       if(update)
       {
           for(int i=0;i<ckmer.length();i++)
               kmerptr.setProbability(ar[i]);
       }
       return(score);
   }
   public int getKmerScore(String ckmer,String tkmer)
   {
       int s=0;
       for(int i=0;i<ckmer.length();i++)
       {
            int index = header.indexOf(String.valueOf(ckmer.charAt(i)));
            String v=  (String)matrix.get(String.valueOf(tkmer.charAt(i))).get(index);
            s+=Integer.parseInt(v);
       }
       
       return(s);
   }
   public int getCharScore(char ref,char check)
   {
       int index = header.indexOf(String.valueOf(ref));
       String v=  (String)matrix.get(String.valueOf(check)).get(index);
       return(Integer.parseInt(v));
   }
}
