Debian Codegraph @

src/dr/evomodel/tree/AbstractTreeParameterModel.java

raw symbols SCIP
1 /*
2 * AbstractTreeParameterModel.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.evolution.tree.TreeTrait;
31 import dr.inference.model.AbstractModel;
32 import dr.inference.model.Model;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35
36 /**
37 * This class maintains a parameter of length equal to the number of nodes in the tree.
38 * It can optionally include the root node. If the root node is not included then this
39 * class will track tree changes that change the root node number and swap the parameter
40 * values so that the parameter values stay with their node when changes to the tree
41 * topology occur.
42 *
43 * @author Alexei Drummond
44 * @author Marc A. Suchard
45 */
46 public abstract class AbstractTreeParameterModel<T> extends AbstractModel implements TreeTrait<T> {
47
48 protected final TreeModel tree;
49
50 // The tree parameter;
51 protected final Parameter parameter;
52
53 // the index of the root node.
54 private int rootNodeNumber;
55 private int storedRootNodeNumber;
56
57 private boolean includeRoot = false;
58 private boolean includeTips = true;
59
60 private final Intent intent;
61
62 /**
63 * This class constructs a tree parameter, and will set the dimension of the parameter
64 * to match the appropriate number of nodes if necessary.
65 *
66 * @param tree the tree that this parameter corresponds to
67 * @param parameter the parameter to keep in sync with tree topology moves.
68 * @param includeRoot true if the parameter includes values associated with the root node.
69 * @param includeTips true if the parameter includes values associated with the tip nodes.
70 */
71 public AbstractTreeParameterModel(TreeModel tree, Parameter parameter, boolean includeRoot, boolean includeTips) {
72 this(tree, parameter, includeRoot, includeTips, Intent.BRANCH);
73 }
74
75 public AbstractTreeParameterModel(TreeModel tree, Parameter parameter, boolean includeRoot, boolean includeTips,
76 Intent intent) {
77
78 super("treeParameterModel");
79 this.tree = tree;
80 this.parameter = parameter;
81
82 this.includeRoot = includeRoot;
83 this.includeTips = includeTips;
84
85 this.intent = intent;
86
87 int dim = parameter.getDimension();
88 int treeSize = getParameterSize();
89 if (dim != treeSize) {
90 parameter.setDimension(treeSize);
91 }
92
93 addModel(tree);
94 addVariable(parameter);
95
96 rootNodeNumber = tree.getRoot().getNumber();
97 storedRootNodeNumber = rootNodeNumber;
98 }
99
100 public abstract int getParameterSize();
101
102 protected Parameter getParameter() {
103 return parameter;
104 }
105
106 protected boolean checkNode(Tree tree, NodeRef node) {
107 assert (!tree.isRoot(node) && !doesIncludeRoot()) : "root node doesn't have a parameter value!";
108
109 assert tree.getRoot().getNumber() == rootNodeNumber :
110 "INTERNAL ERROR! node with number " + rootNodeNumber + " should be the root node.";
111
112 assert (!tree.isExternal(node) && !includeTips) : "tip nodes do not have parameter values!";
113
114 return true;
115 }
116
117 public void handleModelChangedEvent(Model model, Object object, int index) {
118 if (model == tree) {
119 handleRootMove();
120 }
121 }
122
123 protected boolean doesIncludeRoot() {
124 return includeRoot;
125 }
126
127 protected boolean doesIncludeTips() {
128 return includeTips;
129 }
130
131 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
132 int nodeNumber = getNodeNumberFromParameterIndex(index);
133
134 assert (tree.getNode(nodeNumber).getNumber() == nodeNumber);
135
136 fireModelChanged(variable, nodeNumber);
137 }
138
139 protected void storeState() {
140 storedRootNodeNumber = rootNodeNumber;
141 }
142
143 protected void restoreState() {
144 rootNodeNumber = storedRootNodeNumber;
145 }
146
147 protected void acceptState() {
148 }
149
150 public abstract T getNodeValue(Tree tree, NodeRef node);
151
152 public abstract void setNodeValue(Tree tree, NodeRef node, T value);
153
154 protected int getNodeNumberFromParameterIndex(int parameterIndex) {
155 int number = parameterIndex;
156 if (!includeRoot && parameterIndex >= tree.getRoot().getNumber()) {
157 number += 1;
158 }
159 if (!includeTips) {
160 number += tree.getExternalNodeCount();
161 }
162 return number;
163 }
164
165 protected int getParameterIndexFromNodeNumber(int nodeNumber) {
166 int index = nodeNumber;
167 if (!includeRoot && nodeNumber > tree.getRoot().getNumber()) {
168 index -= 1;
169 }
170
171 if (!includeTips) {
172 index -= tree.getExternalNodeCount();
173 }
174 return index;
175
176 }
177
178 private void handleRootMove() {
179
180 if (!includeRoot) {
181
182 final int newRootNodeNumber = tree.getRoot().getNumber();
183
184 if (rootNodeNumber > newRootNodeNumber) {
185
186 final double oldValue = parameter.getParameterValue(newRootNodeNumber);
187
188 final int end = Math.min(parameter.getDimension() - 1, rootNodeNumber);
189 for (int i = newRootNodeNumber; i < end; i++) {
190 parameter.setParameterValue(i, parameter.getParameterValue(i + 1));
191 }
192
193 parameter.setParameterValue(end, oldValue);
194
195 } else if (rootNodeNumber < newRootNodeNumber) {
196
197 final int end = Math.min(parameter.getDimension() - 1, newRootNodeNumber);
198
199 final double oldValue = parameter.getParameterValue(end);
200
201 for (int i = end; i > rootNodeNumber; i--) {
202 parameter.setParameterValue(i, parameter.getParameterValue(i - 1));
203 }
204
205 parameter.setParameterValue(rootNodeNumber, oldValue);
206 }
207 rootNodeNumber = newRootNodeNumber;
208 }
209 }
210
211 /**
212 * @return the tree model that this parameter is synchronized with
213 */
214 public TreeModel getTreeModel() {
215 return tree;
216 }
217
218 public String getTraitName() {
219 return parameter.getId();
220 }
221
222 public Intent getIntent() {
223 return intent;
224 }
225
226 public Class getTraitClass() {
227 return Double.class;
228 }
229
230 public boolean getLoggable() {
231 return true;
232 }
233
234 public abstract T getTrait(Tree tree, NodeRef node);
235
236 public abstract String getTraitString(Tree tree, NodeRef node);
237
238 }