package org.opencb.cellbase.build.transform;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opencb.biodata.models.variant.CADD.Cadd;
import org.opencb.biodata.models.variant.CADD.CaddValues;

import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by antonior on 5/22/14.
 */
public class CaddParser {

    public static Float TableFloatToString(String floatName){
        if (floatName.equals("NA")){

            return null;
        }
        else{
            return Float.parseFloat(floatName);
        }



    }


    public static void parse(Path caddFilePath, Path oFilePath){

        Cadd Caddvariant = new Cadd ();


        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(caddFilePath.toFile())))) {
            try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(oFilePath.toFile())))) {

                String line;
                String[] header;
                String ref;
                String alt;
                String chr;
                int pos;

                while ((line = reader.readLine()) != null) {
                    if (line.startsWith("##")) {
                        continue; // Header will just be ignored
                    } else if (line.startsWith("#")) {
                        header = line.split("\t");

                    } else {
                        String[] fields = line.split("\t");
                        ref = fields[2];
                        alt = fields[4];
                        chr = fields[0];
                        pos = Integer.parseInt(fields[1]);

                        if (Caddvariant.getChr()!=null && (Caddvariant.getChr().equals(chr) && Caddvariant.getPos() == pos && Caddvariant.getReference().equals(ref) && Caddvariant.getAllele().equals(alt))) {
                            List <CaddValues> caddinfo = Caddvariant.getValuesCadd();

                            CaddValues values = new CaddValues(Float.parseFloat(fields[88]), Float.parseFloat(fields[89]), fields[68]);
                            caddinfo.add(values);
                            Caddvariant.setValuesCadd(caddinfo);

                        } else {
                            if (Caddvariant.getChr()!=null) {
                                ObjectMapper jsonMapper = new ObjectMapper();
                                jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                                writer.write(Caddvariant.getChr()+"\t"+Integer.toString(Caddvariant.getPos())+"\t"+Caddvariant.getReference()+"\t"+Caddvariant.getAllele()+"\t"+jsonMapper.writeValueAsString(Caddvariant)+"\n");

                                //print this cadd
                            }

                            Float EncExp = TableFloatToString(fields[29]);
                            Float EncH3K27Ac = TableFloatToString(fields[30]);
                            Float EncH3K4Me1 = TableFloatToString(fields[31]);
                            Float EncH3K4Me3 = TableFloatToString(fields[32]);
                            Float EncNucleo = TableFloatToString(fields[33]);

                            Integer EncOCC = null;
                            if (!fields[34].equals("NA")){
                                EncOCC = Integer.parseInt(fields[34]);
                            }



                            Float EncOCCombPVal = TableFloatToString(fields[35]);
                            Float EncOCDNasePVal = TableFloatToString(fields[36]);
                            Float EncOCFairePVal = TableFloatToString(fields[37]);
                            Float EncOCpolIIPVal = TableFloatToString(fields[38]);
                            Float EncOCctcfPVal = TableFloatToString(fields[39]);
                            Float EncOCmycPVal = TableFloatToString(fields[40]);
                            Float EncOCDNaseSig = TableFloatToString(fields[41]);
                            Float EncOCFaireSig = TableFloatToString(fields[42]);
                            Float EncOCpolIISig = TableFloatToString(fields[43]);
                            Float EncOCctcfSig = TableFloatToString(fields[44]);
                            Float EncOCmycSig = TableFloatToString(fields[45]);
                            List<CaddValues> caddValuesList = new ArrayList<>();


                            CaddValues values = new CaddValues(Float.parseFloat(fields[88]), Float.parseFloat(fields[89]), fields[68]);
                            caddValuesList.add(values);


                            Caddvariant = new Cadd(alt, ref, chr, pos, EncExp, EncH3K27Ac, EncH3K4Me1, EncH3K4Me3, EncNucleo, EncOCC, EncOCCombPVal, EncOCDNasePVal, EncOCFairePVal, EncOCpolIIPVal, EncOCctcfPVal, EncOCmycPVal, EncOCDNaseSig, EncOCFaireSig, EncOCpolIISig, EncOCctcfSig, EncOCmycSig, caddValuesList);

                        }
                    }
                }

                ObjectMapper jsonMapper = new ObjectMapper();
                jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                writer.write(Caddvariant.getChr()+"\t"+Integer.toString(Caddvariant.getPos())+"\t"+Caddvariant.getReference()+"\t"+Caddvariant.getAllele()+"\t"+jsonMapper.writeValueAsString(Caddvariant)+"\n");



            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}

@@ -448,34 +448,29 @@
 c Float 
-TableFloatToString
+stringToFloat
 (String 
@@ -2882,34 +2882,29 @@
 ncExp = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -2956,34 +2956,29 @@
 K27Ac = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3030,34 +3030,29 @@
 K4Me1 = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3104,34 +3104,29 @@
 K4Me3 = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3177,34 +3177,29 @@
 ucleo = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3469,34 +3469,29 @@
 bPVal = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3547,34 +3547,29 @@
 ePVal = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3625,34 +3625,29 @@
 ePVal = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3703,34 +3703,29 @@
 IPVal = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3780,34 +3780,29 @@
 fPVal = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3856,34 +3856,29 @@
 cPVal = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -3933,34 +3933,29 @@
 seSig = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -4010,34 +4010,29 @@
 reSig = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -4087,34 +4087,29 @@
 IISig = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -4163,34 +4163,29 @@
 cfSig = 
-TableFloatToString
+stringToFloat
 (fields%5B
@@ -4242,26 +4242,21 @@
 g = 
-TableFloatToString
+stringToFloat
 (fie

change function name