Debian Codegraph @

src/dr/oldevomodel/clock/UniversalClock.java

raw symbols SCIP
1 /*
2 * UniversalClock.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.oldevomodel.clock;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.branchratemodel.AbstractBranchRateModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 /**
36 * A class that calculates the rate of evolution based on of the mass and temperature
37 *
38 * @author Alexei Drummond
39 * @version $Id: UniversalClock.java,v 1.5 2005/02/21 15:16:09 rambaut Exp $
40 */
41
42 public class UniversalClock extends AbstractBranchRateModel {
43
44 public static final String UNIVERSAL_CLOCK = "universalClock";
45
46 /**
47 * creates a universal clock model
48 *
49 * @param rateParameter the rate vector of *all* nodes
50 * @param massParameter the mass vector of *all* nodes
51 * @param temperatureParameter the temperature vector of *all* nodes
52 * @param scaleParameter a single-dimensional scale parameter
53 */
54 public UniversalClock(
55 Parameter rateParameter,
56 Parameter massParameter,
57 Parameter temperatureParameter,
58 Parameter scaleParameter
59 ) {
60
61 super(UNIVERSAL_CLOCK);
62
63 this.rateParameter = rateParameter;
64 this.massParameter = massParameter;
65 this.temperatureParameter = temperatureParameter;
66 this.scaleParameter = scaleParameter;
67
68 // don't add rate parameter, cause that is what you are changing!
69 // you don't care if it changes
70 addVariable(massParameter);
71 addVariable(temperatureParameter);
72 addVariable(scaleParameter);
73
74 }
75
76 // *****************************************************************
77 // Interface Model
78 // *****************************************************************
79
80 protected void handleModelChangedEvent(Model model, Object object, int index) {
81 // no submodels so nothing to do
82 }
83
84 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
85
86 if ((variable == massParameter) || (variable == temperatureParameter)) {
87 if (index == -1) {
88 calculateAllRates();
89 } else calculateRate(index);
90 } else if (variable == scaleParameter) {
91 calculateAllRates();
92 } else {
93 throw new RuntimeException("unknown parameter changed in " + UNIVERSAL_CLOCK);
94 }
95 }
96
97 protected void storeState() {
98 } // no additional state needs storing
99
100 protected void restoreState() {
101 } // no additional state needs restoring
102
103 protected void acceptState() {
104 } // no additional state needs accepting
105
106 // **************************************************************
107 // Private methods
108 // **************************************************************
109
110 private void calculateAllRates() {
111 int numNodes = massParameter.getDimension();
112 for (int i = 0; i < numNodes; i++) {
113 calculateRate(i);
114 }
115 }
116
117 private void calculateRate(int index) {
118
119 double mass = massParameter.getParameterValue(index);
120 double temperature = temperatureParameter.getParameterValue(index);
121
122 double scale = scaleParameter.getParameterValue(0);
123
124 // replace this with the real equation!!
125 double substitutionRate = scale * Math.pow(mass, -0.25) * Math.exp(temperature);
126
127 rateParameter.setParameterValue(index, substitutionRate);
128 }
129
130 public double getBranchRate(Tree tree, NodeRef node) {
131 throw new RuntimeException("Look at code before running this class!");
132 }
133
134 Parameter rateParameter = null;
135 Parameter massParameter = null;
136 Parameter temperatureParameter = null;
137 Parameter scaleParameter = null;
138
139 }
140