Debian Codegraph @

src/dr/evomodel/tree/StarTreeModel.java

raw symbols SCIP
1 /*
2 * StarTreeModel.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.evomodel.tree;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.inference.model.Bounds;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 /**
36 * @author Marc Suchard
37 */
38 public class StarTreeModel extends TreeModel {
39
40 public StarTreeModel(String id, Tree tree) {
41 super(id, tree);
42 maxTipHeightKnown = false;
43 rootHeightParameter = null;
44 sharedRoot = null;
45 }
46
47 @Override
48 public void setupHeightBounds() {
49
50 if (heightBoundsSetup) {
51 throw new IllegalArgumentException("Node height bounds set up twice");
52 }
53
54 for (int i = 0; i < getNodeCount(); i++) {
55 setupHeightBounds((Node) getNode(i));
56 }
57
58 heightBoundsSetup = true;
59 }
60
61 // private void fixInternalNodeHeightToRoot() {
62 // double rootHeight = getNodeHeight(getRoot());
63 // for (int i = 0; i < getInternalNodeCount(); ++i) {
64 // Node node = (Node) getInternalNode(i);
65 // if (node != getRoot()) {
66 // node.heightParameter.setParameterValueQuietly(0, rootHeight);
67 // }
68 // }
69 // fixedInternalNodes = true;
70 // }
71
72 // public void setRootHeightParameter(Parameter p) {
73 // addVariable(p);
74 // rootHeightParameter = p;
75 // }
76
77 private void setupHeightBounds(Node node) {
78 node.heightParameter.addBounds(new StarTreeNodeHeightBounds(node.heightParameter));
79 }
80
81 protected void handleModelChangedEvent(Model model, Object object, int index) {
82 if (model == sharedRoot) {
83 if (object instanceof TreeChangedEvent) {
84 TreeChangedEvent event = (TreeChangedEvent) object;
85 if (event.getParameter() == sharedRoot.getRootHeightParameter()) {
86 pushTreeChangedEvent();
87 }
88 }
89 }
90 }
91
92 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
93 if (variable == rootHeightParameter) {
94 pushTreeChangedEvent();
95 } else {
96 final Node node = getNodeOfParameter((Parameter) variable);
97 if (node.isRoot()) {
98 pushTreeChangedEvent();
99 } else if (node.isExternal()) {
100 maxTipHeightKnown = false;
101 pushTreeChangedEvent();
102 } else {
103 throw new IllegalArgumentException("Can not sample internal nodes in StarTree");
104 }
105 super.handleVariableChangedEvent(variable, index, type);
106 }
107 }
108
109 public double getNodeHeight(final NodeRef nr) {
110 Node node = (Node) nr;
111 if (!node.isExternal()) {
112 if (rootHeightParameter != null) {
113 return rootHeightParameter.getParameterValue(0);
114 } else if (sharedRoot != null) {
115 return sharedRoot.getNodeHeight(sharedRoot.getRoot());
116 } else {
117 return ((Node) getRoot()).getHeight();
118 }
119 }
120 return node.getHeight();
121 }
122
123 public void setSharedRootHeightParameter(TreeModel sharedRoot) {
124 this.sharedRoot = sharedRoot;
125 addModel(sharedRoot);
126 }
127
128 private class StarTreeNodeHeightBounds implements Bounds<Double> {
129
130 public StarTreeNodeHeightBounds(Parameter parameter) {
131 nodeHeightParameter = parameter;
132 }
133
134 public Double getUpperLimit(int i) {
135
136 Node node = getNodeOfParameter(nodeHeightParameter);
137 if (node.isRoot()) {
138 return Double.POSITIVE_INFINITY;
139 } else {
140
141 return getNodeHeight(getRoot());
142 }
143 }
144
145 public Double getLowerLimit(int i) {
146
147 Node node = getNodeOfParameter(nodeHeightParameter);
148 if (node.isExternal()) {
149 return 0.0;
150 } else {
151 return getMaxTipHeight();
152 }
153 }
154
155 public int getBoundsDimension() {
156 return 1;
157 }
158
159 private Parameter nodeHeightParameter = null;
160 }
161
162 public void storeState() {
163 super.storeState();
164 savedMaxTipHeight = maxTipHeight;
165 savedMaxTipHeightKnown = maxTipHeightKnown;
166 }
167
168 public void restoreState() {
169 super.restoreState();
170 maxTipHeight = savedMaxTipHeight;
171 maxTipHeightKnown = savedMaxTipHeightKnown;
172 }
173
174 private double getMaxTipHeight() {
175 if (!maxTipHeightKnown) {
176 maxTipHeight = getNodeHeight(getExternalNode(0));
177 for (int i = 1; i < getExternalNodeCount(); ++i) {
178 double height = getNodeHeight(getExternalNode(i));
179 if (height > maxTipHeight) {
180 maxTipHeight = height;
181 }
182 }
183 maxTipHeightKnown = true;
184 }
185 return maxTipHeight;
186 }
187
188 private boolean maxTipHeightKnown = false;
189 private boolean savedMaxTipHeightKnown;
190 private double maxTipHeight = 5;
191 private double savedMaxTipHeight;
192
193 private Parameter rootHeightParameter = null;
194 private TreeModel sharedRoot = null;
195 }