Debian Codegraph @

src/dr/evomodelxml/branchratemodel/BranchCategoriesParser.java

raw symbols SCIP
1 /*
2 * BranchCategoriesParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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; // XML index-start = 1 not 0
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; // XML index-start = 1 not 0
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 // AbstractXMLObjectParser implementation
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 // AttributeRule.newBooleanRule(RELATIVE, true),
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 // AttributeRule.newBooleanRule(RELATIVE, true),
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 }