/*
 * ArbitraryBranchRatesParser.java
 *
 * Copyright (c) 2002-2015 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
 */

package dr.evomodelxml.branchratemodel;

import dr.evomodel.branchratemodel.ArbitraryBranchRates;
import dr.evomodel.branchratemodel.BranchSpecificFixedEffects;
import dr.evomodel.tree.TreeModel;
import dr.inference.model.Parameter;
import dr.xml.*;

import java.util.logging.Logger;

import static dr.evomodel.branchratemodel.ArbitraryBranchRates.make;

/**
 */
public class ArbitraryBranchRatesParser extends AbstractXMLObjectParser {

    public static final String ARBITRARY_BRANCH_RATES = "arbitraryBranchRates";
    private static final String RATES = "rates";
    public static final String RECIPROCAL = "reciprocal";
    public static final String EXP = "exp";
    private static final String MULTIPLER = "multiplier";
    private static final String CENTER_AT_ONE = "centerAtOne";

    public static final String LOCATION = "location";
    public static final String SCALE = "scale";

    public String getParserName() {
        return ARBITRARY_BRANCH_RATES;
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);

        XMLObject cxo = xo.getChild(RATES);

        Parameter rateCategoryParameter = (Parameter) cxo.getChild(Parameter.class);

        boolean centerAtOne = xo.getAttribute(CENTER_AT_ONE, true);


        final int numBranches = tree.getNodeCount() - 1;
        if (rateCategoryParameter.getDimension() != numBranches) {
            rateCategoryParameter.setDimension(numBranches);
        }

        Logger.getLogger("dr.evomodel").info("\nUsing an scaled mixture of normals model.");
        Logger.getLogger("dr.evomodel").info("  rates = " + rateCategoryParameter.getDimension());
        Logger.getLogger("dr.evomodel").info("  NB: Make sure you have a prior on " + rateCategoryParameter.getId() + " and do not use this model in a treeLikelihood for sequence data");


        ArbitraryBranchRates.BranchRateTransform transform = parseTransform(xo);

        return new ArbitraryBranchRates(tree, rateCategoryParameter, transform, centerAtOne);
    }

    //************************************************************************
    // AbstractXMLObjectParser implementation
    //************************************************************************

    public String getParserDescription() {
        return "This element returns an arbitrary rate model." +
                "The branch rates are drawn from an arbitrary distribution determine by the prior.";
    }

    public static ArbitraryBranchRates.BranchRateTransform parseTransform (XMLObject xo) throws XMLParseException {

        boolean reciprocal = xo.getAttribute(RECIPROCAL, false);
        Logger.getLogger("dr.evomodel").info("  reciprocal = " + reciprocal);

        boolean exp = xo.getAttribute(EXP, false);

        boolean multipler = xo.getAttribute(MULTIPLER, false);

        BranchSpecificFixedEffects locationParameter = null;
        if (xo.hasChildNamed(LOCATION)) {
            Object locationObject = xo.getElementFirstChild(LOCATION);
            if ( locationObject instanceof BranchSpecificFixedEffects) {
                locationParameter = (BranchSpecificFixedEffects) locationObject;
            } else {
                locationParameter = new BranchSpecificFixedEffects.None((Parameter) locationObject);
            }
        }

        Parameter scaleParameter = null;
        if (xo.hasChildNamed(SCALE)) {
            scaleParameter = (Parameter) xo.getElementFirstChild(SCALE);
        }

        return make(reciprocal, exp, multipler, locationParameter, scaleParameter);
    }

    public Class getReturnType() {
        return ArbitraryBranchRates.class;
    }

    public XMLSyntaxRule[] getSyntaxRules() {
        return rules;
    }

    private final XMLSyntaxRule[] rules = {
            new ElementRule(TreeModel.class),
            new ElementRule(RATES, Parameter.class, "The rate parameter"),
            AttributeRule.newBooleanRule(RECIPROCAL, true),
            AttributeRule.newBooleanRule(CENTER_AT_ONE, true),
            AttributeRule.newBooleanRule(EXP, true),
            new ElementRule(SCALE, Parameter.class, "optional scale parameter", true),
            new ElementRule(LOCATION, Parameter.class, "optional location parameter", true),
    };


}

@@ -1604,39 +1604,32 @@
 = %22rates%22;%0D%0A    
-public 
 static final Str
@@ -1656,39 +1656,32 @@
 ciprocal%22;%0D%0A    
-public 
 static final Str
@@ -1737,16 +1737,17 @@
  MULTIPL
+I
 ER = %22mu
@@ -1820,39 +1820,32 @@
 rAtOne%22;%0D%0A%0D%0A    
-public 
 static final Str
@@ -1872,31 +1872,24 @@
 tion%22;%0D%0A    
-public 
 static final
@@ -2351,26 +2351,24 @@
 , true);%0D%0A%0D%0A
-%0D%0A
         fina
@@ -2413,16 +2413,224 @@
 ) - 1;%0D%0A
+        if (rateCategoryParameter.getDimension() %3E 1 && (rateCategoryParameter.getDimension() != numBranches)) %7B%0D%0A            throw new XMLParseException(%22Incorrect number of rate parameters%22);%0D%0A        %7D%0D%0A%0D%0A
         
@@ -3768,31 +3768,24 @@
    %7D%0D%0A%0D%0A    
-public 
 static Arbit
@@ -4104,16 +4104,17 @@
  multipl
+i
 er = xo.
@@ -4133,16 +4133,17 @@
 (MULTIPL
+I
 ER, fals
@@ -4847,16 +4847,17 @@
  multipl
+i
 er, loca
@@ -5603,15 +5603,11 @@
     %7D;%0D%0A
-%0D%0A%0D%0A
 %7D%0D%0A

clean warnings