| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodelxml.branchratemodel; |
| 27 |
|
| 28 |
import dr.evomodel.branchratemodel.ContinuousBranchRates; |
| 29 |
import dr.evomodel.tree.TreeModel; |
| 30 |
import dr.inference.distribution.ParametricDistributionModel; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import java.util.logging.Logger; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class ContinuousBranchRatesParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
public static final String CONTINUOUS_BRANCH_RATES = "continuousBranchRates"; |
| 42 |
public static final String DISTRIBUTION = "distribution"; |
| 43 |
public static final String RATE_CATEGORY_QUANTILES = "rateCategoryQuantiles"; |
| 44 |
public static final String RATE_QUANTILES = "rateQuantiles"; |
| 45 |
public static final String SINGLE_ROOT_RATE = "singleRootRate"; |
| 46 |
public static final String NORMALIZE = "normalize"; |
| 47 |
public static final String NORMALIZE_BRANCH_RATE_TO = "normalizeBranchRateTo"; |
| 48 |
|
| 49 |
|
| 50 |
public String getParserName() { |
| 51 |
return CONTINUOUS_BRANCH_RATES; |
| 52 |
} |
| 53 |
|
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
final boolean normalize = xo.getAttribute(NORMALIZE, false); |
| 57 |
final double normalizeBranchRateTo = xo.getAttribute(NORMALIZE_BRANCH_RATE_TO, Double.NaN); |
| 58 |
|
| 59 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 60 |
ParametricDistributionModel distributionModel = (ParametricDistributionModel) xo.getElementFirstChild(DISTRIBUTION); |
| 61 |
|
| 62 |
Parameter rateQuantilesParameter; |
| 63 |
if (xo.hasChildNamed(RATE_QUANTILES)) { |
| 64 |
rateQuantilesParameter = (Parameter) xo.getElementFirstChild(RATE_QUANTILES); |
| 65 |
} else { |
| 66 |
rateQuantilesParameter = (Parameter) xo.getElementFirstChild(RATE_CATEGORY_QUANTILES); |
| 67 |
} |
| 68 |
|
| 69 |
Logger.getLogger("dr.evomodel").info("Using continuous relaxed clock model."); |
| 70 |
Logger.getLogger("dr.evomodel").info(" parametric model = " + distributionModel.getModelName()); |
| 71 |
Logger.getLogger("dr.evomodel").info(" rate categories = " + rateQuantilesParameter.getDimension()); |
| 72 |
if(normalize) { |
| 73 |
Logger.getLogger("dr.evomodel").info(" mean rate is normalized to " + normalizeBranchRateTo); |
| 74 |
} |
| 75 |
|
| 76 |
if (xo.hasAttribute(SINGLE_ROOT_RATE)) { |
| 77 |
Logger.getLogger("dr.evomodel").warning(" WARNING: single root rate is not implemented!"); |
| 78 |
} |
| 79 |
|
| 80 |
return new ContinuousBranchRates(tree, rateQuantilesParameter, distributionModel, normalize, normalizeBranchRateTo); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public String getParserDescription() { |
| 88 |
return |
| 89 |
"This element returns a continuous quantile uncorrelated relaxed clock model."; |
| 90 |
} |
| 91 |
|
| 92 |
public Class getReturnType() { |
| 93 |
return ContinuousBranchRates.class; |
| 94 |
} |
| 95 |
|
| 96 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 97 |
return rules; |
| 98 |
} |
| 99 |
|
| 100 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 101 |
AttributeRule.newBooleanRule(SINGLE_ROOT_RATE, true, "Whether only a single rate should be used for the two children branches of the root"), |
| 102 |
AttributeRule.newBooleanRule(NORMALIZE, true, "Whether the mean rate has to be normalized to a particular value"), |
| 103 |
AttributeRule.newDoubleRule(NORMALIZE_BRANCH_RATE_TO, true, "The mean rate to normalize to, if normalizing"), |
| 104 |
new ElementRule(TreeModel.class), |
| 105 |
new ElementRule(DISTRIBUTION, ParametricDistributionModel.class, "The distribution model for rates among branches", false), |
| 106 |
new XORRule( |
| 107 |
new ElementRule(RATE_QUANTILES, Parameter.class, "The quantiles for each branch", false), |
| 108 |
new ElementRule(RATE_CATEGORY_QUANTILES, Parameter.class, "The quantiles for each branch", false) |
| 109 |
) |
| 110 |
}; |
| 111 |
} |