Debian Codegraph @

src/dr/evomodelxml/branchmodel/EpochBranchModelParser.java

raw symbols SCIP
1 /*
2 * EpochBranchModelParser.java
3 *
4 * Copyright (c) 2002-2016 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.branchmodel;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.logging.Logger;
32
33 import dr.evomodel.branchmodel.EpochBranchModel;
34 import dr.evomodel.substmodel.SubstitutionModel;
35 import dr.evomodel.branchratemodel.RateEpochBranchRateModel;
36 import dr.evomodel.tree.TreeModel;
37 import dr.inference.model.CompoundParameter;
38 import dr.inference.model.Parameter;
39 import dr.xml.AbstractXMLObjectParser;
40 import dr.xml.AttributeRule;
41 import dr.xml.ElementRule;
42 import dr.xml.XMLObject;
43 import dr.xml.XMLParseException;
44 import dr.xml.XMLSyntaxRule;
45
46 /**
47 */
48 public class EpochBranchModelParser extends AbstractXMLObjectParser {
49
50 public static final String EPOCH_BRANCH_MODEL = "epochBranchModel";
51 public static final String EPOCH = "epoch";
52 public static final String TRANSITION_TIME = "transitionTime";
53
54 public String getParserName() {
55 return EPOCH_BRANCH_MODEL;
56 }
57
58 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
59
60 Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch branch model.");
61
62 TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
63 SubstitutionModel ancestralSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
64
65 List<Epoch> epochs = new ArrayList<Epoch>();
66
67 for (int i = 0; i < xo.getChildCount(); i++) {
68 if (xo.getChild(i) instanceof XMLObject) {
69 XMLObject xoc = (XMLObject) xo.getChild(i);
70 if (xoc.getName().equals(EPOCH)) {
71
72 Parameter tt = null;
73
74 if (xoc.hasAttribute(TRANSITION_TIME)) {
75 double t = xoc.getAttribute(TRANSITION_TIME, 0.0);
76 tt = new Parameter.Default(1, t);
77 }
78
79 SubstitutionModel s = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
80
81 if (xoc.hasChildNamed(TRANSITION_TIME)) {
82 if (tt != null) {
83 throw new XMLParseException("An epoch cannot have a transitionTime attribute and a parameter");
84 }
85
86 tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME);
87 }
88 epochs.add(new Epoch(s, tt));
89 }
90 }
91 }
92
93 Collections.sort(epochs);
94 List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
95 CompoundParameter transitionTimes = new CompoundParameter("epochTimes");
96
97 for (Epoch epoch : epochs) {
98 substitutionModels.add(epoch.substitutionModel);
99 transitionTimes.addParameter(epoch.timeParameter);
100 }
101
102 substitutionModels.add(ancestralSubstitutionModel);
103
104 return new EpochBranchModel(tree, substitutionModels, transitionTimes);
105 }
106
107 class Epoch implements Comparable<Object> {
108
109 private final double transitionTime;
110 private final SubstitutionModel substitutionModel;
111 private final Parameter timeParameter;
112
113 public Epoch(SubstitutionModel substitutionModel, Parameter timeParameter) {
114 this.transitionTime = timeParameter.getParameterValue(0);
115 this.substitutionModel = substitutionModel;
116 this.timeParameter = timeParameter;
117 }
118
119 public int compareTo(Object o) {
120 return Double.compare(transitionTime, ((Epoch) o).transitionTime);
121 }
122
123 }
124 //************************************************************************
125 // AbstractXMLObjectParser implementation
126 //************************************************************************
127
128 public String getParserDescription() {
129 return
130 "This element provides a branch model which has multiple epoch. " +
131 "All branches (or portions of them) have the same substitution " +
132 "model within a given epoch. If parameters are used to sample " +
133 "transition times, these must be kept in ascending order by judicious " +
134 "use of bounds or priors.";
135 }
136
137 public Class<RateEpochBranchRateModel> getReturnType() {
138 return RateEpochBranchRateModel.class;
139 }
140
141 public XMLSyntaxRule[] getSyntaxRules() {
142 return rules;
143 }
144
145 private final XMLSyntaxRule[] rules = {
146 new ElementRule(TreeModel.class, "The tree across which the epochs will be assigned"),
147 new ElementRule(SubstitutionModel.class, "The ancestral substitution model after the last epoch"),
148 new ElementRule(EPOCH,
149 new XMLSyntaxRule[]{
150 AttributeRule.newDoubleRule(TRANSITION_TIME, true, "The time of transition between this epoch and the previous one"),
151 new ElementRule(SubstitutionModel.class, "The substitution model for this epoch"),
152 new ElementRule(TRANSITION_TIME, Parameter.class, "The transition time parameter for this epoch", true)
153 }, "An epoch that lasts until transitionTime",
154 1, Integer.MAX_VALUE
155 ),
156 };
157
158 }