| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evomodel.siteratemodel.SiteRateModel; |
| 29 |
import dr.evomodel.treelikelihood.AncestralStateBeagleTreeLikelihood; |
| 30 |
import dr.evomodel.treelikelihood.utilities.TreeTraitLogger; |
| 31 |
import dr.evolution.datatype.Codons; |
| 32 |
import dr.evolution.tree.NodeRef; |
| 33 |
import dr.evolution.tree.Tree; |
| 34 |
import dr.evolution.tree.TreeTrait; |
| 35 |
import dr.evolution.tree.TreeTraitProvider; |
| 36 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 37 |
import dr.evomodel.tree.TreeModel; |
| 38 |
import dr.inference.loggers.LogColumn; |
| 39 |
import dr.inference.loggers.Loggable; |
| 40 |
import dr.inference.markovjumps.StateHistory; |
| 41 |
import dr.inference.model.AbstractModel; |
| 42 |
import dr.inference.model.Model; |
| 43 |
import dr.inference.model.Parameter; |
| 44 |
import dr.inference.model.Variable; |
| 45 |
import dr.math.MathUtils; |
| 46 |
import dr.util.Citable; |
| 47 |
import dr.util.Citation; |
| 48 |
import dr.util.CommonCitations; |
| 49 |
|
| 50 |
import java.util.ArrayList; |
| 51 |
import java.util.Collections; |
| 52 |
import java.util.List; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public class CodonPartitionedRobustCounting extends AbstractModel implements TreeTraitProvider, Loggable, Citable { |
| 66 |
|
| 67 |
private static final boolean DEBUG = false; |
| 68 |
|
| 69 |
public static final String UNCONDITIONED_PREFIX = "u_"; |
| 70 |
public static final String SITE_SPECIFIC_PREFIX = "c_"; |
| 71 |
public static final String TOTAL_PREFIX = "total_"; |
| 72 |
public static final String UNCONDITIONED_TOTAL_PREFIX = "utotal_"; |
| 73 |
public static final String BASE_TRAIT_PREFIX = "base_"; |
| 74 |
public static final String COMPLETE_HISTORY_PREFIX = "all_"; |
| 75 |
public static final String UNCONDITIONED_PER_BRANCH_PREFIX = "b_u_"; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public CodonPartitionedRobustCounting(String name, TreeModel tree, |
| 88 |
AncestralStateBeagleTreeLikelihood[] partition, |
| 89 |
Codons codons, |
| 90 |
CodonLabeling codonLabeling, |
| 91 |
boolean useUniformization, |
| 92 |
boolean includeExternalBranches, |
| 93 |
boolean includeInternalBranches, |
| 94 |
boolean doUnconditionalPerBranch, |
| 95 |
boolean saveCompleteHistory, |
| 96 |
boolean tryNewNeutralModel, |
| 97 |
StratifiedTraitOutputFormat branchFormat, |
| 98 |
StratifiedTraitOutputFormat logFormat, |
| 99 |
String prefix) { |
| 100 |
this(name, tree, partition, codons, codonLabeling, useUniformization, includeExternalBranches, |
| 101 |
includeInternalBranches, doUnconditionalPerBranch, saveCompleteHistory, false, tryNewNeutralModel, |
| 102 |
branchFormat, logFormat, prefix); |
| 103 |
} |
| 104 |
|
| 105 |
public CodonPartitionedRobustCounting(String name, TreeModel tree, |
| 106 |
AncestralStateBeagleTreeLikelihood[] partition, |
| 107 |
Codons codons, |
| 108 |
CodonLabeling codonLabeling, |
| 109 |
boolean useUniformization, |
| 110 |
boolean includeExternalBranches, |
| 111 |
boolean includeInternalBranches, |
| 112 |
boolean doUnconditionalPerBranch, |
| 113 |
boolean saveCompleteHistory, |
| 114 |
boolean forceUnconditionalAverageRate, |
| 115 |
boolean tryNewNeutralModel, |
| 116 |
StratifiedTraitOutputFormat branchFormat, |
| 117 |
StratifiedTraitOutputFormat logFormat, |
| 118 |
String prefix) { |
| 119 |
super(name); |
| 120 |
this.tree = tree; |
| 121 |
addModel(tree); |
| 122 |
|
| 123 |
if (partition.length != 3) { |
| 124 |
throw new RuntimeException("CodonPartition models require 3 partitions"); |
| 125 |
} |
| 126 |
|
| 127 |
this.partition = partition; |
| 128 |
this.codonLabeling = codonLabeling; |
| 129 |
branchRateModel = partition[0].getBranchRateModel(); |
| 130 |
addModel(branchRateModel); |
| 131 |
|
| 132 |
List<SubstitutionModel> substModelsList = new ArrayList<SubstitutionModel>(3); |
| 133 |
List<SiteRateModel> siteRateModelsList = new ArrayList<SiteRateModel>(3); |
| 134 |
|
| 135 |
numCodons = partition[0].getPatternWeights().length; |
| 136 |
|
| 137 |
for (int i = 0; i < 3; i++) { |
| 138 |
substModelsList.add(partition[i].getBranchModel().getRootSubstitutionModel()); |
| 139 |
siteRateModelsList.add(partition[i].getSiteRateModel()); |
| 140 |
if (partition[i].getPatternWeights().length != numCodons) { |
| 141 |
throw new RuntimeException("All sequence lengths must be equal in CodonPartitionedRobustCounting"); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
this.saveCompleteHistory = saveCompleteHistory; |
| 146 |
|
| 147 |
productChainModel = |
| 148 |
new ProductChainSubstitutionModel("codonLabeling", substModelsList, siteRateModelsList, false); |
| 149 |
addModel(productChainModel); |
| 150 |
|
| 151 |
this.forceUnconditionalAverageRate = forceUnconditionalAverageRate; |
| 152 |
|
| 153 |
if (forceUnconditionalAverageRate) { |
| 154 |
averagedProductChainModel = new ProductChainSubstitutionModel("codonLabeling", substModelsList, siteRateModelsList, true); |
| 155 |
addModel(averagedProductChainModel); |
| 156 |
} |
| 157 |
|
| 158 |
this.useUniformization = useUniformization; |
| 159 |
if (useUniformization) { |
| 160 |
markovJumps = new UniformizedSubstitutionModel(productChainModel); |
| 161 |
((UniformizedSubstitutionModel) markovJumps).setSaveCompleteHistory(saveCompleteHistory); |
| 162 |
|
| 163 |
if (forceUnconditionalAverageRate) { |
| 164 |
averagedMarkovJumps = new UniformizedSubstitutionModel(averagedProductChainModel); |
| 165 |
((UniformizedSubstitutionModel) averagedMarkovJumps).setSaveCompleteHistory(saveCompleteHistory); |
| 166 |
} |
| 167 |
} else { |
| 168 |
markovJumps = new MarkovJumpsSubstitutionModel(productChainModel); |
| 169 |
|
| 170 |
if (forceUnconditionalAverageRate) { |
| 171 |
averagedMarkovJumps = new MarkovJumpsSubstitutionModel(averagedProductChainModel); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
double[] synRegMatrix = CodonLabeling.getRegisterMatrix(codonLabeling, codons, true); |
| 176 |
markovJumps.setRegistration(synRegMatrix); |
| 177 |
|
| 178 |
condMeanMatrix = new double[64 * 64]; |
| 179 |
|
| 180 |
this.branchFormat = branchFormat; |
| 181 |
this.logFormat = logFormat; |
| 182 |
|
| 183 |
computedCounts = new double[tree.getNodeCount()][]; |
| 184 |
|
| 185 |
this.includeExternalBranches = includeExternalBranches; |
| 186 |
this.includeInternalBranches = includeInternalBranches; |
| 187 |
this.doUnconditionedPerBranch = doUnconditionalPerBranch; |
| 188 |
|
| 189 |
this.tryNewNeutralModel = tryNewNeutralModel; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
this.prefix = prefix; |
| 194 |
|
| 195 |
setupTraits(); |
| 196 |
} |
| 197 |
|
| 198 |
public double[] getUnconditionalCountsForBranch(NodeRef child) { |
| 199 |
if (!unconditionsPerBranchKnown) { |
| 200 |
computeAllUnconditionalCountsPerBranch(); |
| 201 |
unconditionsPerBranchKnown = true; |
| 202 |
} |
| 203 |
return unconditionedCountsPerBranch[child.getNumber()]; |
| 204 |
} |
| 205 |
|
| 206 |
public double[] getExpectedCountsForBranch(NodeRef child) { |
| 207 |
if (!countsKnown) { |
| 208 |
computeAllExpectedCounts(); |
| 209 |
} |
| 210 |
return computedCounts[child.getNumber()]; |
| 211 |
} |
| 212 |
|
| 213 |
private void computeAllExpectedCounts() { |
| 214 |
for (int i = 0; i < tree.getNodeCount(); i++) { |
| 215 |
NodeRef child = tree.getNode(i); |
| 216 |
if (!tree.isRoot(child)) { |
| 217 |
computedCounts[child.getNumber()] = computeExpectedCountsForBranch(child); |
| 218 |
} |
| 219 |
} |
| 220 |
countsKnown = true; |
| 221 |
} |
| 222 |
|
| 223 |
private double[] computeExpectedCountsForBranch(NodeRef child) { |
| 224 |
|
| 225 |
|
| 226 |
final int[] childSeq0 = partition[0].getStatesForNode(tree, child); |
| 227 |
final int[] childSeq1 = partition[1].getStatesForNode(tree, child); |
| 228 |
final int[] childSeq2 = partition[2].getStatesForNode(tree, child); |
| 229 |
|
| 230 |
|
| 231 |
final NodeRef parent = tree.getParent(child); |
| 232 |
final int[] parentSeq0 = partition[0].getStatesForNode(tree, parent); |
| 233 |
final int[] parentSeq1 = partition[1].getStatesForNode(tree, parent); |
| 234 |
final int[] parentSeq2 = partition[2].getStatesForNode(tree, parent); |
| 235 |
|
| 236 |
double branchRateTime = branchRateModel.getBranchRate(tree, child) * tree.getBranchLength(child); |
| 237 |
|
| 238 |
double[] count = new double[numCodons]; |
| 239 |
|
| 240 |
if (!useUniformization) { |
| 241 |
markovJumps.computeCondStatMarkovJumps(branchRateTime, condMeanMatrix); |
| 242 |
} else { |
| 243 |
|
| 244 |
markovJumps.getSubstitutionModel().getTransitionProbabilities(branchRateTime, condMeanMatrix); |
| 245 |
} |
| 246 |
|
| 247 |
for (int i = 0; i < numCodons; i++) { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
final int childState = getCanonicalState(childSeq0[i], childSeq1[i], childSeq2[i]); |
| 252 |
final int parentState = getCanonicalState(parentSeq0[i], parentSeq1[i], parentSeq2[i]); |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
final double codonCount; |
| 258 |
|
| 259 |
if (!useUniformization) { |
| 260 |
codonCount = condMeanMatrix[parentState * 64 + childState]; |
| 261 |
} else { |
| 262 |
codonCount = ((UniformizedSubstitutionModel) markovJumps).computeCondStatMarkovJumps( |
| 263 |
parentState, |
| 264 |
childState, |
| 265 |
branchRateTime, |
| 266 |
condMeanMatrix[parentState * 64 + childState] |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
if (useUniformization && saveCompleteHistory) { |
| 272 |
UniformizedSubstitutionModel usModel = (UniformizedSubstitutionModel) markovJumps; |
| 273 |
|
| 274 |
if (completeHistoryPerNode == null) { |
| 275 |
completeHistoryPerNode = new String[tree.getNodeCount()][numCodons]; |
| 276 |
} |
| 277 |
|
| 278 |
StateHistory history = usModel.getStateHistory(); |
| 279 |
|
| 280 |
|
| 281 |
double[] register = usModel.getRegistration(); |
| 282 |
history = history.filterChanges(register); |
| 283 |
|
| 284 |
int historyCount = history.getNumberOfJumps(); |
| 285 |
if (historyCount > 0) { |
| 286 |
double parentTime = tree.getNodeHeight(tree.getParent(child)); |
| 287 |
double childTime = tree.getNodeHeight(child); |
| 288 |
history.rescaleTimesOfEvents(parentTime, childTime); |
| 289 |
|
| 290 |
int n = history.getNumberOfJumps(); |
| 291 |
|
| 292 |
String hstring = history.toStringChanges(i + 1, usModel.dataType, false); |
| 293 |
if (DEBUG) { |
| 294 |
System.err.println("site " + (i + 1) + " : " |
| 295 |
+ history.getNumberOfJumps() |
| 296 |
+ " : " |
| 297 |
+ history.toStringChanges(i + 1, usModel.dataType) |
| 298 |
+ " " + codonLabeling.getText()); |
| 299 |
} |
| 300 |
completeHistoryPerNode[child.getNumber()][i] = hstring; |
| 301 |
} else { |
| 302 |
completeHistoryPerNode[child.getNumber()][i] = null; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
count[i] = codonCount; |
| 307 |
} |
| 308 |
|
| 309 |
return count; |
| 310 |
} |
| 311 |
|
| 312 |
private void setupTraits() { |
| 313 |
|
| 314 |
TreeTrait baseTrait = new TreeTrait.DA() { |
| 315 |
|
| 316 |
public String getTraitName() { |
| 317 |
return BASE_TRAIT_PREFIX + codonLabeling.getText(); |
| 318 |
} |
| 319 |
|
| 320 |
public Intent getIntent() { |
| 321 |
return Intent.BRANCH; |
| 322 |
} |
| 323 |
|
| 324 |
public double[] getTrait(Tree tree, NodeRef node) { |
| 325 |
return getExpectedCountsForBranch(node); |
| 326 |
} |
| 327 |
|
| 328 |
public boolean getLoggable() { |
| 329 |
return false; |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
if (saveCompleteHistory) { |
| 334 |
TreeTrait stringTrait = new TreeTrait.SA() { |
| 335 |
|
| 336 |
public String getTraitName() { |
| 337 |
return COMPLETE_HISTORY_PREFIX + codonLabeling.getText(); |
| 338 |
} |
| 339 |
|
| 340 |
public Intent getIntent() { |
| 341 |
return Intent.BRANCH; |
| 342 |
} |
| 343 |
|
| 344 |
public boolean getFormatAsArray() { |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
public String[] getTrait(Tree tree, NodeRef node) { |
| 349 |
double[] count = getExpectedCountsForBranch(node); |
| 350 |
List<String> events = new ArrayList<String>(); |
| 351 |
for (int i = 0; i < numCodons; i++) { |
| 352 |
String eventString = completeHistoryPerNode[node.getNumber()][i]; |
| 353 |
if (eventString != null) { |
| 354 |
if (eventString.contains("},{")) { |
| 355 |
String[] elements = eventString.split("(?<=\\}),(?=\\{)"); |
| 356 |
for (String e : elements) { |
| 357 |
events.add(e); |
| 358 |
} |
| 359 |
} else { |
| 360 |
events.add(eventString); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
if (DEBUG) { |
| 365 |
double sum = 0.0; |
| 366 |
for (double d : count) { |
| 367 |
if (d > 0.0) { |
| 368 |
sum += 1; |
| 369 |
} |
| 370 |
} |
| 371 |
System.err.println(events.size() + " " + sum); |
| 372 |
if (Math.abs(events.size() - sum) > 0.5) { |
| 373 |
System.err.println("Error"); |
| 374 |
for (int i = 0; i < count.length; ++i) { |
| 375 |
if (count[i] != 0.0) { |
| 376 |
System.err.println(i + ": " + count[i] + completeHistoryPerNode[node.getNumber()][i]); |
| 377 |
} |
| 378 |
} |
| 379 |
System.err.println("Error"); |
| 380 |
int c = 0; |
| 381 |
for (String s : events) { |
| 382 |
c++; |
| 383 |
System.err.println(c + ":" + s); |
| 384 |
} |
| 385 |
System.exit(-1); |
| 386 |
} |
| 387 |
} |
| 388 |
String[] array = new String[events.size()]; |
| 389 |
events.toArray(array); |
| 390 |
return array; |
| 391 |
} |
| 392 |
|
| 393 |
public boolean getLoggable() { |
| 394 |
return true; |
| 395 |
} |
| 396 |
}; |
| 397 |
treeTraits.addTrait(stringTrait); |
| 398 |
} |
| 399 |
|
| 400 |
TreeTrait unconditionedSum; |
| 401 |
if (!TRIAL) { |
| 402 |
unconditionedSum = new TreeTrait.D() { |
| 403 |
public String getTraitName() { |
| 404 |
return UNCONDITIONED_PREFIX + codonLabeling.getText(); |
| 405 |
} |
| 406 |
|
| 407 |
public Intent getIntent() { |
| 408 |
return Intent.WHOLE_TREE; |
| 409 |
} |
| 410 |
|
| 411 |
public Double getTrait(Tree tree, NodeRef node) { |
| 412 |
return getUnconditionedTraitValue(); |
| 413 |
} |
| 414 |
|
| 415 |
public boolean getLoggable() { |
| 416 |
return false; |
| 417 |
} |
| 418 |
}; |
| 419 |
} else { |
| 420 |
unconditionedSum = new TreeTrait.DA() { |
| 421 |
public String getTraitName() { |
| 422 |
return UNCONDITIONED_PREFIX + codonLabeling.getText(); |
| 423 |
} |
| 424 |
|
| 425 |
public Intent getIntent() { |
| 426 |
return Intent.WHOLE_TREE; |
| 427 |
} |
| 428 |
|
| 429 |
public double[] getTrait(Tree tree, NodeRef node) { |
| 430 |
return getUnconditionedTraitValues(); |
| 431 |
} |
| 432 |
|
| 433 |
public boolean getLoggable() { |
| 434 |
return false; |
| 435 |
} |
| 436 |
}; |
| 437 |
} |
| 438 |
|
| 439 |
TreeTrait sumOverTreeTrait = new TreeTrait.SumOverTreeDA( |
| 440 |
SITE_SPECIFIC_PREFIX + codonLabeling.getText(), |
| 441 |
baseTrait, |
| 442 |
includeExternalBranches, |
| 443 |
includeInternalBranches) { |
| 444 |
@Override |
| 445 |
public boolean getLoggable() { |
| 446 |
return false; |
| 447 |
} |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
TreeTrait sumOverSitesTrait = new TreeTrait.SumAcrossArrayD( |
| 452 |
codonLabeling.getText(), |
| 453 |
baseTrait) { |
| 454 |
@Override |
| 455 |
public boolean getLoggable() { |
| 456 |
return true; |
| 457 |
} |
| 458 |
}; |
| 459 |
|
| 460 |
|
| 461 |
String name = prefix != null ? prefix + TOTAL_PREFIX + codonLabeling.getText() : |
| 462 |
TOTAL_PREFIX + codonLabeling.getText(); |
| 463 |
TreeTrait sumOverSitesAndTreeTrait = new TreeTrait.SumOverTreeD( |
| 464 |
name, |
| 465 |
sumOverSitesTrait, |
| 466 |
includeExternalBranches, |
| 467 |
includeInternalBranches) { |
| 468 |
@Override |
| 469 |
public boolean getLoggable() { |
| 470 |
return true; |
| 471 |
} |
| 472 |
}; |
| 473 |
|
| 474 |
|
| 475 |
treeTraitLogger = new TreeTraitLogger( |
| 476 |
tree, |
| 477 |
new TreeTrait[]{sumOverSitesAndTreeTrait} |
| 478 |
); |
| 479 |
|
| 480 |
treeTraits.addTrait(baseTrait); |
| 481 |
treeTraits.addTrait(unconditionedSum); |
| 482 |
treeTraits.addTrait(sumOverSitesTrait); |
| 483 |
treeTraits.addTrait(sumOverTreeTrait); |
| 484 |
treeTraits.addTrait(sumOverSitesAndTreeTrait); |
| 485 |
|
| 486 |
if (doUnconditionedPerBranch) { |
| 487 |
TreeTrait unconditionedBase = new TreeTrait.DA() { |
| 488 |
|
| 489 |
public String getTraitName() { |
| 490 |
return UNCONDITIONED_PER_BRANCH_PREFIX + codonLabeling.getText(); |
| 491 |
} |
| 492 |
|
| 493 |
public Intent getIntent() { |
| 494 |
return Intent.BRANCH; |
| 495 |
} |
| 496 |
|
| 497 |
public double[] getTrait(Tree tree, NodeRef node) { |
| 498 |
return getUnconditionalCountsForBranch(node); |
| 499 |
} |
| 500 |
|
| 501 |
public boolean getLoggable() { |
| 502 |
return false; |
| 503 |
} |
| 504 |
}; |
| 505 |
|
| 506 |
TreeTrait sumUnconditionedOverSitesTrait = new TreeTrait.SumAcrossArrayD( |
| 507 |
UNCONDITIONED_PER_BRANCH_PREFIX + codonLabeling.getText(), |
| 508 |
unconditionedBase) { |
| 509 |
@Override |
| 510 |
public boolean getLoggable() { |
| 511 |
return true; |
| 512 |
} |
| 513 |
}; |
| 514 |
|
| 515 |
String nameU = prefix != null ? prefix + UNCONDITIONED_TOTAL_PREFIX + codonLabeling.getText() : |
| 516 |
UNCONDITIONED_TOTAL_PREFIX + codonLabeling.getText(); |
| 517 |
TreeTrait sumUnconditionedOverSitesAndTreeTrait = new TreeTrait.SumOverTreeD( |
| 518 |
nameU, |
| 519 |
sumUnconditionedOverSitesTrait, |
| 520 |
includeExternalBranches, |
| 521 |
includeInternalBranches) { |
| 522 |
public boolean getLoggable() { |
| 523 |
return true; |
| 524 |
} |
| 525 |
}; |
| 526 |
|
| 527 |
treeTraitLogger = new TreeTraitLogger(tree, |
| 528 |
new TreeTrait[]{sumOverSitesAndTreeTrait, sumUnconditionedOverSitesAndTreeTrait}); |
| 529 |
|
| 530 |
treeTraits.addTrait(unconditionedBase); |
| 531 |
treeTraits.addTrait(sumUnconditionedOverSitesTrait); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
public TreeTrait[] getTreeTraits() { |
| 536 |
return treeTraits.getTreeTraits(); |
| 537 |
} |
| 538 |
|
| 539 |
public TreeTrait getTreeTrait(String key) { |
| 540 |
return treeTraits.getTreeTrait(key); |
| 541 |
} |
| 542 |
|
| 543 |
private int getCanonicalState(int i, int j, int k) { |
| 544 |
return i * 16 + j * 4 + k; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public LogColumn[] getColumns() { |
| 561 |
return treeTraitLogger.getColumns(); |
| 562 |
} |
| 563 |
|
| 564 |
public int getDimension() { |
| 565 |
return numCodons; |
| 566 |
} |
| 567 |
|
| 568 |
private void computeAllUnconditionalCountsPerBranch() { |
| 569 |
if (unconditionedCountsPerBranch == null) { |
| 570 |
unconditionedCountsPerBranch = new double[tree.getNodeCount()][numCodons]; |
| 571 |
} |
| 572 |
double[] rootDistribution = getUnconditionalRootDistribution(); |
| 573 |
for (int i = 0; i < tree.getNodeCount(); i++) { |
| 574 |
NodeRef node = tree.getNode(i); |
| 575 |
if (!tree.isRoot(node)) { |
| 576 |
final double expectedLength = getExpectedBranchLength(node); |
| 577 |
fillInUnconditionalTraitValues(expectedLength, rootDistribution, unconditionedCountsPerBranch[node.getNumber()]); |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
private void computeUnconditionedTraitValues() { |
| 583 |
if (unconditionedCounts == null) { |
| 584 |
unconditionedCounts = new double[numCodons]; |
| 585 |
} |
| 586 |
final double treeLength = getExpectedTreeLength(); |
| 587 |
double[] rootDistribution = getUnconditionalRootDistribution(); |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
fillInUnconditionalTraitValues(treeLength, rootDistribution, unconditionedCounts); |
| 603 |
} |
| 604 |
|
| 605 |
private double[] getUnconditionalRootDistribution() { |
| 606 |
if (forceUnconditionalAverageRate) { |
| 607 |
return averagedProductChainModel.getFrequencyModel().getFrequencies(); |
| 608 |
} else { |
| 609 |
return productChainModel.getFrequencyModel().getFrequencies(); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
private void fillInUnconditionalQMatrix(double[] lambda) { |
| 614 |
if (forceUnconditionalAverageRate) { |
| 615 |
averagedProductChainModel.getInfinitesimalMatrix(lambda); |
| 616 |
} else { |
| 617 |
productChainModel.getInfinitesimalMatrix(lambda); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
private void fillInUnconditionalTraitValues(double expectedLength, double[] freq, double[] out) { |
| 622 |
final int stateCount = 64; |
| 623 |
double[] lambda = new double[stateCount * stateCount]; |
| 624 |
fillInUnconditionalQMatrix(lambda); |
| 625 |
for (int i = 0; i < numCodons; i++) { |
| 626 |
final int startingState = MathUtils.randomChoicePDF(freq); |
| 627 |
StateHistory history = StateHistory.simulateUnconditionalOnEndingState( |
| 628 |
0.0, |
| 629 |
startingState, |
| 630 |
expectedLength, |
| 631 |
lambda, |
| 632 |
stateCount |
| 633 |
); |
| 634 |
out[i] = markovJumps.getProcessForSimulant(history); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
private double[] getUnconditionedTraitValues() { |
| 639 |
if (!unconditionsKnown) { |
| 640 |
computeUnconditionedTraitValues(); |
| 641 |
unconditionsKnown = true; |
| 642 |
} |
| 643 |
return unconditionedCounts; |
| 644 |
} |
| 645 |
|
| 646 |
public Double getUnconditionedTraitValue() { |
| 647 |
if (!TRIAL) { |
| 648 |
throw new RuntimeException("Believed broken for neutral models"); |
| 649 |
|
| 650 |
} else { |
| 651 |
final double treeLength = getExpectedTreeLength(); |
| 652 |
double[] rootDistribution = getUnconditionalRootDistribution(); |
| 653 |
final int startingState = MathUtils.randomChoicePDF(rootDistribution); |
| 654 |
final int stateCount = 64; |
| 655 |
double[] lambda = new double[stateCount * stateCount]; |
| 656 |
fillInUnconditionalQMatrix(lambda); |
| 657 |
StateHistory history = StateHistory.simulateUnconditionalOnEndingState( |
| 658 |
0.0, |
| 659 |
startingState, |
| 660 |
treeLength, |
| 661 |
lambda, |
| 662 |
stateCount |
| 663 |
); |
| 664 |
return markovJumps.getProcessForSimulant(history); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
private double getExpectedBranchLength(NodeRef node) { |
| 669 |
return branchRateModel.getBranchRate(tree, node) * tree.getBranchLength(node); |
| 670 |
} |
| 671 |
|
| 672 |
private double getExpectedTreeLength() { |
| 673 |
double expectedTreeLength = 0; |
| 674 |
if (includeExternalBranches) { |
| 675 |
for (int i = 0; i < tree.getExternalNodeCount(); i++) { |
| 676 |
NodeRef node = tree.getExternalNode(i); |
| 677 |
expectedTreeLength += getExpectedBranchLength(node); |
| 678 |
} |
| 679 |
} |
| 680 |
if (includeInternalBranches) { |
| 681 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 682 |
NodeRef node = tree.getInternalNode(i); |
| 683 |
if (!tree.isRoot(node)) { |
| 684 |
expectedTreeLength += getExpectedBranchLength(node); |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
return expectedTreeLength; |
| 689 |
} |
| 690 |
|
| 691 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 692 |
countsKnown = false; |
| 693 |
unconditionsKnown = false; |
| 694 |
unconditionsPerBranchKnown = false; |
| 695 |
} |
| 696 |
|
| 697 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 698 |
countsKnown = false; |
| 699 |
unconditionsKnown = false; |
| 700 |
} |
| 701 |
|
| 702 |
protected void storeState() { |
| 703 |
|
| 704 |
} |
| 705 |
|
| 706 |
protected void restoreState() { |
| 707 |
countsKnown = false; |
| 708 |
unconditionsKnown = false; |
| 709 |
unconditionsPerBranchKnown = false; |
| 710 |
} |
| 711 |
|
| 712 |
protected void acceptState() { |
| 713 |
|
| 714 |
} |
| 715 |
|
| 716 |
private final AncestralStateBeagleTreeLikelihood[] partition; |
| 717 |
private final MarkovJumpsSubstitutionModel markovJumps; |
| 718 |
private MarkovJumpsSubstitutionModel averagedMarkovJumps = null; |
| 719 |
|
| 720 |
private final boolean forceUnconditionalAverageRate; |
| 721 |
|
| 722 |
private final boolean useUniformization; |
| 723 |
private final BranchRateModel branchRateModel; |
| 724 |
private final ProductChainSubstitutionModel productChainModel; |
| 725 |
private ProductChainSubstitutionModel averagedProductChainModel = null; |
| 726 |
|
| 727 |
private final CodonLabeling codonLabeling; |
| 728 |
private final Tree tree; |
| 729 |
|
| 730 |
private final String prefix; |
| 731 |
|
| 732 |
private final StratifiedTraitOutputFormat branchFormat; |
| 733 |
private final StratifiedTraitOutputFormat logFormat; |
| 734 |
|
| 735 |
private final double[] condMeanMatrix; |
| 736 |
|
| 737 |
private int numCodons; |
| 738 |
|
| 739 |
private boolean countsKnown = false; |
| 740 |
private boolean unconditionsKnown = false; |
| 741 |
private boolean unconditionsPerBranchKnown = false; |
| 742 |
private double[] unconditionedCounts; |
| 743 |
private double[][] unconditionedCountsPerBranch; |
| 744 |
private double[][] computedCounts; |
| 745 |
|
| 746 |
private String[][] completeHistoryPerNode; |
| 747 |
|
| 748 |
protected Helper treeTraits = new Helper(); |
| 749 |
protected TreeTraitLogger treeTraitLogger; |
| 750 |
|
| 751 |
private final boolean includeExternalBranches; |
| 752 |
private final boolean includeInternalBranches; |
| 753 |
private final boolean doUnconditionedPerBranch; |
| 754 |
|
| 755 |
private static final boolean TRIAL = true; |
| 756 |
|
| 757 |
private boolean saveCompleteHistory = false; |
| 758 |
|
| 759 |
private boolean tryNewNeutralModel = false; |
| 760 |
|
| 761 |
@Override |
| 762 |
public Citation.Category getCategory() { |
| 763 |
return Citation.Category.COUNTING_PROCESSES; |
| 764 |
} |
| 765 |
|
| 766 |
@Override |
| 767 |
public String getDescription() { |
| 768 |
StringBuilder sb = new StringBuilder("Using robust counting (first citation) for labeled distances between sequences" + |
| 769 |
" to efficiently estimate site-specific dN/dS rate ratios (second citation)"); |
| 770 |
if (saveCompleteHistory) { |
| 771 |
sb.append(" and inferring the complete transition history (third citation)"); |
| 772 |
} |
| 773 |
return sb.toString(); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
@Override |
| 780 |
public List<Citation> getCitations() { |
| 781 |
List<Citation> list = new ArrayList<Citation>(); |
| 782 |
list.add(CommonCitations.OBRIEN_2009_LEARNING); |
| 783 |
list.add(CommonCitations.LEMEY_2012_RENAISSANCE); |
| 784 |
if (saveCompleteHistory) { |
| 785 |
list.add(CommonCitations.BLOOM_2013_STABILITY); |
| 786 |
} |
| 787 |
return list; |
| 788 |
} |
| 789 |
} |