| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
|
| 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 |
} |