Debian Codegraph @

src/dr/evomodel/branchratemodel/RelaxedDriftModel.java

raw symbols SCIP
1 /*
2 * RelaxedDriftModel.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.RelaxedDriftModelParser;
34 import dr.inference.model.Model;
35 import dr.inference.model.Parameter;
36 import dr.inference.model.Variable;
37
38 /**
39 * Created by IntelliJ IDEA.
40 * User: mandevgill
41 * Date: 7/25/14
42 * Time: 4:27 PM
43 * To change this template use File | Settings | File Templates.
44 */
45
46
47 public class RelaxedDriftModel extends AbstractBranchRateModel
48 implements RandomLocalTreeVariable {
49
50 public RelaxedDriftModel(TreeModel treeModel,
51 Parameter rateIndicatorParameter,
52 Parameter ratesParameter,
53 Parameter driftRates,
54 ArbitraryBranchRates branchChanges) {
55
56 super(RelaxedDriftModelParser.RELAXED_DRIFT);
57
58 rates = new TreeParameterModel(treeModel, ratesParameter, true);
59 ratesParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, -Double.MAX_VALUE, ratesParameter.getDimension()));
60 indicators = new TreeParameterModel(treeModel, rateIndicatorParameter, true);
61 rateIndicatorParameter.addBounds(new Parameter.DefaultBounds(1, -1, rateIndicatorParameter.getDimension()));
62
63 for (int i = 0; i < rateIndicatorParameter.getDimension(); i++) {
64 rateIndicatorParameter.setParameterValue(i, 0.0);
65 }
66 for (int i = 0; i < ratesParameter.getDimension(); i++) {
67 ratesParameter.setParameterValue(i, 0.0);
68 }
69
70 addModel(treeModel);
71 this.treeModel = treeModel;
72 addModel(indicators);
73 addModel(rates);
74
75 if (driftRates != null) {
76 this.driftRates = driftRates;
77 driftRates.setDimension(ratesParameter.getDimension());
78 }
79 if (branchChanges != null){
80 this.branchChanges = branchChanges;
81 }
82
83 branchRates = new double[treeModel.getNodeCount()];
84
85 // Logger.getLogger("dr.evomodel").info(" indicator parameter name is '" + rateIndicatorParameter.getId() + "'");
86
87 calculateBranchRates(treeModel);
88 //recalculateScaleFactor();
89 }
90
91
92 /**
93 * @param tree the tree
94 * @param node the node to retrieve the variable of
95 * @return the raw real-valued variable at this node
96 */
97 public final double getVariable(Tree tree, NodeRef node) {
98 return rates.getNodeValue(tree, node);
99 }
100
101 /**
102 * @param tree the tree
103 * @param node the node
104 * @return true of the variable at this node is included in function, thus representing a change in the
105 * function looking down the tree.
106 */
107 public final boolean isVariableSelected(Tree tree, NodeRef node) {
108 return indicators.getNodeValue(tree, node) != 0;
109 }
110
111 public void handleModelChangedEvent(Model model, Object object, int index) {
112 recalculationNeeded = true;
113 fireModelChanged();
114 }
115
116 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
117 recalculationNeeded = true;
118 fireModelChanged();
119 }
120
121 protected void storeState() {
122 }
123
124 protected void restoreState() {
125 calculateBranchRates(treeModel);
126 // recalculateScaleFactor();
127 }
128
129 protected void acceptState() {
130 }
131
132 public double getBranchRate(final Tree tree, final NodeRef node) {
133 if (recalculationNeeded) {
134 calculateBranchRates(treeModel);
135 // recalculateScaleFactor();
136 recalculationNeeded = false;
137 }
138 return branchRates[node.getNumber()];
139 }
140
141 private void calculateBranchRates(TreeModel tree) {
142 branchRates[tree.getRoot().getNumber()] = getVariable(tree, tree.getRoot());
143 if (driftRates != null) {
144 driftRates.setParameterValue(tree.getRoot().getNumber(), getVariable(tree, tree.getRoot()));
145 }
146
147 cbr(tree, tree.getRoot(), branchRates[tree.getRoot().getNumber()]);
148 }
149
150 /**
151 * This is a recursive function that does the work of
152 * calculating the unscaled branch rates across the tree
153 * taking into account the indicator variables.
154 *
155 * @param tree the tree
156 * @param node the node
157 * @param rate the rate of the parent node
158 */
159 private void cbr(TreeModel tree, NodeRef node, double rate) {
160
161 NodeRef childNode0 = tree.getChild(node, 0);
162 NodeRef childNode1 = tree.getChild(node, 1);
163 int nodeNumber0 = childNode0.getNumber();
164 int nodeNumber1 = childNode1.getNumber();
165 double nodeIndicator = indicators.getNodeValue(tree, node);
166 if (indicators.getNodeValue(tree, node) != 0) {
167 // System.err.println("nodeIndicator: " + nodeIndicator);
168 }
169 if (nodeIndicator < 0) {
170 // System.err.println("child0 change");
171 branchRates[nodeNumber0] = rate + getVariable(tree, childNode0);
172 // branchRates[nodeNumber0] = getVariable(tree, childNode0);
173 branchRates[nodeNumber1] = rate;
174
175 if (driftRates != null) {
176 driftRates.setParameterValue(nodeNumber0, rate + getVariable(tree, childNode0));
177 driftRates.setParameterValue(nodeNumber1, rate);
178 }
179 if (branchChanges != null){
180 branchChanges.setBranchRate(tree, childNode0, 1.0);
181 branchChanges.setBranchRate(tree, childNode1, 0.0);
182 }
183
184 } else if (nodeIndicator > 0) {
185 // System.err.println("child1 change");
186 branchRates[nodeNumber0] = rate;
187 branchRates[nodeNumber1] = rate + getVariable(tree, childNode1);
188 // branchRates[nodeNumber1] = getVariable(tree, childNode1);
189
190 if (driftRates != null) {
191 driftRates.setParameterValue(nodeNumber0, rate);
192 driftRates.setParameterValue(nodeNumber1, rate + getVariable(tree, childNode1));
193 }
194 if (branchChanges != null){
195 branchChanges.setBranchRate(tree, childNode0, 0.0);
196 branchChanges.setBranchRate(tree, childNode1, 1.0);
197 }
198
199 } else {
200 // System.err.println("NO CHANGES!!!");
201 branchRates[nodeNumber0] = rate;
202 branchRates[nodeNumber1] = rate;
203
204 if (driftRates != null) {
205 driftRates.setParameterValue(nodeNumber0, rate);
206 driftRates.setParameterValue(nodeNumber1, rate);
207 }
208 if (branchChanges != null){
209 branchChanges.setBranchRate(tree, childNode0, 0.0);
210 branchChanges.setBranchRate(tree, childNode1, 0.0);
211 }
212 }
213
214 if (tree.getChildCount(childNode0) > 0) {
215 cbr(tree, childNode0, branchRates[nodeNumber0]);
216 }
217 if (tree.getChildCount(childNode1) > 0) {
218 cbr(tree, childNode1, branchRates[nodeNumber1]);
219 }
220
221
222 /*
223 if (indicators.getNodeValue(tree, childNode0) > 0.5 && indicators.getNodeValue(tree, childNode1) < 0.5) {
224 branchRates[nodeNumber0] = rate + getVariable(tree, childNode0);
225 // branchRates[nodeNumber0] = getVariable(tree, childNode0);
226 branchRates[nodeNumber1] = rate;
227 } else if (indicators.getNodeValue(tree, childNode0) < 0.5 && indicators.getNodeValue(tree, childNode1) > 0.5) {
228 branchRates[nodeNumber0] = rate;
229 branchRates[nodeNumber1] = rate + getVariable(tree, childNode1);
230 // branchRates[nodeNumber1] = getVariable(tree, childNode1);
231 } else {
232 branchRates[nodeNumber0] = rate;
233 branchRates[nodeNumber1] = rate;
234 }
235
236 if (tree.getChildCount(childNode0) > 0) {
237 cbr(tree, childNode0, branchRates[nodeNumber0]);
238 }
239 if (tree.getChildCount(childNode1) > 0) {
240 cbr(tree, childNode1, branchRates[nodeNumber1]);
241 }
242 */
243 }
244
245
246 // the tree model
247 private TreeModel treeModel;
248
249 // the unscaled rates of each branch, taking into account the indicators
250 private double[] branchRates;
251
252 private TreeParameterModel indicators;
253 private TreeParameterModel rates;
254 private Parameter driftRates;
255 private ArbitraryBranchRates branchChanges;
256
257 boolean recalculationNeeded = true;
258 }