Debian Codegraph @

src/dr/evomodelxml/branchratemodel/RateEpochBranchRateModelParser.java

raw symbols SCIP
1 /*
2 * RateEpochBranchRateModelParser.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.evomodel.branchratemodel.ContinuousEpochBranchRateModel;
29 import dr.evomodel.branchratemodel.RateEpochBranchRateModel;
30 import dr.evomodelxml.tree.TreeModelParser;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.logging.Logger;
38
39 /**
40 */
41 public class RateEpochBranchRateModelParser extends AbstractXMLObjectParser {
42
43 public static final String RATE_EPOCH_BRANCH_RATES = "rateEpochBranchRates";
44 public static final String RATE = "rate";
45 public static final String EPOCH = "epoch";
46 public static final String TRANSITION_TIME = "transitionTime";
47 public static final String CONTINUOUS_NORMALIZATION = "continuousNormalization";
48
49 public String getParserName() {
50 return RATE_EPOCH_BRANCH_RATES;
51 }
52
53 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
54
55 Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch rate model.");
56
57 List<Epoch> epochs = new ArrayList<Epoch>();
58
59 for (int i = 0; i < xo.getChildCount(); i++) {
60 XMLObject xoc = (XMLObject) xo.getChild(i);
61 if (xoc.getName().equals(EPOCH)) {
62 double t = xoc.getAttribute(TRANSITION_TIME, 0.0);
63
64 Parameter p = (Parameter) xoc.getChild(Parameter.class);
65
66 Parameter tt = null;
67 if (xoc.hasChildNamed(TRANSITION_TIME)) {
68 tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME);
69 }
70 epochs.add(new Epoch(t, p, tt));
71 }
72 }
73
74 Parameter ancestralRateParameter = (Parameter) xo.getElementFirstChild(RATE);
75
76 Collections.sort(epochs);
77 Parameter[] rateParameters = new Parameter[epochs.size() + 1];
78 Parameter[] timeParameters = new Parameter[epochs.size()];
79
80 int i = 0;
81 for (Epoch epoch : epochs) {
82 rateParameters[i] = epoch.rateParameter;
83 if (epoch.timeParameter != null) {
84 timeParameters[i] = epoch.timeParameter;
85 } else {
86 timeParameters[i] = new Parameter.Default(1);
87 timeParameters[i].setParameterValue(0, epoch.transitionTime);
88 }
89 i++;
90 }
91 rateParameters[i] = ancestralRateParameter;
92
93 if (xo.hasAttribute(CONTINUOUS_NORMALIZATION) && xo.getBooleanAttribute(CONTINUOUS_NORMALIZATION)) {
94 Parameter rootHeight = (Parameter) xo.getChild(TreeModelParser.ROOT_HEIGHT).getChild(Parameter.class);
95 return new ContinuousEpochBranchRateModel(timeParameters, rateParameters, rootHeight);
96 }
97
98 return new RateEpochBranchRateModel(timeParameters, rateParameters);
99 }
100
101 class Epoch implements Comparable {
102
103 private final double transitionTime;
104 private final Parameter rateParameter;
105 private final Parameter timeParameter;
106
107 public Epoch(double transitionTime, Parameter rateParameter, Parameter timeParameter) {
108 this.transitionTime = transitionTime;
109 this.rateParameter = rateParameter;
110 this.timeParameter = timeParameter;
111 }
112
113 public int compareTo(Object o) {
114 return Double.compare(transitionTime, ((Epoch) o).transitionTime);
115 }
116
117 }
118 //************************************************************************
119 // AbstractXMLObjectParser implementation
120 //************************************************************************
121
122 public String getParserDescription() {
123 return "This element provides a multiple epoch molecular clock model. " +
124 "All branches (or portions of them) have the same rate of molecular " +
125 "evolution within a given epoch. If parameters are used to sample " +
126 "transition times, these must be kept in ascending order by judicious " +
127 "use of bounds or priors.";
128 }
129
130 public Class getReturnType() {
131 return RateEpochBranchRateModel.class;
132 }
133
134 public XMLSyntaxRule[] getSyntaxRules() {
135 return rules;
136 }
137
138 private final XMLSyntaxRule[] rules = {
139 new ElementRule(EPOCH,
140 new XMLSyntaxRule[]{
141 AttributeRule.newDoubleRule(TRANSITION_TIME, true, "The time of transition between this epoch and the previous one"),
142 new ElementRule(Parameter.class, "The evolutionary rate parameter for this epoch"),
143 new ElementRule(TRANSITION_TIME, Parameter.class, "The transition time parameter for this epoch", true)
144 }, "An epoch that lasts until transitionTime",
145 1, Integer.MAX_VALUE
146 ),
147 new ElementRule(RATE, Parameter.class, "The ancestral molecular evolutionary rate parameter", false),
148 AttributeRule.newBooleanRule(CONTINUOUS_NORMALIZATION, true, "Special rate normalization for a Brownian diffusion process"),
149 new ElementRule(TreeModelParser.ROOT_HEIGHT,
150 new XMLSyntaxRule[]{
151 new ElementRule(Parameter.class, "The tree root height")
152 }, "Parameterization may require the root height", 0, 1)
153 };
154
155 }