Debian Codegraph @

src/dr/evomodel/alloppnet/tree/SlidableTree.java

raw symbols SCIP
1
2 /*
3 * SlidableTree.java
4 *
5 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
6 *
7 * This file is part of BEAST.
8 * See the NOTICE file distributed with this work for additional
9 * information regarding copyright ownership and licensing.
10 *
11 * BEAST is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * BEAST is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with BEAST; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 */
26
27 package dr.evomodel.alloppnet.tree;
28
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.util.Taxon;
31 import dr.math.MathUtils;
32
33 /**
34 * Interface for a binary tree which can support a Mau-et-al MCMC move
35 * in which node heights `slide' and can change topology.
36 *
37 * @author Graham Jones
38 * Date 2012-03-30
39 *
40 */
41
42 /*
43 The aim is to simplify code by not having to use a MutableTree with its long list of
44 methods. The users of SlidableTree are
45
46 1. AlloppLeggedTree, for moves within tetraploid subtrees.
47
48 2. AlloppDiploidHistory. A SimpleTree is made for testing purposes.
49
50 3. PopsIOSpeciesTreeModel. Needs to be a Tree for logging. So I
51 make and update a SimpleTree and delegate to that.
52
53 4. MulSpeciesTreeModel does not use it yet, but could do. Currently a MutableTree and
54 uses near-copy of JH's code for MCMC moves. Same issue as PopsIOSpeciesTreeModel if use
55 SlidableTree.
56
57 It uses NodeRefs cause that's what SimpleTree uses. This means that clients have
58 to implement get/setNumber when they have no use for node numbers.
59
60 */
61
62
63 public interface SlidableTree {
64
65 NodeRef getSlidableRoot();
66
67 int getSlidableNodeCount();
68
69 Taxon getSlidableNodeTaxon(NodeRef node);
70
71 double getSlidableNodeHeight(NodeRef node);
72
73 void setSlidableNodeHeight(NodeRef node, double height);
74
75 boolean isExternalSlidable(NodeRef node);
76
77 NodeRef getSlidableChild(NodeRef node, int i);
78
79 void replaceSlidableChildren(NodeRef node, NodeRef lft, NodeRef rgt);
80
81 void replaceSlidableRoot(NodeRef root);
82
83
84 public class Utils {
85
86 /*
87 * This is an implementation of the ideas of
88 *
89 * Bayesian Phylogenetic Inference via Markov Chain Monte Carlo Methods
90 * Bob Mau, Michael A. Newton, Bret Larget
91 * Biometrics, Vol. 55, No. 1 (Mar., 1999), pp. 1-12
92 *
93 * similar to implementation by Joseph Heled in TreeNodeSlide.
94 * This version works on a SlidableTree rather than a SimpleTree and is
95 * simpler than JH's version. It does not keep track of what was swapped.
96 * Instead I assign popvalues to nodes via an ordering of species (see
97 * fillinpopvals() in network).
98 */
99
100
101 /*
102 * For Mau-Newton-Larget MCMC moves on tree.
103 * Do this, then change a node height, then call mnlReconstruct()
104 *
105 */
106 static public NodeRef[] mnlCanonical(SlidableTree tree) {
107 final int count = tree.getSlidableNodeCount();
108 NodeRef[] order = new NodeRef[count];
109 mnlCanonicalSub(tree, tree.getSlidableRoot(), 0, order);
110 return order;
111 }
112
113
114 /*
115 * For Mau-etal MCMC moves on tree
116 */
117 static public void mnlReconstruct(SlidableTree tree, NodeRef[] order) {
118 final NodeRef root = mnlReconstructSub(tree, 0, (order.length - 1) / 2, order);
119 tree.replaceSlidableRoot(root);
120 }
121
122
123 /*
124 * ****************** private methods **********************
125 */
126
127
128 static private int mnlCanonicalSub(SlidableTree tree, NodeRef node,
129 int nextloc, NodeRef[] order) {
130 if( tree.isExternalSlidable(node) ) {
131 order[nextloc] = node; assert (nextloc & 0x1) == 0;
132 nextloc++;
133 return nextloc;
134 }
135 final boolean swap = MathUtils.nextBoolean();
136 nextloc = mnlCanonicalSub(tree, tree.getSlidableChild(node, swap ? 1 : 0), nextloc, order);
137 order[nextloc] = node; assert (nextloc & 0x1) == 1;
138 nextloc++;
139 nextloc = mnlCanonicalSub(tree, tree.getSlidableChild(node, swap ? 0 : 1), nextloc, order);
140 return nextloc;
141 }
142
143
144
145 static private NodeRef mnlReconstructSub(SlidableTree tree, int from, int to,
146 NodeRef[] order) {
147 if (from == to) {
148 return order[2*from];
149 }
150 int rootIndex = highestNode(tree, order, from, to);
151 NodeRef root = order[2 * rootIndex + 1];
152 NodeRef lft = mnlReconstructSub(tree, from, rootIndex, order);
153 NodeRef rgt = mnlReconstructSub(tree, rootIndex+1, to, order);
154 tree.replaceSlidableChildren(root, lft, rgt);
155 return root;
156 }
157
158
159
160
161
162
163 static private int highestNode(SlidableTree tree, NodeRef[] order, int from, int to) {
164 int rootIndex = -1;
165 double maxh = -1.0;
166
167 for (int i = from; i < to; ++i) {
168 final double h = tree.getSlidableNodeHeight(order[2 * i + 1]);
169 if (h > maxh) {
170 maxh = h;
171 rootIndex = i;
172 }
173 }
174 return rootIndex;
175 }
176
177
178
179 }
180 }