Debian Codegraph @

src/dr/evomodel/branchmodel/EpochBranchModel.java

raw symbols SCIP
1 /*
2 * EpochBranchModel.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.branchmodel;
27
28 import dr.evomodel.substmodel.FrequencyModel;
29 import dr.evomodel.substmodel.SubstitutionModel;
30 import dr.evolution.tree.NodeRef;
31 import dr.evomodel.tree.TreeModel;
32 import dr.inference.model.AbstractModel;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36 import dr.util.Author;
37 import dr.util.Citable;
38 import dr.util.Citation;
39
40 import java.util.*;
41
42 /**
43 * @author Filip Bielejec
44 * @author Andrew Rambaut
45 * @author Marc A. Suchard
46 * @version $Id$
47 */
48 @SuppressWarnings("serial")
49 public class EpochBranchModel extends AbstractModel implements BranchModel, Citable {
50
51 public static final String EPOCH_BRANCH_MODEL = "EpochBranchModel";
52
53
54 public EpochBranchModel(TreeModel tree,
55 List<SubstitutionModel> substitutionModels,
56 Parameter epochTimes) {
57
58 super(EPOCH_BRANCH_MODEL);
59
60 this.substitutionModels = substitutionModels;
61 if (substitutionModels == null || substitutionModels.size() == 0) {
62 throw new IllegalArgumentException("EpochBranchModel must be provided with at least one substitution model");
63 }
64
65 this.epochTimes = epochTimes;
66 this.tree = tree;
67
68 for (SubstitutionModel model : substitutionModels) {
69 addModel(model);
70 }
71
72 addModel(tree);
73 addVariable(epochTimes);
74 }// END: Constructor
75
76 @Override
77 public Mapping getBranchModelMapping(NodeRef node) {
78
79 int nModels = substitutionModels.size();
80 int epochCount = nModels - 1;
81
82 double[] transitionTimes = epochTimes.getParameterValues();
83
84 double parentHeight = tree.getNodeHeight(tree.getParent(node));
85 double nodeHeight = tree.getNodeHeight(node);
86
87 List<Double> weightList = new ArrayList<Double>();
88 List<Integer> orderList = new ArrayList<Integer>();
89
90 // find the epoch that the node height is in...
91 int epoch = 0;
92 while (epoch < epochCount && nodeHeight >= transitionTimes[epoch]) {
93 epoch ++;
94 }
95
96 double currentHeight = nodeHeight;
97
98 // find the epoch that the parent height is in...
99 while (epoch < epochCount && parentHeight >= transitionTimes[epoch]) {
100 weightList.add( transitionTimes[epoch] - currentHeight );
101 orderList.add(epoch);
102
103 currentHeight = transitionTimes[epoch];
104
105 epoch ++;
106 }
107
108 weightList.add( parentHeight - currentHeight );
109 orderList.add(epoch);
110
111
112 if (orderList.size() == 0) {
113 throw new RuntimeException("EpochBranchModel failed to give a valid mapping");
114 }
115
116 final int[] order = new int[orderList.size()];
117 final double[] weights = new double[weightList.size()];
118 for (int i = 0; i < orderList.size(); i++) {
119 order[i] = orderList.get(i);
120 weights[i] = weightList.get(i);
121 }
122
123 return new Mapping() {
124 @Override
125 public int[] getOrder() {
126 return order;
127 }
128
129 @Override
130 public double[] getWeights() {
131 return weights;
132 }
133 };
134 }// END: getBranchModelMapping
135
136 @Override
137 public boolean requiresMatrixConvolution() {
138 return true;
139 }
140
141 @Override
142 public List<SubstitutionModel> getSubstitutionModels() {
143 return substitutionModels;
144 }
145
146 @Override
147 public SubstitutionModel getRootSubstitutionModel() {
148 return substitutionModels.get(substitutionModels.size() - 1);
149 }
150
151 public FrequencyModel getRootFrequencyModel() {
152 return getRootSubstitutionModel().getFrequencyModel();
153 }
154
155 protected void handleModelChangedEvent(Model model, Object object, int index) {
156 fireModelChanged();
157 }// END: handleModelChangedEvent
158
159 @SuppressWarnings("rawtypes")
160 protected void handleVariableChangedEvent(Variable variable, int index,
161 Parameter.ChangeType type) {
162 }// END: handleVariableChangedEvent
163
164 protected void storeState() {
165 }// END: storeState
166
167 protected void restoreState() {
168 }// END: restoreState
169
170 protected void acceptState() {
171 }// END: acceptState
172
173 @Override
174 public Citation.Category getCategory() {
175 return Citation.Category.SUBSTITUTION_MODELS;
176 }
177
178 @Override
179 public String getDescription() {
180 return "Epoch Branch model";
181 }
182
183 public List<Citation> getCitations() {
184 return Arrays.asList(
185 new Citation(new Author[]{new Author("F", "Bielejec"),
186 new Author("P", "Lemey"), new Author("G", "Baele"), new Author("A", "Rambaut"),
187 new Author("MA", "Suchard")}, Citation.Status.IN_PREPARATION));
188 }// END: getCitations
189
190 private final TreeModel tree;
191 private final List<SubstitutionModel> substitutionModels;
192 private final Parameter epochTimes;
193 }// END: class