Debian Codegraph @

src/dr/evomodel/alloppnet/operators/MulTreeNodeSlide.java

raw symbols SCIP
1 /*
2 * MulTreeNodeSlide.java
3 *
4 * Copyright (c) 2002-2017 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.alloppnet.operators;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.alloppnet.speciation.MulSpeciesTreeModel;
31 import dr.evomodel.alloppnet.speciation.MulSpeciesBindings;
32 import dr.evomodelxml.operators.TreeNodeSlideParser;
33 import dr.inference.model.Parameter;
34 import dr.inference.operators.SimpleMCMCOperator;
35 import dr.math.MathUtils;
36 import jebl.util.FixedBitSet;
37
38 import java.util.Arrays;
39
40 /**
41 * An operator on a multiply labelled species tree based on the ideas of Mau et all. Very similar to
42 * JH's TreeNodeSlide.
43 *
44 * <a href="http://citeseer.ist.psu.edu/rd/27056960%2C5592%2C1%2C0.25%2CDownload/http://citeseer.ist.psu.edu/cache/papers/cs/5768/ftp:zSzzSzftp.stat.wisc.eduzSzpubzSznewtonzSzlastfinal.pdf/mau98bayesian.pdf">
45 * Bayesian Phylogenetic Inference via Markov Chain Monte Carlo Methods</a>
46 *
47 * @author Joseph Heled, Graham Jones
48 * Date: 21/12/2011
49 */
50 public class MulTreeNodeSlide extends SimpleMCMCOperator {
51
52 private final MulSpeciesTreeModel multree;
53 private final MulSpeciesBindings species;
54
55 private final int[] preOrderIndexBefore;
56 private final int[] preOrderIndexAfter;
57
58 public MulTreeNodeSlide(MulSpeciesTreeModel tree, MulSpeciesBindings species /*, boolean outgroupOnly*/, double weight) {
59 this.multree = tree;
60 this.species = species;
61 // this.outgroupOnly = outgroupOnly;
62
63 preOrderIndexBefore = new int[tree.getNodeCount()];
64 Arrays.fill(preOrderIndexBefore, -1);
65
66 preOrderIndexAfter= new int[tree.getNodeCount()];
67 Arrays.fill(preOrderIndexAfter, -1);
68
69 setWeight(weight);
70 }
71
72 public String getPerformanceSuggestion() {
73 return "none";
74 }
75
76 public String getOperatorName() {
77 return TreeNodeSlideParser.TREE_NODE_REHEIGHT + "(" + multree.getId() + "," + species.getId() + ")";
78 }
79
80 public double doOperation() {
81 operateOneNode(0.0);
82 return 0;
83 }
84
85
86
87 public void operateOneNode(final double factor) {
88
89 // #print "operate: tree", ut.treerep(t)
90 // if( verbose) System.out.println(" Mau at start: " + tree.getSimpleTree());
91
92 final int count = multree.getExternalNodeCount();
93 assert count == species.nSpSeqs();
94
95 NodeRef[] order = new NodeRef[2 * count - 1];
96 boolean[] swapped = new boolean[count-1];
97 mauCanonical(multree, order, swapped);
98
99 // internal node to change
100 // count-1 - number of internal nodes
101 int which = MathUtils.nextInt(count - 1);
102
103 FixedBitSet left = new FixedBitSet(count);
104 FixedBitSet right = new FixedBitSet(count);
105
106 for(int k = 0; k < 2*which+1; k += 2) {
107 left.set(multree.speciesIndex(order[k]));
108 }
109
110 for(int k = 2*(which+1); k < 2*count; k += 2) {
111 right.set(multree.speciesIndex(order[k]));
112 }
113
114 double newHeight;
115
116 if( factor > 0 ) {
117 newHeight = multree.getNodeHeight(order[2*which+1]) * factor;
118 } else {
119 final double limit = species.speciationUpperBound(left, right);
120 newHeight = MathUtils.nextDouble() * limit;
121 }
122
123 multree.beginTreeEdit();
124
125 multree.setPreorderIndices(preOrderIndexBefore);
126
127 final NodeRef node = order[2 * which + 1];
128
129 multree.setNodeHeight(node, newHeight);
130
131 mauReconstruct(multree, order, swapped);
132
133 // restore pre-order of pops -
134 {
135 multree.setPreorderIndices(preOrderIndexAfter);
136
137 double[] splitPopValues = null;
138
139 for(int k = 0; k < preOrderIndexBefore.length; ++k) {
140 final int b = preOrderIndexBefore[k];
141 if( b >= 0 ) {
142 final int a = preOrderIndexAfter[k];
143 if( a != b ) {
144 //if( verbose) System.out.println("pops: " + a + " <- " + b);
145
146 final Parameter p1 = multree.sppSplitPopulations;
147 if( splitPopValues == null ) {
148 splitPopValues = p1.getParameterValues();
149 }
150
151 if( multree.constPopulation() ) {
152 p1.setParameterValue(count + a, splitPopValues[count + b]);
153 } else {
154 for(int i = 0; i < 2; ++i) {
155 p1.setParameterValue(count + 2*a + i, splitPopValues[count + 2*b + i]);
156 }
157 }
158 }
159 }
160 }
161 }
162
163 multree.endTreeEdit();
164 }
165
166
167 /**
168 * Obtain an ordering of tree tips from randomly swaping the children order in internal nodes.
169 *
170 * @param tree tree to create order from
171 * @param order Nodes in their random order (only odd indices are filled)
172 * @param wasSwapped true if internal node was swapped
173 */
174 static private void mauCanonical(Tree tree, NodeRef[] order, boolean[] wasSwapped) {
175 mauCanonicalSub(tree, tree.getRoot(), 0, order, wasSwapped);
176 }
177
178 static private int mauCanonicalSub(Tree tree, NodeRef node, int loc, NodeRef[] order, boolean[] wasSwaped) {
179 if( tree.isExternal(node) ) {
180 order[loc] = node; assert (loc & 0x1) == 0;
181 return loc + 1;
182 }
183
184 final boolean swap = MathUtils.nextBoolean();
185 //wasSwaped[(loc-1)/2] = swap;
186
187 int l = mauCanonicalSub(tree, tree.getChild(node, swap ? 1 : 0), loc, order, wasSwaped);
188
189 order[l] = node; assert (l & 0x1) == 1;
190 wasSwaped[(l-1)/2] = swap;
191
192 l = mauCanonicalSub(tree, tree.getChild(node, swap ? 0 : 1), l+1, order, wasSwaped);
193 return l;
194 }
195
196 static private void mauReconstruct(MulSpeciesTreeModel tree, NodeRef[] order, boolean[] swapped) {
197 final NodeRef root = mauReconstructSub(tree, 0, swapped.length, order, swapped);
198 if( tree.getRoot() != root ) {
199 tree.setRoot(root);
200 }
201 }
202
203 static private NodeRef mauReconstructSub(MulSpeciesTreeModel tree, int from, int to, NodeRef[] order, boolean[] wasSwaped) {
204 if( from == to ) {
205 return order[2*from];
206 }
207
208 int rootIndex = -1;
209 {
210 double h = -1;
211
212 for(int i = from; i < to; ++i) {
213 final double v = tree.getNodeHeight(order[2 * i + 1]);
214 if( h < v ) {
215 h = v;
216 rootIndex = i;
217 }
218 }
219 }
220
221 final NodeRef root = order[2 * rootIndex + 1];
222
223 final NodeRef lchild = tree.getChild(root, 0);
224 final NodeRef rchild = tree.getChild(root, 1);
225
226 NodeRef lTargetChild = mauReconstructSub(tree, from, rootIndex, order, wasSwaped);
227 NodeRef rTargetChild = mauReconstructSub(tree, rootIndex+1, to, order, wasSwaped);
228
229 if( wasSwaped[rootIndex] ) {
230 NodeRef z = lTargetChild;
231 lTargetChild = rTargetChild;
232 rTargetChild = z;
233 }
234
235 if( lchild != lTargetChild ) {
236 tree.replaceChild(root, lchild, lTargetChild);
237 }
238
239 if( rchild != rTargetChild ) {
240 tree.replaceChild(root, rchild, rTargetChild);
241 }
242
243 return root;
244 }
245
246 }