| 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.evomodel.alloppnet.tree.SlidableTree; |
| 30 |
import dr.evomodel.alloppnet.speciation.AlloppDiploidHistory; |
| 31 |
import dr.evomodel.alloppnet.speciation.AlloppLeggedTree; |
| 32 |
import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings; |
| 33 |
import dr.evomodel.alloppnet.speciation.AlloppSpeciesNetworkModel; |
| 34 |
import dr.evomodel.alloppnet.parsers.AlloppNetworkNodeSlideParser; |
| 35 |
import dr.inference.operators.SimpleMCMCOperator; |
| 36 |
import dr.math.MathUtils; |
| 37 |
import jebl.util.FixedBitSet; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class AlloppNetworkNodeSlide extends SimpleMCMCOperator { |
| 57 |
|
| 58 |
private final AlloppSpeciesNetworkModel apspnet; |
| 59 |
private final AlloppSpeciesBindings apsp; |
| 60 |
|
| 61 |
public AlloppNetworkNodeSlide(AlloppSpeciesNetworkModel apspnet, AlloppSpeciesBindings apsp, double weight) { |
| 62 |
this.apspnet = apspnet; |
| 63 |
this.apsp = apsp; |
| 64 |
setWeight(weight); |
| 65 |
} |
| 66 |
|
| 67 |
public String getPerformanceSuggestion() { |
| 68 |
return "None"; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public String getOperatorName() { |
| 73 |
return AlloppNetworkNodeSlideParser.NETWORK_NODE_REHEIGHT + "(" + apspnet.getId() + |
| 74 |
"," + apsp.getId() + ")"; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public double doOperation() { |
| 79 |
operateOneNodeInNet(0.0); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private class NodeHeightInNetIndex { |
| 87 |
public int ploidy; |
| 88 |
public int tree; |
| 89 |
public int index; |
| 90 |
public boolean doHybheight; |
| 91 |
|
| 92 |
public NodeHeightInNetIndex(int ploidy, int tree, int index, boolean doHybheight) { |
| 93 |
this.ploidy = ploidy; |
| 94 |
this.tree = tree; |
| 95 |
this.index = index; |
| 96 |
this.doHybheight = doHybheight; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
private NodeHeightInNetIndex randomnode() { |
| 102 |
int noftettrees = apspnet.getNumberOfTetraTrees(); |
| 103 |
int dhcount; |
| 104 |
int hybhcount; |
| 105 |
int count = 0; |
| 106 |
|
| 107 |
dhcount = apspnet.getNumberOfInternalNodesInDipHist(); |
| 108 |
count += dhcount; |
| 109 |
hybhcount = noftettrees; |
| 110 |
count += hybhcount; |
| 111 |
|
| 112 |
for (int i = 0; i < noftettrees; i++) { |
| 113 |
int n = apspnet.getNumberOfInternalNodesInTetTree(i); |
| 114 |
count += n; |
| 115 |
} |
| 116 |
int which = MathUtils.nextInt(count); |
| 117 |
if (which < dhcount) { |
| 118 |
return new NodeHeightInNetIndex(2, 0, which, false); |
| 119 |
} else { |
| 120 |
which -= dhcount; |
| 121 |
if (which < hybhcount) { |
| 122 |
|
| 123 |
int w = which + (MathUtils.nextBoolean() ? 0 : hybhcount); |
| 124 |
return new NodeHeightInNetIndex(2, 0, w, true); |
| 125 |
} else { |
| 126 |
which -= hybhcount; |
| 127 |
for (int i = 0; i < noftettrees; i++) { |
| 128 |
int n = apspnet.getNumberOfInternalNodesInTetTree(i); |
| 129 |
if (which < n) { |
| 130 |
return new NodeHeightInNetIndex(4, i, which, false); |
| 131 |
} else { |
| 132 |
which -= n; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
assert false; |
| 138 |
return new NodeHeightInNetIndex(-1, -1, -1, false); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private void operateOneNodeInNet(double factor) { |
| 145 |
assert apspnet.getDiploidHistory().diphistOK(apspnet.getDiploidRootIsRoot()); |
| 146 |
NodeHeightInNetIndex nhi = randomnode(); |
| 147 |
if (nhi.doHybheight) { |
| 148 |
operateHybridHeight(nhi.index); |
| 149 |
} else { |
| 150 |
if (nhi.ploidy == 2) { |
| 151 |
operateOneNodeInDiploidHistory(nhi.index, factor); |
| 152 |
} else { |
| 153 |
assert nhi.ploidy == 4; |
| 154 |
AlloppLeggedTree altree = apspnet.getTetraploidTree(nhi.tree); |
| 155 |
operateOneNodeInTetraTree(altree, nhi.index, factor); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private void operateHybridHeight(int footindex) { |
| 163 |
AlloppDiploidHistory diphist = apspnet.getDiploidHistory(); |
| 164 |
ArrayList<Integer> feet = diphist.collectFeet(); |
| 165 |
|
| 166 |
assert footindex < feet.size(); |
| 167 |
int foot = feet.get(footindex); |
| 168 |
int tt = diphist.getNodeTettree(foot); |
| 169 |
AlloppLeggedTree tettree = apspnet.getTetraploidTree(tt); |
| 170 |
double minh = tettree.getRootHeight(); |
| 171 |
int f1 = tettree.getDiphistLftLeg(); |
| 172 |
int f2 = tettree.getDiphistRgtLeg(); |
| 173 |
assert (foot == f1) || (foot == f2); |
| 174 |
apspnet.beginNetworkEdit(); |
| 175 |
diphist.moveHybridHeight(f1, f2, minh); |
| 176 |
apspnet.endNetworkEdit(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
private void operateOneNodeInTetraTree(AlloppLeggedTree tettree, int which, double factor) { |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
NodeRef[] order = SlidableTree.Utils.mnlCanonical(tettree); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
FixedBitSet left = apsp.speciesseqEmptyUnion(); |
| 190 |
FixedBitSet right = apsp.speciesseqEmptyUnion(); |
| 191 |
for (int k = 0; k < 2 * which + 1; k += 2) { |
| 192 |
FixedBitSet left0 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 0); |
| 193 |
FixedBitSet left1 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 1); |
| 194 |
left.union(left0); |
| 195 |
left.union(left1); |
| 196 |
} |
| 197 |
for (int k = 2 * (which + 1); k < order.length; k += 2) { |
| 198 |
FixedBitSet right0 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 0); |
| 199 |
FixedBitSet right1 = apsp.taxonseqToTipUnion(tettree.getSlidableNodeTaxon(order[k]), 1); |
| 200 |
right.union(right0); |
| 201 |
right.union(right1); |
| 202 |
} |
| 203 |
double genelimit = apsp.spseqUpperBound(left, right); |
| 204 |
|
| 205 |
|
| 206 |
AlloppDiploidHistory diphist = apspnet.getDiploidHistory(); |
| 207 |
double hybridheight = diphist.getHybHeight(tettree); |
| 208 |
|
| 209 |
final double limit = Math.min(genelimit, hybridheight); |
| 210 |
|
| 211 |
|
| 212 |
double newHeight = -1.0; |
| 213 |
if( factor > 0 ) { |
| 214 |
newHeight = tettree.getSlidableNodeHeight(order[2*which+1]) * factor; |
| 215 |
} else { |
| 216 |
newHeight = MathUtils.nextDouble() * limit; |
| 217 |
} |
| 218 |
|
| 219 |
apspnet.beginNetworkEdit(); |
| 220 |
final NodeRef node = order[2 * which + 1]; |
| 221 |
tettree.setSlidableNodeHeight(node, newHeight); |
| 222 |
SlidableTree.Utils.mnlReconstruct(tettree, order); |
| 223 |
apspnet.endNetworkEdit(); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private class RootHeightRange { |
| 229 |
public double lowerlimit; |
| 230 |
public double upperlimit; |
| 231 |
RootHeightRange(double lowerlimit, double upperlimit) { |
| 232 |
this.lowerlimit = lowerlimit; |
| 233 |
this.upperlimit = upperlimit; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
RootHeightRange findRootRangeForDiploidRootIsRoot(AlloppDiploidHistory diphist, NodeRef[] order, int slidingn) { |
| 245 |
RootHeightRange rootrange = new RootHeightRange(0.0, Double.MAX_VALUE); |
| 246 |
int rootn = -1; |
| 247 |
double maxhgt = 0.0; |
| 248 |
for (int k = 1; k < order.length; k += 2) { |
| 249 |
double hgt = diphist.getSlidableNodeHeight(order[k]); |
| 250 |
if (hgt > maxhgt) { |
| 251 |
maxhgt = hgt; |
| 252 |
rootn = k; |
| 253 |
} |
| 254 |
} |
| 255 |
int secondn = -1; |
| 256 |
double secondhgt = 0.0; |
| 257 |
for (int k = 1; k < order.length; k += 2) { |
| 258 |
if (k != rootn) { |
| 259 |
double hgt = diphist.getSlidableNodeHeight(order[k]); |
| 260 |
if (hgt > secondhgt) { |
| 261 |
secondhgt = hgt; |
| 262 |
secondn = k; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
int leftmostdip = -1; |
| 267 |
int rightmostdip = -1; |
| 268 |
for (int k = 0; k < order.length; k += 2) { |
| 269 |
if (diphist.tipIsDiploidTip(order[k])) { |
| 270 |
if (leftmostdip < 0) { |
| 271 |
leftmostdip = k; |
| 272 |
} |
| 273 |
rightmostdip = k; |
| 274 |
} |
| 275 |
} |
| 276 |
if (slidingn == rootn && (secondn < leftmostdip || secondn > rightmostdip)) { |
| 277 |
rootrange.lowerlimit = diphist.getSlidableNodeHeight(order[secondn]); |
| 278 |
} |
| 279 |
if (slidingn < leftmostdip || slidingn > rightmostdip) { |
| 280 |
rootrange.upperlimit = diphist.getSlidableNodeHeight(order[rootn]); |
| 281 |
} |
| 282 |
return rootrange; |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
private void operateOneNodeInDiploidHistory(int which, double factor) { |
| 288 |
apspnet.beginNetworkEdit(); |
| 289 |
int slidingn = 2 * which + 1; |
| 290 |
AlloppDiploidHistory diphist = apspnet.getDiploidHistory(); |
| 291 |
|
| 292 |
NodeRef[] order = SlidableTree.Utils.mnlCanonical(diphist); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
FixedBitSet left = apsp.speciesseqEmptyUnion(); |
| 297 |
FixedBitSet right = apsp.speciesseqEmptyUnion(); |
| 298 |
for (int k = 0; k < slidingn; k += 2) { |
| 299 |
FixedBitSet u = apspnet.calculateDipHistTipUnion(order[k]); |
| 300 |
left.union(u); |
| 301 |
} |
| 302 |
for (int k = slidingn + 1; k < order.length; k += 2) { |
| 303 |
FixedBitSet u = apspnet.calculateDipHistTipUnion(order[k]); |
| 304 |
right.union(u); |
| 305 |
} |
| 306 |
double genelimit = apsp.spseqUpperBound(left, right); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
double hybtiplimit = 0.0; |
| 312 |
if (slidingn-1 >= 0) { |
| 313 |
hybtiplimit = Math.max(hybtiplimit, diphist.getSlidableNodeHeight(order[slidingn-1])); |
| 314 |
} |
| 315 |
if (slidingn+1 < order.length) { |
| 316 |
hybtiplimit = Math.max(hybtiplimit, diphist.getSlidableNodeHeight(order[slidingn+1])); |
| 317 |
} |
| 318 |
RootHeightRange rootrange = new RootHeightRange(0.0, Double.MAX_VALUE); |
| 319 |
if (apspnet.getDiploidRootIsRoot()) { |
| 320 |
rootrange = findRootRangeForDiploidRootIsRoot(diphist, order, slidingn); |
| 321 |
} |
| 322 |
final double upperlimit = Math.min(genelimit, rootrange.upperlimit); |
| 323 |
final double lowerlimit = Math.max(hybtiplimit, rootrange.lowerlimit); |
| 324 |
|
| 325 |
|
| 326 |
double newHeight = -1.0; |
| 327 |
if( factor > 0 ) { |
| 328 |
newHeight = diphist.getSlidableNodeHeight(order[slidingn]) * factor; |
| 329 |
} else { |
| 330 |
newHeight = MathUtils.uniform(lowerlimit, upperlimit); |
| 331 |
} |
| 332 |
|
| 333 |
assert diphist.diphistOK(apspnet.getDiploidRootIsRoot()); |
| 334 |
final NodeRef node = order[slidingn]; |
| 335 |
diphist.setSlidableNodeHeight(node, newHeight); |
| 336 |
SlidableTree.Utils.mnlReconstruct(diphist, order); |
| 337 |
if (!diphist.diphistOK(apspnet.getDiploidRootIsRoot())) { |
| 338 |
System.out.println("BUG in operateOneNodeInDiploidHistory()"); |
| 339 |
} |
| 340 |
assert diphist.diphistOK(apspnet.getDiploidRootIsRoot()); |
| 341 |
apspnet.endNetworkEdit(); |
| 342 |
} |
| 343 |
|
| 344 |
} |