Debian Codegraph @

src/dr/evomodel/branchratemodel/CountableMixtureBranchRates.java

raw symbols SCIP
1 /*
2 * CountableMixtureBranchRates.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.evolution.tree.TreeTrait;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodelxml.branchratemodel.CountableMixtureBranchRatesParser;
33 import dr.inference.loggers.LogColumn;
34 import dr.inference.loggers.Loggable;
35 import dr.inference.loggers.NumberColumn;
36 import dr.inference.model.Model;
37 import dr.inference.model.Parameter;
38 import dr.inference.model.Variable;
39
40 import java.util.List;
41
42 /**
43 * @author Marc Suchard
44 * @author Philippe Lemey
45 */
46 public class CountableMixtureBranchRates extends AbstractBranchRateModel implements Loggable {
47
48 private final Parameter ratesParameter;
49 private final TreeModel treeModel;
50 private final List<AbstractBranchRateModel> randomEffectsModels;
51 private final int categoryCount;
52 private final Parameter timeCoefficient;
53
54 public CountableMixtureBranchRates(CountableBranchCategoryProvider rateCategories,
55 TreeModel treeModel, Parameter ratesParameter, Parameter timeCoefficient,
56 List<AbstractBranchRateModel> randomEffects, boolean inLogSpace) {
57 super(CountableMixtureBranchRatesParser.COUNTABLE_CLOCK_BRANCH_RATES);
58
59 this.treeModel = treeModel;
60 categoryCount = ratesParameter.getDimension();
61 this.rateCategories = rateCategories;
62 rateCategories.setCategoryCount(categoryCount);
63
64 if (rateCategories instanceof Model) {
65 addModel((Model)rateCategories);
66 }
67 this.ratesParameter = ratesParameter;
68 addVariable(ratesParameter);
69
70 this.timeCoefficient = timeCoefficient;
71 if (timeCoefficient!=null){
72 addVariable(timeCoefficient);
73 }
74
75 // Handle random effects
76 this.randomEffectsModels = randomEffects;
77 if (randomEffectsModels != null) {
78 for (AbstractBranchRateModel model : randomEffectsModels)
79 addModel(model);
80 }
81 // TODO Check that randomEffectsModel means are zero
82
83 modelInLogSpace = inLogSpace;
84
85 helper.addTrait(this);
86 helper.addTrait(new TreeTrait.I() {
87
88 @Override
89 public String getTraitName() {
90 return getCategoryTraitName();
91 }
92
93 @Override
94 public Intent getIntent() {
95 return Intent.BRANCH;
96 }
97
98 @Override
99 public Integer getTrait(Tree tree, NodeRef node) {
100 return getBranchCategory(tree, node);
101 }
102 });
103 helper.addTrait(new TreeTrait.D() {
104
105 @Override
106 public String getTraitName() {
107 return getCategoryEffectTraitName();
108 }
109
110 @Override
111 public Intent getIntent() {
112 return Intent.BRANCH;
113 }
114
115 @Override
116 public Double getTrait(Tree tree, NodeRef node) {
117 return getBranchCategoryEffect(tree, node);
118 }
119 });
120 helper.addTrait(new TreeTrait.D() {
121
122 @Override
123 public String getTraitName() {
124 return getCategoryRateTraitName();
125 }
126
127 @Override
128 public Intent getIntent() {
129 return Intent.BRANCH;
130 }
131
132 @Override
133 public Double getTrait(Tree tree, NodeRef node) {
134 return getBranchCategoryRate(tree, node);
135 }
136 });
137 helper.addTrait(new TreeTrait.D() {
138
139 @Override
140 public String getTraitName() {
141 return getRandomEffectTraitName();
142 }
143
144 @Override
145 public Intent getIntent() {
146 return Intent.BRANCH;
147 }
148
149 @Override
150 public Double getTrait(Tree tree, NodeRef node) {
151 return getBranchRandomEffect(tree, node);
152 }
153 });
154 helper.addTrait(new TreeTrait.D() {
155
156 @Override
157 public String getTraitName() {
158 return getBranchTimeEffectTraitName();
159 }
160
161 @Override
162 public Intent getIntent() {
163 return Intent.BRANCH;
164 }
165
166 @Override
167 public Double getTrait(Tree tree, NodeRef node) {
168 return getBranchTimeEffect(tree, node);
169 }
170 });
171 }
172
173 private String getCategoryTraitName() {
174 return getTraitName() + ".category";
175 }
176
177 private String getCategoryEffectTraitName() {
178 return getTraitName() + ".category.effect";
179 }
180
181 private String getCategoryRateTraitName() {
182 return getTraitName() + ".category.rate";
183 }
184
185 private String getRandomEffectTraitName() {
186 return getTraitName() + ".random.effect";
187 }
188
189 private int getBranchCategory(Tree tree, NodeRef node) {
190 return rateCategories.getBranchCategory(tree, node);
191 }
192
193 private String getBranchTimeEffectTraitName() {
194 return getTraitName() + ".time.effect";
195 }
196
197 private double getBranchCategoryRate(Tree tree, NodeRef node) {
198 if (modelInLogSpace) {
199 return ratesParameter.getParameterValue(getBranchCategory(tree, node));
200 } else {
201 return Math.exp(ratesParameter.getParameterValue(getBranchCategory(tree, node)));
202 }
203 }
204
205 private double getBranchCategoryEffect(Tree tree, NodeRef node) {
206 if (modelInLogSpace) {
207 return (getBranchCategoryRate(tree, node) - ratesParameter.getParameterValue(0));
208 } else {
209 return (getBranchCategoryRate(tree, node) / ratesParameter.getParameterValue(0));
210 }
211 }
212
213 private double getBranchRandomEffect(Tree tree, NodeRef node) {
214 double effect;
215 if (modelInLogSpace) {
216 effect = 0;
217 } else {
218 effect = 1;
219 }
220 if (randomEffectsModels != null) {
221 for (AbstractBranchRateModel model : randomEffectsModels) {
222 if (modelInLogSpace) {
223 effect += model.getBranchRate(tree, node);
224 } else {
225 effect *= model.getBranchRate(tree, node);
226 }
227 }
228 }
229 return effect;
230 }
231
232 private double getMidpointHeight(Tree tree, NodeRef node, boolean log){
233 double nodeHeight = tree.getNodeHeight(node);
234 double parentNodeHeight = tree.getNodeHeight(tree.getParent(node));
235 double midpoint = nodeHeight+(parentNodeHeight-nodeHeight)/2;
236 if(log){
237 return Math.log(midpoint);
238 } else {
239 return midpoint;
240 }
241 }
242
243 private double getBranchTimeEffect(Tree tree, NodeRef node) {
244 if (timeCoefficient!=null) {
245 int rateCategory = rateCategories.getBranchCategory(tree, node);
246 double coefficient = timeCoefficient.getParameterValue(rateCategory);
247 // attempt to implement a proportional time coefficient
248 // double coefficient;
249 // if (timeCoefficient.getDimension() == rateCategories.getCategoryCount()){
250 // coefficient = timeCoefficient.getParameterValue(rateCategory);
251 // } else {
252 // coefficient = timeCoefficient.getParameterValue(0);
253 // coefficient *= ratesParameter.getParameterValue(0)/ratesParameter.getParameterValue(rateCategory);
254 // }
255
256 if (modelInLogSpace) {
257 return coefficient * getMidpointHeight(tree, node, true);
258 } else {
259 return Math.pow(coefficient,getMidpointHeight(tree, node, false));
260 }
261 } else {
262 if (modelInLogSpace) {
263 return 0.0;
264 } else {
265 return 1;
266 }
267 }
268 }
269
270 public TreeTrait[] getTreeTraits() {
271 return helper.getTreeTraits();
272 }
273
274 public TreeTrait getTreeTrait(String key) {
275 return helper.getTreeTrait(key);
276 }
277
278 public double getLogLikelihood() {
279 double logLike = 0.0;
280 if (randomEffectsModels != null) {
281 for (AbstractBranchRateModel model : randomEffectsModels) {
282 logLike += model.getLogLikelihood();
283 }
284 }
285 return logLike;
286 }
287
288 void test() {
289 getTrait(null, null);
290 }
291
292 public LogColumn[] getColumns() {
293 LogColumn[] columns = new LogColumn[ratesParameter.getDimension()];
294 for (int i = 0; i < ratesParameter.getDimension(); ++i) {
295 columns[i] = new OccupancyColumn(i);
296 }
297
298 return columns;
299 }
300
301 private class OccupancyColumn extends NumberColumn {
302 private final int index;
303
304 public OccupancyColumn(int index) {
305 super("Occupancy");
306 this.index = index;
307 }
308
309 public double getDoubleValue() {
310 int occupancy = 0;
311 for (NodeRef node : treeModel.getNodes()) {
312 if (node != treeModel.getRoot()) {
313 if (rateCategories.getBranchCategory(treeModel, node) == index) {
314 occupancy++;
315 }
316 }
317 }
318 return occupancy;
319 }
320 }
321
322 public void handleModelChangedEvent(Model model, Object object, int index) {
323 if (model == rateCategories) {
324 fireModelChanged();
325 } else {
326 AbstractBranchRateModel foundModel = findRandomEffectsModel(model);
327 if (foundModel != null) {
328 if (object == model) {
329 fireModelChanged();
330 } else if (object == null) {
331 fireModelChanged(null, index);
332 } else {
333 throw new IllegalArgumentException("Unknown object component!");
334 }
335 } else {
336 throw new IllegalArgumentException("Unknown model component!");
337 }
338 }
339 }
340
341 private AbstractBranchRateModel findRandomEffectsModel(Model model) {
342 AbstractBranchRateModel found = null;
343 int index = randomEffectsModels.indexOf(model);
344 if (index != -1) {
345 found = randomEffectsModels.get(index);
346 }
347 return found;
348 }
349
350 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
351 fireModelChanged();
352 }
353
354 protected void storeState() {
355 // nothing to do
356 }
357
358 protected void restoreState() {
359 // nothing to do
360 }
361
362 protected void acceptState() {
363 // nothing to do
364 }
365
366 public double getBranchRate(final Tree tree, final NodeRef node) {
367
368 assert !tree.isRoot(node) : "root node doesn't have a rate!";
369
370 int rateCategory = rateCategories.getBranchCategory(tree, node);
371 double effect = ratesParameter.getParameterValue(rateCategory);
372
373 double coefficient = 0;
374 if (timeCoefficient!=null){
375 coefficient = timeCoefficient.getParameterValue(rateCategory);
376 }
377
378 // attempt to implement a proportional time coefficient
379 // double coefficient;
380 // if (timeCoefficient.getDimension() == rateCategories.getCategoryCount()){
381 // coefficient = timeCoefficient.getParameterValue(rateCategory);
382 // } else {
383 // coefficient = timeCoefficient.getParameterValue(0);
384 // coefficient *= ratesParameter.getParameterValue(0)/ratesParameter.getParameterValue(rateCategory);
385 // }
386
387 if (timeCoefficient!=null) {
388 if (modelInLogSpace) {
389 effect += coefficient * getMidpointHeight(tree, node, true);
390 } else {
391 effect *= Math.pow(getMidpointHeight(tree, node, false),coefficient);
392 }
393 }
394
395 if (randomEffectsModels != null) {
396 for (AbstractBranchRateModel model : randomEffectsModels) {
397 if (modelInLogSpace) {
398 effect += model.getBranchRate(tree, node);
399 } else {
400 effect *= model.getBranchRate(tree, node);
401 }
402 }
403 }
404 if (modelInLogSpace) {
405 effect = Math.exp(effect);
406 }
407 return effect;
408 }
409
410 private final Helper helper = new Helper();
411
412 private final CountableBranchCategoryProvider rateCategories;
413 private final boolean modelInLogSpace;
414 }