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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author legrand
 */
public class JavaApplication1 {

    private static ReadAlignement ReadAlignement;
    static Bscore bscore;
    static ArrayList<ArrayList> precision;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        String fileDichletMixture = "/home/legrand/Documents/recode3.20comp";
        
         String fileAlignement = "/home/legrand/Documents/valdar.tfa";
         ReadAlignement = new ReadAlignement(fileAlignement);
         ReadAlignement.fileToSequence();
         bscore = new Bscore(ReadAlignement, fileDichletMixture);
         ArrayList scoreAlignement = bscore.scoreAlignement();
         bscore.jalaview(scoreAlignement, "valda");//pour jalaview

         
         
         //ArrayList precision = new ArrayList();
        
         //String fileAlignement="/gstock/EFFamily/ballast/EF013.msf";
         //String fileScores="/home/legrand/Documents/EF_family/1B93A_.fea";

        //String fileScores="/home/legrand/Documents/EF_family/1N45A_.fea";
        /*
        for (int i = 1; i < 390; i++) {
            String fileScores;
            if (i < 10) {
                fileScores = "00" + Integer.toString(i);
            } else if (i < 100) {
                fileScores = "0" + Integer.toString(i);
            } else {
                fileScores = Integer.toString(i);
            }
            try {
                System.out.println(i);

                String fileAlignement = "/gstock/EFFamily/msf/EF" + fileScores;
                //String fileAlignement=args[0];
               //System.out.println("all_mean_id="+   aln_meanid(fileAlignement));
                ReadAlignement = new ReadAlignement(fileAlignement);
                ReadAlignement.msfToSequence();
         //Sequence seq=ReadAlignement.sequenceArray.get(10);
                //System.out.println(seq.Seq.toString())
                //ReadAlignement.PrintSequence();
                readscore readscore = new readscore(fileScores);
         //System.out.println(readscore.fileName("001"));

                bscore = new Bscore(ReadAlignement, fileDichletMixture, readscore.scores());

                ArrayList scoreAlignement = bscore.scoreAlignement();

         // bscore.jalaview(scoreAlignement,Integer.toString(i));//pour jalaview
         //System.out.println(bscore.resultats.toString());
                //String s=bscore.resultats.toString();
                //tofile(readscore.Efname+"\n"+s+"\n"+"\n");
                precision.add(bscore.scoregarder());
         //System.out.println(bscore.scoregarder().toString());
                //double[] c=(double[]) precision.get(0);
                // System.out.println(c[0]+""+c[1]);

         //System.out.println(bscore.resultats.toString());
            } catch (Exception e) {
            }//e.printStackTrace();

        }
        //bscore.mean_score();
        double pre = 0;
        ArrayList apre=new  ArrayList();
        ArrayList arec=new  ArrayList();
        ArrayList aspe=new  ArrayList();
        double rec = 0;
        double spe = 0;
        for (int i = 0; i < precision.size(); i++) {

            ArrayList<Double> c = (ArrayList) precision.get(i);
            arec.add(c.get(1));
            apre.add(c.get(0));
            aspe.add(c.get(2));
            pre += c.get(0);
            rec += c.get(1);
            spe += c.get(2);

        }
        System.out.println("precision=" + pre / precision.size());
        System.out.println("sensitivité=" + rec / precision.size());
        System.out.println("specificité=" + spe / precision.size());
        System.out.println(arec.toString());
        System.out.println(apre.toString());
        System.out.println(aspe.toString());
         //bscore.scorecolonne(25);
        //System.out.println(scoreAlignement);
        //bscore.jalaview(scoreAlignement);//pour jalaview
        //System.out.println(bscore.scorewindows(scoreAlignement, 2));
         //bscore.scoregarder(scoreAlignement);
        //System.out.println(bscore.scorecolonne(3));
        */
    }
/**
 * Enregistrer le text dans un fichier
 * @param text 
 */
    static void tofile(String text) {
        try {

            File file = new File("/home/legrand/Documents/test2.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(text);
            bw.close();

            System.out.println("Done");

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

    }
/**
 * execute le programme /home/julie/MACSIMS/aln_meanid
 * @param file
 * @return similarité de l'alignement
 * @throws IOException
 * @throws InterruptedException 
 */
    static String aln_meanid(String file) throws IOException, InterruptedException {
        Process p = Runtime.getRuntime().exec("/home/julie/MACSIMS/aln_meanid " + file);
        p.waitFor();

        BufferedReader reader
                = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        String mean = "";
        while ((line = reader.readLine()) != null) {
            mean += line;
        }
        return mean;

    }

}
