Debian Codegraph @

src/dr/evomodel/alloppnet/speciation/AlloppLeggedTree.java

raw symbols SCIP
1 /*
2 * AlloppLeggedTree.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.speciation;
27
28 import java.util.ArrayList;
29 import java.util.Formatter;
30 import java.util.Locale;
31 import java.util.Stack;
32
33
34 import dr.evomodel.alloppnet.util.AlloppMisc;
35 import jebl.util.FixedBitSet;
36
37 import dr.evolution.tree.NodeRef;
38 import dr.evomodel.alloppnet.tree.SlidableTree;
39 import dr.evolution.util.Taxon;
40 import dr.math.MathUtils;
41
42
43 /**
44 *
45 * A 'tree with legs' for a single ploidy level in an allopolyploid network.
46 *
47 * @author Graham Jones
48 * Date: 01/05/2011
49 */
50
51
52
53 /*
54 * class AlloppLeggedTree
55 *
56 * This is a `tree with legs', which is a homoploid
57 * species tree which is attached to a tree of lower ploidy
58 * via its legs, as part of a AlloppSpeciesNetworkModel.
59 *
60 * altnodes is an array of ALTNode's which implements the homoploid species tree.
61 * rootn is the index of the root node.
62 *
63 * The fields diphistlftleg, diphistrgtleg are indices giving the
64 * hyb-tips in the AlloppDiploidHistory.
65 *
66 */
67
68
69
70
71 public class AlloppLeggedTree implements SlidableTree {
72
73 private ALTNode[] altnodes;
74 private int rootn;
75
76 private int diphistlftleg;
77 private int diphistrgtleg;
78
79
80
81 /*
82 * parent, child[] implement the tree topology.
83 *
84 * height is the node height; can be > 0 for hyb-tips.
85 *
86 * union is a spseq-union. At diploid tips they are used normally.
87 * Hyb-tips have unions derived from the tetratree and leg index (0 or 1).
88 * Unions are taken towards the root.
89 *
90 * For a hyb-tip, tettree specifies the index of the tetratree whose
91 * root comes from it. It is not used for other nodes.
92 */
93 private class ALTNode extends AlloppNode.Abstract implements AlloppNode, NodeRef {
94 private int anc;
95 private int lft;
96 private int rgt;
97 private double height;
98 private Taxon taxon;
99 private FixedBitSet union;
100 private int nodeNumber;
101
102 // dud constuctor
103 ALTNode(int nn) {
104 anc = -1;
105 lft = -1;
106 rgt = -1;
107 height = -1.0;
108 taxon = new Taxon("");
109 union = null;
110 nodeNumber = nn;
111 }
112
113 // copy constructor
114 public ALTNode(ALTNode node) {
115 anc = node.anc;
116 lft = node.lft;
117 rgt = node.rgt;
118 nodeNumber = node.nodeNumber;
119 copyNonTopologyFields(node);
120 }
121
122 // no-topology constructor. Copies all fields of argument node,
123 // except the anc, lft, rgt fields which are set to `unknown',
124 // and the index field, which is set to nn
125 public ALTNode(int nn, ALTNode node) {
126 anc = -1;
127 lft = -1;
128 rgt = -1;
129 nodeNumber = nn;
130 copyNonTopologyFields(node);
131 }
132
133
134
135 private void copyNonTopologyFields(ALTNode node) {
136 height = node.height;
137 taxon = new Taxon(node.taxon.getId());
138 if (node.union == null) {
139 union = null;
140 } else {
141 union = new FixedBitSet(node.union);
142 }
143 }
144
145 @Override
146 public AlloppNode getChild(int ch) {
147 return ch==0 ? altnodes[lft] : altnodes[rgt];
148 }
149
150 @Override
151 public AlloppNode getAnc() {
152 return altnodes[anc];
153 }
154
155
156 @Override
157 public double getHeight() {
158 return height;
159 }
160
161 @Override
162 public Taxon getTaxon() {
163 return taxon;
164 }
165
166 @Override
167 public FixedBitSet getUnion() {
168 return union;
169 }
170
171 @Override
172 public void setChild(int ch, AlloppNode newchild) {
173 int newch = ((ALTNode)newchild).nodeNumber;
174 if (ch == 0) {
175 lft = newch;
176 } else {
177 rgt = newch;
178 }
179 }
180
181 @Override
182 public void setAnc(AlloppNode anc) {
183 this.anc = ((ALTNode)anc).nodeNumber;
184 }
185
186 @Override
187 public void setTaxon(String name) {
188 this.taxon = new Taxon(name);
189
190 }
191
192
193 @Override
194 public void setHeight(double height) {
195 this.height = height;
196
197 }
198
199 @Override
200 public void setUnion(FixedBitSet union) {
201 this.union = union;
202 }
203
204 @Override
205 public void addChildren(AlloppNode c0, AlloppNode c1) {
206 lft = ((ALTNode)c0).nodeNumber;
207 altnodes[lft].anc = nodeNumber;
208 rgt = ((ALTNode)c1).nodeNumber;
209 altnodes[rgt].anc = nodeNumber;
210 }
211
212 @Override
213 public String asText(int indentlen) {
214 StringBuilder s = new StringBuilder();
215 Formatter formatter = new Formatter(s, Locale.US);
216 if (lft < 0) {
217 formatter.format("%s ", taxon.getId());
218 } else {
219 formatter.format("%s ", "+");
220 }
221 while (s.length() < 20-indentlen) {
222 formatter.format("%s", " ");
223 }
224 formatter.format("%s ", AlloppMisc.nonnegIn8Chars(height));
225 return s.toString();
226 }
227
228
229 @Override
230 public int nofChildren() {
231 return (lft < 0) ? 0 : 2;
232 }
233
234 @Override
235 public int getNumber() {
236 return nodeNumber;
237 }
238
239 @Override
240 public void setNumber(int n) {
241 nodeNumber = n;
242
243 }
244
245
246
247 }
248
249
250 /*
251 * Constructor makes a random starting (homoploid) tree.
252 * Legs are left to AlloppDiploidHistory to do.
253 */
254 public AlloppLeggedTree(Taxon[] taxa, double rate) {
255 int noftets = taxa.length;
256
257 // Make array of dud nodes
258 altnodes = new ALTNode[2 * noftets - 1];
259 for (int i = 0; i < altnodes.length; i++) {
260 altnodes[i] = new ALTNode(i);
261 }
262 ArrayList<Integer> tojoin = new ArrayList<Integer>(noftets);
263 for (int n = 0; n < noftets; n++) {
264 altnodes[n].setTaxon(taxa[n].getId());
265 altnodes[n].setHeight(0.0);
266 tojoin.add(n);
267 }
268 double treeheight = 0.0;
269 for (int i = 0; i < noftets-1; i++) {
270 int numtojoin = tojoin.size();
271 int j = MathUtils.nextInt(numtojoin);
272 Integer child0 = tojoin.get(j);
273 tojoin.remove(j);
274 int k = MathUtils.nextInt(numtojoin-1);
275 Integer child1 = tojoin.get(k);
276 tojoin.remove(k);
277 altnodes[noftets+i].addChildren(altnodes[child0], altnodes[child1]);
278 altnodes[noftets+i].setHeight(treeheight + randomnodeheight(numtojoin*rate));
279 treeheight = altnodes[noftets+i].getHeight();
280 tojoin.add(noftets+i);
281 }
282 diphistlftleg = -1;
283 diphistrgtleg = -1;
284 rootn = altnodes.length - 1;
285 }
286
287
288
289 /**
290 * copy constructor
291 */
292 public AlloppLeggedTree(AlloppLeggedTree x) {
293 altnodes = new ALTNode[x.altnodes.length];
294 for (int n = 0; n < altnodes.length; n++) {
295 altnodes[n] = new ALTNode(x.altnodes[n]);
296 }
297 rootn = x.rootn;
298 this.diphistlftleg = x.diphistlftleg;
299 this.diphistrgtleg = x.diphistrgtleg;
300
301 }
302
303
304 /*
305 * Constructor. Makes a merged tree from two trees. ttree2 has the more ancient hyb time,
306 * so the merged tree gets its legs.
307 */
308 public AlloppLeggedTree(AlloppLeggedTree ttree1, AlloppLeggedTree ttree2, double hybHeight) {
309 altnodes = new ALTNode[1 + ttree1.altnodes.length + ttree2.altnodes.length];
310 diphistlftleg = ttree2.diphistlftleg;
311 diphistrgtleg = ttree2.diphistrgtleg;
312 int nextn = copySubtree(0, (ALTNode)ttree1.getSlidableRoot());
313 int lft = nextn - 1;
314 nextn = copySubtree(nextn, (ALTNode)ttree2.getSlidableRoot());
315 int rgt = nextn - 1;
316 assert nextn == altnodes.length - 1;
317 altnodes[nextn] = new ALTNode(nextn);
318 altnodes[nextn].addChildren(altnodes[lft], altnodes[rgt]);
319 altnodes[nextn].setHeight(hybHeight);
320 rootn = nextn;
321 }
322
323
324 /*
325 * Constructor. Makes a tree from subtree of tetTree
326 * does not fill in legs
327 */
328 public AlloppLeggedTree(AlloppLeggedTree tetTree, AlloppNode sub) {
329 ALTNode node = (ALTNode)sub;
330 int ntips = tetTree.noftipsSubtree(node);
331 altnodes = new ALTNode[2*ntips-1];
332 for (int n = 0; n < altnodes.length; n++) {
333 altnodes[n] = new ALTNode(n);
334 }
335 int nextn = copySubtree(0, node);
336 rootn = nextn - 1;
337 }
338
339
340 /*
341 * Constructor for testing.
342 */
343 public AlloppLeggedTree(Taxon[] taxa) {
344 int nTaxa = taxa.length;
345 assert(nTaxa <= 4);
346 int nNodes = 2 * nTaxa - 1;
347 altnodes = new ALTNode[nNodes];
348 for (int n = 0; n < nNodes; n++) {
349 altnodes[n] = new ALTNode(n);
350 }
351
352 for (int t = 0; t<nTaxa; t++) {
353 altnodes[t].setTaxon(taxa[t].getId());
354 altnodes[t].setHeight(0.0);
355 }
356 if (nTaxa == 2) {
357 altnodes[2].setHeight(1.0);
358 altnodes[2].addChildren(altnodes[0], altnodes[1]);
359 }
360 if (nTaxa == 3) {
361 altnodes[3].setHeight(altnodes[0].getHeight() + 1.0);
362 altnodes[3].addChildren(altnodes[0], altnodes[1]);
363 altnodes[4].setHeight(altnodes[3].getHeight() + 1.0);
364 altnodes[4].addChildren(altnodes[2], altnodes[3]);
365 }
366 if (nTaxa == 4) {
367 altnodes[4].setHeight(altnodes[0].getHeight() + 1.0);
368 altnodes[4].addChildren(altnodes[0], altnodes[1]);
369 altnodes[5].setHeight(altnodes[4].getHeight() + 1.0);
370 altnodes[5].addChildren(altnodes[2], altnodes[4]);
371 altnodes[6].setHeight(altnodes[5].getHeight() + 1.0);
372 altnodes[6].addChildren(altnodes[3], altnodes[5]);
373 }
374 rootn = altnodes.length - 1;
375 }
376
377
378
379 // SlidableTree implementation
380
381 @Override
382 public NodeRef getSlidableRoot() {
383 assert altnodes[rootn].anc < 0;
384 return altnodes[rootn];
385 }
386
387
388
389 @Override
390 public void replaceSlidableRoot(NodeRef root) {
391 rootn = root.getNumber();
392 altnodes[rootn].anc = -1;
393 }
394
395
396
397 @Override
398 public int getSlidableNodeCount() {
399 return altnodes.length;
400 }
401
402
403
404 @Override
405 public double getSlidableNodeHeight(NodeRef node) {
406 return altnodes[node.getNumber()].getHeight();
407 }
408
409 @Override
410 public Taxon getSlidableNodeTaxon(NodeRef node) {
411 return altnodes[node.getNumber()].getTaxon();
412 }
413
414 @Override
415 public void setSlidableNodeHeight(NodeRef node, double height) {
416 altnodes[node.getNumber()].height = height;
417
418 }
419
420
421
422 @Override
423 public boolean isExternalSlidable(NodeRef node) {
424 return (altnodes[node.getNumber()].lft < 0);
425 }
426
427
428
429 @Override
430 public NodeRef getSlidableChild(NodeRef node, int j) {
431 int n = node.getNumber();
432 return j == 0 ? altnodes[ altnodes[n].lft ] : altnodes[ altnodes[n].rgt ];
433 }
434
435
436
437 @Override
438 public void replaceSlidableChildren(NodeRef node, NodeRef lft, NodeRef rgt) {
439 int nn = node.getNumber();
440 int lftn = lft.getNumber();
441 int rgtn = rgt.getNumber();
442 assert altnodes[nn].lft >= 0;
443 altnodes[nn].lft = lftn;
444 altnodes[nn].rgt = rgtn;
445 altnodes[lftn].anc = altnodes[nn].nodeNumber;
446 altnodes[rgtn].anc = altnodes[nn].nodeNumber;
447 }
448
449
450
451 String asText(int tt) {
452 String header = "Tetraploid tree " + String.valueOf(tt) + " height" + System.getProperty("line.separator");
453 String s = "";
454 Stack<Integer> x = new Stack<Integer>();
455 return header + AlloppNode.Abstract.subtreeAsText(altnodes[rootn], s, x, 0, "");
456
457 }
458
459
460
461
462 boolean leggedtreeOK() {
463 int nroots = 0;
464 for (int i = 0; i < altnodes.length; i++) {
465 if (altnodes[i].anc < 0) {
466 nroots++;
467 }
468 }
469 if (nroots != 1) {
470 return false;
471 }
472 for (int i = 0; i < altnodes.length; i++) {
473 int nparents = 0;
474 for (int j = 0; j < altnodes.length; j++) {
475 if (altnodes[j].lft == i) { nparents++; }
476 if (altnodes[j].rgt == i) { nparents++; }
477 }
478 if (altnodes[i].anc < 0 && nparents != 0) {
479 return false;
480 }
481 if (altnodes[i].anc >= 0 && nparents != 1) {
482 return false;
483 }
484 }
485 for (int i = 0; i < altnodes.length; i++) {
486 if (altnodes[i].getNumber() != i) {
487 return false;
488 }
489 }
490 for (int i = 0; i < altnodes.length; i++) {
491 if (altnodes[i].lft >= 0) {
492 if (altnodes[i].rgt < 0) {
493 return false;
494 }
495 int lft = altnodes[i].lft;
496 int rgt = altnodes[i].rgt;
497 if (altnodes[lft].anc != i) {
498 return false;
499 }
500 if (altnodes[rgt].anc != i) {
501 return false;
502 }
503 if (altnodes[i].height <= altnodes[lft].height) {
504 return false;
505 }
506 if (altnodes[i].height <= altnodes[rgt].height) {
507 return false;
508 }
509 } else {
510 if (altnodes[i].height != 0) {
511 return false;
512 }
513 }
514 }
515 if (altnodes[rootn].anc >= 0) {
516 return false;
517 }
518 return true;
519 }
520
521
522
523
524
525 public int scaleAllHeights(double scale) {
526 int count = 0;
527 for (int n = 0; n < altnodes.length; n++) {
528 if (altnodes[n].nofChildren() > 0) {
529 altnodes[n].height *= scale;
530 count++;
531 }
532 }
533 return count;
534 }
535
536
537 public ArrayList<Taxon> getSpeciesTaxons() {
538 ArrayList<Taxon> sptxs = new ArrayList<Taxon>();
539 for (int n = 0; n < altnodes.length; n++) {
540 if (altnodes[n].nofChildren() == 0) {
541 Taxon taxon = altnodes[n].getTaxon();
542 sptxs.add(taxon);
543 }
544 }
545 assert sptxs.size() == getExternalNodeCount();
546 return sptxs;
547 }
548
549
550
551
552 public void fillinTipUnions(AlloppSpeciesBindings apsp, int leg) {
553 for (int n = 0; n < altnodes.length; n++) {
554 if (altnodes[n].nofChildren() == 0) {
555 altnodes[n].setUnion(apsp.taxonseqToTipUnion(altnodes[n].taxon, leg));
556 }
557 }
558 }
559
560 public double getRootHeight() {
561 return altnodes[rootn].height;
562 }
563
564
565
566 public int getExternalNodeCount() {
567 return (altnodes.length + 1) / 2;
568 }
569
570 public int getInternalNodeCount() {
571 return (altnodes.length - 1) / 2;
572 }
573
574
575
576
577 public void collectInternalHeights(ArrayList<Double> heights) {
578 for (int n = 0; n < altnodes.length; n++) {
579 if (altnodes[n].nofChildren() > 0) {
580 heights.add(altnodes[n].height);
581 }
582 }
583 }
584
585
586 public void setDiphistLftLeg(int lftleg) {
587 diphistlftleg = lftleg;
588 }
589
590 public void setDiphistRgtLeg(int rgtleg) {
591 diphistrgtleg = rgtleg;
592 }
593
594 public int getDiphistLftLeg() {
595 return diphistlftleg;
596 }
597
598 public int getDiphistRgtLeg() {
599 return diphistrgtleg;
600 }
601
602
603
604
605 /***********************************************************************/
606 /************************** private ************************************/
607 /***********************************************************************/
608
609 private int copySubtree(int nextn, ALTNode node) {
610 if (node.nofChildren() == 0) {
611 altnodes[nextn] = new ALTNode(nextn, node);
612 nextn++;
613 } else {
614 nextn = copySubtree(nextn, (ALTNode)node.getChild(0));
615 int lft = nextn - 1;
616 nextn = copySubtree(nextn, (ALTNode)node.getChild(1));
617 int rgt = nextn - 1;
618 altnodes[nextn] = new ALTNode(nextn, node);
619 altnodes[nextn].anc = -1;
620 altnodes[nextn].nodeNumber = nextn;
621 altnodes[nextn].addChildren(altnodes[lft], altnodes[rgt]);
622 nextn++;
623 }
624 return nextn;
625 }
626
627 private int noftipsSubtree(ALTNode node) {
628 int ntips = 0;
629 if (node.lft >= 0) {
630 int lftntips = noftipsSubtree(altnodes[node.lft]);
631 int rgtntips = noftipsSubtree(altnodes[node.rgt]);
632 ntips = lftntips + rgtntips;
633 } else {
634 ntips = 1;
635 }
636 return ntips;
637 }
638
639
640 private double randomnodeheight(double rate) {
641 return MathUtils.nextExponential(rate) + 1e-6/rate;
642 // 1e-6/rate to avoid very tiny heights
643 }
644
645
646
647
648
649
650
651
652
653
654 }