Debian Codegraph @

src/dr/evomodelxml/branchratemodel/CountableMixtureBranchRatesParser.java

raw symbols SCIP
1 /*
2 * CountableMixtureBranchRatesParser.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.AbstractBranchRateModel;
32 import dr.evomodel.branchratemodel.CountableBranchCategoryProvider;
33 import dr.evomodel.branchratemodel.CountableMixtureBranchRates;
34 import dr.evomodel.branchratemodel.CountableModelMixtureBranchRates;
35 import dr.evomodel.tree.TreeModel;
36 import dr.inference.model.Parameter;
37 import dr.xml.*;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.logging.Logger;
42
43 /**
44 */
45 public class CountableMixtureBranchRatesParser extends AbstractXMLObjectParser {
46
47 public static final String COUNTABLE_CLOCK_BRANCH_RATES = "countableMixtureBranchRates";
48 public static final String RATES = "rates";
49 public static final String ALLOCATION = "rateCategories";
50 public static final String CATEGORY = "category";
51 public static final String RANDOMIZE = "randomize";
52 public static final String RANDOM_EFFECTS = "randomEffects";
53 public static final String FIXED_EFFECTS = "fixedEffects";
54 public static final String IN_LOG_SPACE = "inLogSpace";
55 public static final String TIME_COEFFICIENT = "branchTimeEffect";
56
57 public String getParserName() {
58 return COUNTABLE_CLOCK_BRANCH_RATES;
59 }
60
61 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
62
63 Parameter ratesParameter = null;
64
65 List<AbstractBranchRateModel> fixedEffects = null;
66 if (xo.hasChildNamed(FIXED_EFFECTS)) {
67 XMLObject cxo = xo.getChild(FIXED_EFFECTS);
68 fixedEffects = new ArrayList<AbstractBranchRateModel>();
69 for (int i = 0; i < cxo.getChildCount(); ++i) {
70 fixedEffects.add((AbstractBranchRateModel)cxo.getChild(i));
71 }
72 } else {
73 ratesParameter = (Parameter) xo.getElementFirstChild(RATES);
74 }
75
76 Parameter timeCoefficient;
77 if (xo.hasChildNamed(TIME_COEFFICIENT)){
78 timeCoefficient = (Parameter) xo.getElementFirstChild(TIME_COEFFICIENT);
79 } else {
80 timeCoefficient = null;
81 }
82
83 Parameter allocationParameter = (Parameter) xo.getElementFirstChild(ALLOCATION);
84 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
85 List<AbstractBranchRateModel> randomEffects = null;
86 if (xo.hasChildNamed(RANDOM_EFFECTS)) {
87 XMLObject cxo = xo.getChild(RANDOM_EFFECTS);
88 randomEffects = new ArrayList<AbstractBranchRateModel>();
89 for (int i = 0; i < cxo.getChildCount(); ++i) {
90 randomEffects.add((AbstractBranchRateModel)cxo.getChild(i));
91 }
92 }
93
94 boolean inLogSpace = xo.getAttribute(IN_LOG_SPACE, false);
95
96 Logger.getLogger("dr.evomodel").info("Using a countable mixture molecular clock model.");
97
98 CountableBranchCategoryProvider.BranchCategoryModel cladeModel;
99
100 if (!xo.getAttribute(RANDOMIZE, true)) {
101 CountableBranchCategoryProvider.CladeBranchCategoryModel cm = new
102 CountableBranchCategoryProvider.CladeBranchCategoryModel(treeModel, allocationParameter);
103 for (int i = 0; i < xo.getChildCount(); ++i) {
104 if (xo.getChild(i) instanceof XMLObject) {
105 XMLObject xoc = (XMLObject) xo.getChild(i);
106 if (xoc.getName().equals(LocalClockModelParser.CLADE)) {
107 TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
108
109 boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false);
110 boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false);
111 int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1; // XML index-start = 1 not 0
112 try {
113 cm.setClade(taxonList, rateCategory, includeStem, excludeClade, false);
114 } catch (TreeUtils.MissingTaxonException e) {
115 throw new XMLParseException("Unable to find taxon for clade in countable mixture model: " + e.getMessage());
116 }
117 } else if (xoc.getName().equals(LocalClockModelParser.TRUNK)) {
118 TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
119
120 boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false);
121 boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false);
122 int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1; // XML index-start = 1 not 0
123 try {
124 cm.setClade(taxonList, rateCategory, includeStem, excludeClade, true);
125 } catch (TreeUtils.MissingTaxonException e) {
126 throw new XMLParseException("Unable to find taxon for trunk in countable mixture model: " + e.getMessage());
127 }
128 }
129 }
130 }
131 cladeModel = cm;
132 } else {
133 CountableBranchCategoryProvider.IndependentBranchCategoryModel cm = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(treeModel, allocationParameter);
134 cm.randomize();
135 cladeModel = cm;
136 }
137
138 if (fixedEffects != null) {
139 return new CountableModelMixtureBranchRates(cladeModel, treeModel, fixedEffects, randomEffects, inLogSpace);
140 } else {
141 return new CountableMixtureBranchRates(cladeModel, treeModel, ratesParameter, timeCoefficient, randomEffects, inLogSpace);
142 }
143 }
144
145 //************************************************************************
146 // AbstractXMLObjectParser implementation
147 //************************************************************************
148
149 public String getParserDescription() {
150 return
151 "This element provides a clock consisting of a mixture of fixed effects and random effects.";
152 }
153
154 public Class getReturnType() {
155 return CountableMixtureBranchRates.class;
156 }
157
158 public XMLSyntaxRule[] getSyntaxRules() {
159 return rules;
160 }
161
162 private final XMLSyntaxRule[] rules = {
163 new ElementRule(TreeModel.class),
164 new XORRule(
165 new ElementRule(RATES, Parameter.class, "The molecular evolutionary rate parameter", false),
166 new ElementRule(FIXED_EFFECTS,
167 new XMLSyntaxRule[] {
168 new ElementRule(AbstractBranchRateModel.class, 0, Integer.MAX_VALUE),
169 },
170 "Fixed effects", false)
171 ),
172 new ElementRule(ALLOCATION, Parameter.class, "Allocation parameter", false),
173 new ElementRule(TIME_COEFFICIENT, Parameter.class, "The coefficient for the branch time-dependent rate function", true),
174 new ElementRule(RANDOM_EFFECTS,
175 new XMLSyntaxRule[] {
176 new ElementRule(AbstractBranchRateModel.class, 0, Integer.MAX_VALUE),
177 },
178 "Possible random effects", true),
179 AttributeRule.newBooleanRule(IN_LOG_SPACE, true),
180 AttributeRule.newBooleanRule(RANDOMIZE, true),
181 new ElementRule(LocalClockModelParser.CLADE,
182 new XMLSyntaxRule[]{
183 // AttributeRule.newBooleanRule(RELATIVE, true),
184 AttributeRule.newIntegerRule(CATEGORY, false),
185 AttributeRule.newBooleanRule(LocalClockModelParser.INCLUDE_STEM, true, "determines whether or not the stem branch above this clade is included in the siteModel (default false)."),
186 AttributeRule.newBooleanRule(LocalClockModelParser.EXCLUDE_CLADE, true, "determines whether to exclude actual branches of the clade from the siteModel (default false)."),
187 new ElementRule(Taxa.class, "A set of taxa which defines a clade to apply a different site model to"),
188 }, 0, Integer.MAX_VALUE),
189 };
190 }