| 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.treedatalikelihood; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
import beagle.*; |
| 39 |
import dr.evomodel.branchmodel.BranchModel; |
| 40 |
import dr.evomodel.siteratemodel.SiteRateModel; |
| 41 |
import dr.evomodel.treelikelihood.*; |
| 42 |
import dr.evolution.alignment.PatternList; |
| 43 |
import dr.evolution.alignment.UncertainSiteList; |
| 44 |
import dr.evolution.datatype.DataType; |
| 45 |
import dr.evolution.tree.Tree; |
| 46 |
import dr.evolution.util.TaxonList; |
| 47 |
import dr.evomodel.tipstatesmodel.TipStatesModel; |
| 48 |
import dr.inference.model.AbstractModel; |
| 49 |
import dr.inference.model.Model; |
| 50 |
import dr.inference.model.Parameter; |
| 51 |
import dr.inference.model.Variable; |
| 52 |
import dr.math.matrixAlgebra.Vector; |
| 53 |
import dr.util.Citable; |
| 54 |
import dr.util.Citation; |
| 55 |
import dr.util.CommonCitations; |
| 56 |
|
| 57 |
import java.util.ArrayList; |
| 58 |
import java.util.Collections; |
| 59 |
import java.util.List; |
| 60 |
import java.util.logging.Logger; |
| 61 |
|
| 62 |
public class BeagleDataLikelihoodDelegate extends AbstractModel implements DataLikelihoodDelegate, Citable { |
| 63 |
|
| 64 |
public static boolean IS_THREAD_COUNT_COMPATIBLE() { |
| 65 |
int[] versionNumbers = BeagleInfo.getVersionNumbers(); |
| 66 |
return versionNumbers.length != 0 && versionNumbers[0] >= 3 && versionNumbers[1] >= 1; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private static final String RESOURCE_AUTO_PROPERTY = "beagle.resource.auto"; |
| 73 |
private static final String RESOURCE_ORDER_PROPERTY = "beagle.resource.order"; |
| 74 |
private static final String PREFERRED_FLAGS_PROPERTY = "beagle.preferred.flags"; |
| 75 |
private static final String REQUIRED_FLAGS_PROPERTY = "beagle.required.flags"; |
| 76 |
private static final String SCALING_PROPERTY = "beagle.scaling"; |
| 77 |
private static final String RESCALE_FREQUENCY_PROPERTY = "beagle.rescale"; |
| 78 |
private static final String DELAY_SCALING_PROPERTY = "beagle.delay.scaling"; |
| 79 |
private static final String EXTRA_BUFFER_COUNT_PROPERTY = "beagle.extra.buffer.count"; |
| 80 |
private static final String FORCE_VECTORIZATION = "beagle.force.vectorization"; |
| 81 |
private static final String THREAD_COUNT = "beagle.thread.count"; |
| 82 |
|
| 83 |
|
| 84 |
private static final PartialsRescalingScheme DEFAULT_RESCALING_SCHEME = PartialsRescalingScheme.DYNAMIC; |
| 85 |
|
| 86 |
private static int instanceCount = 0; |
| 87 |
private static List<Integer> resourceOrder = null; |
| 88 |
private static List<Integer> preferredOrder = null; |
| 89 |
private static List<Integer> requiredOrder = null; |
| 90 |
private static List<String> scalingOrder = null; |
| 91 |
private static List<Integer> extraBufferOrder = null; |
| 92 |
|
| 93 |
|
| 94 |
private static final int RESCALE_FREQUENCY = 100; |
| 95 |
private static final int RESCALE_TIMES = 1; |
| 96 |
|
| 97 |
private static final boolean RESCALING_OFF = false; |
| 98 |
|
| 99 |
private static final boolean DEBUG = false; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public BeagleDataLikelihoodDelegate(Tree tree, |
| 110 |
PatternList patternList, |
| 111 |
BranchModel branchModel, |
| 112 |
SiteRateModel siteRateModel, |
| 113 |
boolean useAmbiguities, |
| 114 |
PartialsRescalingScheme rescalingScheme, |
| 115 |
boolean delayRescalingUntilUnderflow) { |
| 116 |
|
| 117 |
super("BeagleDataLikelihoodDelegate"); |
| 118 |
final Logger logger = Logger.getLogger("dr.evomodel"); |
| 119 |
|
| 120 |
logger.info("\nUsing BEAGLE DataLikelihood Delegate"); |
| 121 |
setId(patternList.getId()); |
| 122 |
|
| 123 |
this.dataType = patternList.getDataType(); |
| 124 |
this.patternList = patternList; |
| 125 |
patternCount = patternList.getPatternCount(); |
| 126 |
stateCount = dataType.getStateCount(); |
| 127 |
|
| 128 |
|
| 129 |
int stateCount2 = branchModel.getRootFrequencyModel().getFrequencyCount(); |
| 130 |
if (stateCount != stateCount2) { |
| 131 |
throw new IllegalArgumentException("Pattern state count (" + stateCount |
| 132 |
+ ") does not match substitution model state count (" + stateCount2 + ")"); |
| 133 |
} |
| 134 |
|
| 135 |
patternWeights = patternList.getPatternWeights(); |
| 136 |
|
| 137 |
this.branchModel = branchModel; |
| 138 |
addModel(this.branchModel); |
| 139 |
|
| 140 |
this.siteRateModel = siteRateModel; |
| 141 |
addModel(this.siteRateModel); |
| 142 |
|
| 143 |
this.categoryCount = this.siteRateModel.getCategoryCount(); |
| 144 |
|
| 145 |
nodeCount = tree.getNodeCount(); |
| 146 |
tipCount = tree.getExternalNodeCount(); |
| 147 |
internalNodeCount = nodeCount - tipCount; |
| 148 |
|
| 149 |
branchUpdateIndices = new int[nodeCount]; |
| 150 |
branchLengths = new double[nodeCount]; |
| 151 |
scaleBufferIndices = new int[internalNodeCount]; |
| 152 |
storedScaleBufferIndices = new int[internalNodeCount]; |
| 153 |
|
| 154 |
operations = new int[internalNodeCount * Beagle.OPERATION_TUPLE_SIZE]; |
| 155 |
|
| 156 |
firstRescaleAttempt = true; |
| 157 |
|
| 158 |
try { |
| 159 |
|
| 160 |
int compactPartialsCount = tipCount; |
| 161 |
if (useAmbiguities) { |
| 162 |
|
| 163 |
compactPartialsCount = 0; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
partialBufferHelper = new BufferIndexHelper(nodeCount, tipCount); |
| 168 |
|
| 169 |
|
| 170 |
scaleBufferHelper = new BufferIndexHelper(getScaleBufferCount(), 0); |
| 171 |
|
| 172 |
evolutionaryProcessDelegate = new HomogenousSubstitutionModelDelegate(tree, branchModel); |
| 173 |
|
| 174 |
|
| 175 |
if (resourceOrder == null) { |
| 176 |
resourceOrder = parseSystemPropertyIntegerArray(RESOURCE_ORDER_PROPERTY); |
| 177 |
} |
| 178 |
if (preferredOrder == null) { |
| 179 |
preferredOrder = parseSystemPropertyIntegerArray(PREFERRED_FLAGS_PROPERTY); |
| 180 |
} |
| 181 |
if (requiredOrder == null) { |
| 182 |
requiredOrder = parseSystemPropertyIntegerArray(REQUIRED_FLAGS_PROPERTY); |
| 183 |
} |
| 184 |
if (scalingOrder == null) { |
| 185 |
scalingOrder = parseSystemPropertyStringArray(SCALING_PROPERTY); |
| 186 |
} |
| 187 |
if (extraBufferOrder == null) { |
| 188 |
extraBufferOrder = parseSystemPropertyIntegerArray(EXTRA_BUFFER_COUNT_PROPERTY); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
this.rescalingScheme = rescalingScheme; |
| 193 |
this.delayRescalingUntilUnderflow = delayRescalingUntilUnderflow; |
| 194 |
|
| 195 |
int[] resourceList = null; |
| 196 |
long preferenceFlags = 0; |
| 197 |
long requirementFlags = 0; |
| 198 |
|
| 199 |
if (scalingOrder.size() > 0) { |
| 200 |
this.rescalingScheme = PartialsRescalingScheme.parseFromString( |
| 201 |
scalingOrder.get(instanceCount % scalingOrder.size())); |
| 202 |
} |
| 203 |
|
| 204 |
if (resourceOrder.size() > 0) { |
| 205 |
|
| 206 |
resourceList = new int[]{resourceOrder.get(instanceCount % resourceOrder.size()), 0}; |
| 207 |
if (resourceList[0] > 0) { |
| 208 |
preferenceFlags |= BeagleFlag.PROCESSOR_GPU.getMask(); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (preferredOrder.size() > 0) { |
| 213 |
preferenceFlags = preferredOrder.get(instanceCount % preferredOrder.size()); |
| 214 |
} |
| 215 |
|
| 216 |
if (requiredOrder.size() > 0) { |
| 217 |
requirementFlags = requiredOrder.get(instanceCount % requiredOrder.size()); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if (this.rescalingScheme == PartialsRescalingScheme.DEFAULT) { |
| 223 |
|
| 224 |
if (resourceList != null && resourceList[0] > 1) { |
| 225 |
this.rescalingScheme = DEFAULT_RESCALING_SCHEME; |
| 226 |
} else { |
| 227 |
|
| 228 |
|
| 229 |
this.rescalingScheme = DEFAULT_RESCALING_SCHEME; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if (this.rescalingScheme == PartialsRescalingScheme.DELAYED) { |
| 235 |
this.delayRescalingUntilUnderflow = true; |
| 236 |
this.rescalingScheme = PartialsRescalingScheme.ALWAYS; |
| 237 |
} |
| 238 |
|
| 239 |
if (this.rescalingScheme == PartialsRescalingScheme.AUTO) { |
| 240 |
preferenceFlags |= BeagleFlag.SCALING_AUTO.getMask(); |
| 241 |
useAutoScaling = true; |
| 242 |
} else { |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
String r = System.getProperty(RESCALE_FREQUENCY_PROPERTY); |
| 247 |
if (r != null) { |
| 248 |
rescalingFrequency = Integer.parseInt(r); |
| 249 |
if (rescalingFrequency < 1) { |
| 250 |
rescalingFrequency = RESCALE_FREQUENCY; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
String d = System.getProperty(DELAY_SCALING_PROPERTY); |
| 255 |
if (d != null) { |
| 256 |
this.delayRescalingUntilUnderflow = Boolean.parseBoolean(d); |
| 257 |
} |
| 258 |
|
| 259 |
if (preferenceFlags == 0 && resourceList == null) { |
| 260 |
if (stateCount == 4 && patternList.getPatternCount() < 10000) |
| 261 |
preferenceFlags |= BeagleFlag.PROCESSOR_CPU.getMask(); |
| 262 |
} |
| 263 |
|
| 264 |
boolean forceVectorization = false; |
| 265 |
String vectorizationString = System.getProperty(FORCE_VECTORIZATION); |
| 266 |
if (vectorizationString != null) { |
| 267 |
forceVectorization = true; |
| 268 |
} |
| 269 |
|
| 270 |
String tc = System.getProperty(THREAD_COUNT); |
| 271 |
if (tc != null) { |
| 272 |
threadCount = Integer.parseInt(tc); |
| 273 |
if (threadCount < 2) { |
| 274 |
threadCount = 1; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (BeagleFlag.VECTOR_SSE.isSet(preferenceFlags) && (stateCount != 4) |
| 279 |
&& !forceVectorization |
| 280 |
) { |
| 281 |
|
| 282 |
|
| 283 |
preferenceFlags &= ~BeagleFlag.VECTOR_SSE.getMask(); |
| 284 |
preferenceFlags |= BeagleFlag.VECTOR_NONE.getMask(); |
| 285 |
|
| 286 |
if (stateCount > 4 && this.rescalingScheme == PartialsRescalingScheme.DYNAMIC) { |
| 287 |
this.rescalingScheme = PartialsRescalingScheme.DELAYED; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (!BeagleFlag.PRECISION_SINGLE.isSet(preferenceFlags)) { |
| 292 |
|
| 293 |
preferenceFlags |= BeagleFlag.PRECISION_DOUBLE.getMask(); |
| 294 |
} |
| 295 |
|
| 296 |
if (evolutionaryProcessDelegate.canReturnComplexDiagonalization()) { |
| 297 |
requirementFlags |= BeagleFlag.EIGEN_COMPLEX.getMask(); |
| 298 |
} |
| 299 |
|
| 300 |
if ((resourceList == null && |
| 301 |
(BeagleFlag.PROCESSOR_GPU.isSet(preferenceFlags) || |
| 302 |
BeagleFlag.FRAMEWORK_CUDA.isSet(preferenceFlags) || |
| 303 |
BeagleFlag.FRAMEWORK_OPENCL.isSet(preferenceFlags))) |
| 304 |
|| |
| 305 |
(resourceList != null && resourceList[0] > 0)) { |
| 306 |
|
| 307 |
|
| 308 |
preferenceFlags &= ~BeagleFlag.VECTOR_SSE.getMask(); |
| 309 |
preferenceFlags &= ~BeagleFlag.THREADING_CPP.getMask(); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
String resourceAuto = System.getProperty(RESOURCE_AUTO_PROPERTY); |
| 314 |
if (resourceAuto != null && Boolean.parseBoolean(resourceAuto)) { |
| 315 |
|
| 316 |
long benchmarkFlags = 0; |
| 317 |
|
| 318 |
if (this.rescalingScheme == PartialsRescalingScheme.NONE) { |
| 319 |
benchmarkFlags = BeagleBenchmarkFlag.SCALING_NONE.getMask(); |
| 320 |
} else if (this.rescalingScheme == PartialsRescalingScheme.ALWAYS) { |
| 321 |
benchmarkFlags = BeagleBenchmarkFlag.SCALING_ALWAYS.getMask(); |
| 322 |
} else { |
| 323 |
benchmarkFlags = BeagleBenchmarkFlag.SCALING_DYNAMIC.getMask(); |
| 324 |
} |
| 325 |
|
| 326 |
logger.info("\nRunning benchmarks to automatically select fastest BEAGLE resource for analysis or partition... "); |
| 327 |
|
| 328 |
List<BenchmarkedResourceDetails> benchmarkedResourceDetails = |
| 329 |
BeagleFactory.getBenchmarkedResourceDetails( |
| 330 |
tipCount, |
| 331 |
compactPartialsCount, |
| 332 |
stateCount, |
| 333 |
patternCount, |
| 334 |
categoryCount, |
| 335 |
resourceList, |
| 336 |
preferenceFlags, |
| 337 |
requirementFlags, |
| 338 |
1, |
| 339 |
1, |
| 340 |
0, |
| 341 |
benchmarkFlags); |
| 342 |
|
| 343 |
|
| 344 |
logger.info(" Benchmark results, from fastest to slowest:"); |
| 345 |
|
| 346 |
for (BenchmarkedResourceDetails benchmarkedResource : benchmarkedResourceDetails) { |
| 347 |
logger.info(benchmarkedResource.toString()); |
| 348 |
} |
| 349 |
|
| 350 |
resourceList = new int[]{benchmarkedResourceDetails.get(0).getResourceNumber()}; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
beagle = BeagleFactory.loadBeagleInstance( |
| 355 |
tipCount, |
| 356 |
partialBufferHelper.getBufferCount(), |
| 357 |
compactPartialsCount, |
| 358 |
stateCount, |
| 359 |
patternCount, |
| 360 |
evolutionaryProcessDelegate.getEigenBufferCount(), |
| 361 |
evolutionaryProcessDelegate.getMatrixBufferCount(), |
| 362 |
categoryCount, |
| 363 |
scaleBufferHelper.getBufferCount(), |
| 364 |
resourceList, |
| 365 |
preferenceFlags, |
| 366 |
requirementFlags |
| 367 |
); |
| 368 |
|
| 369 |
InstanceDetails instanceDetails = beagle.getDetails(); |
| 370 |
ResourceDetails resourceDetails = null; |
| 371 |
|
| 372 |
if (instanceDetails != null) { |
| 373 |
resourceDetails = BeagleFactory.getResourceDetails(instanceDetails.getResourceNumber()); |
| 374 |
if (resourceDetails != null) { |
| 375 |
StringBuilder sb = new StringBuilder(" Using BEAGLE resource "); |
| 376 |
sb.append(resourceDetails.getNumber()).append(": "); |
| 377 |
sb.append(resourceDetails.getName()).append("\n"); |
| 378 |
if (resourceDetails.getDescription() != null) { |
| 379 |
String[] description = resourceDetails.getDescription().split("\\|"); |
| 380 |
for (String desc : description) { |
| 381 |
if (desc.trim().length() > 0) { |
| 382 |
sb.append(" ").append(desc.trim()).append("\n"); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
sb.append(" with instance flags: ").append(instanceDetails.toString()); |
| 387 |
logger.info(sb.toString()); |
| 388 |
} else { |
| 389 |
logger.info(" Error retrieving BEAGLE resource for instance: " + instanceDetails.toString()); |
| 390 |
} |
| 391 |
} else { |
| 392 |
logger.info(" No external BEAGLE resources available, or resource list/requirements not met, using Java implementation"); |
| 393 |
} |
| 394 |
|
| 395 |
instanceFlags = instanceDetails.getFlags(); |
| 396 |
|
| 397 |
if (IS_THREAD_COUNT_COMPATIBLE() && threadCount > 1) { |
| 398 |
beagle.setCPUThreadCount(threadCount); |
| 399 |
} |
| 400 |
|
| 401 |
if (patternList instanceof UncertainSiteList) { |
| 402 |
useAmbiguities = true; |
| 403 |
} |
| 404 |
|
| 405 |
logger.info(" " + (useAmbiguities ? "Using" : "Ignoring") + " ambiguities in tree likelihood."); |
| 406 |
logger.info(" With " + patternList.getPatternCount() + " unique site patterns."); |
| 407 |
|
| 408 |
if (patternList.areUncertain() && !useAmbiguities) { |
| 409 |
logger.info(" WARNING: Uncertain site patterns will be ignored."); |
| 410 |
} |
| 411 |
|
| 412 |
for (int i = 0; i < tipCount; i++) { |
| 413 |
|
| 414 |
String id = tree.getTaxonId(i); |
| 415 |
int index = patternList.getTaxonIndex(id); |
| 416 |
|
| 417 |
if (index == -1) { |
| 418 |
throw new TaxonList.MissingTaxonException("Taxon, " + id + ", in tree, " + tree.getId() + |
| 419 |
", is not found in patternList, " + patternList.getId()); |
| 420 |
} else { |
| 421 |
if (useAmbiguities) { |
| 422 |
setPartials(beagle, patternList, index, i); |
| 423 |
} else { |
| 424 |
setStates(beagle, patternList, index, i); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
beagle.setPatternWeights(patternWeights); |
| 430 |
|
| 431 |
String rescaleMessage = " Using rescaling scheme : " + this.rescalingScheme.getText(); |
| 432 |
if (this.rescalingScheme == PartialsRescalingScheme.AUTO && |
| 433 |
resourceDetails != null && |
| 434 |
(resourceDetails.getFlags() & BeagleFlag.SCALING_AUTO.getMask()) == 0) { |
| 435 |
|
| 436 |
this.rescalingScheme = PartialsRescalingScheme.DYNAMIC; |
| 437 |
rescaleMessage = " Auto rescaling not supported in BEAGLE, using : " + this.rescalingScheme.getText(); |
| 438 |
} |
| 439 |
boolean parenthesis = false; |
| 440 |
if (this.rescalingScheme == PartialsRescalingScheme.DYNAMIC) { |
| 441 |
rescaleMessage += " (rescaling every " + rescalingFrequency + " evaluations"; |
| 442 |
parenthesis = true; |
| 443 |
} |
| 444 |
if (this.delayRescalingUntilUnderflow) { |
| 445 |
rescaleMessage += (parenthesis ? ", " : "(") + "delay rescaling until first overflow"; |
| 446 |
parenthesis = true; |
| 447 |
} |
| 448 |
rescaleMessage += (parenthesis ? ")" : ""); |
| 449 |
logger.info(rescaleMessage); |
| 450 |
|
| 451 |
if (this.rescalingScheme == PartialsRescalingScheme.DYNAMIC) { |
| 452 |
everUnderflowed = false; |
| 453 |
} |
| 454 |
|
| 455 |
updateSubstitutionModel = true; |
| 456 |
updateSiteModel = true; |
| 457 |
|
| 458 |
} catch (TaxonList.MissingTaxonException mte) { |
| 459 |
throw new RuntimeException(mte.toString()); |
| 460 |
} |
| 461 |
|
| 462 |
instanceCount++; |
| 463 |
} |
| 464 |
|
| 465 |
@Override |
| 466 |
public String getReport() { |
| 467 |
return null; |
| 468 |
} |
| 469 |
|
| 470 |
@Override |
| 471 |
public TreeTraversal.TraversalType getOptimalTraversalType() { |
| 472 |
if ((instanceFlags & BeagleFlag.FRAMEWORK_CPU.getMask()) != 0) { |
| 473 |
return TreeTraversal.TraversalType.POST_ORDER; |
| 474 |
} else { |
| 475 |
return TreeTraversal.TraversalType.REVERSE_LEVEL_ORDER; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
@Override |
| 480 |
public int getTraitCount() { |
| 481 |
return 1; |
| 482 |
} |
| 483 |
|
| 484 |
@Override |
| 485 |
public int getTraitDim() { |
| 486 |
return patternCount; |
| 487 |
} |
| 488 |
|
| 489 |
@Override |
| 490 |
public RateRescalingScheme getRateRescalingScheme() { |
| 491 |
return RateRescalingScheme.NONE; |
| 492 |
} |
| 493 |
|
| 494 |
public PatternList getPatternList() { |
| 495 |
return this.patternList; |
| 496 |
} |
| 497 |
|
| 498 |
private static List<Integer> parseSystemPropertyIntegerArray(String propertyName) { |
| 499 |
List<Integer> order = new ArrayList<Integer>(); |
| 500 |
String r = System.getProperty(propertyName); |
| 501 |
if (r != null) { |
| 502 |
String[] parts = r.split(","); |
| 503 |
for (String part : parts) { |
| 504 |
try { |
| 505 |
int n = Integer.parseInt(part.trim()); |
| 506 |
order.add(n); |
| 507 |
} catch (NumberFormatException nfe) { |
| 508 |
System.err.println("Invalid entry '" + part + "' in " + propertyName); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
return order; |
| 513 |
} |
| 514 |
|
| 515 |
private static List<String> parseSystemPropertyStringArray(String propertyName) { |
| 516 |
|
| 517 |
List<String> order = new ArrayList<String>(); |
| 518 |
|
| 519 |
String r = System.getProperty(propertyName); |
| 520 |
if (r != null) { |
| 521 |
String[] parts = r.split(","); |
| 522 |
for (String part : parts) { |
| 523 |
try { |
| 524 |
String s = part.trim(); |
| 525 |
order.add(s); |
| 526 |
} catch (NumberFormatException nfe) { |
| 527 |
System.err.println("Invalid entry '" + part + "' in " + propertyName); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
return order; |
| 532 |
} |
| 533 |
|
| 534 |
private int getScaleBufferCount() { |
| 535 |
return internalNodeCount + 1; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
private final void setPartials(Beagle beagle, |
| 547 |
PatternList patternList, |
| 548 |
int sequenceIndex, |
| 549 |
int nodeIndex) { |
| 550 |
double[] partials = new double[patternCount * stateCount * categoryCount]; |
| 551 |
|
| 552 |
int v = 0; |
| 553 |
for (int i = 0; i < patternCount; i++) { |
| 554 |
|
| 555 |
if (patternList instanceof UncertainSiteList) { |
| 556 |
((UncertainSiteList) patternList).fillPartials(sequenceIndex, i, partials, v); |
| 557 |
v += stateCount; |
| 558 |
|
| 559 |
} else if (patternList.areUncertain()) { |
| 560 |
|
| 561 |
double[] prob = patternList.getUncertainPatternState(sequenceIndex, i); |
| 562 |
System.arraycopy(prob, 0, partials, v, stateCount); |
| 563 |
v += stateCount; |
| 564 |
|
| 565 |
} else { |
| 566 |
int state = patternList.getPatternState(sequenceIndex, i); |
| 567 |
boolean[] stateSet = dataType.getStateSet(state); |
| 568 |
|
| 569 |
for (int j = 0; j < stateCount; j++) { |
| 570 |
if (stateSet[j]) { |
| 571 |
partials[v] = 1.0; |
| 572 |
} else { |
| 573 |
partials[v] = 0.0; |
| 574 |
} |
| 575 |
v++; |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
int n = patternCount * stateCount; |
| 582 |
int k = n; |
| 583 |
for (int i = 1; i < categoryCount; i++) { |
| 584 |
System.arraycopy(partials, 0, partials, k, n); |
| 585 |
k += n; |
| 586 |
} |
| 587 |
|
| 588 |
beagle.setPartials(nodeIndex, partials); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
private final void setPartials(Beagle beagle, |
| 595 |
TipStatesModel tipStatesModel, |
| 596 |
int nodeIndex) { |
| 597 |
double[] partials = new double[patternCount * stateCount * categoryCount]; |
| 598 |
|
| 599 |
tipStatesModel.getTipPartials(nodeIndex, partials); |
| 600 |
|
| 601 |
|
| 602 |
int n = patternCount * stateCount; |
| 603 |
int k = n; |
| 604 |
for (int i = 1; i < categoryCount; i++) { |
| 605 |
System.arraycopy(partials, 0, partials, k, n); |
| 606 |
k += n; |
| 607 |
} |
| 608 |
|
| 609 |
beagle.setPartials(nodeIndex, partials); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
private final void setStates(Beagle beagle, |
| 621 |
PatternList patternList, |
| 622 |
int sequenceIndex, |
| 623 |
int nodeIndex) { |
| 624 |
int i; |
| 625 |
|
| 626 |
int[] states = new int[patternCount]; |
| 627 |
|
| 628 |
for (i = 0; i < patternCount; i++) { |
| 629 |
|
| 630 |
states[i] = patternList.getPatternState(sequenceIndex, i); |
| 631 |
} |
| 632 |
|
| 633 |
beagle.setTipStates(nodeIndex, states); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
@Override |
| 642 |
public double calculateLikelihood(List<BranchOperation> branchOperations, List<NodeOperation> nodeOperations, int rootNodeNumber) throws LikelihoodException { |
| 643 |
|
| 644 |
|
| 645 |
if (DEBUG) { |
| 646 |
System.out.println("Partition: " + this.getModelName()); |
| 647 |
} |
| 648 |
|
| 649 |
if (!this.delayRescalingUntilUnderflow || everUnderflowed) { |
| 650 |
if (this.rescalingScheme == PartialsRescalingScheme.ALWAYS || this.rescalingScheme == PartialsRescalingScheme.DELAYED) { |
| 651 |
useScaleFactors = true; |
| 652 |
recomputeScaleFactors = true; |
| 653 |
} else if (this.rescalingScheme == PartialsRescalingScheme.DYNAMIC) { |
| 654 |
useScaleFactors = true; |
| 655 |
|
| 656 |
if (rescalingCount > rescalingFrequency) { |
| 657 |
if (DEBUG) { |
| 658 |
System.out.println("rescalingCount > rescalingFrequency"); |
| 659 |
} |
| 660 |
rescalingCount = 0; |
| 661 |
rescalingCountInner = 0; |
| 662 |
} |
| 663 |
|
| 664 |
if (DEBUG) { |
| 665 |
System.out.println("rescalingCountInner = " + rescalingCountInner); |
| 666 |
} |
| 667 |
|
| 668 |
if (rescalingCountInner < RESCALE_TIMES) { |
| 669 |
if (DEBUG) { |
| 670 |
System.out.println("rescalingCountInner < RESCALE_TIMES"); |
| 671 |
} |
| 672 |
|
| 673 |
recomputeScaleFactors = true; |
| 674 |
|
| 675 |
rescalingCountInner++; |
| 676 |
|
| 677 |
throw new LikelihoodRescalingException(); |
| 678 |
|
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
if (initialEvaluation) { |
| 684 |
if (underflowHandling < 1) { |
| 685 |
underflowHandling++; |
| 686 |
if (DEBUG) { |
| 687 |
System.out.println("underflowHandling < 1"); |
| 688 |
} |
| 689 |
} else if (underflowHandling == 1) { |
| 690 |
if (DEBUG) { |
| 691 |
System.out.println("underflowHandling == 1"); |
| 692 |
} |
| 693 |
recomputeScaleFactors = true; |
| 694 |
underflowHandling++; |
| 695 |
initialEvaluation = false; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
rescalingCount++; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
if (RESCALING_OFF) { |
| 704 |
useScaleFactors = false; |
| 705 |
recomputeScaleFactors = false; |
| 706 |
} |
| 707 |
|
| 708 |
int branchUpdateCount = 0; |
| 709 |
for (BranchOperation op : branchOperations) { |
| 710 |
branchUpdateIndices[branchUpdateCount] = op.getBranchNumber(); |
| 711 |
branchLengths[branchUpdateCount] = op.getBranchLength(); |
| 712 |
branchUpdateCount ++; |
| 713 |
} |
| 714 |
|
| 715 |
if (updateSubstitutionModel) { |
| 716 |
evolutionaryProcessDelegate.updateSubstitutionModels(beagle, flip); |
| 717 |
|
| 718 |
|
| 719 |
} |
| 720 |
|
| 721 |
if (updateSiteModel) { |
| 722 |
double[] categoryRates = this.siteRateModel.getCategoryRates(); |
| 723 |
if (categoryRates == null) { |
| 724 |
|
| 725 |
|
| 726 |
return Double.NEGATIVE_INFINITY; |
| 727 |
} |
| 728 |
beagle.setCategoryRates(categoryRates); |
| 729 |
} |
| 730 |
|
| 731 |
if (branchUpdateCount > 0) { |
| 732 |
evolutionaryProcessDelegate.updateTransitionMatrices( |
| 733 |
beagle, |
| 734 |
branchUpdateIndices, |
| 735 |
branchLengths, |
| 736 |
branchUpdateCount, |
| 737 |
flip); |
| 738 |
} |
| 739 |
|
| 740 |
if (flip) { |
| 741 |
|
| 742 |
for (NodeOperation op : nodeOperations) { |
| 743 |
partialBufferHelper.flipOffset(op.getNodeNumber()); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
int operationCount = nodeOperations.size(); |
| 748 |
int k = 0; |
| 749 |
for (NodeOperation op : nodeOperations) { |
| 750 |
int nodeNum = op.getNodeNumber(); |
| 751 |
|
| 752 |
operations[k] = partialBufferHelper.getOffsetIndex(nodeNum); |
| 753 |
|
| 754 |
if (useScaleFactors) { |
| 755 |
|
| 756 |
int n = nodeNum - tipCount; |
| 757 |
|
| 758 |
if (recomputeScaleFactors) { |
| 759 |
|
| 760 |
scaleBufferHelper.flipOffset(n); |
| 761 |
|
| 762 |
|
| 763 |
scaleBufferIndices[n] = scaleBufferHelper.getOffsetIndex(n); |
| 764 |
|
| 765 |
operations[k + 1] = scaleBufferIndices[n]; |
| 766 |
operations[k + 2] = Beagle.NONE; |
| 767 |
|
| 768 |
} else { |
| 769 |
operations[k + 1] = Beagle.NONE; |
| 770 |
operations[k + 2] = scaleBufferIndices[n]; |
| 771 |
} |
| 772 |
|
| 773 |
} else { |
| 774 |
|
| 775 |
if (useAutoScaling) { |
| 776 |
scaleBufferIndices[nodeNum - tipCount] = partialBufferHelper.getOffsetIndex(nodeNum); |
| 777 |
} |
| 778 |
operations[k + 1] = Beagle.NONE; |
| 779 |
operations[k + 2] = Beagle.NONE; |
| 780 |
} |
| 781 |
|
| 782 |
operations[k + 3] = partialBufferHelper.getOffsetIndex(op.getLeftChild()); |
| 783 |
operations[k + 4] = evolutionaryProcessDelegate.getMatrixIndex(op.getLeftChild()); |
| 784 |
operations[k + 5] = partialBufferHelper.getOffsetIndex(op.getRightChild()); |
| 785 |
operations[k + 6] = evolutionaryProcessDelegate.getMatrixIndex(op.getRightChild()); |
| 786 |
|
| 787 |
k += Beagle.OPERATION_TUPLE_SIZE; |
| 788 |
} |
| 789 |
|
| 790 |
beagle.updatePartials(operations, operationCount, Beagle.NONE); |
| 791 |
|
| 792 |
int rootIndex = partialBufferHelper.getOffsetIndex(rootNodeNumber); |
| 793 |
|
| 794 |
double[] categoryWeights = this.siteRateModel.getCategoryProportions(); |
| 795 |
|
| 796 |
|
| 797 |
double[] frequencies = evolutionaryProcessDelegate.getRootStateFrequencies(); |
| 798 |
|
| 799 |
int cumulateScaleBufferIndex = Beagle.NONE; |
| 800 |
|
| 801 |
if (useScaleFactors) { |
| 802 |
if (recomputeScaleFactors) { |
| 803 |
scaleBufferHelper.flipOffset(internalNodeCount); |
| 804 |
cumulateScaleBufferIndex = scaleBufferHelper.getOffsetIndex(internalNodeCount); |
| 805 |
beagle.resetScaleFactors(cumulateScaleBufferIndex); |
| 806 |
beagle.accumulateScaleFactors(scaleBufferIndices, internalNodeCount, cumulateScaleBufferIndex); |
| 807 |
} else { |
| 808 |
cumulateScaleBufferIndex = scaleBufferHelper.getOffsetIndex(internalNodeCount); |
| 809 |
} |
| 810 |
} else if (useAutoScaling) { |
| 811 |
beagle.accumulateScaleFactors(scaleBufferIndices, internalNodeCount, Beagle.NONE); |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
beagle.setCategoryWeights(0, categoryWeights); |
| 816 |
beagle.setStateFrequencies(0, frequencies); |
| 817 |
|
| 818 |
double[] sumLogLikelihoods = new double[1]; |
| 819 |
|
| 820 |
if (DEBUG) { |
| 821 |
System.out.println("useScaleFactors=" + useScaleFactors + " recomputeScaleFactors=" + recomputeScaleFactors + " (" + getId() + ")"); |
| 822 |
} |
| 823 |
|
| 824 |
beagle.calculateRootLogLikelihoods(new int[]{rootIndex}, new int[]{0}, new int[]{0}, |
| 825 |
new int[]{cumulateScaleBufferIndex}, 1, sumLogLikelihoods); |
| 826 |
|
| 827 |
double logL = sumLogLikelihoods[0]; |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if (Double.isNaN(logL) || Double.isInfinite(logL)) { |
| 837 |
|
| 838 |
if (DEBUG) { |
| 839 |
System.out.println("Double.isNaN(logL) || Double.isInfinite(logL) (" + getId() + ")"); |
| 840 |
} |
| 841 |
|
| 842 |
everUnderflowed = true; |
| 843 |
|
| 844 |
logL = Double.NEGATIVE_INFINITY; |
| 845 |
|
| 846 |
if (firstRescaleAttempt && (delayRescalingUntilUnderflow || rescalingScheme == PartialsRescalingScheme.DELAYED)) { |
| 847 |
|
| 848 |
if (rescalingScheme == PartialsRescalingScheme.DYNAMIC || (rescalingCount == 0)) { |
| 849 |
|
| 850 |
if (rescalingMessageCount % 1000 == 0) { |
| 851 |
if (rescalingMessageCount > 0) { |
| 852 |
Logger.getLogger("dr.evomodel").info("Underflow calculating likelihood (" + rescalingMessageCount + " messages not shown; " + getId() + ")."); |
| 853 |
} else { |
| 854 |
Logger.getLogger("dr.evomodel").info("Underflow calculating likelihood. Attempting a rescaling... (" + getId() + ")"); |
| 855 |
} |
| 856 |
} |
| 857 |
rescalingMessageCount += 1; |
| 858 |
} |
| 859 |
|
| 860 |
useScaleFactors = true; |
| 861 |
recomputeScaleFactors = true; |
| 862 |
|
| 863 |
firstRescaleAttempt = false; |
| 864 |
|
| 865 |
rescalingCount--; |
| 866 |
|
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
flip = false; |
| 873 |
underflowHandling = 0; |
| 874 |
throw new LikelihoodUnderflowException(); |
| 875 |
|
| 876 |
} else { |
| 877 |
|
| 878 |
firstRescaleAttempt = true; |
| 879 |
recomputeScaleFactors = false; |
| 880 |
flip = true; |
| 881 |
|
| 882 |
} |
| 883 |
|
| 884 |
updateSubstitutionModel = false; |
| 885 |
updateSiteModel = false; |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
return logL; |
| 895 |
} |
| 896 |
|
| 897 |
public void getPartials(int number, double[] partials) { |
| 898 |
int cumulativeBufferIndex = Beagle.NONE; |
| 899 |
|
| 900 |
beagle.getPartials(partialBufferHelper.getOffsetIndex(number), cumulativeBufferIndex, partials); |
| 901 |
} |
| 902 |
|
| 903 |
private void setPartials(int number, double[] partials) { |
| 904 |
beagle.setPartials(partialBufferHelper.getOffsetIndex(number), partials); |
| 905 |
} |
| 906 |
|
| 907 |
@Override |
| 908 |
public void makeDirty() { |
| 909 |
updateSiteModel = true; |
| 910 |
updateSubstitutionModel = true; |
| 911 |
} |
| 912 |
|
| 913 |
@Override |
| 914 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 915 |
if (model == siteRateModel) { |
| 916 |
updateSiteModel = true; |
| 917 |
} else if (model == branchModel) { |
| 918 |
updateSubstitutionModel = true; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
fireModelChanged(); |
| 923 |
} |
| 924 |
|
| 925 |
@Override |
| 926 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 927 |
|
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
@Override |
| 934 |
public void storeState() { |
| 935 |
partialBufferHelper.storeState(); |
| 936 |
evolutionaryProcessDelegate.storeState(); |
| 937 |
|
| 938 |
if (useScaleFactors || useAutoScaling) { |
| 939 |
scaleBufferHelper.storeState(); |
| 940 |
System.arraycopy(scaleBufferIndices, 0, storedScaleBufferIndices, 0, scaleBufferIndices.length); |
| 941 |
|
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
flip = true; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
@Override |
| 952 |
public void restoreState() { |
| 953 |
updateSiteModel = true; |
| 954 |
|
| 955 |
partialBufferHelper.restoreState(); |
| 956 |
evolutionaryProcessDelegate.restoreState(); |
| 957 |
|
| 958 |
if (useScaleFactors || useAutoScaling) { |
| 959 |
scaleBufferHelper.restoreState(); |
| 960 |
int[] tmp = storedScaleBufferIndices; |
| 961 |
storedScaleBufferIndices = scaleBufferIndices; |
| 962 |
scaleBufferIndices = tmp; |
| 963 |
|
| 964 |
} |
| 965 |
|
| 966 |
} |
| 967 |
|
| 968 |
@Override |
| 969 |
public void setCallback(TreeDataLikelihood treeDataLikelihood) { |
| 970 |
|
| 971 |
} |
| 972 |
|
| 973 |
@Override |
| 974 |
public int vectorizeNodeOperations(List<ProcessOnTreeDelegate.NodeOperation> nodeOperations, int[] operations) { |
| 975 |
throw new RuntimeException("Not yet implemented"); |
| 976 |
} |
| 977 |
|
| 978 |
@Override |
| 979 |
protected void acceptState() { |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
@Override |
| 987 |
public Citation.Category getCategory() { |
| 988 |
return Citation.Category.FRAMEWORK; |
| 989 |
} |
| 990 |
|
| 991 |
@Override |
| 992 |
public String getDescription() { |
| 993 |
return "Using BEAGLE likelihood calculation library"; |
| 994 |
} |
| 995 |
|
| 996 |
@Override |
| 997 |
public List<Citation> getCitations() { |
| 998 |
return Collections.singletonList(CommonCitations.AYRES_2012_BEAGLE); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
private final int nodeCount; |
| 1006 |
private final int tipCount; |
| 1007 |
private final int internalNodeCount; |
| 1008 |
|
| 1009 |
private final int[] branchUpdateIndices; |
| 1010 |
private final double[] branchLengths; |
| 1011 |
|
| 1012 |
private int[] scaleBufferIndices; |
| 1013 |
private int[] storedScaleBufferIndices; |
| 1014 |
|
| 1015 |
private final int[] operations; |
| 1016 |
|
| 1017 |
private boolean flip = true; |
| 1018 |
private final BufferIndexHelper partialBufferHelper; |
| 1019 |
private final BufferIndexHelper scaleBufferHelper; |
| 1020 |
|
| 1021 |
private PartialsRescalingScheme rescalingScheme; |
| 1022 |
private int rescalingFrequency = RESCALE_FREQUENCY; |
| 1023 |
private boolean delayRescalingUntilUnderflow = true; |
| 1024 |
|
| 1025 |
private boolean useScaleFactors = false; |
| 1026 |
private boolean useAutoScaling = false; |
| 1027 |
|
| 1028 |
private boolean recomputeScaleFactors = false; |
| 1029 |
private boolean everUnderflowed = false; |
| 1030 |
private int rescalingCount = 0; |
| 1031 |
private int rescalingCountInner = 0; |
| 1032 |
|
| 1033 |
private int threadCount = 1; |
| 1034 |
private long instanceFlags; |
| 1035 |
|
| 1036 |
private boolean firstRescaleAttempt = false; |
| 1037 |
private int rescalingMessageCount = 0; |
| 1038 |
|
| 1039 |
|
| 1040 |
private int underflowHandling = 0; |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
private final PatternList patternList; |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
private final DataType dataType; |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
private final double[] patternWeights; |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
private final int patternCount; |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
private final int stateCount; |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
private final BranchModel branchModel; |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
private final EvolutionaryProcessDelegate evolutionaryProcessDelegate; |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
private final SiteRateModel siteRateModel; |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
private double[] patternLogLikelihoods = null; |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
private final int categoryCount; |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
private double[] tipPartials; |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
private int[] tipStates; |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
private final Beagle beagle; |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
private boolean updateSubstitutionModel; |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
private boolean updateSiteModel; |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
private boolean initialEvaluation = true; |
| 1121 |
|
| 1122 |
} |