Debian Codegraph @

src/dr/evomodel/tree/TreeMetricStatistic.java

raw symbols SCIP
1 /*
2 * TreeMetricStatistic.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.tree;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.tree.TreeUtils;
30 import dr.evolution.tree.treemetrics.TreeMetric;
31
32 /**
33 * A statistic that returns the distance between two trees.
34 *
35 * @author Andrew Rambaut
36 */
37 public class TreeMetricStatistic extends TreeStatistic {
38
39 /**
40 * Constructor which creates statistic that just says whether two trees have the same topology
41 * @param name
42 * @param referenceTree
43 */
44 public TreeMetricStatistic(String name, Tree referenceTree, Tree targetTree) {
45 this(name, referenceTree, targetTree, null);
46 }
47
48 public TreeMetricStatistic(String name, Tree referenceTree, Tree targetTree, TreeMetric treeMetric) {
49 super(name);
50
51 this.referenceTree = referenceTree;
52 this.targetTree = targetTree;
53 this.treeMetric = treeMetric;
54 this.focalNewick = TreeUtils.uniqueNewick(referenceTree, referenceTree.getRoot());
55 }
56
57 public void setTree(Tree tree) {
58 this.targetTree = tree;
59 }
60
61 public Tree getTree() {
62 return targetTree;
63 }
64
65 public int getDimension() {
66 return 1;
67 }
68
69 /**
70 * @return value.
71 */
72 public double getStatisticValue(int dim) {
73
74 if (treeMetric == null) {
75 // simply return if the two trees have the same topology
76 return compareTreesByTopology();
77 }
78
79 return treeMetric.getMetric(referenceTree, targetTree);
80 }
81
82 private double compareTreesByTopology() {
83 final String targetNewick = TreeUtils.uniqueNewick(targetTree, targetTree.getRoot());
84 return targetNewick.equals(focalNewick) ? 1.0 : 0.0;
85 }
86
87 private Tree targetTree;
88 private final Tree referenceTree;
89
90 private final String focalNewick;
91
92 private final TreeMetric treeMetric;
93 }