Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * AlloppNode.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
29
30 import java.util.Stack;
31
32 import dr.evolution.tree.SimpleNode;
33 import dr.evolution.util.Taxon;
34
35 import jebl.util.FixedBitSet;
36
37
38 /**
39 * An AlloppNode is an interface implemented by DipHistNode in AlloppDiploidHistory,
40 * and MulLabNode in AlloppMulLabTree.
41 * The ABC AlloppNode.Abstract contains some common functionality.
42 *
43 * @author Graham Jones
44 * Date: 20/03/2012
45 */
46
47
48 public interface AlloppNode {
49 int nofChildren();
50 AlloppNode getChild(int ch);
51 AlloppNode getAnc();
52 Taxon getTaxon();
53 double getHeight();
54 FixedBitSet getUnion();
55
56 void setChild(int ch, AlloppNode newchild);
57 void setAnc(AlloppNode anc);
58 void setTaxon(String name);
59 void setHeight(double height);
60 void setUnion(FixedBitSet union);
61 void addChildren(AlloppNode c0, AlloppNode c1);
62
63 String asText(int indentlen);
64
65 void fillinUnionsInSubtree(int unionsize);
66 AlloppNode nodeOfUnionInSubtree(FixedBitSet x);
67
68
69 public abstract class Abstract implements AlloppNode {
70
71
72 public void fillinUnionsInSubtree(int unionsize) {
73 if (nofChildren() > 0) {
74 getChild(0).fillinUnionsInSubtree(unionsize);
75 getChild(1).fillinUnionsInSubtree(unionsize);
76 FixedBitSet union = new FixedBitSet(unionsize);
77 union.union(getChild(0).getUnion());
78 union.union(getChild(1).getUnion());
79 setUnion(union);
80 }
81 }
82
83
84 /*
85 * For constructor of AlloppDiploidHistory, AlloppMulLabTree.
86 * Searches subtree rooted at node for most tipward node
87 * whose union contains x. If x is known to be a union of one of the nodes,
88 * it finds that node, so acts as a map union -> node
89 */
90 public AlloppNode nodeOfUnionInSubtree(FixedBitSet x) {
91 if (nofChildren() == 0) {
92 return this;
93 }
94 if (x.setInclusion(getChild(0).getUnion())) {
95 return getChild(0).nodeOfUnionInSubtree(x);
96 } else if (x.setInclusion(getChild(1).getUnion())) {
97 return getChild(1).nodeOfUnionInSubtree(x);
98 } else {
99 return this;
100 }
101 }
102
103 public static String subtreeAsText(AlloppNode node, String s, Stack<Integer> x, int depth, String b) {
104 Integer[] y = x.toArray(new Integer[x.size()]);
105 StringBuffer indent = new StringBuffer();
106 for (int i = 0; i < depth; i++) {
107 indent.append(" ");
108 }
109 for (int i = 0; i < y.length; i++) {
110 indent.replace(2*y[i], 2*y[i]+1, "|");
111 }
112 if (b.length() > 0) {
113 indent.replace(indent.length()-b.length(), indent.length(), b);
114 }
115 s += indent;
116 s += node.asText(indent.length());
117 s += System.getProperty("line.separator");
118 String subs = "";
119 if (node.nofChildren() > 0) {
120 x.push(depth);
121 subs += subtreeAsText(node.getChild(0), "", x, depth+1, "-");
122 x.pop();
123 subs += subtreeAsText(node.getChild(1), "", x, depth+1, "`-");
124 }
125 return s + subs;
126 }
127
128 }
129
130
131 }