Debian Codegraph @

src/dr/evomodel/treedatalikelihood/SimulationTreeTraversal.java

raw symbols SCIP
1 /*
2 * SimulationTreeTraversal.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.ArrayList;
33 import java.util.List;
34
35 /**
36 * Created by msuchard on 10/6/16.
37 */
38 public class SimulationTreeTraversal extends TreeTraversal {
39
40
41 public SimulationTreeTraversal(final Tree treeModel,
42 final BranchRateModel branchRateModel,
43 final TraversalType traversalType) {
44 super(treeModel, branchRateModel, traversalType);
45 }
46
47 @Override
48 public final void dispatchTreeTraversalCollectBranchAndNodeOperations() {
49 branchNodeOperations.clear();
50 nodeOperations.clear();
51
52 switch (traversalType) {
53
54 case PRE_ORDER:
55 traversePreOrder(treeModel);
56 break;
57 default:
58 assert false : "Unknown traversal type";
59 }
60 }
61
62 public List<DataLikelihoodDelegate.BranchNodeOperation> getBranchNodeOperations() {
63 return branchNodeOperations;
64 }
65
66 public List<DataLikelihoodDelegate.NodeOperation> getNodeOperations() {
67 return nodeOperations;
68 }
69
70 /**
71 * Traverse the tree in pre order.
72 *
73 * @param tree tree
74 * @return boolean
75 */
76 private void traversePreOrder(Tree tree) {
77 traversePreOrder(tree, tree.getRoot(), null, null);
78 }
79
80 /**
81 * Traverse the tree in pre order.
82 *
83 * @param tree tree
84 * @param node node
85 * @return boolean
86 */
87 private boolean traversePreOrder(final Tree tree, final NodeRef node, final NodeRef parent, final NodeRef sibling) {
88
89 boolean update = (parent == null) ? true : false;
90
91 final int nodeNum = node.getNumber();
92
93 // First update the transition probability matrix(ices) for this branch
94 if (parent != null && updateNode[nodeNum]) {
95 // @todo - at the moment a matrix is updated even if a branch length doesn't change
96
97 branchNodeOperations.add(new DataLikelihoodDelegate.BranchNodeOperation(
98 nodeNum, parent.getNumber(), computeBranchLength(tree, node)
99 ));
100
101 nodeOperations.add(new ProcessOnTreeDelegate.NodeOperation(
102 parent.getNumber(),
103 nodeNum,
104 sibling.getNumber()));
105
106 update = true;
107 }
108
109 if (!tree.isExternal(node)) {
110 final NodeRef child1 = tree.getChild(node, 0);
111 final NodeRef child2 = tree.getChild(node, 1);
112
113 if (update) {
114 final boolean update1 = traversePreOrder(tree, child1, node, child2);
115 final boolean update2 = traversePreOrder(tree, child2, node, child1);
116 }
117 }
118
119 return update;
120 }
121
122 private final List<DataLikelihoodDelegate.BranchNodeOperation> branchNodeOperations =
123 new ArrayList<DataLikelihoodDelegate.BranchNodeOperation>();
124
125 private final List<DataLikelihoodDelegate.NodeOperation> nodeOperations =
126 new ArrayList<DataLikelihoodDelegate.NodeOperation>();
127 }