Debian Codegraph @

src/dr/evomodel/tree/TMRCAStatistic.java

raw symbols SCIP
1 /*
2 * TMRCAStatistic.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.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evolution.util.Taxon;
32 import dr.evolution.util.TaxonList;
33 import dr.inference.model.Statistic;
34
35 import java.util.Set;
36
37 /**
38 * A statistic that tracks the time of MRCA of a set of taxa
39 *
40 * @author Alexei Drummond
41 * @author Andrew Rambaut
42 * @version $Id: TMRCAStatistic.java,v 1.21 2005/07/11 14:06:25 rambaut Exp $
43 */
44 public class TMRCAStatistic extends TreeStatistic {
45
46 public TMRCAStatistic(String name, Tree tree, TaxonList taxa, boolean absoluteTime, boolean forParent)
47 throws TreeUtils.MissingTaxonException {
48 super(name);
49 this.tree = tree;
50 if (absoluteTime && Taxon.getMostRecentDate() != null) {
51 isBackwards = Taxon.getMostRecentDate().isBackwards();
52 mostRecentTipTime = Taxon.getMostRecentDate().getAbsoluteTimeValue();
53 } else {
54 // give node heights or taxa don't have dates
55 mostRecentTipTime = Double.NaN;
56 isBackwards = false;
57 }
58 if (taxa != null) {
59 this.leafSet = TreeUtils.getLeavesForTaxa(tree, taxa);
60 } else {
61 // if no taxa are given then use the root of the tree
62 this.leafSet = null;
63 }
64 this.forParent = forParent;
65 }
66
67 public void setTree(Tree tree) {
68 this.tree = tree;
69 }
70
71 public Tree getTree() {
72 return tree;
73 }
74
75 public int getDimension() {
76 return 1;
77 }
78
79 /**
80 * @return the height of the MRCA node.
81 */
82 public double getStatisticValue(int dim) {
83
84 NodeRef node;
85 if (leafSet != null) {
86 node = TreeUtils.getCommonAncestorNode(tree, leafSet);
87 if (forParent && !tree.isRoot(node)) {
88 node = tree.getParent(node);
89 }
90 } else {
91 node = tree.getRoot();
92 }
93 if (node == null) throw new RuntimeException("No node found that is MRCA of " + leafSet);
94
95 if (!Double.isNaN(mostRecentTipTime)) {
96 if (isBackwards) {
97 return mostRecentTipTime + tree.getNodeHeight(node);
98 } else {
99 return mostRecentTipTime - tree.getNodeHeight(node);
100 }
101 } else {
102 return tree.getNodeHeight(node);
103 }
104 }
105
106 private Tree tree = null;
107 private Set<String> leafSet = null;
108 private final double mostRecentTipTime;
109 private final boolean isBackwards;
110 private final boolean forParent;
111
112 }