Debian Codegraph @

src/dr/evomodel/branchratemodel/RandomLocalClockModel.java

raw symbols SCIP
1 /*
2 * RandomLocalClockModel.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.branchratemodel;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.tree.TreeModel;
31 import dr.evomodel.tree.TreeParameterModel;
32 import dr.evomodel.tree.randomlocalmodel.RandomLocalTreeVariable;
33 import dr.evomodelxml.branchratemodel.RandomLocalClockModelParser;
34 import dr.inference.model.Model;
35 import dr.inference.model.Parameter;
36 import dr.inference.model.Variable;
37 import dr.util.Author;
38 import dr.util.Citable;
39 import dr.util.Citation;
40
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.logging.Logger;
44
45 /**
46 * A model of rates evolving along a tree, such that at each node the rate may change or not depending on an
47 * indicator which chooses whether the parent rate is inherited or a new rate begins on the branch above the node.
48 * This model is implemented using stochastic variable selection.
49 *
50 * @author Alexei Drummond
51 * @author Andrew Rambaut
52 * @author Marc A. Suchard
53 * @version $Id: DiscretizedBranchRates.java,v 1.11 2006/01/09 17:44:30 rambaut Exp $
54 */
55 public class RandomLocalClockModel extends AbstractBranchRateModel
56 implements RandomLocalTreeVariable, Citable {
57
58 public RandomLocalClockModel(TreeModel treeModel,
59 Parameter meanRateParameter,
60 Parameter rateIndicatorParameter,
61 Parameter ratesParameter,
62 boolean ratesAreMultipliers,
63 double threshold) {
64
65 super(RandomLocalClockModelParser.LOCAL_BRANCH_RATES);
66
67 this.ratesAreMultipliers = ratesAreMultipliers;
68
69 indicators = new TreeParameterModel(treeModel, rateIndicatorParameter, false);
70 rates = new TreeParameterModel(treeModel, ratesParameter, false);
71
72 if (Double.isNaN(threshold)) { // NaN == binary values
73 rateIndicatorParameter.addBounds(new Parameter.DefaultBounds(1, 0, rateIndicatorParameter.getDimension()));
74 this.threshold = 0.5;
75 for (int i = 0; i < rateIndicatorParameter.getDimension(); ++i) {
76 rateIndicatorParameter.setParameterValue(i, 0.0);
77 }
78 } else {
79 rateIndicatorParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, -Double.MAX_VALUE, rateIndicatorParameter.getDimension()));
80 this.threshold = threshold;
81 }
82 ratesParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0, ratesParameter.getDimension()));
83
84 for (int i = 0; i < rateIndicatorParameter.getDimension(); i++) {
85 ratesParameter.setParameterValue(i, 1.0);
86 }
87
88 this.meanRateParameter = meanRateParameter;
89
90 addModel(treeModel);
91 this.treeModel = treeModel;
92
93 addModel(indicators);
94 addModel(rates);
95 if (meanRateParameter != null) addVariable(meanRateParameter);
96
97 unscaledBranchRates = new double[treeModel.getNodeCount()];
98
99 Logger.getLogger("dr.evomodel").info(" indicator parameter name is '" + rateIndicatorParameter.getId() + "' with threshold = " + threshold);
100
101 recalculateScaleFactor();
102 }
103
104 /**
105 * @param tree the tree
106 * @param node the node to retrieve the variable of
107 * @return the raw real-valued variable at this node
108 */
109 public final double getVariable(Tree tree, NodeRef node) {
110 return rates.getNodeValue(tree, node);
111 }
112
113 /**
114 * @param tree the tree
115 * @param node the node
116 * @return true of the variable at this node is included in function, thus representing a change in the
117 * function looking down the tree.
118 */
119 public final boolean isVariableSelected(Tree tree, NodeRef node) {
120 return indicators.getNodeValue(tree, node) > threshold;
121 }
122
123 public void handleModelChangedEvent(Model model, Object object, int index) {
124 recalculationNeeded = true;
125 fireModelChanged();
126 }
127
128 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
129 recalculationNeeded = true;
130 fireModelChanged();
131 }
132
133 protected void storeState() {
134 }
135
136 protected void restoreState() {
137 recalculationNeeded = true;
138 // recalculateScaleFactor(); // Careful about change here; seems fine ... but who knows without unit-tests
139 }
140
141 protected void acceptState() {
142 }
143
144 public double getBranchRate(final Tree tree, final NodeRef node) {
145 if (recalculationNeeded) {
146 recalculateScaleFactor();
147 recalculationNeeded = false;
148 }
149 return unscaledBranchRates[node.getNumber()] * scaleFactor;
150 }
151
152 private void calculateUnscaledBranchRates(TreeModel tree) {
153 cubr(tree, tree.getRoot(), 1.0);
154 }
155
156 /**
157 * This is a recursive function that does the work of
158 * calculating the unscaled branch rates across the tree
159 * taking into account the indicator variables.
160 *
161 * @param tree the tree
162 * @param node the node
163 * @param rate the rate of the parent node
164 */
165 private void cubr(TreeModel tree, NodeRef node, double rate) {
166
167 int nodeNumber = node.getNumber();
168
169 if (!tree.isRoot(node)) {
170 if (isVariableSelected(tree, node)) {
171 if (ratesAreMultipliers) {
172 rate *= getVariable(tree, node);
173 } else {
174 rate = getVariable(tree, node);
175 }
176 }
177 }
178 unscaledBranchRates[nodeNumber] = rate;
179
180 int childCount = tree.getChildCount(node);
181 for (int i = 0; i < childCount; i++) {
182 cubr(tree, tree.getChild(node, i), rate);
183 }
184 }
185
186 private void recalculateScaleFactor() {
187
188 calculateUnscaledBranchRates(treeModel);
189
190 double timeTotal = 0.0;
191 double branchTotal = 0.0;
192
193 for (int i = 0; i < treeModel.getNodeCount(); i++) {
194 NodeRef node = treeModel.getNode(i);
195 if (!treeModel.isRoot(node)) {
196
197 double branchInTime =
198 treeModel.getNodeHeight(treeModel.getParent(node)) -
199 treeModel.getNodeHeight(node);
200
201 double branchLength = branchInTime * unscaledBranchRates[node.getNumber()];
202
203 timeTotal += branchInTime;
204 branchTotal += branchLength;
205 }
206 }
207
208 scaleFactor = timeTotal / branchTotal;
209
210 if (meanRateParameter != null)
211 scaleFactor *= meanRateParameter.getParameterValue(0);
212 }
213
214 // AR - as TreeParameterModels are now loggable, the indicator parameter should be logged
215 // directly.
216 // private static String[] attributeLabel = {"changed"};
217 //
218 // public String[] getNodeAttributeLabel() {
219 // return attributeLabel;
220 // }
221 //
222 // public String[] getAttributeForNode(Tree tree, NodeRef node) {
223 //
224 // if (tree.isRoot(node)) {
225 // return new String[]{"false"};
226 // }
227 //
228 // return new String[]{(isVariableSelected((TreeModel) tree, node) ? "true" : "false")};
229 // }
230
231 // the scale factor necessary to maintain the mean rate
232 private double scaleFactor;
233
234 // the tree model
235 private TreeModel treeModel;
236
237 // true if the rate variables are treated as relative
238 // to the parent rate rather than absolute rates
239 private boolean ratesAreMultipliers = false;
240
241 // the unscaled rates of each branch, taking into account the indicators
242 private double[] unscaledBranchRates;
243
244 // the mean rate across all the tree, if null then mean rate is scaled to 1.0
245 private Parameter meanRateParameter;
246
247 private TreeParameterModel indicators;
248 private TreeParameterModel rates;
249
250 boolean recalculationNeeded = true;
251
252 private final double threshold;
253
254 @Override
255 public Citation.Category getCategory() {
256 return Citation.Category.MOLECULAR_CLOCK;
257 }
258
259 @Override
260 public String getDescription() {
261 return "Local clock model";
262 }
263
264 @Override
265 public List<Citation> getCitations() {
266 return Collections.singletonList(CITATION);
267 }
268
269 public static Citation CITATION = new Citation(
270 new Author[]{
271 new Author("AJ", "Drummond"),
272 new Author("MA", "Suchard")
273 },
274 "Bayesian random local clocks, or one rate to rule them all",
275 2010,
276 "BMC Biology",
277 "8: 114",
278 "10.1186/1741-7007-8-114"
279 );
280 }