Debian Codegraph @

src/dr/evomodel/continuous/BivariateTraitBranchAttributeProvider.java

raw symbols SCIP
1 /*
2 * BivariateTraitBranchAttributeProvider.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.continuous;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTrait;
31
32 /**
33 * @author Marc Suchard
34 */
35 public abstract class BivariateTraitBranchAttributeProvider extends TreeTrait.DefaultBehavior implements TreeTrait<Double> {
36
37 public static final String FORMAT = "%5.4f";
38
39 public BivariateTraitBranchAttributeProvider(AbstractMultivariateTraitLikelihood traitLikelihood) {
40
41 traitName = traitLikelihood.getTraitParameter().getId();
42 label = traitName + extensionName();
43
44 double[] rootTrait = traitLikelihood.getRootNodeTrait();
45 if (rootTrait.length != 2)
46 throw new RuntimeException("BivariateTraitBranchAttributeProvider only works for 2D traits");
47 }
48
49 protected abstract String extensionName();
50
51 protected double branchFunction(double[] startValue, double[] endValue, double startTime, double endTime) {
52 return convert(endValue[0]-startValue[0], endValue[1] - startValue[1], startTime - endTime);
53 }
54
55 protected abstract double convert(double latDifference, double longDifference, double timeDifference);
56
57 public String getTraitName() {
58 return label;
59 }
60
61 public Intent getIntent() {
62 return Intent.BRANCH;
63 }
64
65 public Class getTraitClass() {
66 return Double.class;
67 }
68
69 public int getDimension() {
70 return 1;
71 }
72
73 public Double getTrait(Tree tree, NodeRef node) {
74 if (tree != traitLikelihood.getTreeModel())
75 throw new RuntimeException("Bad bug.");
76
77 NodeRef parent = tree.getParent(node);
78 double[] startTrait = traitLikelihood.getTraitForNode(tree, parent, traitName);
79 double[] endTrait = traitLikelihood.getTraitForNode(tree, node, traitName);
80 double startTime = tree.getNodeHeight(parent);
81 double endTime = tree.getNodeHeight(node);
82
83 return branchFunction(startTrait, endTrait, startTime, endTime);
84 }
85
86 public String getTraitString(Tree tree, NodeRef node) {
87 NodeRef parent = tree.getParent(node);
88 double[] startTrait = traitLikelihood.getTraitForNode(tree, parent, traitName);
89 double[] endTrait = traitLikelihood.getTraitForNode(tree, node, traitName);
90 double startTime = tree.getNodeHeight(parent);
91 double endTime = tree.getNodeHeight(node);
92
93 return String.format(BivariateTraitBranchAttributeProvider.FORMAT,
94 branchFunction(startTrait, endTrait, startTime, endTime));
95 }
96
97 protected AbstractMultivariateTraitLikelihood traitLikelihood;
98 protected String traitName;
99 protected String label;
100
101
102 }