| 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.beauti.options; |
| 27 |
|
| 28 |
import dr.app.beauti.types.PriorScaleType; |
| 29 |
import dr.app.beauti.types.PriorType; |
| 30 |
import dr.evolution.util.Taxa; |
| 31 |
import dr.math.distributions.Distribution; |
| 32 |
|
| 33 |
import java.io.Serializable; |
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Map; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class Parameter implements Serializable { |
| 43 |
private static final long serialVersionUID = -8008521511485034329L; |
| 44 |
|
| 45 |
public static final double UNIFORM_MAX_BOUND = 1.0E100; |
| 46 |
|
| 47 |
private String prefix = null; |
| 48 |
private boolean priorEdited; |
| 49 |
|
| 50 |
private boolean meanInRealSpace = false; |
| 51 |
|
| 52 |
|
| 53 |
private String baseName; |
| 54 |
private final String description; |
| 55 |
|
| 56 |
private int dimensionWeight = 1; |
| 57 |
|
| 58 |
private final List<Parameter> subParameters = new ArrayList<Parameter>(); |
| 59 |
|
| 60 |
|
| 61 |
public String taxaId; |
| 62 |
public final boolean isNodeHeight; |
| 63 |
public final boolean isStatistic; |
| 64 |
public final boolean isDiscrete; |
| 65 |
public final boolean isHierarchical; |
| 66 |
public final boolean isCMTCRate; |
| 67 |
public final boolean isNonNegative; |
| 68 |
public final boolean isZeroOne; |
| 69 |
public final boolean isCached; |
| 70 |
public final boolean isAdaptiveMultivariateCompatible; |
| 71 |
public boolean isMaintainedSum; |
| 72 |
public boolean isCalibratedYule = false; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private PartitionOptions options; |
| 77 |
|
| 78 |
public final PriorScaleType scaleType; |
| 79 |
|
| 80 |
public final boolean isPriorFixed; |
| 81 |
public PriorType priorType; |
| 82 |
|
| 83 |
private Parameter parent; |
| 84 |
|
| 85 |
private Taxa taxonSet = null; |
| 86 |
|
| 87 |
public double getInitial() { |
| 88 |
return initial; |
| 89 |
} |
| 90 |
|
| 91 |
public void setInitial(double initial) { |
| 92 |
this.initial = initial; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
private boolean isFixed; |
| 97 |
private double initial; |
| 98 |
public double maintainedSum; |
| 99 |
public double dimension; |
| 100 |
public boolean isTruncated; |
| 101 |
public double truncationUpper; |
| 102 |
public double truncationLower; |
| 103 |
public double mean; |
| 104 |
public double stdev; |
| 105 |
public double shape; |
| 106 |
public double shapeB; |
| 107 |
public double scale; |
| 108 |
public double offset; |
| 109 |
public double precision; |
| 110 |
public double uniformUpper; |
| 111 |
public double uniformLower; |
| 112 |
|
| 113 |
public boolean isLinked; |
| 114 |
public String linkedName; |
| 115 |
|
| 116 |
public static class Builder { |
| 117 |
|
| 118 |
private final String baseName; |
| 119 |
private final String description; |
| 120 |
|
| 121 |
|
| 122 |
private PriorScaleType scaleType = PriorScaleType.NONE; |
| 123 |
|
| 124 |
private String taxaId = null; |
| 125 |
private boolean isNodeHeight = false; |
| 126 |
private boolean isDiscrete = false; |
| 127 |
private boolean isHierarchical = false; |
| 128 |
private boolean isCMTCRate = false; |
| 129 |
private boolean isNonNegative = false; |
| 130 |
private boolean isZeroOne = false; |
| 131 |
private boolean isMaintainedSum = false; |
| 132 |
private boolean isStatistic = false; |
| 133 |
private boolean isCached = false; |
| 134 |
|
| 135 |
private PartitionOptions options = null; |
| 136 |
|
| 137 |
private Taxa taxonSet = null; |
| 138 |
|
| 139 |
private PriorType priorType = PriorType.NONE_TREE_PRIOR; |
| 140 |
private boolean isPriorFixed = false; |
| 141 |
|
| 142 |
private boolean isAdaptiveMultivariateCompatible = false; |
| 143 |
|
| 144 |
private double maintainedSum = 1.0; |
| 145 |
private double dimension = 1; |
| 146 |
private double initial = Double.NaN; |
| 147 |
|
| 148 |
|
| 149 |
private boolean isTruncated = false; |
| 150 |
public double truncationUpper = Double.POSITIVE_INFINITY; |
| 151 |
public double truncationLower = Double.NEGATIVE_INFINITY; |
| 152 |
public double mean = 0.0; |
| 153 |
public double stdev = 1.0; |
| 154 |
public double shape = 1.0; |
| 155 |
public double shapeB = 3.0; |
| 156 |
public double scale = 1.0; |
| 157 |
public double offset = 0.0; |
| 158 |
public double precision = 1.0; |
| 159 |
|
| 160 |
|
| 161 |
public double uniformUpper = UNIFORM_MAX_BOUND; |
| 162 |
public double uniformLower = -UNIFORM_MAX_BOUND; |
| 163 |
|
| 164 |
private boolean isFixed = false; |
| 165 |
|
| 166 |
|
| 167 |
public Builder(String name, String description) { |
| 168 |
this.baseName = name; |
| 169 |
this.description = description; |
| 170 |
} |
| 171 |
|
| 172 |
public Builder duplicate(Parameter source) { |
| 173 |
scaleType = source.scaleType; |
| 174 |
taxaId = source.taxaId; |
| 175 |
isNodeHeight = source.isNodeHeight; |
| 176 |
isDiscrete = source.isDiscrete; |
| 177 |
isHierarchical = source.isHierarchical; |
| 178 |
isCMTCRate = source.isCMTCRate; |
| 179 |
isNonNegative = source.isNonNegative; |
| 180 |
isZeroOne = source.isZeroOne; |
| 181 |
isMaintainedSum = source.isMaintainedSum; |
| 182 |
isStatistic = source.isStatistic; |
| 183 |
isCached = source.isCached; |
| 184 |
options = source.options; |
| 185 |
priorType = source.priorType; |
| 186 |
isPriorFixed = source.isPriorFixed; |
| 187 |
isAdaptiveMultivariateCompatible = source.isAdaptiveMultivariateCompatible; |
| 188 |
initial = source.initial; |
| 189 |
dimension = source.dimension; |
| 190 |
maintainedSum = source.maintainedSum; |
| 191 |
isTruncated = source.isTruncated; |
| 192 |
truncationUpper = source.truncationUpper; |
| 193 |
truncationLower = source.truncationLower; |
| 194 |
mean = source.mean; |
| 195 |
stdev = source.stdev; |
| 196 |
shape = source.shape; |
| 197 |
shapeB = source.shapeB; |
| 198 |
scale = source.scale; |
| 199 |
offset = source.offset; |
| 200 |
precision = source.precision; |
| 201 |
uniformUpper = source.uniformUpper; |
| 202 |
uniformLower = source.uniformLower; |
| 203 |
isFixed = source.isFixed; |
| 204 |
return this; |
| 205 |
} |
| 206 |
|
| 207 |
public Builder scaleType(PriorScaleType scaleType) { |
| 208 |
this.scaleType = scaleType; |
| 209 |
return this; |
| 210 |
} |
| 211 |
|
| 212 |
public Builder initial(double initial) { |
| 213 |
this.initial = initial; |
| 214 |
return this; |
| 215 |
} |
| 216 |
|
| 217 |
public Builder dimension(int dimension) { |
| 218 |
this.dimension = dimension; |
| 219 |
return this; |
| 220 |
} |
| 221 |
|
| 222 |
public Builder maintainedSum(double maintainedSum) { |
| 223 |
this.maintainedSum = maintainedSum; |
| 224 |
this.isMaintainedSum = true; |
| 225 |
return this; |
| 226 |
} |
| 227 |
|
| 228 |
public Builder taxaId(String taxaId) { |
| 229 |
this.taxaId = taxaId; |
| 230 |
return this; |
| 231 |
} |
| 232 |
|
| 233 |
public Builder isNodeHeight(boolean isNodeHeight) { |
| 234 |
this.isNodeHeight = isNodeHeight; |
| 235 |
return this; |
| 236 |
} |
| 237 |
|
| 238 |
public Builder isCached(boolean isCached) { |
| 239 |
this.isCached = isCached; |
| 240 |
return this; |
| 241 |
} |
| 242 |
|
| 243 |
public Builder isStatistic(boolean isStatistic) { |
| 244 |
this.isStatistic = isStatistic; |
| 245 |
return this; |
| 246 |
} |
| 247 |
|
| 248 |
public Builder partitionOptions(PartitionOptions options) { |
| 249 |
this.options = options; |
| 250 |
return this; |
| 251 |
} |
| 252 |
|
| 253 |
public Builder taxonSet(Taxa taxonSet) { |
| 254 |
this.taxonSet = taxonSet; |
| 255 |
return this; |
| 256 |
} |
| 257 |
public Builder prior(PriorType priorType) { |
| 258 |
this.priorType = priorType; |
| 259 |
return this; |
| 260 |
} |
| 261 |
|
| 262 |
public Builder isDiscrete(boolean isDiscrete) { |
| 263 |
this.isDiscrete = isDiscrete; |
| 264 |
return this; |
| 265 |
} |
| 266 |
|
| 267 |
public Builder isHierarchical(boolean isHierarchical) { |
| 268 |
this.isHierarchical = isHierarchical; |
| 269 |
return this; |
| 270 |
} |
| 271 |
|
| 272 |
public Builder isNonNegative(boolean isNonNegative) { |
| 273 |
this.isNonNegative = isNonNegative; |
| 274 |
if (isNonNegative) { |
| 275 |
this.uniformLower = 0.0; |
| 276 |
} |
| 277 |
return this; |
| 278 |
} |
| 279 |
|
| 280 |
public Builder isZeroOne(boolean isZeroOne) { |
| 281 |
this.isZeroOne = isZeroOne; |
| 282 |
if (isZeroOne) { |
| 283 |
this.uniformLower = 0.0; |
| 284 |
this.uniformUpper = 1.0; |
| 285 |
} |
| 286 |
return this; |
| 287 |
} |
| 288 |
|
| 289 |
public Builder isMaintainedSum(boolean isMaintainedMean) { |
| 290 |
this.isMaintainedSum = isMaintainedMean; |
| 291 |
return this; |
| 292 |
} |
| 293 |
|
| 294 |
public Builder isAdaptiveMultivariateCompatible(boolean isAdaptiveMultivariateCompatible) { |
| 295 |
this.isAdaptiveMultivariateCompatible = isAdaptiveMultivariateCompatible; |
| 296 |
return this; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
public Builder isCMTCRate(boolean isCMTCRate) { |
| 301 |
this.isCMTCRate = isCMTCRate; |
| 302 |
return this; |
| 303 |
} |
| 304 |
|
| 305 |
public Builder isFixed(boolean isFixed) { |
| 306 |
this.isFixed = isFixed; |
| 307 |
return this; |
| 308 |
} |
| 309 |
|
| 310 |
public Builder isPriorFixed(boolean priorFixed) { |
| 311 |
this.isPriorFixed = priorFixed; |
| 312 |
return this; |
| 313 |
} |
| 314 |
|
| 315 |
public Builder uniformUpper(double upper) { |
| 316 |
this.uniformUpper = upper; |
| 317 |
return this; |
| 318 |
} |
| 319 |
|
| 320 |
public Builder uniformLower(double lower) { |
| 321 |
this.uniformLower = lower; |
| 322 |
return this; |
| 323 |
} |
| 324 |
|
| 325 |
public Builder truncationUpper(double truncationUpper) { |
| 326 |
this.isTruncated = true; |
| 327 |
this.truncationUpper = truncationUpper; |
| 328 |
return this; |
| 329 |
} |
| 330 |
|
| 331 |
public Builder truncationLower(double truncationLower) { |
| 332 |
this.isTruncated = true; |
| 333 |
this.truncationLower = truncationLower; |
| 334 |
return this; |
| 335 |
} |
| 336 |
|
| 337 |
public Builder mean(double mean) { |
| 338 |
this.mean = mean; |
| 339 |
return this; |
| 340 |
} |
| 341 |
|
| 342 |
public Builder stdev(double stdev) { |
| 343 |
this.stdev = stdev; |
| 344 |
return this; |
| 345 |
} |
| 346 |
|
| 347 |
public Builder precision(double precision) { |
| 348 |
this.precision = precision; |
| 349 |
return this; |
| 350 |
} |
| 351 |
|
| 352 |
public Builder shape(double shape) { |
| 353 |
this.shape = shape; |
| 354 |
return this; |
| 355 |
} |
| 356 |
|
| 357 |
public Builder shapeB(double shapeB) { |
| 358 |
this.shapeB = shapeB; |
| 359 |
return this; |
| 360 |
} |
| 361 |
|
| 362 |
public Builder scale(double scale) { |
| 363 |
this.scale = scale; |
| 364 |
return this; |
| 365 |
} |
| 366 |
|
| 367 |
public Builder offset(double offset) { |
| 368 |
this.offset = offset; |
| 369 |
return this; |
| 370 |
} |
| 371 |
|
| 372 |
public Parameter build() { |
| 373 |
return new Parameter(this); |
| 374 |
} |
| 375 |
|
| 376 |
public Parameter build(Map<String, Parameter> map) { |
| 377 |
final Parameter parameter = new Parameter(this); |
| 378 |
map.put(baseName, parameter); |
| 379 |
return parameter; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
private Parameter(Builder builder) { |
| 384 |
baseName = builder.baseName; |
| 385 |
description = builder.description; |
| 386 |
scaleType = builder.scaleType; |
| 387 |
initial = builder.initial; |
| 388 |
maintainedSum = builder.maintainedSum; |
| 389 |
dimension = builder.dimension; |
| 390 |
taxaId = builder.taxaId; |
| 391 |
isNodeHeight = builder.isNodeHeight; |
| 392 |
isStatistic = builder.isStatistic; |
| 393 |
isCached = builder.isCached; |
| 394 |
options = builder.options; |
| 395 |
priorType = builder.priorType; |
| 396 |
isDiscrete = builder.isDiscrete; |
| 397 |
isFixed = builder.isFixed; |
| 398 |
isHierarchical = builder.isHierarchical; |
| 399 |
isCMTCRate = builder.isCMTCRate; |
| 400 |
isNonNegative = builder.isNonNegative; |
| 401 |
isZeroOne = builder.isZeroOne; |
| 402 |
isMaintainedSum = builder.isMaintainedSum; |
| 403 |
isPriorFixed = builder.isPriorFixed; |
| 404 |
isAdaptiveMultivariateCompatible = builder.isAdaptiveMultivariateCompatible; |
| 405 |
|
| 406 |
taxonSet = builder.taxonSet; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
isTruncated = builder.isTruncated; |
| 411 |
truncationUpper = builder.truncationUpper; |
| 412 |
truncationLower = builder.truncationLower; |
| 413 |
mean = builder.mean; |
| 414 |
stdev = builder.stdev; |
| 415 |
shape = builder.shape; |
| 416 |
shapeB = builder.shapeB; |
| 417 |
scale = builder.scale; |
| 418 |
offset = builder.offset; |
| 419 |
uniformUpper = builder.uniformUpper; |
| 420 |
uniformLower = builder.uniformLower; |
| 421 |
|
| 422 |
|
| 423 |
if (priorType == PriorType.EXPONENTIAL_PRIOR && mean == 0) mean = 1; |
| 424 |
if (priorType == PriorType.LOGNORMAL_PRIOR && meanInRealSpace && mean <= 0) mean = 0.01; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
public String getPrefix() { |
| 429 |
return prefix; |
| 430 |
} |
| 431 |
|
| 432 |
public void setPrefix(String prefix) { |
| 433 |
this.prefix = prefix; |
| 434 |
} |
| 435 |
|
| 436 |
private String getFullName() { |
| 437 |
if (prefix != null) return prefix + getBaseName(); |
| 438 |
return getBaseName(); |
| 439 |
} |
| 440 |
|
| 441 |
public String getBaseName() { |
| 442 |
if (taxaId != null) { |
| 443 |
return taxaId; |
| 444 |
} |
| 445 |
return baseName; |
| 446 |
} |
| 447 |
|
| 448 |
public String getName() { |
| 449 |
if (taxaId != null) { |
| 450 |
return "tmrca(" + getFullName() + ")"; |
| 451 |
} else { |
| 452 |
return getFullName(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
public void setName(String name) { |
| 457 |
this.baseName = name; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
public String getXMLName() { |
| 462 |
if (taxaId != null) { |
| 463 |
return "tmrca_" + getFullName(); |
| 464 |
} else { |
| 465 |
return getFullName(); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
public String getDescription() { |
| 470 |
if (taxaId != null && options != null) { |
| 471 |
return description + " " + taxaId + " on tree " + options.getName(); |
| 472 |
} |
| 473 |
return description; |
| 474 |
} |
| 475 |
|
| 476 |
public double getPriorExpectationMean() { |
| 477 |
double expMean = 1.0; |
| 478 |
Distribution dist = priorType.getDistributionInstance(this); |
| 479 |
if (dist != null) { |
| 480 |
expMean = dist.mean(); |
| 481 |
|
| 482 |
if (expMean == 0) { |
| 483 |
expMean = dist.quantile(0.975); |
| 484 |
} |
| 485 |
|
| 486 |
if (expMean == 0) { |
| 487 |
expMean = 1.0; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
return expMean; |
| 492 |
} |
| 493 |
|
| 494 |
public PartitionOptions getOptions() { |
| 495 |
return options; |
| 496 |
} |
| 497 |
|
| 498 |
public void setOptions(PartitionOptions options) { |
| 499 |
this.options = options; |
| 500 |
} |
| 501 |
|
| 502 |
public Taxa getTaxonSet() { |
| 503 |
return taxonSet; |
| 504 |
} |
| 505 |
|
| 506 |
public void setTaxonSet(Taxa taxonSet) { |
| 507 |
this.taxonSet = taxonSet; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
public void setPriorEdited(boolean priorEdited) { |
| 512 |
this.priorEdited = priorEdited; |
| 513 |
} |
| 514 |
|
| 515 |
public boolean isPriorEdited() { |
| 516 |
return priorEdited; |
| 517 |
} |
| 518 |
|
| 519 |
public boolean isPriorImproper() { |
| 520 |
if ( |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
(priorType == PriorType.NONE_IMPROPER) || |
| 526 |
(priorType == PriorType.UNIFORM_PRIOR && (Double.isInfinite(getLowerBound()) || Double.isInfinite(getUpperBound())))) { |
| 527 |
return true; |
| 528 |
} |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
public double getLowerBound() { |
| 533 |
double lower = Double.NEGATIVE_INFINITY; |
| 534 |
|
| 535 |
if (priorType == PriorType.UNIFORM_PRIOR || priorType == PriorType.DISCRETE_UNIFORM_PRIOR) { |
| 536 |
lower = uniformLower; |
| 537 |
} |
| 538 |
|
| 539 |
if (isNonNegative || isZeroOne) { |
| 540 |
if (lower < 0) lower = 0.0; |
| 541 |
} |
| 542 |
|
| 543 |
if (isTruncated && !Double.isInfinite(truncationLower)) { |
| 544 |
lower = truncationLower; |
| 545 |
} |
| 546 |
|
| 547 |
return lower; |
| 548 |
} |
| 549 |
|
| 550 |
public double getUpperBound() { |
| 551 |
double upper = Double.POSITIVE_INFINITY; |
| 552 |
|
| 553 |
if (isZeroOne) { |
| 554 |
if (upper > 1) { |
| 555 |
upper = 1.0; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
if (priorType == PriorType.UNIFORM_PRIOR || priorType == PriorType.DISCRETE_UNIFORM_PRIOR) { |
| 560 |
upper = uniformUpper; |
| 561 |
} |
| 562 |
|
| 563 |
if (isTruncated && !Double.isInfinite(truncationUpper)) { |
| 564 |
upper = truncationUpper; |
| 565 |
} |
| 566 |
|
| 567 |
return upper; |
| 568 |
} |
| 569 |
|
| 570 |
public boolean isFixed() { |
| 571 |
return priorType == PriorType.NONE_FIXED; |
| 572 |
} |
| 573 |
|
| 574 |
public void setFixed(boolean isFixed) { |
| 575 |
if (isFixed) { |
| 576 |
priorType = PriorType.NONE_FIXED; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
public boolean isInRealSpace() { |
| 581 |
return meanInRealSpace; |
| 582 |
} |
| 583 |
|
| 584 |
public void setMeanInRealSpace(boolean meanInRealSpace) { |
| 585 |
this.meanInRealSpace = meanInRealSpace; |
| 586 |
} |
| 587 |
|
| 588 |
public int[] getParameterDimensionWeights() { |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
if (getSubParameters().size() > 0) { |
| 593 |
int[] weights = new int[getSubParameters().size()]; |
| 594 |
for (int i = 0; i < weights.length; i++) { |
| 595 |
weights[i] = getSubParameters().get(i).getDimensionWeight(); |
| 596 |
} |
| 597 |
return weights; |
| 598 |
} |
| 599 |
return new int[] { dimensionWeight }; |
| 600 |
} |
| 601 |
|
| 602 |
public int getDimensionWeight() { |
| 603 |
return dimensionWeight; |
| 604 |
} |
| 605 |
|
| 606 |
public void setDimensionWeight(int dimensionWeight) { |
| 607 |
this.dimensionWeight = dimensionWeight; |
| 608 |
} |
| 609 |
|
| 610 |
public void setParent(Parameter parent) { |
| 611 |
this.parent = parent; |
| 612 |
} |
| 613 |
|
| 614 |
public Parameter getParent() { |
| 615 |
return parent; |
| 616 |
} |
| 617 |
|
| 618 |
public void addSubParameter(Parameter parameter) { |
| 619 |
parameter.setParent(this); |
| 620 |
subParameters.add(parameter); |
| 621 |
} |
| 622 |
|
| 623 |
public void clearSubParameters() { |
| 624 |
subParameters.clear(); |
| 625 |
} |
| 626 |
|
| 627 |
public List<Parameter> getSubParameters() { |
| 628 |
return subParameters; |
| 629 |
} |
| 630 |
|
| 631 |
@Override |
| 632 |
public String toString() { |
| 633 |
return getName(); |
| 634 |
} |
| 635 |
} |