| 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.app.beagle.tools; |
| 27 |
|
| 28 |
import java.util.LinkedHashMap; |
| 29 |
import java.util.Map; |
| 30 |
|
| 31 |
import dr.evomodel.treedatalikelihood.BufferIndexHelper; |
| 32 |
import org.apache.commons.math.random.MersenneTwister; |
| 33 |
|
| 34 |
import beagle.Beagle; |
| 35 |
import beagle.BeagleFactory; |
| 36 |
import dr.evomodel.branchmodel.BranchModel; |
| 37 |
import dr.evomodel.siteratemodel.GammaSiteRateModel; |
| 38 |
import dr.evomodel.substmodel.FrequencyModel; |
| 39 |
import dr.evomodel.treelikelihood.SubstitutionModelDelegate; |
| 40 |
import dr.app.bss.Utils; |
| 41 |
import dr.evolution.datatype.Codons; |
| 42 |
import dr.evolution.datatype.DataType; |
| 43 |
import dr.evolution.sequence.Sequence; |
| 44 |
import dr.evolution.tree.NodeRef; |
| 45 |
import dr.evolution.util.Taxon; |
| 46 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 47 |
import dr.evomodel.tree.TreeModel; |
| 48 |
import dr.math.MathUtils; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class Partition { |
| 55 |
|
| 56 |
private static final boolean DEBUG = false; |
| 57 |
|
| 58 |
|
| 59 |
public int from; |
| 60 |
public int to; |
| 61 |
public int every; |
| 62 |
private BranchModel branchModel; |
| 63 |
private TreeModel treeModel; |
| 64 |
private GammaSiteRateModel siteRateModel; |
| 65 |
private BranchRateModel branchRateModel; |
| 66 |
private FrequencyModel freqModel; |
| 67 |
|
| 68 |
|
| 69 |
private BufferIndexHelper partialBufferHelper; |
| 70 |
private BufferIndexHelper scaleBufferHelper; |
| 71 |
private BufferIndexHelper matrixBufferHelper; |
| 72 |
|
| 73 |
|
| 74 |
private Beagle beagle; |
| 75 |
private SubstitutionModelDelegate substitutionModelDelegate; |
| 76 |
|
| 77 |
|
| 78 |
private Integer partitionNumber; |
| 79 |
private int partitionSiteCount; |
| 80 |
private int nodeCount; |
| 81 |
private int tipCount; |
| 82 |
private int internalNodeCount; |
| 83 |
private int stateCount; |
| 84 |
private int compactPartialsCount; |
| 85 |
private int patternCount; |
| 86 |
private int siteRateCategoryCount; |
| 87 |
|
| 88 |
|
| 89 |
private LinkedHashMap<Taxon, int[]> alignmentMap; |
| 90 |
|
| 91 |
private DataType dataType; |
| 92 |
private boolean hasRootSequence = false; |
| 93 |
private Sequence rootSequence = null; |
| 94 |
private boolean outputAncestralSequences = false; |
| 95 |
|
| 96 |
|
| 97 |
private MersenneTwister random; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public Partition(TreeModel treeModel, |
| 103 |
BranchModel branchModel, |
| 104 |
GammaSiteRateModel siteModel, |
| 105 |
BranchRateModel branchRateModel, |
| 106 |
FrequencyModel freqModel, |
| 107 |
int from, |
| 108 |
int to, |
| 109 |
int every, |
| 110 |
DataType dType |
| 111 |
) { |
| 112 |
|
| 113 |
this.treeModel = treeModel; |
| 114 |
this.siteRateModel = siteModel; |
| 115 |
this.freqModel = freqModel; |
| 116 |
this.branchModel = branchModel; |
| 117 |
this.branchRateModel = branchRateModel; |
| 118 |
|
| 119 |
this.from = from; |
| 120 |
this.to = to; |
| 121 |
this.every = every; |
| 122 |
|
| 123 |
if(dType == null) { |
| 124 |
dataType = freqModel.getDataType(); |
| 125 |
}else{ |
| 126 |
dataType = dType; |
| 127 |
} |
| 128 |
partitionSiteCount = getPartitionSiteCount(); |
| 129 |
|
| 130 |
setBufferHelpers(); |
| 131 |
setSubstitutionModelDelegate(); |
| 132 |
loadBeagleInstance(); |
| 133 |
|
| 134 |
alignmentMap = new LinkedHashMap<Taxon, int[]>(); |
| 135 |
random = new MersenneTwister(MathUtils.nextLong()); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
public Partition(TreeModel treeModel, |
| 140 |
BranchModel branchModel, |
| 141 |
GammaSiteRateModel siteModel, |
| 142 |
BranchRateModel branchRateModel, |
| 143 |
FrequencyModel freqModel, |
| 144 |
int from, |
| 145 |
int to, |
| 146 |
int every |
| 147 |
){ |
| 148 |
this(treeModel, branchModel, siteModel, branchRateModel, freqModel, from, to, every, null); |
| 149 |
} |
| 150 |
|
| 151 |
private void setSubstitutionModelDelegate() { |
| 152 |
substitutionModelDelegate = new SubstitutionModelDelegate(treeModel, |
| 153 |
branchModel); |
| 154 |
} |
| 155 |
|
| 156 |
private void setBufferHelpers() { |
| 157 |
|
| 158 |
nodeCount = treeModel.getNodeCount(); |
| 159 |
matrixBufferHelper = new BufferIndexHelper(nodeCount, 0); |
| 160 |
|
| 161 |
tipCount = treeModel.getExternalNodeCount(); |
| 162 |
internalNodeCount = treeModel.getInternalNodeCount(); |
| 163 |
|
| 164 |
partialBufferHelper = new BufferIndexHelper(nodeCount, tipCount); |
| 165 |
scaleBufferHelper = new BufferIndexHelper(internalNodeCount + 1, 0); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
public void loadBeagleInstance() { |
| 170 |
|
| 171 |
compactPartialsCount = tipCount; |
| 172 |
stateCount = dataType.getStateCount(); |
| 173 |
patternCount = partitionSiteCount; |
| 174 |
siteRateCategoryCount = siteRateModel.getCategoryCount(); |
| 175 |
|
| 176 |
int[] resourceList = new int[] { 0 }; |
| 177 |
long preferenceFlags = 0; |
| 178 |
long requirementFlags = 0; |
| 179 |
|
| 180 |
beagle = BeagleFactory.loadBeagleInstance(tipCount, |
| 181 |
partialBufferHelper.getBufferCount(), |
| 182 |
compactPartialsCount, |
| 183 |
stateCount, |
| 184 |
patternCount, |
| 185 |
substitutionModelDelegate.getEigenBufferCount(), |
| 186 |
substitutionModelDelegate.getMatrixBufferCount(), |
| 187 |
siteRateCategoryCount, |
| 188 |
scaleBufferHelper.getBufferCount(), |
| 189 |
resourceList, |
| 190 |
preferenceFlags, |
| 191 |
requirementFlags); |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
public void simulatePartition() { |
| 196 |
|
| 197 |
try { |
| 198 |
|
| 199 |
NodeRef root = treeModel.getRoot(); |
| 200 |
|
| 201 |
|
| 202 |
double[] categoryRates = siteRateModel.getCategoryRates(); |
| 203 |
beagle.setCategoryRates(categoryRates); |
| 204 |
|
| 205 |
|
| 206 |
double[] categoryProbs = siteRateModel.getCategoryProportions(); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
int[] category = new int[partitionSiteCount]; |
| 213 |
for (int i = 0; i < partitionSiteCount; i++) { |
| 214 |
|
| 215 |
category[i] = randomChoicePDF(categoryProbs, partitionNumber, |
| 216 |
"categories"); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if(DEBUG){ |
| 223 |
System.out.println("category for each site:"); |
| 224 |
Utils.printArray(category); |
| 225 |
} |
| 226 |
|
| 227 |
int[] parentSequence = new int[partitionSiteCount]; |
| 228 |
|
| 229 |
|
| 230 |
if (hasRootSequence) { |
| 231 |
|
| 232 |
if (rootSequence.getLength() == partitionSiteCount) { |
| 233 |
|
| 234 |
parentSequence = sequence2intArray(rootSequence); |
| 235 |
|
| 236 |
} else if (dataType instanceof Codons && rootSequence.getLength() == 3 * partitionSiteCount) { |
| 237 |
|
| 238 |
parentSequence = sequence2intArray(rootSequence); |
| 239 |
|
| 240 |
} else { |
| 241 |
|
| 242 |
throw new RuntimeException("Ancestral sequence length of " |
| 243 |
+ rootSequence.getLength() |
| 244 |
+ " does not match partition site count of " |
| 245 |
+ partitionSiteCount + "."); |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
} else { |
| 250 |
|
| 251 |
double[] frequencies = freqModel.getFrequencies(); |
| 252 |
for (int i = 0; i < partitionSiteCount; i++) { |
| 253 |
|
| 254 |
parentSequence[i] = randomChoicePDF(frequencies, |
| 255 |
partitionNumber, "root"); |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
if (DEBUG) { |
| 262 |
synchronized (this) { |
| 263 |
System.out.println(); |
| 264 |
System.out.println("root Sequence:"); |
| 265 |
Utils.printArray(parentSequence); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
substitutionModelDelegate.updateSubstitutionModels(beagle); |
| 270 |
|
| 271 |
traverse(root, parentSequence, category); |
| 272 |
|
| 273 |
if (DEBUG) { |
| 274 |
synchronized (this) { |
| 275 |
System.out.println("Simulated alignment:"); |
| 276 |
printSequences(); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
beagle.finalize(); |
| 281 |
|
| 282 |
} catch (Exception e) { |
| 283 |
e.printStackTrace(); |
| 284 |
} catch (Throwable e) { |
| 285 |
System.err.println("BeagleException: " + e.getMessage()); |
| 286 |
System.exit(-1); |
| 287 |
} |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
private void traverse(NodeRef node, |
| 292 |
int[] parentSequence, |
| 293 |
int[] category |
| 294 |
) { |
| 295 |
|
| 296 |
if (DEBUG) { |
| 297 |
synchronized (this) { |
| 298 |
System.out.println(); |
| 299 |
System.out.println("I'm at: " + node.toString()); |
| 300 |
System.out.println(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
for (int iChild = 0; iChild < treeModel.getChildCount(node); iChild++) { |
| 305 |
|
| 306 |
NodeRef child = treeModel.getChild(node, iChild); |
| 307 |
int[] partitionSequence = new int[partitionSiteCount]; |
| 308 |
double[] cProb = new double[stateCount]; |
| 309 |
|
| 310 |
double[][] probabilities = getTransitionProbabilities(child); |
| 311 |
|
| 312 |
if (DEBUG) { |
| 313 |
synchronized (this) { |
| 314 |
System.out.println("Going to child " + iChild + ": " + child.toString()); |
| 315 |
System.out.println("Child finite transition probs matrix:"); |
| 316 |
Utils.print2DArray(probabilities, stateCount); |
| 317 |
System.out.println(); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
for (int i = 0; i < partitionSiteCount; i++) { |
| 322 |
|
| 323 |
if (DEBUG) { |
| 324 |
System.out.println("length of copy: " + stateCount); |
| 325 |
System.out.println("source length: " + probabilities[category[i]].length); |
| 326 |
System.out.println("destination length: " + cProb.length); |
| 327 |
} |
| 328 |
|
| 329 |
System.arraycopy(probabilities[category[i]], parentSequence[i] * stateCount, cProb, 0, stateCount); |
| 330 |
|
| 331 |
if (DEBUG) { |
| 332 |
synchronized (this) { |
| 333 |
System.out.println("site:" + i); |
| 334 |
System.out.println("site probs:"); |
| 335 |
Utils.printArray(cProb); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
partitionSequence[i] = randomChoicePDF(cProb, partitionNumber, |
| 340 |
"seq"); |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
if (DEBUG) { |
| 345 |
synchronized (this) { |
| 346 |
|
| 347 |
System.out.println("Simulated sequence:"); |
| 348 |
Utils.printArray(partitionSequence); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
if (treeModel.getChildCount(child) == 0) { |
| 359 |
|
| 360 |
Taxon taxon = treeModel.getNodeTaxon(child); |
| 361 |
alignmentMap.put(taxon, partitionSequence); |
| 362 |
|
| 363 |
if (DEBUG) { |
| 364 |
synchronized (this) { |
| 365 |
System.out.println("Simulated sequence (translated):"); |
| 366 |
System.out.println(Utils.intArray2Sequence(taxon, partitionSequence, BeagleSequenceSimulator.gapFlag, dataType).getSequenceString()); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
} else { |
| 371 |
|
| 372 |
if(outputAncestralSequences) { |
| 373 |
|
| 374 |
alignmentMap.put(new Taxon("internalNodeHeight" + treeModel.getNodeHeight(child)), partitionSequence); |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
traverse(treeModel.getChild(node, iChild), partitionSequence, |
| 381 |
category); |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
private double[][] getTransitionProbabilities(NodeRef node |
| 388 |
) { |
| 389 |
|
| 390 |
double[][] probabilities = new double[siteRateCategoryCount][stateCount |
| 391 |
* stateCount]; |
| 392 |
|
| 393 |
int nodeNum = node.getNumber(); |
| 394 |
matrixBufferHelper.flipOffset(nodeNum); |
| 395 |
int branchIndex = nodeNum; |
| 396 |
|
| 397 |
double branchRate = branchRateModel.getBranchRate(treeModel, node); |
| 398 |
|
| 399 |
double branchLength = treeModel.getBranchLength(node); |
| 400 |
double branchTime = branchLength * branchRate; |
| 401 |
|
| 402 |
if (DEBUG) { |
| 403 |
synchronized (this) { |
| 404 |
System.out.println("Branch length: " + branchLength |
| 405 |
+ " branch rate: " + branchRate + " branch time: " |
| 406 |
+ branchTime); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
int count = 1; |
| 411 |
substitutionModelDelegate.updateTransitionMatrices(beagle, |
| 412 |
new int[] { branchIndex }, new double[] { branchTime }, count); |
| 413 |
|
| 414 |
double transitionMatrix[] = new double[siteRateCategoryCount |
| 415 |
* stateCount * stateCount]; |
| 416 |
|
| 417 |
beagle.getTransitionMatrix(branchIndex, |
| 418 |
transitionMatrix |
| 419 |
); |
| 420 |
|
| 421 |
for (int siteRateCat = 0; siteRateCat < siteRateCategoryCount; siteRateCat++) { |
| 422 |
|
| 423 |
System.arraycopy(transitionMatrix, siteRateCat * stateCount * stateCount, |
| 424 |
probabilities[siteRateCat], 0, stateCount * stateCount); |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
return probabilities; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
private int[] sequence2intArray(Sequence sequence) { |
| 494 |
|
| 495 |
int array[] = new int[partitionSiteCount]; |
| 496 |
|
| 497 |
if (dataType instanceof Codons) { |
| 498 |
|
| 499 |
int k = 0; |
| 500 |
for (int i = 0; i < partitionSiteCount; i++) { |
| 501 |
array[i] = ((Codons) dataType).getState(sequence.getChar(k), |
| 502 |
sequence.getChar(k + 1), sequence.getChar(k + 2)); |
| 503 |
k += 3; |
| 504 |
} |
| 505 |
|
| 506 |
} else { |
| 507 |
|
| 508 |
for (int i = 0; i < partitionSiteCount; i++) { |
| 509 |
array[i] = dataType.getState(sequence.getChar(i)); |
| 510 |
} |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
return array; |
| 515 |
} |
| 516 |
|
| 517 |
private int randomChoicePDF(double[] pdf, int partitionNumber, String error) { |
| 518 |
|
| 519 |
int samplePos = -Integer.MAX_VALUE; |
| 520 |
double cumProb = 0.0; |
| 521 |
double u = random.nextDouble(); |
| 522 |
|
| 523 |
for (int i = 0; i < pdf.length; i++) { |
| 524 |
|
| 525 |
cumProb += pdf[i]; |
| 526 |
|
| 527 |
if (u < cumProb) { |
| 528 |
samplePos = i; |
| 529 |
break; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
return samplePos; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public void setPartitionNumber(Integer partitionNumber) { |
| 541 |
this.partitionNumber = partitionNumber; |
| 542 |
} |
| 543 |
|
| 544 |
public void setRootSequence(Sequence rootSequence) { |
| 545 |
this.rootSequence = rootSequence; |
| 546 |
this.hasRootSequence = true; |
| 547 |
} |
| 548 |
|
| 549 |
public void setOutputAncestralSequences(boolean outputAncestralSequences) { |
| 550 |
this.outputAncestralSequences = outputAncestralSequences; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
public TreeModel getTreeModel() { |
| 562 |
return treeModel; |
| 563 |
} |
| 564 |
|
| 565 |
public int getPartitionSiteCount() { |
| 566 |
return ((to - from) / every) + 1; |
| 567 |
} |
| 568 |
|
| 569 |
public BranchModel getBranchModel() { |
| 570 |
return this.branchModel; |
| 571 |
} |
| 572 |
|
| 573 |
public FrequencyModel getFreqModel() { |
| 574 |
return freqModel; |
| 575 |
} |
| 576 |
|
| 577 |
public Integer getPartitionNumber() { |
| 578 |
return partitionNumber; |
| 579 |
} |
| 580 |
|
| 581 |
public DataType getDataType() { |
| 582 |
return dataType; |
| 583 |
} |
| 584 |
|
| 585 |
public Map<Taxon, int[]> getTaxonSequencesMap() { |
| 586 |
return alignmentMap; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public Sequence getRootSequence() { |
| 594 |
return rootSequence; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public void printSequences() { |
| 602 |
System.out.println("partition " + partitionNumber); |
| 603 |
Utils.printMap(alignmentMap); |
| 604 |
} |
| 605 |
|
| 606 |
} |