| 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.evomodel.arg.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTree; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evomodel.arg.ARGModel; |
| 31 |
import dr.evomodel.operators.ExchangeOperator; |
| 32 |
import dr.inference.operators.MCMCOperator; |
| 33 |
import dr.inference.operators.SimpleMCMCOperator; |
| 34 |
import dr.math.MathUtils; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.util.ArrayList; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class ARGExchangeOperator extends SimpleMCMCOperator { |
| 49 |
|
| 50 |
public static final String NARROW_EXCHANGE = "argNarrowExchange"; |
| 51 |
public static final String WIDE_EXCHANGE = "argWideExchange"; |
| 52 |
|
| 53 |
public static final int NARROW = 0; |
| 54 |
public static final int WIDE = 1; |
| 55 |
|
| 56 |
private static final int MAX_TRIES = 10000; |
| 57 |
|
| 58 |
private int mode = NARROW; |
| 59 |
private ARGModel tree; |
| 60 |
|
| 61 |
public ARGExchangeOperator(int mode, ARGModel tree, int weight) { |
| 62 |
this.mode = mode; |
| 63 |
this.tree = tree; |
| 64 |
setWeight(weight); |
| 65 |
} |
| 66 |
|
| 67 |
public double doOperation() { |
| 68 |
|
| 69 |
double logHastings = 0.0; |
| 70 |
int tipCount = tree.getExternalNodeCount(); |
| 71 |
|
| 72 |
if (mode == NARROW) { |
| 73 |
if (tree.getReassortmentNodeCount() < 2) |
| 74 |
logHastings = narrow(); |
| 75 |
else |
| 76 |
return 0.0; |
| 77 |
} else { |
| 78 |
logHastings = wide(); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (tree.getExternalNodeCount() != tipCount) { |
| 83 |
throw new RuntimeException("Lost some tips in " + ((mode == NARROW) ? "NARROW mode." : "WIDE mode.")); |
| 84 |
} |
| 85 |
|
| 86 |
assert !Double.isInfinite(logHastings) && !Double.isNaN(logHastings); |
| 87 |
|
| 88 |
return logHastings; |
| 89 |
} |
| 90 |
|
| 91 |
public int getAllValidNarrowMoves() { |
| 92 |
NodeRef iP = null, j = null, jP = null; |
| 93 |
ArrayList<NodeRef> nodes = new ArrayList<NodeRef>(tree.getNodeCount()); |
| 94 |
ArrayList<NarrowMove> moves = new ArrayList<NarrowMove>(tree.getNodeCount()); |
| 95 |
|
| 96 |
for (int k = 0, n = tree.getNodeCount(); k < n; k++) { |
| 97 |
NodeRef x = tree.getNode(k); |
| 98 |
if (!tree.isRoot(x) && !tree.isRoot(tree.getParent(x, 0)) |
| 99 |
&& !tree.isRoot(tree.getParent(x, 1))) { |
| 100 |
nodes.add(x); |
| 101 |
} |
| 102 |
} |
| 103 |
NarrowMove a; |
| 104 |
for (NodeRef i : nodes) { |
| 105 |
for (int k = 0; k < 2; k++) { |
| 106 |
iP = tree.getParent(i, k); |
| 107 |
for (int m = 0; m < 2; m++) { |
| 108 |
jP = tree.getParent(iP, m); |
| 109 |
j = tree.getOtherChild(jP, iP); |
| 110 |
a = new NarrowMove(i, iP, j, jP); |
| 111 |
if (validMove(a) && !moves.contains(a)) |
| 112 |
moves.add(a); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
assert moves.size() > 0; |
| 117 |
return moves.size(); |
| 118 |
} |
| 119 |
|
| 120 |
private boolean validMove(NarrowMove move) { |
| 121 |
if (move.j != move.iP && move.i != move.j && |
| 122 |
(tree.getNodeHeight(move.j) < tree.getNodeHeight(move.iP)) && |
| 123 |
(tree.getNodeHeight(move.i) < tree.getNodeHeight(move.jP))) { |
| 124 |
return true; |
| 125 |
|
| 126 |
} |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
private class NarrowMove { |
| 131 |
public NodeRef i; |
| 132 |
public NodeRef j; |
| 133 |
public NodeRef iP; |
| 134 |
public NodeRef jP; |
| 135 |
|
| 136 |
public NarrowMove(NodeRef i, NodeRef iP, NodeRef j, NodeRef jP) { |
| 137 |
this.i = i; |
| 138 |
this.j = j; |
| 139 |
this.iP = iP; |
| 140 |
this.jP = jP; |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
public boolean equals(Object o) { |
| 145 |
if (!(o instanceof NarrowMove)) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
NarrowMove move = (NarrowMove) o; |
| 150 |
|
| 151 |
if (this.i == move.i && this.j == move.j && |
| 152 |
this.iP == move.iP && this.jP == move.jP) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
if (this.i == move.j && this.j == move.i && |
| 156 |
this.iP == move.jP && this.jP == move.iP) { |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
public String toString() { |
| 164 |
return "(" + i.toString() + ", " + iP.toString() + |
| 165 |
", " + jP.toString() + ", " + j.toString() + ")"; |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public double narrow() { |
| 174 |
|
| 175 |
NodeRef i = null, iP = null, j = null, jP = null; |
| 176 |
int tries = 0; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
int beforeMoves = getAllValidNarrowMoves(); |
| 181 |
|
| 182 |
while (tries < MAX_TRIES) { |
| 183 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 184 |
while (tree.getRoot() == i || tree.getParent(i, 0) == tree.getRoot() || tree.getParent(i, 1) == tree.getRoot()) { |
| 185 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 186 |
} |
| 187 |
|
| 188 |
iP = tree.getParent(i, 0); |
| 189 |
if (tree.isReassortment(i) && MathUtils.nextBoolean()) |
| 190 |
iP = tree.getParent(i, 1); |
| 191 |
|
| 192 |
jP = tree.getParent(iP, 0); |
| 193 |
if (tree.isReassortment(iP) && MathUtils.nextBoolean()) |
| 194 |
jP = tree.getParent(iP, 1); |
| 195 |
|
| 196 |
j = tree.getChild(jP, 0); |
| 197 |
if (j == iP) { |
| 198 |
j = tree.getChild(jP, 1); |
| 199 |
} |
| 200 |
|
| 201 |
if (j != iP && i != j && |
| 202 |
(tree.getNodeHeight(j) < tree.getNodeHeight(iP)) && (tree.getNodeHeight(i) < tree.getNodeHeight(jP))) { |
| 203 |
|
| 204 |
break; |
| 205 |
} |
| 206 |
tries += 1; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
if (tries < MAX_TRIES) { |
| 212 |
try { |
| 213 |
eupdateARG(i, j, iP, jP); |
| 214 |
} catch (ARGOperatorFailedException e) { |
| 215 |
return Double.NEGATIVE_INFINITY; |
| 216 |
} |
| 217 |
|
| 218 |
tree.pushTreeChangedEvent(iP); |
| 219 |
tree.pushTreeChangedEvent(jP); |
| 220 |
} else { |
| 221 |
|
| 222 |
return Double.NEGATIVE_INFINITY; |
| 223 |
} |
| 224 |
|
| 225 |
return Math.log((double) beforeMoves / getAllValidNarrowMoves()); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public double wide() { |
| 232 |
|
| 233 |
NodeRef i = null, iP = null, j = null, jP = null; |
| 234 |
int tries = 0; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
while (tries < MAX_TRIES) { |
| 239 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 240 |
while (tree.getRoot() == i) { |
| 241 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 242 |
} |
| 243 |
|
| 244 |
j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 245 |
while (j == i || j == tree.getRoot()) { |
| 246 |
j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 247 |
} |
| 248 |
|
| 249 |
iP = tree.getParent(i); |
| 250 |
jP = tree.getParent(j); |
| 251 |
|
| 252 |
if ((iP != jP) && (i != jP) && (j != iP) && |
| 253 |
(tree.getNodeHeight(j) < tree.getNodeHeight(iP)) && |
| 254 |
(tree.getNodeHeight(i) < tree.getNodeHeight(jP))) { |
| 255 |
break; |
| 256 |
} |
| 257 |
tries += 1; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
if (tries < MAX_TRIES) { |
| 263 |
try { |
| 264 |
eupdateARG(i, j, iP, jP); |
| 265 |
} catch (ARGOperatorFailedException e) { |
| 266 |
return Double.NEGATIVE_INFINITY; |
| 267 |
} |
| 268 |
} else { |
| 269 |
|
| 270 |
return Double.NEGATIVE_INFINITY; |
| 271 |
} |
| 272 |
return 0.0; |
| 273 |
} |
| 274 |
|
| 275 |
public int getMode() { |
| 276 |
return mode; |
| 277 |
} |
| 278 |
|
| 279 |
public String getOperatorName() { |
| 280 |
return ((mode == NARROW) ? "Narrow" : "Wide") + " Exchange"; |
| 281 |
} |
| 282 |
|
| 283 |
private void eupdateARG(NodeRef i, NodeRef j, NodeRef iP, NodeRef jP) throws ARGOperatorFailedException { |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
tree.beginTreeEdit(); |
| 289 |
|
| 290 |
boolean iBifurcation = tree.isBifurcation(i); |
| 291 |
boolean jBifurcation = tree.isBifurcation(j); |
| 292 |
|
| 293 |
if (iBifurcation && jBifurcation) { |
| 294 |
tree.removeChild(iP, i); |
| 295 |
tree.removeChild(jP, j); |
| 296 |
tree.addChild(jP, i); |
| 297 |
tree.addChild(iP, j); |
| 298 |
} else if (!iBifurcation && !jBifurcation) { |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
} else { |
| 304 |
if (jBifurcation) { |
| 305 |
|
| 306 |
NodeRef t = i; |
| 307 |
NodeRef tP = iP; |
| 308 |
i = j; |
| 309 |
iP = jP; |
| 310 |
j = t; |
| 311 |
jP = tP; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
tree.endTreeEdit(); |
| 324 |
try { |
| 325 |
tree.checkTreeIsValid(); |
| 326 |
} catch (MutableTree.InvalidTreeException ite) { |
| 327 |
throw new ARGOperatorFailedException(ite.toString()); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
public double getMinimumAcceptanceLevel() { |
| 332 |
if (mode == NARROW) return 0.05; |
| 333 |
else return 0.01; |
| 334 |
} |
| 335 |
|
| 336 |
public double getMinimumGoodAcceptanceLevel() { |
| 337 |
if (mode == NARROW) return 0.05; |
| 338 |
else return 0.01; |
| 339 |
} |
| 340 |
|
| 341 |
public static XMLObjectParser NARROW_EXCHANGE_PARSER = new AbstractXMLObjectParser() { |
| 342 |
|
| 343 |
public String getParserName() { |
| 344 |
return NARROW_EXCHANGE; |
| 345 |
} |
| 346 |
|
| 347 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 348 |
|
| 349 |
ARGModel treeModel = (ARGModel) xo.getChild(ARGModel.class); |
| 350 |
int weight = xo.getIntegerAttribute("weight"); |
| 351 |
|
| 352 |
return new ARGExchangeOperator(NARROW, treeModel, weight); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public String getParserDescription() { |
| 360 |
return "This element represents a narrow exchange operator. " + |
| 361 |
"This operator swaps a random subtree with its uncle."; |
| 362 |
} |
| 363 |
|
| 364 |
public Class getReturnType() { |
| 365 |
return ExchangeOperator.class; |
| 366 |
} |
| 367 |
|
| 368 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 369 |
return rules; |
| 370 |
} |
| 371 |
|
| 372 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 373 |
AttributeRule.newIntegerRule("weight"), |
| 374 |
new ElementRule(ARGModel.class) |
| 375 |
}; |
| 376 |
|
| 377 |
}; |
| 378 |
|
| 379 |
public static XMLObjectParser WIDE_EXCHANGE_PARSER = new AbstractXMLObjectParser() { |
| 380 |
|
| 381 |
public String getParserName() { |
| 382 |
return WIDE_EXCHANGE; |
| 383 |
} |
| 384 |
|
| 385 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 386 |
|
| 387 |
ARGModel treeModel = (ARGModel) xo.getChild(ARGModel.class); |
| 388 |
int weight = xo.getIntegerAttribute("weight"); |
| 389 |
|
| 390 |
return new ARGExchangeOperator(WIDE, treeModel, weight); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public String getParserDescription() { |
| 398 |
return "This element represents a wide exchange operator. " + |
| 399 |
"This operator swaps two random subtrees."; |
| 400 |
} |
| 401 |
|
| 402 |
public Class getReturnType() { |
| 403 |
return ExchangeOperator.class; |
| 404 |
} |
| 405 |
|
| 406 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 407 |
return rules; |
| 408 |
} |
| 409 |
|
| 410 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 411 |
AttributeRule.newIntegerRule("weight"), |
| 412 |
new ElementRule(ARGModel.class) |
| 413 |
}; |
| 414 |
|
| 415 |
}; |
| 416 |
} |