| 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.DiscretizedBranchRates; |
| 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 DiscretizedBranchRatesParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
public static final String DISCRETIZED_BRANCH_RATES = "discretizedBranchRates"; |
| 42 |
public static final String DISTRIBUTION = "distribution"; |
| 43 |
public static final String RATE_CATEGORIES = "rateCategories"; |
| 44 |
public static final String SINGLE_ROOT_RATE = "singleRootRate"; |
| 45 |
public static final String OVERSAMPLING = "overSampling"; |
| 46 |
public static final String NORMALIZE = "normalize"; |
| 47 |
public static final String NORMALIZE_BRANCH_RATE_TO = "normalizeBranchRateTo"; |
| 48 |
public static final String RANDOMIZE_RATES = "randomizeRates"; |
| 49 |
public static final String KEEP_RATES = "keepRates"; |
| 50 |
public static final String CACHED_RATES = "cachedRates"; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public String getParserName() { |
| 56 |
return DISCRETIZED_BRANCH_RATES; |
| 57 |
} |
| 58 |
|
| 59 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 60 |
|
| 61 |
final int overSampling = xo.getAttribute(OVERSAMPLING, 1); |
| 62 |
|
| 63 |
|
| 64 |
final boolean normalize = xo.getAttribute(NORMALIZE, false); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
final double normalizeBranchRateTo = xo.getAttribute(NORMALIZE_BRANCH_RATE_TO, Double.NaN); |
| 70 |
|
| 71 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 72 |
ParametricDistributionModel distributionModel = (ParametricDistributionModel) xo.getElementFirstChild(DISTRIBUTION); |
| 73 |
|
| 74 |
Parameter rateCategoryParameter = (Parameter) xo.getElementFirstChild(RATE_CATEGORIES); |
| 75 |
|
| 76 |
Logger.getLogger("dr.evomodel").info("\nUsing discretized relaxed clock model."); |
| 77 |
Logger.getLogger("dr.evomodel").info(" over sampling = " + overSampling); |
| 78 |
Logger.getLogger("dr.evomodel").info(" parametric model = " + distributionModel.getModelName()); |
| 79 |
Logger.getLogger("dr.evomodel").info(" rate categories = " + rateCategoryParameter.getDimension()); |
| 80 |
if(normalize) { |
| 81 |
Logger.getLogger("dr.evomodel").info(" mean rate is normalized to " + normalizeBranchRateTo); |
| 82 |
} |
| 83 |
|
| 84 |
if (xo.hasAttribute(SINGLE_ROOT_RATE)) { |
| 85 |
|
| 86 |
Logger.getLogger("dr.evomodel").warning(" WARNING: single root rate is not implemented!"); |
| 87 |
} |
| 88 |
|
| 89 |
final boolean randomizeRates = xo.getAttribute(RANDOMIZE_RATES, true); |
| 90 |
final boolean keepRates = xo.getAttribute(KEEP_RATES, false); |
| 91 |
|
| 92 |
final boolean cachedRates = xo.getAttribute(CACHED_RATES, false); |
| 93 |
|
| 94 |
if (randomizeRates && keepRates) { |
| 95 |
throw new XMLParseException("Unable to both randomize and keep current rate categories"); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
return new DiscretizedBranchRates(tree, rateCategoryParameter, distributionModel, overSampling, normalize, |
| 103 |
normalizeBranchRateTo, randomizeRates, keepRates, cachedRates); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public String getParserDescription() { |
| 111 |
return |
| 112 |
"This element returns an discretized relaxed clock model." + |
| 113 |
"The branch rates are drawn from a discretized parametric distribution."; |
| 114 |
} |
| 115 |
|
| 116 |
public Class getReturnType() { |
| 117 |
return DiscretizedBranchRates.class; |
| 118 |
} |
| 119 |
|
| 120 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 121 |
return rules; |
| 122 |
} |
| 123 |
|
| 124 |
private final XMLSyntaxRule[] rules = { |
| 125 |
AttributeRule.newBooleanRule(SINGLE_ROOT_RATE, true, "Whether only a single rate should be used for the two children branches of the root"), |
| 126 |
|
| 127 |
AttributeRule.newIntegerRule(OVERSAMPLING, true, "The integer factor for oversampling the distribution model (1 means no oversampling)"), |
| 128 |
AttributeRule.newBooleanRule(NORMALIZE, true, "Whether the mean rate has to be normalized to a particular value"), |
| 129 |
AttributeRule.newDoubleRule(NORMALIZE_BRANCH_RATE_TO, true, "The mean rate to normalize to, if normalizing"), |
| 130 |
AttributeRule.newBooleanRule(RANDOMIZE_RATES, true, "Randomize initial categories"), |
| 131 |
AttributeRule.newBooleanRule(KEEP_RATES, true, "Keep current rate category specification"), |
| 132 |
AttributeRule.newBooleanRule(CACHED_RATES, true, "Cache rates between steps (default off)"), |
| 133 |
new ElementRule(TreeModel.class), |
| 134 |
new ElementRule(DISTRIBUTION, ParametricDistributionModel.class, "The distribution model for rates among branches", false), |
| 135 |
new ElementRule(RATE_CATEGORIES, Parameter.class, "The rate categories parameter", false), |
| 136 |
}; |
| 137 |
} |