Debian Codegraph @

src/dr/evomodel/treedatalikelihood/LikelihoodTreeTraversal.java

raw symbols SCIP
1 /*
2 * LikelihoodTreeTraversal.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 * Created by msuchard on 10/6/16.
36 */
37 public final class LikelihoodTreeTraversal extends TreeTraversal {
38
39
40 public LikelihoodTreeTraversal(final Tree treeModel,
41 final BranchRateModel branchRateModel,
42 final TraversalType traversalType) {
43 super(treeModel, branchRateModel, traversalType);
44 }
45
46 @Override
47 public final void dispatchTreeTraversalCollectBranchAndNodeOperations() {
48 branchOperations.clear();
49 nodeOperations.clear();
50
51 switch (traversalType) {
52
53 case POST_ORDER:
54 traversePostOrder(treeModel);
55 break;
56 case REVERSE_LEVEL_ORDER:
57 traverseReverseLevelOrder(treeModel);
58 break;
59 default:
60 assert false : "Unknown traversal type";
61 }
62 }
63
64 public final List<DataLikelihoodDelegate.BranchOperation> getBranchOperations() {
65 return branchOperations;
66 }
67
68 public final List<DataLikelihoodDelegate.NodeOperation> getNodeOperations() {
69 return nodeOperations;
70 }
71
72 /**
73 * Traverse the tree in post order.
74 *
75 * @param tree tree
76 * @return boolean
77 */
78 private void traversePostOrder(Tree tree) {
79 traversePostOrder(tree, tree.getRoot());
80 }
81
82 /**
83 * Traverse the tree in post order.
84 *
85 * @param tree tree
86 * @param node node
87 * @return boolean
88 */
89 private boolean traversePostOrder(Tree tree, NodeRef node) {
90
91 boolean update = false;
92
93 int nodeNum = node.getNumber();
94
95 // First update the transition probability matrix(ices) for this branch
96 if (tree.getParent(node) != null && updateNode[nodeNum]) {
97 addBranchUpdateOperation(tree, node);
98
99 update = true;
100 }
101
102 // If the node is internal, update the partial likelihoods.
103 if (!tree.isExternal(node)) {
104
105 // Traverse down the two child nodes
106 NodeRef child1 = tree.getChild(node, 0);
107 final boolean update1 = traversePostOrder(tree, child1);
108
109 NodeRef child2 = tree.getChild(node, 1);
110 final boolean update2 = traversePostOrder(tree, child2);
111
112 // If either child node was updated then update this node too
113 if (update1 || update2) {
114
115 nodeOperations.add(new DataLikelihoodDelegate.NodeOperation(nodeNum, child1.getNumber(), child2.getNumber()));
116
117 update = true;
118
119 }
120 }
121
122 return update;
123
124 }
125
126 /**
127 * Traverse the tree in reverse level order.
128 *
129 * @param tree tree
130 */
131 private void traverseReverseLevelOrder(final Tree tree) {
132
133 // create a map of all the operations at each particular level
134 Map<Integer, List<DataLikelihoodDelegate.NodeOperation>> operationMap =
135 new HashMap<Integer, List<DataLikelihoodDelegate.NodeOperation>>();
136
137 traverseLevelOrder(tree, tree.getRoot(), 0, operationMap);
138
139 // get the levels as keys in reverse order (they are currently largest towards
140 // the tips) and add the operations to the nodeOperation array.
141 List<Integer> keyList = new ArrayList<Integer>(operationMap.keySet());
142 Collections.sort(keyList, Collections.reverseOrder());
143
144 for (Integer key : keyList) {
145 List<DataLikelihoodDelegate.NodeOperation> opList = operationMap.get(key);
146 for (DataLikelihoodDelegate.NodeOperation op : opList) {
147 nodeOperations.add(op);
148 }
149 }
150 }
151
152 /**
153 * Traverse the tree in level order.
154 *
155 * @param tree tree
156 * @param node node
157 * @return boolean
158 */
159 private boolean traverseLevelOrder(final Tree tree, final NodeRef node,
160 final int level,
161 Map<Integer, List<DataLikelihoodDelegate.NodeOperation>> operationMap) {
162 boolean update = false;
163
164 int nodeNum = node.getNumber();
165
166 // First update the transition probability matrix(ices) for this branch
167 if (tree.getParent(node) != null && updateNode[nodeNum]) {
168 // @todo - at the moment a matrix is updated even if a branch length doesn't change
169
170 addBranchUpdateOperation(tree, node);
171
172 update = true;
173 }
174
175 // If the node is internal, update the partial likelihoods.
176 if (!tree.isExternal(node)) {
177
178 // Traverse down the two child nodes incrementing the level (this will give
179 // level order but we will reverse these later
180 NodeRef child1 = tree.getChild(node, 0);
181 final boolean update1 = traverseLevelOrder(tree, child1, level + 1, operationMap);
182
183 NodeRef child2 = tree.getChild(node, 1);
184 final boolean update2 = traverseLevelOrder(tree, child2, level + 1, operationMap);
185
186 // If either child node was updated then update this node too
187 if (update1 || update2) {
188
189 List<DataLikelihoodDelegate.NodeOperation> ops = operationMap.get(level);
190 if (ops == null) {
191 ops = new ArrayList<DataLikelihoodDelegate.NodeOperation>();
192 operationMap.put(level, ops);
193 }
194 ops.add(new DataLikelihoodDelegate.NodeOperation(nodeNum, child1.getNumber(), child2.getNumber()));
195
196 update = true;
197
198 }
199 }
200
201 return update;
202 }
203
204 /**
205 * Add this node to the branchOperation list for updating of the transition probability matrix.
206 *
207 * @param tree tree
208 * @param node node
209 */
210 private void addBranchUpdateOperation(final Tree tree, final NodeRef node) {
211 branchOperations.add(new DataLikelihoodDelegate.BranchOperation(node.getNumber(),
212 computeBranchLength(tree, node)));
213 }
214
215 private final List<DataLikelihoodDelegate.BranchOperation> branchOperations = new ArrayList<DataLikelihoodDelegate.BranchOperation>();
216 private final List<DataLikelihoodDelegate.NodeOperation> nodeOperations = new ArrayList<DataLikelihoodDelegate.NodeOperation>();
217
218 private List<DataLikelihoodDelegate.BranchNodeOperation> savedWholeTreeBranchOperations;
219 private List<DataLikelihoodDelegate.NodeOperation> savedWholeTreeNodeOperations;
220 }