Debian Codegraph @

src/dr/evomodel/branchratemodel/ContinuousBranchRates.java

raw symbols SCIP
1 /*
2 * ContinuousBranchRates.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.evomodelxml.branchratemodel.ContinuousBranchRatesParser;
33 import dr.inference.distribution.ParametricDistributionModel;
34 import dr.inference.model.*;
35 import dr.math.MathUtils;
36
37 /**
38 *
39 * Uses the implementation of continuous branch rates described in:
40 * Li, W.L.S. and A.J. Drummond, Model Averaging and Bayes Factor
41 * Calculation of Relaxed Molecular Clocks in Bayesian Phylogenetics.
42 * Molecular Biology and Evolution, 2012. 29(2): p. 751-761.
43 *
44 * @author Wai Lok Sibon Li
45 * @version $Id: ContinuousBranchRates.java,v 1.11 2006/01/09 17:44:30 rambaut Exp $
46 */
47 public class ContinuousBranchRates extends AbstractBranchRateModel {
48
49 private final ParametricDistributionModel distributionModel;
50
51 // The rate quantiles of each branch
52 final TreeParameterModel rateCategoryQuantiles;
53
54 private final double[] rates;
55 private boolean normalize = false;
56 private double normalizeBranchRateTo = Double.NaN;
57 private double scaleFactor = 1.0;
58 private TreeModel treeModel;
59
60 private boolean updateScaleFactor = false;
61 private boolean updateRates = true;
62
63 public ContinuousBranchRates(
64 TreeModel tree,
65 Parameter rateCategoryQuantilesParameter,
66 ParametricDistributionModel model) {
67 this(tree, rateCategoryQuantilesParameter, model, false, Double.NaN);
68
69 }
70
71 public ContinuousBranchRates(
72 TreeModel tree,
73 Parameter rateCategoryQuantilesParameter,
74 ParametricDistributionModel model,
75 boolean normalize,
76 double normalizeBranchRateTo) {
77
78 super(ContinuousBranchRatesParser.CONTINUOUS_BRANCH_RATES);
79
80 this.rateCategoryQuantiles = new TreeParameterModel(tree, rateCategoryQuantilesParameter, false);
81
82 rates = new double[tree.getNodeCount()];
83
84 this.normalize = normalize;
85
86 this.treeModel = tree;
87 this.distributionModel = model;
88 this.normalizeBranchRateTo = normalizeBranchRateTo;
89
90 Parameter.DefaultBounds bound = new Parameter.DefaultBounds(1.0, 0.0, rateCategoryQuantilesParameter.getDimension());
91 rateCategoryQuantilesParameter.addBounds(bound);
92
93 randomizeRates();
94
95 addModel(model);
96 addModel(rateCategoryQuantiles);
97
98 if (normalize) {
99 // if we want to normalize the rates then we need to listen for changes on the tree
100 addModel(treeModel);
101 updateScaleFactor = true;
102 }
103
104 updateRates = true;
105 }
106
107 public void handleModelChangedEvent(Model model, Object object, int index) {
108 if (model == distributionModel) {
109 updateRates = true;
110 fireModelChanged();
111 } else if (model == rateCategoryQuantiles) {
112 updateRates = true;
113 fireModelChanged(null, index);
114 } else if (model == treeModel && normalize) {
115 updateScaleFactor = true;
116 }
117 }
118
119 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
120 }
121
122 protected void storeState() {
123 }
124
125 protected void restoreState() {
126 updateRates = true;
127 }
128
129 protected void acceptState() {
130 }
131
132 public double getBranchRate(final Tree tree, final NodeRef node) {
133
134 assert !tree.isRoot(node) : "root node doesn't have a rate!";
135
136 if (updateRates) {
137 computeRates();
138 }
139
140 if (updateScaleFactor) {
141 computeFactor();
142 }
143
144 return rates[node.getNumber()] * scaleFactor;
145 }
146
147 /**
148 * Calculates the actual rates corresponding to the quantiles.
149 */
150 private void randomizeRates() {
151
152 for (int i = 0; i < treeModel.getNodeCount(); i++) {
153 if (!treeModel.isRoot(treeModel.getNode(i))) {
154 double r = MathUtils.nextDouble();
155 rateCategoryQuantiles.setNodeValue(treeModel, treeModel.getNode(i), r);
156 }
157 }
158
159 updateRates = false;
160 }
161
162 /**
163 * Calculates the actual rates corresponding to the quantiles.
164 */
165 private void computeRates() {
166
167 for (int i = 0; i < treeModel.getNodeCount(); i++) {
168 if (!treeModel.isRoot(treeModel.getNode(i))) {
169 rates[treeModel.getNode(i).getNumber()] = distributionModel.quantile(rateCategoryQuantiles.getNodeValue(treeModel, treeModel.getNode(i)));
170 }
171 }
172
173 updateRates = false;
174 }
175
176 // compute scale factor
177 private void computeFactor() {
178
179 //scale mean rate to 1.0 or separate parameter
180
181 double treeRate = 0.0;
182 double treeTime = 0.0;
183
184 for (int i = 0; i < treeModel.getNodeCount(); i++) {
185 NodeRef node = treeModel.getNode(i);
186 if (!treeModel.isRoot(node)) {
187 treeTime += treeModel.getBranchLength(node);
188 }
189 }
190
191 scaleFactor = normalizeBranchRateTo / (treeRate / treeTime);
192
193 updateScaleFactor = false;
194 }
195
196 }