Debian Codegraph @

src/dr/evomodel/treedatalikelihood/TreeTraversal.java

raw symbols SCIP
1 /*
2 * TreeTraversal.java
3 *
4 * Copyright (c) 2002-2016 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.treedatalikelihood;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.branchratemodel.BranchRateModel;
31
32 import java.util.*;
33
34
35 /**
36 * @author Andrew Rambaut
37 * @author Marc A Suchard
38 */
39 public abstract class TreeTraversal {
40
41 public enum TraversalType {
42 POST_ORDER, // likelihood
43 REVERSE_LEVEL_ORDER, // likelihood
44 PRE_ORDER // simulation
45 }
46
47 protected TreeTraversal(final Tree treeModel,
48 final BranchRateModel branchRateModel,
49 final TraversalType traversalType) {
50 this.treeModel = treeModel;
51 this.branchRateModel = branchRateModel;
52 this.traversalType = traversalType;
53
54 updateNode = new boolean[treeModel.getNodeCount()];
55 updateAllNodes();
56 }
57
58 public abstract void dispatchTreeTraversalCollectBranchAndNodeOperations();
59
60 public final Tree getTree() {
61 return treeModel;
62 }
63
64 public final void setAllNodesUpdated() {
65 Arrays.fill(updateNode, false);
66 updateAllNodes = false;
67 }
68
69 public final void updateAllNodes() {
70 Arrays.fill(updateNode, true);
71 updateAllNodes = true;
72 }
73
74 public final void updateNode(final NodeRef node) {
75 updateNode[node.getNumber()] = true;
76 }
77
78 public final void updateNodeAndChildren(final NodeRef node) {
79 updateNode[node.getNumber()] = true;
80
81 for (int i = 0; i < treeModel.getChildCount(node); i++) {
82 final NodeRef child = treeModel.getChild(node, i);
83 updateNode[child.getNumber()] = true;
84 }
85 }
86
87 public final void updateNodeAndDescendents(final NodeRef node) {
88 updateNode[node.getNumber()] = true;
89
90 for (int i = 0; i < treeModel.getChildCount(node); i++) {
91 final NodeRef child = treeModel.getChild(node, i);
92 updateNodeAndDescendents(child);
93 }
94 }
95
96 public final void updateNodeAndAncestors(final NodeRef node) {
97 updateNode[node.getNumber()] = true;
98
99 if (!treeModel.isRoot(node)) {
100 final NodeRef parent = treeModel.getParent(node);
101 updateNodeAndAncestors(parent);
102 }
103 }
104
105 protected final double computeBranchLength(final Tree tree, final NodeRef node) {
106 final double branchRate;
107
108 synchronized (branchRateModel) {
109 branchRate = branchRateModel.getBranchRate(tree, node);
110 }
111 final double parentHeight = tree.getNodeHeight(tree.getParent(node));
112 final double nodeHeight = tree.getNodeHeight(node);
113
114 // Get the operational time of the branch
115 final double branchLength = branchRate * (parentHeight - nodeHeight);
116
117 assert branchLength >= 0.0 : "Negative branch length: " + branchLength + " for node " +
118 node.getNumber() + (tree.isExternal(node) ?
119 " (" + tree.getNodeTaxon(node).getId() + ")" : "");
120
121 return branchLength;
122 }
123
124 protected final Tree treeModel;
125 protected final BranchRateModel branchRateModel;
126 protected final boolean[] updateNode;
127 protected boolean updateAllNodes;
128
129 protected final TraversalType traversalType;
130 }
131