/*
 * CoalescentTreeIntervalStatistic.java
 *
 * Copyright (c) 2002-2013 Alexei Drummond, Andrew Rambaut and Marc Suchard
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

/**
 * @author Guy Baele
 */

package dr.evomodel.coalescent;

import dr.evolution.coalescent.TreeIntervals;
import dr.evolution.tree.Tree;
import dr.inference.model.Statistic;


public class CoalescentTreeIntervalStatistic extends Statistic.Abstract {

    private Tree tree;
    private TreeIntervals intervals;

    public CoalescentTreeIntervalStatistic(Tree tree) {
        this.tree = tree;
        this.intervals = new TreeIntervals(tree);
    }

    public int getDimension() {
    	return intervals.getIntervalCount();
    }

    public double getStatisticValue(int i) {
    	//need more elegant way to trigger recalculation of intervals
    	if (i == 0) {
    		intervals.setIntervalsUnknown();
    	}
    	
        double interval = intervals.getInterval(i);

        return interval;
    }
}

@@ -1181,16 +1181,42 @@
 istic;%0A%0A
+/**%0A* @author Guy Baele%0A*/
 %0Apublic 
@@ -1342,16 +1342,82 @@
 tervals;
+%0A    private int dimension;%0A    private double%5B%5D nonZeroIntervals;
 %0A%0A    pu
@@ -1550,64 +1550,221 @@
     
-%7D%0A%0A
     
-public int getDimension() %7B%0A    %09return i
+//detect here if there are zero-length intervals and set the dimension accordingly%0A        setDimension(intervals);%0A        this.nonZeroIntervals = new double%5Bthis.dimension%5D;%0A        removeZeroLengthI
 ntervals
 .get
@@ -1763,27 +1763,86 @@
 vals
-.getI
+(i
 nterval
-Count()
+s);%0A    %7D%0A%0A    public int getDimension() %7B%0A    %09return this.dimension
 ;%0A  
@@ -2016,16 +2016,60 @@
 nown();%0A
+    %09%09removeZeroLengthIntervals(intervals);%0A
     %09%7D%0A 
@@ -2081,16 +2081,18 @@
         
+//
 double i
@@ -2131,33 +2131,781 @@
 i);%0A
+    %09//return interval;
 %0A    
+%09%0A
     
+%09
 return 
-interval;
+this.nonZeroIntervals%5Bi%5D;%0A    %7D%0A    %0A    public String getStatisticName() %7B%0A    %09return %22coalescentTreeIntervalStatistic%22;%0A    %7D%0A    %0A    private void setDimension(TreeIntervals intervals) %7B%0A    %09int dim = intervals.getIntervalCount();%0A    %09for (int i = 0; i %3C intervals.getIntervalCount(); i++) %7B%0A    %09%09if (intervals.getInterval(i) == 0.0) %7B%0A    %09%09%09dim--;%0A    %09%09%7D%0A    %09%7D%0A    %09this.dimension = dim;%0A    %7D%0A    %0A    private void removeZeroLengthIntervals(TreeIntervals intervals) %7B%0A    %09int dim = 0;%0A    %09for (int i = 0; i %3C intervals.getIntervalCount(); i++) %7B%0A    %09%09double intervalLength = intervals.getInterval(i);%0A    %09%09if (intervalLength != 0.0) %7B%0A    %09%09%09this.nonZeroIntervals%5Bdim%5D = intervalLength;%0A    %09%09%09dim++;%0A    %09%09%7D%0A    %09%7D
 %0A   

CoalescentTreeIntervalStatistic no longer outputs zero-length intervals.