package model;

import global.Data;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author nick
 */
public class Input {

    public static ArrayList<String> geneNameList = new ArrayList<String>();
    public static ArrayList<String> alternativeGeneNameList = new ArrayList<String>();
    public static ArrayList<String> invalidGeneList = new ArrayList<String>();

    public static String query;

    public static void init(HttpServletRequest request) throws Exception {
        geneNameList.clear();
        invalidGeneList.clear();

        initAlternativeGeneNameList();

        initGeneList(request);
    }

    public static void initAlternativeGeneNameList() throws Exception {
        File file = new File(Data.rootPath + File.separator + Data.GenicIntoleranceAlternativeGeneName);
        FileInputStream fstream = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;

        HashSet<String> tempSet = new HashSet<String>();
        String value = "";

        while ((line = br.readLine()) != null) {
            line = line.toUpperCase();

            for (String str : line.split(Data.TAB)) {
                if (tempSet.isEmpty()) {
                    tempSet.add(str);
                    value = str;
                } else {
                    if (!tempSet.contains(str)) {
                        value += Data.TAB + str;
                    }

                    tempSet.add(str);
                }
            }

            alternativeGeneNameList.add(value);
            tempSet.clear();
        }
    }

    private static void initGeneList(HttpServletRequest request) throws Exception {
        query = request.getParameter("query");

        if (query.equalsIgnoreCase("all.gene")) {
            initAllGeneList();
        } else {
            initGeneListFromQuery();
        }
    }

    private static void initGeneListFromQuery() {
        String[] geneNames = query.replaceAll("\\s+", "").split(",");

        for (String name : geneNames) {
            String alternativeName = getGeneName(name);

            if (alternativeName.isEmpty()) {
                invalidGeneList.add(name);
                continue;
            }

            String[] temp = alternativeName.split(Data.TAB);

            geneNameList.add(temp[0]);
        }
    }

    private static void initAllGeneList() {
        try {
            File file = new File(Data.rootPath + File.separator + Data.INPUT_DATA);
            FileInputStream fstream = new FileInputStream(file);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line;
            boolean isFirst = true;

            while ((line = br.readLine()) != null) {
                if (isFirst) {
                    isFirst = false;
                    continue;
                }

                String[] temp = line.split(Data.TAB);

                geneNameList.add(temp[0].toUpperCase());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getGeneName(String value) {
        for (String alternativeGeneNames : alternativeGeneNameList) {
            String[] names = alternativeGeneNames.split(Data.TAB);

            for (String name : names) {

                if (name.equalsIgnoreCase(value)) {
                    return names[0] + Data.TAB + name;
                }
            }
        }

        return "";
    }
}

@@ -438,32 +438,30 @@
 blic static 
-ArrayLis
+HashSe
 t%3CString%3E al
@@ -473,35 +473,34 @@
 tiveGeneName
-Lis
+Se
 t = new 
 ArrayList%3CSt
@@ -483,32 +483,30 @@
 meSet = new 
-ArrayLis
+HashSe
 t%3CString%3E();
@@ -915,32 +915,85 @@
 ws Exception %7B%0D%0A
+        if (alternativeGeneNameSet.isEmpty()) %7B%0D%0A    
         File fil
@@ -1082,32 +1082,36 @@
 Name);%0D%0A        
+    
 FileInputStream 
@@ -1148,32 +1148,36 @@
 file);%0D%0A        
+   
+ 
 DataInputStream 
@@ -1204,32 +1204,36 @@
 ream(fstream);%0D%0A
+    
         Buffered
@@ -1292,32 +1292,36 @@
 (in));%0D%0A        
+    
 String line;%0D%0A%0D%0A
@@ -1328,16 +1328,20 @@
         
+ 
+   
 HashSet%3C
@@ -1386,24 +1386,28 @@
 );%0D%0A        
+    
 String value
@@ -1416,32 +1416,36 @@
  %22%22;%0D%0A%0D%0A        
+  
+  
 while ((line = b
@@ -1482,16 +1482,20 @@
         
+    
 line = l
@@ -1512,24 +1512,28 @@
 rCase();%0D%0A%0D%0A
+    
             
@@ -1587,24 +1587,28 @@
             
+    
 if (tempSet.
@@ -1633,32 +1633,36 @@
                 
+    
 tempSet.add(str)
@@ -1680,24 +1680,28 @@
             
+  
+  
 value = str;
@@ -1710,32 +1710,36 @@
                 
+    
 %7D else %7B%0D%0A      
@@ -1724,32 +1724,36 @@
       %7D else %7B%0D%0A
+    
                 
@@ -1807,24 +1807,28 @@
             
+    
 value += Dat
@@ -1853,32 +1853,36 @@
                 
+    
 %7D%0D%0A%0D%0A           
@@ -1882,32 +1882,36 @@
                 
+ 
+   
 tempSet.add(str)
@@ -1921,35 +1921,42 @@
                 
-%7D%0D%0A
+    %7D%0D%0A   
             %7D%0D%0A%0D
@@ -1943,37 +1943,42 @@
 %0A               
+ 
 %7D%0D%0A%0D%0A
+    
             alte
@@ -1988,27 +1988,26 @@
 tiveGeneName
-Lis
+Se
 t.add(value)
@@ -2013,32 +2013,36 @@
 );%0D%0A            
+    
 tempSet.clear();
@@ -2035,32 +2035,47 @@
 mpSet.clear();%0D%0A
+            %7D%0D%0A
         %7D%0D%0A    %7D
@@ -3783,19 +3783,18 @@
 GeneName
-Lis
+Se
 t) %7B%0D%0A  

solve a "Out of Memory" bug - reuse alternative Gene set