| 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 |
package dr.evolution.tree; |
| 27 |
|
| 28 |
import dr.math.MathUtils; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Deprecated |
| 42 |
public class SubtreeSlide { |
| 43 |
|
| 44 |
public SubtreeSlide(double size, boolean gaussian) { |
| 45 |
this.size = size; |
| 46 |
this.gaussian = gaussian; |
| 47 |
} |
| 48 |
|
| 49 |
public double getSize() { return size; } |
| 50 |
public void setSize(double size) { this.size = size; } |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public double slideSubtree(MutableTree tree) { |
| 57 |
|
| 58 |
double logHastingsRatio; |
| 59 |
|
| 60 |
NodeRef i, newParent, newChild; |
| 61 |
|
| 62 |
|
| 63 |
do { |
| 64 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 65 |
} while (tree.getRoot() == i); |
| 66 |
NodeRef iP = tree.getParent(i); |
| 67 |
NodeRef CiP = getOtherChild(tree,iP, i); |
| 68 |
NodeRef PiP = tree.getParent(iP); |
| 69 |
|
| 70 |
|
| 71 |
double delta = getDelta(); |
| 72 |
double oldHeight = tree.getNodeHeight(iP); |
| 73 |
double newHeight = oldHeight + delta; |
| 74 |
|
| 75 |
|
| 76 |
if (delta > 0) { |
| 77 |
|
| 78 |
|
| 79 |
if (PiP != null && tree.getNodeHeight(PiP) < newHeight) { |
| 80 |
|
| 81 |
|
| 82 |
newParent = PiP; newChild = iP; |
| 83 |
while (tree.getNodeHeight(newParent) < newHeight) { |
| 84 |
newChild = newParent; |
| 85 |
newParent = tree.getParent(newParent); |
| 86 |
if (newParent == null) break; |
| 87 |
} |
| 88 |
|
| 89 |
tree.beginTreeEdit(); |
| 90 |
|
| 91 |
|
| 92 |
if (tree.isRoot(newChild)) { |
| 93 |
tree.removeChild(iP, CiP); tree.removeChild(PiP, iP); |
| 94 |
tree.addChild(iP, newChild); tree.addChild(PiP, CiP); |
| 95 |
tree.setRoot(iP); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
else { |
| 100 |
tree.removeChild(iP, CiP); tree.removeChild(PiP, iP); |
| 101 |
tree.removeChild(newParent, newChild); |
| 102 |
tree.addChild(iP, newChild); tree.addChild(PiP, CiP); |
| 103 |
tree.addChild(newParent, iP); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
tree.setNodeHeight(iP, newHeight); |
| 108 |
|
| 109 |
tree.endTreeEdit(); |
| 110 |
|
| 111 |
|
| 112 |
int possibleSources = intersectingEdges(tree, newChild, oldHeight, null); |
| 113 |
|
| 114 |
|
| 115 |
logHastingsRatio = Math.log(1.0/(double)possibleSources); |
| 116 |
|
| 117 |
} else { |
| 118 |
|
| 119 |
tree.setNodeHeight(iP, newHeight); |
| 120 |
logHastingsRatio = 0.0; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
else { |
| 125 |
|
| 126 |
|
| 127 |
if (tree.getNodeHeight(i) > newHeight) { |
| 128 |
return Double.NEGATIVE_INFINITY; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
if (tree.getNodeHeight(CiP) > newHeight) { |
| 133 |
|
| 134 |
ArrayList newChildren = new ArrayList(); |
| 135 |
int possibleDestinations = intersectingEdges(tree, CiP, newHeight, newChildren); |
| 136 |
|
| 137 |
|
| 138 |
if (newChildren.size() == 0) { return Double.NEGATIVE_INFINITY; } |
| 139 |
|
| 140 |
|
| 141 |
int childIndex = MathUtils.nextInt(newChildren.size()); |
| 142 |
newChild = (NodeRef)newChildren.get(childIndex); |
| 143 |
newParent = tree.getParent(newChild); |
| 144 |
|
| 145 |
tree.beginTreeEdit(); |
| 146 |
|
| 147 |
|
| 148 |
if (tree.isRoot(iP)) { |
| 149 |
|
| 150 |
tree.removeChild(iP, CiP); tree.removeChild(newParent, newChild); |
| 151 |
tree.addChild(iP, newChild); tree.addChild(newParent, iP); |
| 152 |
tree.setRoot(CiP); |
| 153 |
|
| 154 |
} else { |
| 155 |
tree.removeChild(iP, CiP); tree.removeChild(PiP, iP); |
| 156 |
tree.removeChild(newParent, newChild); |
| 157 |
tree.addChild(iP, newChild); tree.addChild(PiP, CiP); |
| 158 |
tree.addChild(newParent, iP); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
tree.setNodeHeight(iP, newHeight); |
| 163 |
|
| 164 |
tree.endTreeEdit(); |
| 165 |
|
| 166 |
logHastingsRatio = Math.log((double)possibleDestinations); |
| 167 |
} else { |
| 168 |
tree.setNodeHeight(iP, newHeight); |
| 169 |
logHastingsRatio = 0.0; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return logHastingsRatio; |
| 174 |
} |
| 175 |
|
| 176 |
private double getDelta() { |
| 177 |
if (!gaussian) { |
| 178 |
return (MathUtils.nextDouble() * size) - (size/2.0); |
| 179 |
} else { |
| 180 |
return MathUtils.nextGaussian() * size; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
private int intersectingEdges(Tree tree, NodeRef node, double height, ArrayList directChildren) { |
| 185 |
|
| 186 |
NodeRef parent = tree.getParent(node); |
| 187 |
|
| 188 |
if (tree.getNodeHeight(parent) < height) return 0; |
| 189 |
|
| 190 |
if (tree.getNodeHeight(node) < height) { |
| 191 |
if (directChildren != null) directChildren.add(node); |
| 192 |
return 1; |
| 193 |
} |
| 194 |
|
| 195 |
int count = 0; |
| 196 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 197 |
count += intersectingEdges(tree, tree.getChild(node, i), height, directChildren); |
| 198 |
} |
| 199 |
return count; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) { |
| 206 |
|
| 207 |
if (tree.getChild(parent, 0) == child) { |
| 208 |
return tree.getChild(parent, 1); |
| 209 |
} else { |
| 210 |
return tree.getChild(parent, 0); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
private double size = 1.0; |
| 215 |
private boolean gaussian = false; |
| 216 |
} |