| 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.alloppnet.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.alloppnet.speciation.MulSpeciesTreeModel; |
| 31 |
import dr.evomodel.alloppnet.speciation.MulSpeciesBindings; |
| 32 |
import dr.evomodelxml.operators.TreeNodeSlideParser; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.operators.SimpleMCMCOperator; |
| 35 |
import dr.math.MathUtils; |
| 36 |
import jebl.util.FixedBitSet; |
| 37 |
|
| 38 |
import java.util.Arrays; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class MulTreeNodeSlide extends SimpleMCMCOperator { |
| 51 |
|
| 52 |
private final MulSpeciesTreeModel multree; |
| 53 |
private final MulSpeciesBindings species; |
| 54 |
|
| 55 |
private final int[] preOrderIndexBefore; |
| 56 |
private final int[] preOrderIndexAfter; |
| 57 |
|
| 58 |
public MulTreeNodeSlide(MulSpeciesTreeModel tree, MulSpeciesBindings species , double weight) { |
| 59 |
this.multree = tree; |
| 60 |
this.species = species; |
| 61 |
|
| 62 |
|
| 63 |
preOrderIndexBefore = new int[tree.getNodeCount()]; |
| 64 |
Arrays.fill(preOrderIndexBefore, -1); |
| 65 |
|
| 66 |
preOrderIndexAfter= new int[tree.getNodeCount()]; |
| 67 |
Arrays.fill(preOrderIndexAfter, -1); |
| 68 |
|
| 69 |
setWeight(weight); |
| 70 |
} |
| 71 |
|
| 72 |
public String getPerformanceSuggestion() { |
| 73 |
return "none"; |
| 74 |
} |
| 75 |
|
| 76 |
public String getOperatorName() { |
| 77 |
return TreeNodeSlideParser.TREE_NODE_REHEIGHT + "(" + multree.getId() + "," + species.getId() + ")"; |
| 78 |
} |
| 79 |
|
| 80 |
public double doOperation() { |
| 81 |
operateOneNode(0.0); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void operateOneNode(final double factor) { |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
final int count = multree.getExternalNodeCount(); |
| 93 |
assert count == species.nSpSeqs(); |
| 94 |
|
| 95 |
NodeRef[] order = new NodeRef[2 * count - 1]; |
| 96 |
boolean[] swapped = new boolean[count-1]; |
| 97 |
mauCanonical(multree, order, swapped); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
int which = MathUtils.nextInt(count - 1); |
| 102 |
|
| 103 |
FixedBitSet left = new FixedBitSet(count); |
| 104 |
FixedBitSet right = new FixedBitSet(count); |
| 105 |
|
| 106 |
for(int k = 0; k < 2*which+1; k += 2) { |
| 107 |
left.set(multree.speciesIndex(order[k])); |
| 108 |
} |
| 109 |
|
| 110 |
for(int k = 2*(which+1); k < 2*count; k += 2) { |
| 111 |
right.set(multree.speciesIndex(order[k])); |
| 112 |
} |
| 113 |
|
| 114 |
double newHeight; |
| 115 |
|
| 116 |
if( factor > 0 ) { |
| 117 |
newHeight = multree.getNodeHeight(order[2*which+1]) * factor; |
| 118 |
} else { |
| 119 |
final double limit = species.speciationUpperBound(left, right); |
| 120 |
newHeight = MathUtils.nextDouble() * limit; |
| 121 |
} |
| 122 |
|
| 123 |
multree.beginTreeEdit(); |
| 124 |
|
| 125 |
multree.setPreorderIndices(preOrderIndexBefore); |
| 126 |
|
| 127 |
final NodeRef node = order[2 * which + 1]; |
| 128 |
|
| 129 |
multree.setNodeHeight(node, newHeight); |
| 130 |
|
| 131 |
mauReconstruct(multree, order, swapped); |
| 132 |
|
| 133 |
|
| 134 |
{ |
| 135 |
multree.setPreorderIndices(preOrderIndexAfter); |
| 136 |
|
| 137 |
double[] splitPopValues = null; |
| 138 |
|
| 139 |
for(int k = 0; k < preOrderIndexBefore.length; ++k) { |
| 140 |
final int b = preOrderIndexBefore[k]; |
| 141 |
if( b >= 0 ) { |
| 142 |
final int a = preOrderIndexAfter[k]; |
| 143 |
if( a != b ) { |
| 144 |
|
| 145 |
|
| 146 |
final Parameter p1 = multree.sppSplitPopulations; |
| 147 |
if( splitPopValues == null ) { |
| 148 |
splitPopValues = p1.getParameterValues(); |
| 149 |
} |
| 150 |
|
| 151 |
if( multree.constPopulation() ) { |
| 152 |
p1.setParameterValue(count + a, splitPopValues[count + b]); |
| 153 |
} else { |
| 154 |
for(int i = 0; i < 2; ++i) { |
| 155 |
p1.setParameterValue(count + 2*a + i, splitPopValues[count + 2*b + i]); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
multree.endTreeEdit(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static private void mauCanonical(Tree tree, NodeRef[] order, boolean[] wasSwapped) { |
| 175 |
mauCanonicalSub(tree, tree.getRoot(), 0, order, wasSwapped); |
| 176 |
} |
| 177 |
|
| 178 |
static private int mauCanonicalSub(Tree tree, NodeRef node, int loc, NodeRef[] order, boolean[] wasSwaped) { |
| 179 |
if( tree.isExternal(node) ) { |
| 180 |
order[loc] = node; assert (loc & 0x1) == 0; |
| 181 |
return loc + 1; |
| 182 |
} |
| 183 |
|
| 184 |
final boolean swap = MathUtils.nextBoolean(); |
| 185 |
|
| 186 |
|
| 187 |
int l = mauCanonicalSub(tree, tree.getChild(node, swap ? 1 : 0), loc, order, wasSwaped); |
| 188 |
|
| 189 |
order[l] = node; assert (l & 0x1) == 1; |
| 190 |
wasSwaped[(l-1)/2] = swap; |
| 191 |
|
| 192 |
l = mauCanonicalSub(tree, tree.getChild(node, swap ? 0 : 1), l+1, order, wasSwaped); |
| 193 |
return l; |
| 194 |
} |
| 195 |
|
| 196 |
static private void mauReconstruct(MulSpeciesTreeModel tree, NodeRef[] order, boolean[] swapped) { |
| 197 |
final NodeRef root = mauReconstructSub(tree, 0, swapped.length, order, swapped); |
| 198 |
if( tree.getRoot() != root ) { |
| 199 |
tree.setRoot(root); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
static private NodeRef mauReconstructSub(MulSpeciesTreeModel tree, int from, int to, NodeRef[] order, boolean[] wasSwaped) { |
| 204 |
if( from == to ) { |
| 205 |
return order[2*from]; |
| 206 |
} |
| 207 |
|
| 208 |
int rootIndex = -1; |
| 209 |
{ |
| 210 |
double h = -1; |
| 211 |
|
| 212 |
for(int i = from; i < to; ++i) { |
| 213 |
final double v = tree.getNodeHeight(order[2 * i + 1]); |
| 214 |
if( h < v ) { |
| 215 |
h = v; |
| 216 |
rootIndex = i; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
final NodeRef root = order[2 * rootIndex + 1]; |
| 222 |
|
| 223 |
final NodeRef lchild = tree.getChild(root, 0); |
| 224 |
final NodeRef rchild = tree.getChild(root, 1); |
| 225 |
|
| 226 |
NodeRef lTargetChild = mauReconstructSub(tree, from, rootIndex, order, wasSwaped); |
| 227 |
NodeRef rTargetChild = mauReconstructSub(tree, rootIndex+1, to, order, wasSwaped); |
| 228 |
|
| 229 |
if( wasSwaped[rootIndex] ) { |
| 230 |
NodeRef z = lTargetChild; |
| 231 |
lTargetChild = rTargetChild; |
| 232 |
rTargetChild = z; |
| 233 |
} |
| 234 |
|
| 235 |
if( lchild != lTargetChild ) { |
| 236 |
tree.replaceChild(root, lchild, lTargetChild); |
| 237 |
} |
| 238 |
|
| 239 |
if( rchild != rTargetChild ) { |
| 240 |
tree.replaceChild(root, rchild, rTargetChild); |
| 241 |
} |
| 242 |
|
| 243 |
return root; |
| 244 |
} |
| 245 |
|
| 246 |
} |