| 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.MixtureModelBranchRates; |
| 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 |
import java.util.ArrayList; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class MixtureModelBranchRatesParser extends AbstractXMLObjectParser { |
| 41 |
|
| 42 |
public static final String MIXTURE_MODEL_BRANCH_RATES = "mixtureModelBranchRates"; |
| 43 |
public static final String DISTRIBUTION = "distribution"; |
| 44 |
public static final String RATE_CATEGORY_QUANTILES = "rateCategoryQuantiles"; |
| 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 |
public static final String DISTRIBUTION_INDEX = "distributionIndex"; |
| 49 |
|
| 50 |
public static final String USE_QUANTILE = "useQuantilesForRates"; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public String getParserName() { |
| 55 |
return MIXTURE_MODEL_BRANCH_RATES; |
| 56 |
} |
| 57 |
|
| 58 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 59 |
|
| 60 |
ArrayList<ParametricDistributionModel> modelsList = new ArrayList<ParametricDistributionModel>(); |
| 61 |
|
| 62 |
final boolean normalize = xo.getAttribute(NORMALIZE, false); |
| 63 |
final double normalizeBranchRateTo = xo.getAttribute(NORMALIZE_BRANCH_RATE_TO, Double.NaN); |
| 64 |
|
| 65 |
final boolean useQuantilesForRates = xo.getAttribute(USE_QUANTILE, true); |
| 66 |
|
| 67 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 72 |
Object child = xo.getChild(i); |
| 73 |
if( child instanceof XMLObject ) { |
| 74 |
if( ((XMLObject) child).getName().equals(DISTRIBUTION) ) { |
| 75 |
XMLObject childXML = (XMLObject) child; |
| 76 |
modelsList.add((ParametricDistributionModel) childXML.getChild(0)); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
ParametricDistributionModel[] models = modelsList.toArray(new ParametricDistributionModel[modelsList.size()]); |
| 84 |
|
| 85 |
Parameter rateCategoryQuantilesParameter = (Parameter) xo.getElementFirstChild(RATE_CATEGORY_QUANTILES); |
| 86 |
|
| 87 |
Parameter distributionIndexParameter = (Parameter) xo.getElementFirstChild(DISTRIBUTION_INDEX); |
| 88 |
|
| 89 |
Logger.getLogger("dr.evomodel").info("Using random discretized relaxed clock model with a mixture distribution."); |
| 90 |
for(int i=0; i<models.length; i++) { |
| 91 |
Logger.getLogger("dr.evomodel").info(" parametric model " + (i+1) +" = " + models[i].getModelName()); |
| 92 |
} |
| 93 |
|
| 94 |
Logger.getLogger("dr.evomodel").info(" rate categories = " + rateCategoryQuantilesParameter.getDimension()); |
| 95 |
if(normalize) { |
| 96 |
Logger.getLogger("dr.evomodel").info(" mean rate is normalized to " + normalizeBranchRateTo); |
| 97 |
} |
| 98 |
|
| 99 |
if (xo.hasAttribute(SINGLE_ROOT_RATE)) { |
| 100 |
|
| 101 |
Logger.getLogger("dr.evomodel").warning(" WARNING: single root rate is not implemented!"); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
if(!useQuantilesForRates) { |
| 106 |
Logger.getLogger("dr.evomodel").info("Rates are set to not being drawn using quantiles. Thus they are not drawn from any particular distribution."); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
return new MixtureModelBranchRates(tree, rateCategoryQuantilesParameter, models, distributionIndexParameter, useQuantilesForRates, normalize, normalizeBranchRateTo); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public String getParserDescription() { |
| 120 |
return |
| 121 |
"This element returns a random discretized relaxed clock model." + |
| 122 |
"The branch rates are drawn from a continuous parametric distribution."; |
| 123 |
} |
| 124 |
|
| 125 |
public Class getReturnType() { |
| 126 |
return MixtureModelBranchRates.class; |
| 127 |
} |
| 128 |
|
| 129 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 130 |
return rules; |
| 131 |
} |
| 132 |
|
| 133 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 134 |
AttributeRule.newBooleanRule(SINGLE_ROOT_RATE, true, "Whether only a single rate should be used for the two children branches of the root"), |
| 135 |
|
| 136 |
|
| 137 |
AttributeRule.newBooleanRule(NORMALIZE, true, "Whether the mean rate has to be normalized to a particular value"), |
| 138 |
AttributeRule.newDoubleRule(NORMALIZE_BRANCH_RATE_TO, true, "The mean rate to normalize to, if normalizing"), |
| 139 |
AttributeRule.newBooleanRule(USE_QUANTILE, true, "Whether or not to use quantiles to represent rates. If false then rates are not drawn " + |
| 140 |
"specifically from any of the distributions"), |
| 141 |
new ElementRule(TreeModel.class), |
| 142 |
|
| 143 |
|
| 144 |
new ElementRule(DISTRIBUTION, ParametricDistributionModel.class, "The distribution model for rates among branches", 1, Integer.MAX_VALUE), |
| 145 |
new ElementRule(DISTRIBUTION_INDEX, Parameter.class, "Operator that switches between the distributions of the branch rate distribution model", false), |
| 146 |
|
| 147 |
new ElementRule(RATE_CATEGORY_QUANTILES, Parameter.class, "The quantiles for", false), |
| 148 |
}; |
| 149 |
} |