| 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 |
|
| 28 |
|
| 29 |
package dr.evomodel.operators; |
| 30 |
|
| 31 |
import dr.evolution.tree.NodeRef; |
| 32 |
import dr.evomodel.tree.TreeModel; |
| 33 |
import dr.evomodelxml.operators.NNIParser; |
| 34 |
import dr.math.MathUtils; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class NNI extends AbstractTreeOperator { |
| 45 |
|
| 46 |
private TreeModel tree = null; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public NNI(TreeModel tree, double weight) { |
| 52 |
this.tree = tree; |
| 53 |
setWeight(weight); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@Override |
| 62 |
public double doOperation() { |
| 63 |
final int nNodes = tree.getNodeCount(); |
| 64 |
final NodeRef root = tree.getRoot(); |
| 65 |
|
| 66 |
NodeRef i; |
| 67 |
|
| 68 |
|
| 69 |
do { |
| 70 |
i = tree.getNode(MathUtils.nextInt(nNodes)); |
| 71 |
} while( root == i || tree.getParent(i) == root ); |
| 72 |
|
| 73 |
|
| 74 |
final NodeRef iParent = tree.getParent(i); |
| 75 |
|
| 76 |
final NodeRef iGrandParent = tree.getParent(iParent); |
| 77 |
|
| 78 |
NodeRef iUncle = tree.getChild(iGrandParent, 0); |
| 79 |
|
| 80 |
if( iUncle == iParent ) { |
| 81 |
|
| 82 |
iUncle = tree.getChild(iGrandParent, 1); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
final double heightGrandfather = tree.getNodeHeight(iGrandParent); |
| 91 |
final double heightUncle = tree.getNodeHeight(iUncle); |
| 92 |
final double minHeightFather = Math.max(heightUncle, tree.getNodeHeight(getOtherChild(tree, iParent, i))); |
| 93 |
final double heightI = tree.getNodeHeight(i); |
| 94 |
final double minHeightReverse = Math.max(heightI, tree.getNodeHeight(getOtherChild(tree, iParent, i))); |
| 95 |
|
| 96 |
double ran; |
| 97 |
do { |
| 98 |
ran = MathUtils.nextDouble(); |
| 99 |
} while( ran == 0.0 || ran == 1.0 ); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
final double newHeightFather = minHeightFather + (ran * (heightGrandfather - minHeightFather)); |
| 104 |
|
| 105 |
tree.setNodeHeight(iParent, newHeightFather); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
final double hastingsRatio = Math.log((heightGrandfather - minHeightFather) / (heightGrandfather - minHeightReverse)); |
| 111 |
|
| 112 |
exchangeNodes(tree, i, iUncle, iParent, iGrandParent); |
| 113 |
|
| 114 |
tree.pushTreeChangedEvent(iParent); |
| 115 |
tree.pushTreeChangedEvent(iGrandParent); |
| 116 |
|
| 117 |
return hastingsRatio; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Override |
| 126 |
public String getOperatorName() { |
| 127 |
return NNIParser.NNI; |
| 128 |
} |
| 129 |
|
| 130 |
public double getMinimumAcceptanceLevel() { |
| 131 |
return 0.025; |
| 132 |
} |
| 133 |
|
| 134 |
public double getMinimumGoodAcceptanceLevel() { |
| 135 |
return 0.05; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public String getPerformanceSuggestion() { |
| 144 |
|
| 145 |
return null; |
| 146 |
} |
| 147 |
|
| 148 |
} |