| 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.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeUtils; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.evomodelxml.operators.SubtreeJumpOperatorParser; |
| 33 |
import dr.inference.operators.*; |
| 34 |
import dr.math.distributions.NormalDistribution; |
| 35 |
import dr.math.MathUtils; |
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class FixedHeightSubtreePruneRegraftOperator extends AbstractTreeOperator { |
| 46 |
|
| 47 |
private final TreeModel tree; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public FixedHeightSubtreePruneRegraftOperator(TreeModel tree, double weight) { |
| 56 |
this.tree = tree; |
| 57 |
setWeight(weight); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public double doOperation() { |
| 66 |
|
| 67 |
final NodeRef root = tree.getRoot(); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
NodeRef i; |
| 72 |
NodeRef iP = null; |
| 73 |
NodeRef CiP = null; |
| 74 |
NodeRef PiP = null; |
| 75 |
List<NodeRef> destinations = null; |
| 76 |
|
| 77 |
do { |
| 78 |
|
| 79 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 80 |
|
| 81 |
} while (root == i || tree.getParent(i) == root); |
| 82 |
|
| 83 |
iP = tree.getParent(i); |
| 84 |
CiP = getOtherChild(tree, iP, i); |
| 85 |
PiP = tree.getParent(iP); |
| 86 |
|
| 87 |
|
| 88 |
double parentHeight = tree.getNodeHeight(iP); |
| 89 |
|
| 90 |
|
| 91 |
destinations = getIntersectingEdges(tree, parentHeight); |
| 92 |
|
| 93 |
if (destinations.size() == 0) { |
| 94 |
|
| 95 |
return Double.NEGATIVE_INFINITY; |
| 96 |
} |
| 97 |
|
| 98 |
int r = MathUtils.nextInt(destinations.size()); |
| 99 |
|
| 100 |
|
| 101 |
destinations.remove(i); |
| 102 |
destinations.remove(CiP); |
| 103 |
|
| 104 |
final NodeRef j = destinations.get(r); |
| 105 |
final NodeRef jP = tree.getParent(j); |
| 106 |
|
| 107 |
tree.beginTreeEdit(); |
| 108 |
|
| 109 |
|
| 110 |
tree.removeChild(iP, CiP); |
| 111 |
tree.removeChild(PiP, iP); |
| 112 |
tree.addChild(PiP, CiP); |
| 113 |
|
| 114 |
|
| 115 |
tree.removeChild(jP, j); |
| 116 |
|
| 117 |
|
| 118 |
tree.addChild(iP, j); |
| 119 |
|
| 120 |
|
| 121 |
tree.addChild(jP, iP); |
| 122 |
|
| 123 |
tree.endTreeEdit(); |
| 124 |
|
| 125 |
return 0.0; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private List<NodeRef> getIntersectingEdges(Tree tree, double height) { |
| 135 |
|
| 136 |
List<NodeRef> intersectingEdges = new ArrayList<NodeRef>(); |
| 137 |
|
| 138 |
for (int i = 0; i < tree.getNodeCount(); i++) { |
| 139 |
final NodeRef node = tree.getNode(i); |
| 140 |
final NodeRef parent = tree.getParent(node); |
| 141 |
|
| 142 |
|
| 143 |
if (parent != null && tree.getNodeHeight(node) < height && tree.getNodeHeight(parent) > height) { |
| 144 |
intersectingEdges.add(node); |
| 145 |
} |
| 146 |
} |
| 147 |
return intersectingEdges; |
| 148 |
} |
| 149 |
|
| 150 |
public String getPerformanceSuggestion() { |
| 151 |
return null; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
public String getOperatorName() { |
| 156 |
return SubtreeJumpOperatorParser.SUBTREE_JUMP + "(" + tree.getId() + ")"; |
| 157 |
} |
| 158 |
} |