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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author nikos
 */
public class BaselineBagOfWords {
    
    private final HashMap<String, Integer> BowMap;
    private final HashMap<String, Double> BaselineBowMap;
    private ArrayList<String> possibleSequences;

    private int length;
    public BaselineBagOfWords(int length) {
        BowMap = new HashMap<String, Integer>();
        this.length = length;
        
        String alpha = "ACGT";
        char[] seq = alpha.toCharArray();

        possibleSequences = generatePossibleStrings(length, alpha, seq);
        BaselineBowMap = new HashMap<String, Double>();
    }
    
    
    public BaselineBagOfWords(String sequence, int length) {

        this.length = length;
        double A_count = 14317.0;
        double T_count = 14320.0;
        double C_count = 7436.0;
        double G_count = 7545.0;
        double sum_count = A_count + T_count + C_count + G_count;
        
        BowMap = new HashMap<String, Integer>();
        BaselineBowMap = new HashMap<String, Double>();
        
        String alpha = "ACGT";
        char[] seq = alpha.toCharArray();

        possibleSequences = generatePossibleStrings(length, alpha, seq);

        int win = length -1;
        for(int i = 0; i < sequence.length()-win; i++) {
            String key = sequence.substring(i, i+length);
            //System.out.println(key);
            if(!BowMap.containsKey(key))
                BowMap.put(key, 1);
            else
                BowMap.put(key, BowMap.get(key)+1);
        }


        double num_combos = (double)Math.pow(alpha.length(), length);
        for(String s : possibleSequences) {
            //System.out.println(s);
            //System.out.println(BowMap.toString());
            //System.out.println(BowMap.containsKey(s));
            
            if(BowMap.containsKey(s)) {
                //tri
                //double freq = (double)(BowMap.get(s) / 64.0);
                //bi
                double freq = (double)(BowMap.get(s) / num_combos);
                for(int i = 0; i < s.length(); i++) {
                    if(s.charAt(i) == 'A')
                        freq /= A_count / sum_count;
                    else if(s.charAt(i) == 'T')
                        freq /= T_count / sum_count;
                    else if(s.charAt(i) == 'C')
                        freq /= C_count / sum_count;
                    else
                        freq /= G_count / sum_count;
                }
                BaselineBowMap.put(s, freq);
            } else BaselineBowMap.put(s, 0.0);
        }
    }

    public HashMap<String, Double> getBaselineBowMap() {
        
        return BaselineBowMap;
    }

    private ArrayList<String> generatePossibleStrings(int length, String alphabet, char[] sequence) {

        String e = "";
        for(int ee=0; ee<this.length;++ee) e = e + " ";
        //tri
        //StringBuilder builder = new StringBuilder("   ");
        //bi
        StringBuilder builder = new StringBuilder(e);
        
        possibleSequences = new ArrayList<>();

        int[] pos = new int[length];
        int total = (int) Math.pow(alphabet.length(), length);
        for (int i = 0; i < total; i++) {
            for (int x = 0; x < length; x++) {
                if (pos[x] == sequence.length) {
                    pos[x] = 0;
                    if (x + 1 < length) {
                        pos[x + 1]++;
                    }
                }
                builder.setCharAt(x, sequence[pos[x]]);
            }
            pos[0]++;

            possibleSequences.add(builder.toString());
        }
        
        return possibleSequences;
    }

    HashMap<String, Integer> getBowMap() {
        return BowMap;
    }
    
     
}


@@ -1002,13 +1002,9 @@
 t = 
-14317
+0
 .0;%0A
@@ -1032,12 +1032,8 @@
 t = 
-1432
 0.0;
@@ -1062,12 +1062,9 @@
 t = 
-7436
+0
 .0;%0A
@@ -1092,12 +1092,9 @@
 t = 
-7545
+0
 .0;%0A
@@ -2385,35 +2385,17 @@
     
-freq /= A_count / sum_count
+A_count++
 ;%0A  
@@ -2468,35 +2468,325 @@
     
-freq /= T_count / sum_count
+T_count++;%0A                    else if(s.charAt(i) == 'C')%0A                        C_count++;%0A                    else%0A                        G_count++;%0A                %7D%0A                for(int i = 0; i %3C s.length(); i++) %7B%0A                    if(s.charAt(i) == 'A')%0A                        freq /= A_count / length
 ;%0A  
@@ -2819,33 +2819,33 @@
 s.charAt(i) == '
-C
+T
 ')%0A             
@@ -2863,17 +2863,17 @@
 freq /= 
-C
+T
 _count /
@@ -2877,95 +2877,298 @@
 t / 
-sum_count;%0A                    else%0A                        freq /= G_count / sum_count
+length;%0A                    else if(s.charAt(i) == 'C')%0A                        freq /= C_count / length;%0A                    else%0A                        freq /= G_count / length;%0A                %7D%0A                A_count = 0.0;%0A                T_count = 0.0;%0A                C_count = 0.0
 ;%0A  
@@ -3173,33 +3173,46 @@
                 
-%7D
+G_count = 0.0;
 %0A               

Update BaselineBagOfWords.java