/*
 * The Exomiser - A tool to annotate and prioritize genomic variants
 *
 * Copyright (c) 2016-2017 Queen Mary University of London.
 * Copyright (c) 2012-2016 Charité Universitätsmedizin Berlin and Genome Research Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.monarchinitiative.exomiser.core.prioritisers.util;

import java.util.List;

/**
 * @author Sebastian Köhler <dr.sebastian.koehler@gmail.com>
 *
 */
public class ScoreDistribution {

    private double[] scores;
    private double[] pvalues;
    private double numberRandomizations;
    private int numberQueryTerms;
    private int mimNumber;

    public int getNumberQueryTerms() {
        return numberQueryTerms;
    }

    public int getMimNumber() {
        return mimNumber;
    }


    public void setDistribution(List<Double> scoresAL, List<Double> pvaluesAL, double numRandomizations){

        this.scores = new double[scoresAL.size()];
        this.pvalues = new double[pvaluesAL.size()];

        for (int i = 0 ; i < scoresAL.size(); i++){
            this.scores[i] 	= scoresAL.get(i);
            this.pvalues[i] = pvaluesAL.get(i);
        }

        this.numberRandomizations 	= numRandomizations;

    }

    public double getPvalue(double score, double roundFactor) {

        score 	= round(score, roundFactor);
		
		/* maybe this value is too high */
        if ( scores[scores.length-1] < score )
            return (double)1/numberRandomizations;
		
		/* init with 1 */
        double pvalue = 1;

        for (int i = 0; i < scores.length ; i++){

            double scoreCand = scores[i];
            if ( score <= scoreCand ){
                pvalue 	= pvalues[i];
                break;
            }

        }
        return pvalue;
    }

    public static double round( double d , double fact) {
        return Math.rint( d * fact ) / fact;
    }

    @Override
    public String toString() {
        StringBuffer buff = new StringBuffer("score dist for: "+mimNumber+":\n");
        for (int i = 0 ; i< scores.length ; i++){
            buff.append(scores[i]+"-"+pvalues[i]+"\n");
        }
        return buff.toString();
    }

}




@@ -1043,19 +1043,16 @@
 il.com%3E%0A
- *%0A
  */%0Apubl
@@ -1492,16 +1492,17 @@
 zations)
+ 
 %7B%0A%0A     
@@ -1623,17 +1623,16 @@
 nt i = 0
- 
 ; i %3C sc
@@ -1646,24 +1646,25 @@
 size(); i++)
+ 
 %7B%0A          
@@ -1680,17 +1680,16 @@
 ores%5Bi%5D 
-%09
 = scores
@@ -1792,17 +1792,16 @@
 zations 
-%09
 = numRan
@@ -1898,15 +1898,23 @@
     
-score %09
+double rounded 
 = ro
@@ -1938,18 +1938,16 @@
 actor);%0A
-%09%09
 %0A%09%09/* ma
@@ -1984,25 +1984,24 @@
         if (
- 
 scores%5Bscore
@@ -2012,20 +2012,23 @@
 ngth
--
+ - 
 1%5D %3C 
-score 
+rounded
 )%0A  
@@ -2056,10 +2056,13 @@
 ble)
-1/
+ 1 / 
 numb
@@ -2079,18 +2079,16 @@
 ations;%0A
-%09%09
 %0A%09%09/* in
@@ -2169,23 +2169,23 @@
 s.length
- 
 ; i++)
+ 
 %7B%0A%0A     
@@ -2237,22 +2237,23 @@
     if (
- score
+rounded
  %3C= scor
@@ -2257,18 +2257,18 @@
 coreCand
- 
 )
+ 
 %7B%0A      
@@ -2284,17 +2284,16 @@
  pvalue 
-%09
 = pvalue
@@ -2381,21 +2381,22 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
@@ -2412,17 +2412,16 @@
 und(
- 
 double d
  , d
@@ -2416,17 +2416,16 @@
 double d
- 
 , double
@@ -2462,17 +2462,16 @@
 int(
- 
 d * fact
  ) /
@@ -2466,17 +2466,16 @@
 d * fact
- 
 ) / fact
@@ -2548,17 +2548,27 @@
 ngBu
-ffer buff
+ilder stringBuilder
  = n
@@ -2578,18 +2578,19 @@
 StringBu
-ff
+ild
 er(%22scor
@@ -2602,17 +2602,19 @@
 t for: %22
-+
+ + 
 mimNumbe
@@ -2614,17 +2614,19 @@
 imNumber
-+
+ + 
 %22:%5Cn%22);%0A
@@ -2647,20 +2647,20 @@
 nt i = 0
- 
 ; i
+ 
 %3C scores
@@ -2670,15 +2670,15 @@
 ngth
- 
 ; i++)
+ 
 %7B%0A  
@@ -2687,20 +2687,29 @@
         
-buff
+stringBuilder
 .append(
@@ -2721,24 +2721,129 @@
 s%5Bi%5D
-+%22-%22+pvalues%5Bi%5D+
+);%0A            stringBuilder.append(%22-%22);%0A            stringBuilder.append(pvalues%5Bi%5D);%0A            stringBuilder.append(
 %22%5Cn%22
@@ -2874,12 +2874,21 @@
 urn 
-buff
+stringBuilder
 .toS

Reformat ScoreDistribution Change usage of StringBuffer to StringBuilder in ScoreDistribution