package function.external.chm;

import function.variant.base.RegionManager;
import global.Data;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

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

    private static String repeatRegionBedFilePath = Data.ATAV_HOME + "data/CHM-eval/CHR_um75-hs37d5.bed";
    private static HashMap<String, int[][]> repeatRegionByChrMap = new HashMap<>();

    public static String getHeader() {
        return "Repeat Region";
    }

    public static void init() throws Exception {
        if (CHMCommand.isFlag || CHMCommand.isExclude) {
            for (String chr : RegionManager.getChrList()) {
                Path path = Paths.get(repeatRegionBedFilePath.replace("CHR", chr));
                int count = (int) Files.lines(path).count();

                repeatRegionByChrMap.put(chr, new int[count][2]);

                BufferedReader br = new BufferedReader(new FileReader(path.toFile()));
                String lineStr = "";
                int index = 0;
                while ((lineStr = br.readLine()) != null) {
                    String[] tmp = lineStr.split("\t");

                    int start = Integer.valueOf(tmp[1]);
                    int end = Integer.valueOf(tmp[2]);

                    repeatRegionByChrMap.get(chr)[index][0] = start;
                    repeatRegionByChrMap.get(chr)[index++][1] = end;
                }
            }
        }
    }

    public static boolean isRepeatRegion(String chr, int pos) {
        int[][] repeatRegionArray = repeatRegionByChrMap.get(chr);
        int start = 0;
        int last = repeatRegionArray.length - 1;
        pos -= 1; // variant position is 1-based wherease the repeat region bed file is 0-based

        return binarySearch(repeatRegionArray, start, last, pos) != Data.INTEGER_NA;
    }

    /*
        binary search whether a given position fall into a repeat region
     */
    private static int binarySearch(int arr[][], int first, int last, int pos) {
        if (last >= first) {
            int mid = first + (last - first) / 2;

            int start = arr[mid][0];
            int end = arr[mid][1];
            
            if (pos < start) {
                return binarySearch(arr, first, mid - 1, pos); //search in left subarray  
            } else if (pos >= end) {
                return binarySearch(arr, mid + 1, last, pos); //search in right subarray  
            } else {
                return mid;
            }
        }

        return Data.INTEGER_NA;
    }
}

@@ -143,42 +143,163 @@
 File
-Reader;%0Aimport java.nio.file.Files
+;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.io.Reader
 ;%0Aim
@@ -376,16 +376,54 @@
 HashMap;
+%0Aimport java.util.zip.GZIPInputStream;
 %0A%0A/**%0A *
@@ -573,16 +573,19 @@
 37d5.bed
+.gz
 %22;%0A    p
@@ -988,24 +988,25 @@
 HR%22, chr));%0A
+%0A
             
@@ -1025,41 +1025,43 @@
 t = 
-(int) Files.lines(path).count();%0A
+getGzipFileRowCount(path.toFile());
 %0A   
@@ -1144,32 +1144,123 @@
     
-Buffered
+GZIPInputStream in = new GZIPInputStream(new FileInputStream(path.toFile()));%0A                
 Reader 
-b
+decode
 r = new 
 Buff
@@ -1259,55 +1259,97 @@
 new 
-BufferedReader(new FileReader(path.toFile())
+InputStreamReader(in);%0A                BufferedReader br = new BufferedReader(decoder
 );%0A
+%0A
     
@@ -1408,24 +1408,74 @@
  index = 0;%0A
+                br = new BufferedReader(decoder);%0A
             
@@ -1848,39 +1848,556 @@
       %7D%0A
-            %7D%0A        %7D
+%0A                br.close();%0A                decoder.close();%0A                in.close();%0A            %7D%0A        %7D%0A    %7D%0A%0A    private static int getGzipFileRowCount(File file) throws FileNotFoundException, IOException %7B%0A        GZIPInputStream in = new GZIPInputStream(new FileInputStream(file));%0A        Reader decoder = new InputStreamReader(in);%0A        BufferedReader br = new BufferedReader(decoder);%0A%0A        int count = (int) br.lines().count();%0A%0A        br.close();%0A        decoder.close();%0A        in.close();%0A%0A        return count;
 %0A    %7D%0A%0A
@@ -3109,28 +3109,16 @@
 id%5D%5B1%5D;%0A
-            
 %0A       

support read gzip file for CHM