| 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.evolution.tree.TreeUtils; |
| 29 |
import dr.evolution.util.Taxa; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.evomodel.branchratemodel.CountableBranchCategoryProvider; |
| 32 |
import dr.evomodel.tree.TreeModel; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class BranchCategoriesParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String BRANCH_CATEGORIES = "branchCategories"; |
| 41 |
public static final String CATEGORY = "category"; |
| 42 |
public static final String ALLOCATION = "rateCategories"; |
| 43 |
|
| 44 |
public static final String RANDOMIZE = "randomize"; |
| 45 |
|
| 46 |
public String getParserName() { |
| 47 |
return BRANCH_CATEGORIES; |
| 48 |
} |
| 49 |
|
| 50 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 51 |
|
| 52 |
Parameter allocationParameter = (Parameter) xo.getElementFirstChild(ALLOCATION); |
| 53 |
CountableBranchCategoryProvider cladeModel; |
| 54 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 55 |
|
| 56 |
if (!xo.getAttribute(RANDOMIZE, true)) { |
| 57 |
CountableBranchCategoryProvider.CladeBranchCategoryModel cm = new |
| 58 |
CountableBranchCategoryProvider.CladeBranchCategoryModel(treeModel, allocationParameter); |
| 59 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 60 |
if (xo.getChild(i) instanceof XMLObject) { |
| 61 |
XMLObject xoc = (XMLObject) xo.getChild(i); |
| 62 |
if (xoc.getName().equals(LocalClockModelParser.CLADE)) { |
| 63 |
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class); |
| 64 |
|
| 65 |
boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false); |
| 66 |
boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false); |
| 67 |
int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1; |
| 68 |
try { |
| 69 |
cm.setClade(taxonList, rateCategory, includeStem, excludeClade, false); |
| 70 |
} catch (TreeUtils.MissingTaxonException e) { |
| 71 |
throw new XMLParseException("Unable to find taxon for clade in countable mixture model: " + e.getMessage()); |
| 72 |
} |
| 73 |
} else if (xoc.getName().equals(LocalClockModelParser.TRUNK)) { |
| 74 |
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class); |
| 75 |
|
| 76 |
boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false); |
| 77 |
boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false); |
| 78 |
int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1; |
| 79 |
try { |
| 80 |
cm.setClade(taxonList, rateCategory, includeStem, excludeClade, true); |
| 81 |
} catch (TreeUtils.MissingTaxonException e) { |
| 82 |
throw new XMLParseException("Unable to find taxon for trunk in countable mixture model: " + e.getMessage()); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
cladeModel = cm; |
| 88 |
} else { |
| 89 |
CountableBranchCategoryProvider.IndependentBranchCategoryModel cm = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(treeModel, allocationParameter); |
| 90 |
cm.randomize(); |
| 91 |
cladeModel = cm; |
| 92 |
} |
| 93 |
|
| 94 |
return cladeModel; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public String getParserDescription() { |
| 102 |
return |
| 103 |
"This element provides a set of branch categories."; |
| 104 |
} |
| 105 |
|
| 106 |
public Class getReturnType() { |
| 107 |
return CountableBranchCategoryProvider.class; |
| 108 |
} |
| 109 |
|
| 110 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 111 |
return rules; |
| 112 |
} |
| 113 |
|
| 114 |
private final XMLSyntaxRule[] rules = { |
| 115 |
new ElementRule(TreeModel.class), |
| 116 |
new ElementRule(ALLOCATION, Parameter.class, "Allocation parameter", false), |
| 117 |
AttributeRule.newBooleanRule(RANDOMIZE, true), |
| 118 |
new ElementRule(LocalClockModelParser.CLADE, |
| 119 |
new XMLSyntaxRule[]{ |
| 120 |
|
| 121 |
AttributeRule.newIntegerRule(CATEGORY, false), |
| 122 |
AttributeRule.newBooleanRule(LocalClockModelParser.INCLUDE_STEM, true, "determines whether or not the stem branch above this clade is included in the siteModel (default false)."), |
| 123 |
AttributeRule.newBooleanRule(LocalClockModelParser.EXCLUDE_CLADE, true, "determines whether to exclude actual branches of the clade from the siteModel (default false)."), |
| 124 |
new ElementRule(Taxa.class, "A set of taxa which defines a clade to apply a different site model to"), |
| 125 |
}, 0, Integer.MAX_VALUE), |
| 126 |
new ElementRule(LocalClockModelParser.TRUNK, |
| 127 |
new XMLSyntaxRule[]{ |
| 128 |
|
| 129 |
AttributeRule.newIntegerRule(CATEGORY, false), |
| 130 |
AttributeRule.newBooleanRule(LocalClockModelParser.INCLUDE_STEM, true, "determines whether or not the stem branch above this clade is included in the siteModel (default false)."), |
| 131 |
AttributeRule.newBooleanRule(LocalClockModelParser.EXCLUDE_CLADE, true, "determines whether to exclude actual branches of the clade from the siteModel (default false)."), |
| 132 |
new ElementRule(Taxa.class, "A set of taxa which defines a clade to apply a different site model to"), |
| 133 |
}, 0, Integer.MAX_VALUE), |
| 134 |
}; |
| 135 |
} |