Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * AlloppNetworkNodeSlide.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.evomodel.alloppnet.tree.SlidableTree;
30 import dr.evomodel.alloppnet.speciation.AlloppDiploidHistory;
31 import dr.evomodel.alloppnet.speciation.AlloppLeggedTree;
32 import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings;
33 import dr.evomodel.alloppnet.speciation.AlloppSpeciesNetworkModel;
34 import dr.evomodel.alloppnet.parsers.AlloppNetworkNodeSlideParser;
35 import dr.inference.operators.SimpleMCMCOperator;
36 import dr.math.MathUtils;
37 import jebl.util.FixedBitSet;
38
39 import java.util.ArrayList;
40
41
42 /**
43 *
44 * @author Graham Jones
45 * Date: 01/07/2011
46 */
47
48
49 /*
50 * An operator for with allopolyploid networks. Uses mnlCanonical() and mnlReconstruct()
51 * in SlidableTree.Utils to do some of the work. These are called to change one node in
52 * diploid history or one node in a tetratree. There is also a move to change hyb height.
53 *
54 *
55 */
56 public class AlloppNetworkNodeSlide extends SimpleMCMCOperator {
57
58 private final AlloppSpeciesNetworkModel apspnet;
59 private final AlloppSpeciesBindings apsp;
60
61 public AlloppNetworkNodeSlide(AlloppSpeciesNetworkModel apspnet, AlloppSpeciesBindings apsp, double weight) {
62 this.apspnet = apspnet;
63 this.apsp = apsp;
64 setWeight(weight);
65 }
66
67 public String getPerformanceSuggestion() {
68 return "None";
69 }
70
71 @Override
72 public String getOperatorName() {
73 return AlloppNetworkNodeSlideParser.NETWORK_NODE_REHEIGHT + "(" + apspnet.getId() +
74 "," + apsp.getId() + ")";
75 }
76
77 @Override
78 public double doOperation() {
79 operateOneNodeInNet(0.0);
80 return 0;
81 }
82
83
84
85
86 private class NodeHeightInNetIndex {
87 public int ploidy; // 2 for diphist, 4 for a tetra tree
88 public int tree; // indexes tettree
89 public int index; // internal node in diphist or tettree, or foot index for hyb height
90 public boolean doHybheight;
91
92 public NodeHeightInNetIndex(int ploidy, int tree, int index, boolean doHybheight) {
93 this.ploidy = ploidy;
94 this.tree = tree;
95 this.index = index;
96 this.doHybheight = doHybheight;
97 }
98 }
99
100
101 private NodeHeightInNetIndex randomnode() {
102 int noftettrees = apspnet.getNumberOfTetraTrees();
103 int dhcount;
104 int hybhcount;
105 int count = 0;
106
107 dhcount = apspnet.getNumberOfInternalNodesInDipHist();
108 count += dhcount;
109 hybhcount = noftettrees;
110 count += hybhcount;
111 // For each tetratree, the internal/root heights
112 for (int i = 0; i < noftettrees; i++) {
113 int n = apspnet.getNumberOfInternalNodesInTetTree(i);
114 count += n;
115 }
116 int which = MathUtils.nextInt(count);
117 if (which < dhcount) {
118 return new NodeHeightInNetIndex(2, 0, which, false);
119 } else {
120 which -= dhcount;
121 if (which < hybhcount) {
122 // twice as many feet as hybridizations
123 int w = which + (MathUtils.nextBoolean() ? 0 : hybhcount);
124 return new NodeHeightInNetIndex(2, 0, w, true);
125 } else {
126 which -= hybhcount;
127 for (int i = 0; i < noftettrees; i++) {
128 int n = apspnet.getNumberOfInternalNodesInTetTree(i);
129 if (which < n) {
130 return new NodeHeightInNetIndex(4, i, which, false);
131 } else {
132 which -= n;
133 }
134 }
135 }
136 }
137 assert false;
138 return new NodeHeightInNetIndex(-1, -1, -1, false);
139 }
140
141
142
143
144 private void operateOneNodeInNet(double factor) {
145 assert apspnet.getDiploidHistory().diphistOK(apspnet.getDiploidRootIsRoot());
146 NodeHeightInNetIndex nhi = randomnode();
147 if (nhi.doHybheight) {
148 operateHybridHeight(nhi.index);
149 } else {
150 if (nhi.ploidy == 2) {
151 operateOneNodeInDiploidHistory(nhi.index, factor);
152 } else {
153 assert nhi.ploidy == 4;
154 AlloppLeggedTree altree = apspnet.getTetraploidTree(nhi.tree);
155 operateOneNodeInTetraTree(altree, nhi.index, factor);
156 }
157 }
158 }
159
160
161
162 private void operateHybridHeight(int footindex) {
163 AlloppDiploidHistory diphist = apspnet.getDiploidHistory();
164 ArrayList<Integer> feet = diphist.collectFeet();
165
166 assert footindex < feet.size();
167 int foot = feet.get(footindex);
168 int tt = diphist.getNodeTettree(foot);
169 AlloppLeggedTree tettree = apspnet.getTetraploidTree(tt);
170 double minh = tettree.getRootHeight();
171 int f1 = tettree.getDiphistLftLeg();
172 int f2 = tettree.getDiphistRgtLeg();
173 assert (foot == f1) || (foot == f2);
174 apspnet.beginNetworkEdit();
175 diphist.moveHybridHeight(f1, f2, minh);
176 apspnet.endNetworkEdit();
177 }
178
179
180 private void operateOneNodeInTetraTree(AlloppLeggedTree tettree, int which, double factor) {
181
182 // As TreeNodeSlide(). Randomly flip children at each node,
183 // keeping track of node order (in-order order, left to right).
184
185 NodeRef[] order = SlidableTree.Utils.mnlCanonical(tettree);
186
187 // Find the time of the most recent gene coalescence which
188 // has (species,sequence)'s to left and right of this node.
189 FixedBitSet left = apsp.speciesseqEmptyUnion();
190 FixedBitSet right = apsp.speciesseqEmptyUnion();
191 for (int k = 0; k < 2 * which + 1; k += 2) {
192 FixedBitSet left0 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 0);
193 FixedBitSet left1 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 1);
194 left.union(left0);
195 left.union(left1);
196 }
197 for (int k = 2 * (which + 1); k < order.length; k += 2) {
198 FixedBitSet right0 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 0);
199 FixedBitSet right1 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 1);
200 right.union(right0);
201 right.union(right1);
202 }
203 double genelimit = apsp.spseqUpperBound(left, right);
204
205 // also keep this node more recent than the hybridization event that led to this tree.
206 AlloppDiploidHistory diphist = apspnet.getDiploidHistory();
207 double hybridheight = diphist.getHybHeight(tettree);
208
209 final double limit = Math.min(genelimit, hybridheight);
210
211 // On direct call, factor==0.0 and use limit. Else use passed in scaling factor
212 double newHeight = -1.0;
213 if( factor > 0 ) {
214 newHeight = tettree.getSlidableNodeHeight(order[2*which+1]) * factor;
215 } else {
216 newHeight = MathUtils.nextDouble() * limit;
217 }
218
219 apspnet.beginNetworkEdit();
220 final NodeRef node = order[2 * which + 1];
221 tettree.setSlidableNodeHeight(node, newHeight);
222 SlidableTree.Utils.mnlReconstruct(tettree, order);
223 apspnet.endNetworkEdit();
224 }
225
226
227
228 private class RootHeightRange {
229 public double lowerlimit;
230 public double upperlimit;
231 RootHeightRange(double lowerlimit, double upperlimit) {
232 this.lowerlimit = lowerlimit;
233 this.upperlimit = upperlimit;
234 }
235 }
236
237
238
239 // find limit to keep root a diploid
240 // 1. If node to slide is the root, and the second highest node is to left or
241 // right of all diploids, then the root must stay the root: lowerlimit = second highest.
242 // 2. If node to slide is not the root, and is to left or right of all diploids,
243 // then it must not become the root: upperlimit = root height.
244 RootHeightRange findRootRangeForDiploidRootIsRoot(AlloppDiploidHistory diphist, NodeRef[] order, int slidingn) {
245 RootHeightRange rootrange = new RootHeightRange(0.0, Double.MAX_VALUE);
246 int rootn = -1;
247 double maxhgt = 0.0;
248 for (int k = 1; k < order.length; k += 2) {
249 double hgt = diphist.getSlidableNodeHeight(order[k]);
250 if (hgt > maxhgt) {
251 maxhgt = hgt;
252 rootn = k;
253 }
254 }
255 int secondn = -1;
256 double secondhgt = 0.0;
257 for (int k = 1; k < order.length; k += 2) {
258 if (k != rootn) {
259 double hgt = diphist.getSlidableNodeHeight(order[k]);
260 if (hgt > secondhgt) {
261 secondhgt = hgt;
262 secondn = k;
263 }
264 }
265 }
266 int leftmostdip = -1;
267 int rightmostdip = -1;
268 for (int k = 0; k < order.length; k += 2) {
269 if (diphist.tipIsDiploidTip(order[k])) {
270 if (leftmostdip < 0) {
271 leftmostdip = k;
272 }
273 rightmostdip = k;
274 }
275 }
276 if (slidingn == rootn && (secondn < leftmostdip || secondn > rightmostdip)) {
277 rootrange.lowerlimit = diphist.getSlidableNodeHeight(order[secondn]);
278 }
279 if (slidingn < leftmostdip || slidingn > rightmostdip) {
280 rootrange.upperlimit = diphist.getSlidableNodeHeight(order[rootn]);
281 }
282 return rootrange;
283
284 }
285
286
287 private void operateOneNodeInDiploidHistory(int which, double factor) {
288 apspnet.beginNetworkEdit();
289 int slidingn = 2 * which + 1;
290 AlloppDiploidHistory diphist = apspnet.getDiploidHistory();
291
292 NodeRef[] order = SlidableTree.Utils.mnlCanonical(diphist);
293
294 // Find the time of the most recent gene coalescence which
295 // has (species,sequence)'s to left and right of this node.
296 FixedBitSet left = apsp.speciesseqEmptyUnion();
297 FixedBitSet right = apsp.speciesseqEmptyUnion();
298 for (int k = 0; k < slidingn; k += 2) {
299 FixedBitSet u = apspnet.calculateDipHistTipUnion(order[k]);
300 left.union(u);
301 }
302 for (int k = slidingn + 1; k < order.length; k += 2) {
303 FixedBitSet u = apspnet.calculateDipHistTipUnion(order[k]);
304 right.union(u);
305 }
306 double genelimit = apsp.spseqUpperBound(left, right);
307
308 // find limit due to hyb-tips - must be bigger than adjacent heights
309 // Note that adjacent nodes in order[] are tips; if they are not hyb-tips
310 // they have height zero anyway.
311 double hybtiplimit = 0.0;
312 if (slidingn-1 >= 0) {
313 hybtiplimit = Math.max(hybtiplimit, diphist.getSlidableNodeHeight(order[slidingn-1]));
314 }
315 if (slidingn+1 < order.length) {
316 hybtiplimit = Math.max(hybtiplimit, diphist.getSlidableNodeHeight(order[slidingn+1]));
317 }
318 RootHeightRange rootrange = new RootHeightRange(0.0, Double.MAX_VALUE);
319 if (apspnet.getDiploidRootIsRoot()) {
320 rootrange = findRootRangeForDiploidRootIsRoot(diphist, order, slidingn);
321 }
322 final double upperlimit = Math.min(genelimit, rootrange.upperlimit);
323 final double lowerlimit = Math.max(hybtiplimit, rootrange.lowerlimit);
324
325 // On direct call, factor==0.0 and use limit. Else use passed in scaling factor
326 double newHeight = -1.0;
327 if( factor > 0 ) {
328 newHeight = diphist.getSlidableNodeHeight(order[slidingn]) * factor;
329 } else {
330 newHeight = MathUtils.uniform(lowerlimit, upperlimit);
331 }
332
333 assert diphist.diphistOK(apspnet.getDiploidRootIsRoot());
334 final NodeRef node = order[slidingn];
335 diphist.setSlidableNodeHeight(node, newHeight);
336 SlidableTree.Utils.mnlReconstruct(diphist, order);
337 if (!diphist.diphistOK(apspnet.getDiploidRootIsRoot())) {
338 System.out.println("BUG in operateOneNodeInDiploidHistory()");
339 }
340 assert diphist.diphistOK(apspnet.getDiploidRootIsRoot());
341 apspnet.endNetworkEdit();
342 }
343
344 }