Debian Codegraph @

src/dr/evomodel/branchratemodel/ScaledTreeLengthRateModel.java

raw symbols SCIP
1 /*
2 * ScaledTreeLengthRateModel.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.evomodelxml.branchratemodel.ScaledTreeLengthRateModelParser;
32 import dr.inference.model.Model;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35
36 /**
37 * Package: ScaledTreeLengthRateModel
38 * Description:
39 * <p/>
40 * <p/>
41 * Created by
42 * Alexander V. Alekseyenko ([email protected])
43 * Date: Mar 18, 2008
44 * Time: 3:58:43 PM
45 */
46 public class ScaledTreeLengthRateModel extends AbstractBranchRateModel {
47
48 private Parameter totalLength;
49 protected Tree treeModel;
50 private double storedRateFactor;
51 private boolean currentFactorKnown;
52 private double rateFactor;
53
54 public ScaledTreeLengthRateModel(TreeModel treeModel, Parameter totalLength) {
55 super(ScaledTreeLengthRateModelParser.MODEL_NAME);
56 this.totalLength = totalLength;
57 this.treeModel = treeModel;
58 currentFactorKnown = false;
59 addModel(treeModel);
60 addVariable(totalLength);
61 }
62
63 public double getBranchRate(final Tree tree, final NodeRef node) {
64 assert(tree == treeModel);
65
66 if (!currentFactorKnown) {
67 updateCurrentLength();
68 }
69 return rateFactor;
70 }
71
72 public double getTotalLength() {
73 return totalLength.getParameterValue(0);
74 }
75
76 protected void updateCurrentLength() {
77 double currentLength = 0;
78 NodeRef root = treeModel.getRoot();
79 for (int i = 0; i < treeModel.getNodeCount(); ++i) {
80 NodeRef node = treeModel.getNode(i);
81 if (node != root) {
82 currentLength += treeModel.getBranchLength(node);
83 }
84 }
85 rateFactor = totalLength.getParameterValue(0) / currentLength;
86 currentFactorKnown = true;
87 }
88
89 protected void handleModelChangedEvent(Model model, Object object, int index) {
90 if (model == treeModel) {
91 currentFactorKnown = false;
92 }
93 }
94
95 /**
96 * This method is called whenever a parameter is changed.
97 * <p/>
98 * It is strongly recommended that the model component sets a "dirty" flag and does no
99 * further calculations. Recalculation is typically done when the model component is asked for
100 * some information that requires them. This mechanism is 'lazy' so that this method
101 * can be safely called multiple times with minimal computational cost.
102 */
103 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
104 if (variable == totalLength) {
105 currentFactorKnown = false;
106 }
107 }
108
109 /**
110 * Additional state information, outside of the sub-model is stored by this call.
111 */
112 protected void storeState() {
113 storedRateFactor = rateFactor;
114 }
115
116 /**
117 * After this call the model is guaranteed to have returned its extra state information to
118 * the values coinciding with the last storeState call.
119 * Sub-models are handled automatically and do not need to be considered in this method.
120 */
121 protected void restoreState() {
122 rateFactor = storedRateFactor;
123 }
124
125 /**
126 * This call specifies that the current state is accept. Most models will not need to do anything.
127 * Sub-models are handled automatically and do not need to be considered in this method.
128 */
129 protected void acceptState() {
130 }
131
132 }