Debian Codegraph @

src/dr/evomodel/branchratemodel/RateEpochBranchRateModel.java

raw symbols SCIP
1 /*
2 * RateEpochBranchRateModel.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.evomodelxml.branchratemodel.RateEpochBranchRateModelParser;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 /**
36 * Implements a model where time is broken into 'epochs' each with a different but
37 * constant rate. Parameters can be used to sample transition times but it is up
38 * to the user to keep them bounded and in strict order...
39 *
40 * @author Andrew Rambaut
41 * @version $Id$
42 */
43 public class RateEpochBranchRateModel extends AbstractBranchRateModel {
44
45 protected final Parameter[] timeParameters;
46 protected final Parameter[] rateParameters;
47
48 /**
49 * The constructor. For an N-epoch model, there should be N rate paramters and N-1 transition times.
50 *
51 * @param timeParameters an array of transition time parameters
52 * @param rateParameters an array of rate parameters
53 */
54 public RateEpochBranchRateModel(Parameter[] timeParameters,
55 Parameter[] rateParameters) {
56
57 super(RateEpochBranchRateModelParser.RATE_EPOCH_BRANCH_RATES);
58
59 this.timeParameters = timeParameters;
60 for (Parameter parameter : timeParameters) {
61 addVariable(parameter);
62 }
63
64 this.rateParameters = rateParameters;
65 for (Parameter parameter : rateParameters) {
66 addVariable(parameter);
67 }
68 }
69
70 public void handleModelChangedEvent(Model model, Object object, int index) {
71 // nothing to do
72 }
73
74 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
75 fireModelChanged();
76 }
77
78 protected void storeState() {
79 // nothing to do
80 }
81
82 protected void restoreState() {
83 // nothing to do
84 }
85
86 protected void acceptState() {
87 // nothing to do
88 }
89
90 public double getBranchRate(final Tree tree, final NodeRef node) {
91
92 NodeRef parent = tree.getParent(node);
93
94 if (parent != null) {
95 double height0 = tree.getNodeHeight(node);
96 double height1 = tree.getNodeHeight(parent);
97 int i = 0;
98
99 double rate = 0.0;
100 double lastHeight = height0;
101
102 // First find the epoch which contains the node height
103 while (i < timeParameters.length && height0 > timeParameters[i].getParameterValue(0)) {
104 i++;
105 }
106
107 // Now walk up the branch until we reach the last epoch or the height of the parent
108 while (i < timeParameters.length && height1 > timeParameters[i].getParameterValue(0)) {
109 // add the rate for that epoch multiplied by the time spent at that rate
110 rate += rateParameters[i].getParameterValue(0) * (timeParameters[i].getParameterValue(0) - lastHeight);
111 lastHeight = timeParameters[i].getParameterValue(0);
112 i++;
113 }
114
115 // Add that last rate segment
116 rate += rateParameters[i].getParameterValue(0) * (height1 - lastHeight);
117
118 // normalize the rate for the branch length
119 return normalizeRate(rate / (height1 - height0));
120 }
121 throw new IllegalArgumentException("root node doesn't have a rate!");
122 }
123
124 protected double normalizeRate(double rate) {
125 return rate;
126 }
127
128 }