| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 55 |
mostRecentTipTime = Double.NaN; |
| 56 |
isBackwards = false; |
| 57 |
} |
| 58 |
if (taxa != null) { |
| 59 |
this.leafSet = TreeUtils.getLeavesForTaxa(tree, taxa); |
| 60 |
} else { |
| 61 |
|
| 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 |
|
| 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 |
} |