| 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.components.ComponentFactory; |
| 29 |
import dr.app.beauti.types.OperatorType; |
| 30 |
import dr.app.beauti.types.PriorScaleType; |
| 31 |
import dr.app.beauti.types.PriorType; |
| 32 |
import dr.evolution.util.TaxonList; |
| 33 |
|
| 34 |
import java.io.Serializable; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Map; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public abstract class ModelOptions implements Serializable { |
| 45 |
|
| 46 |
private static final long serialVersionUID = 6199011531067286245L; |
| 47 |
|
| 48 |
protected final Map<String, Parameter> parameters = new HashMap<String, Parameter>(); |
| 49 |
protected final Map<String, Operator> operators = new HashMap<String, Operator>(); |
| 50 |
protected final Map<TaxonList, Parameter> statistics = new HashMap<TaxonList, Parameter>(); |
| 51 |
|
| 52 |
public static final double demoTuning = 0.75; |
| 53 |
public static final double demoWeights = 3.0; |
| 54 |
|
| 55 |
protected static final double branchWeights = 30.0; |
| 56 |
protected static final double treeWeights = 30.0; |
| 57 |
protected static final double rateWeights = 3.0; |
| 58 |
|
| 59 |
private final List<ComponentOptions> components = new ArrayList<ComponentOptions>(); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public abstract void initModelParametersAndOpererators(); |
| 64 |
|
| 65 |
public abstract List<Parameter> selectParameters(List<Parameter> params); |
| 66 |
|
| 67 |
public abstract List<Operator> selectOperators(List<Operator> ops); |
| 68 |
|
| 69 |
public abstract String getPrefix(); |
| 70 |
|
| 71 |
|
| 72 |
public Parameter createParameter(String name, String description) { |
| 73 |
return new Parameter.Builder(name, description).build(parameters); |
| 74 |
} |
| 75 |
|
| 76 |
public Parameter createParameter(String name, String description, double initial) { |
| 77 |
return new Parameter.Builder(name, description).initial(initial).isFixed(true).build(parameters); |
| 78 |
} |
| 79 |
|
| 80 |
public Parameter createZeroOneParameter(String name, String description, double initial) { |
| 81 |
return new Parameter.Builder(name, description) |
| 82 |
.initial(initial).isZeroOne(true).build(parameters); |
| 83 |
} |
| 84 |
|
| 85 |
public Parameter createZeroOneParameterUniformPrior(String name, String description, double initial) { |
| 86 |
return createZeroOneParameterUniformPrior(name, description, initial, false); |
| 87 |
} |
| 88 |
|
| 89 |
public Parameter createZeroOneParameterUniformPrior(String name, String description, double initial, boolean amtk) { |
| 90 |
return new Parameter.Builder(name, description).prior(PriorType.UNIFORM_PRIOR) |
| 91 |
.initial(initial).isZeroOne(true).isAdaptiveMultivariateCompatible(amtk).build(parameters); |
| 92 |
} |
| 93 |
|
| 94 |
public Parameter createNonNegativeParameterDirichletPrior(String name, String description, PartitionOptions options, |
| 95 |
double maintainedSum, boolean amtk) { |
| 96 |
return new Parameter.Builder(name, description).prior(PriorType.DIRICHLET_PRIOR) |
| 97 |
.isNonNegative(true).maintainedSum(maintainedSum) |
| 98 |
.isAdaptiveMultivariateCompatible(amtk) |
| 99 |
.partitionOptions(options).build(parameters); |
| 100 |
} |
| 101 |
|
| 102 |
public Parameter createNonNegativeParameterDirichletPrior(String name, String description, PartitionOptions options, |
| 103 |
int dimension, double maintainedSum, boolean amtk) { |
| 104 |
return createNonNegativeParameterDirichletPrior(name, description, options, dimension, maintainedSum, false, amtk); |
| 105 |
} |
| 106 |
|
| 107 |
public Parameter createNonNegativeParameterDirichletPrior(String name, String description, PartitionOptions options, |
| 108 |
int dimension, double maintainedSum, boolean priorFixed, boolean amtk) { |
| 109 |
return new Parameter.Builder(name, description).dimension(dimension).prior(PriorType.DIRICHLET_PRIOR) |
| 110 |
.isNonNegative(true).maintainedSum(maintainedSum).isPriorFixed(priorFixed) |
| 111 |
.isAdaptiveMultivariateCompatible(amtk) |
| 112 |
.partitionOptions(options).build(parameters); |
| 113 |
} |
| 114 |
|
| 115 |
public Parameter createNonNegativeParameterInfinitePrior(String name, String description, PriorScaleType scaleType, double initial) { |
| 116 |
return createNonNegativeParameterInfinitePrior(name, description, null, scaleType, initial, false); |
| 117 |
} |
| 118 |
|
| 119 |
public Parameter createNonNegativeParameterInfinitePrior(String name, String description, PriorScaleType scaleType, double initial, boolean amtk) { |
| 120 |
return createNonNegativeParameterInfinitePrior(name, description, null, scaleType, initial, amtk); |
| 121 |
} |
| 122 |
|
| 123 |
public Parameter createNonNegativeParameterInfinitePrior(String name, String description, PartitionOptions options, PriorScaleType scaleType, double initial, boolean amtk) { |
| 124 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.NONE_IMPROPER).isNonNegative(true) |
| 125 |
.partitionOptions(options).initial(initial).isAdaptiveMultivariateCompatible(amtk).build(parameters); |
| 126 |
} |
| 127 |
|
| 128 |
public Parameter createNonNegativeParameterUniformPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 129 |
double uniformLower, double uniformUpper) { |
| 130 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.UNIFORM_PRIOR).isNonNegative(true) |
| 131 |
.initial(initial).uniformLower(uniformLower).uniformUpper(uniformUpper).build(parameters); |
| 132 |
} |
| 133 |
|
| 134 |
public Parameter createParameterUniformPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 135 |
double uniformLower, double uniformUpper) { |
| 136 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.UNIFORM_PRIOR) |
| 137 |
.initial(initial).uniformLower(uniformLower).uniformUpper(uniformUpper).build(parameters); |
| 138 |
} |
| 139 |
|
| 140 |
public Parameter createParameterGammaPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 141 |
double shape, double scale, boolean priorFixed) { |
| 142 |
return createParameterGammaPrior(name, description, scaleType, initial, shape, scale, priorFixed, false); |
| 143 |
} |
| 144 |
|
| 145 |
public Parameter createParameterGammaPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 146 |
double shape, double scale, boolean priorFixed, boolean amtk) { |
| 147 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.GAMMA_PRIOR) |
| 148 |
.initial(initial).shape(shape).scale(scale).isNonNegative(true).isPriorFixed(priorFixed). |
| 149 |
isAdaptiveMultivariateCompatible(amtk) |
| 150 |
.build(parameters); |
| 151 |
} |
| 152 |
|
| 153 |
public Parameter createCachedGammaPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 154 |
double shape, double scale, boolean priorFixed) { |
| 155 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.GAMMA_PRIOR).initial(initial) |
| 156 |
.shape(shape).scale(scale).isNonNegative(true).isPriorFixed(priorFixed).isCached(true).build(parameters); |
| 157 |
} |
| 158 |
|
| 159 |
public Parameter createParameterOneOverXPrior(String name, String description, PriorScaleType scaleType, double initial) { |
| 160 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.ONE_OVER_X_PRIOR) |
| 161 |
.initial(initial).isNonNegative(true).build(parameters); |
| 162 |
} |
| 163 |
|
| 164 |
public Parameter createParameterExponentialPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 165 |
double mean, double offset) { |
| 166 |
return createParameterExponentialPrior(name, description, scaleType, initial, mean, offset, false); |
| 167 |
} |
| 168 |
|
| 169 |
public Parameter createParameterExponentialPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 170 |
double mean, double offset, boolean amtk) { |
| 171 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.EXPONENTIAL_PRIOR) |
| 172 |
.initial(initial).mean(mean).offset(offset).isNonNegative(true).isAdaptiveMultivariateCompatible(amtk).build(parameters); |
| 173 |
} |
| 174 |
|
| 175 |
public Parameter createParameterLognormalPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 176 |
double mean, double stdev, double offset) { |
| 177 |
return createParameterLognormalPrior(name, description, scaleType, initial, mean, stdev, offset, false); |
| 178 |
} |
| 179 |
|
| 180 |
public Parameter createParameterLognormalPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 181 |
double mean, double stdev, double offset, boolean amtk) { |
| 182 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.LOGNORMAL_PRIOR) |
| 183 |
.initial(initial).mean(mean).stdev(stdev).offset(offset).isNonNegative(true) |
| 184 |
.isAdaptiveMultivariateCompatible(amtk) |
| 185 |
.build(parameters); |
| 186 |
} |
| 187 |
|
| 188 |
public Parameter createParameterNormalPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 189 |
double mean, double stdev, double offset) { |
| 190 |
return createParameterNormalPrior(name, description, scaleType, initial, mean, stdev, offset, false); |
| 191 |
} |
| 192 |
|
| 193 |
public Parameter createParameterNormalPrior(String name, String description, PriorScaleType scaleType, double initial, |
| 194 |
double mean, double stdev, double offset, boolean amtk) { |
| 195 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.NORMAL_PRIOR) |
| 196 |
.initial(initial).mean(mean).stdev(stdev).offset(offset).isAdaptiveMultivariateCompatible(amtk) |
| 197 |
.build(parameters); |
| 198 |
} |
| 199 |
|
| 200 |
public Parameter createParameterLaplacePrior(String name, String description, PriorScaleType scaleType, double initial, |
| 201 |
double mean, double scale) { |
| 202 |
return new Parameter.Builder(name, description).scaleType(scaleType).prior(PriorType.LAPLACE_PRIOR) |
| 203 |
.initial(initial).mean(mean).scale(scale).build(parameters); |
| 204 |
} |
| 205 |
|
| 206 |
public Parameter createParameterBetaDistributionPrior(String name, String description, double initial, |
| 207 |
double shape, double shapeB, double offset) { |
| 208 |
return new Parameter.Builder(name, description).prior(PriorType.BETA_PRIOR).initial(initial) |
| 209 |
.isZeroOne(true).shape(shape).shapeB(shapeB).offset(offset).build(parameters); |
| 210 |
} |
| 211 |
|
| 212 |
public Parameter createDuplicate(String name, String description, Parameter source) { |
| 213 |
return new Parameter.Builder(name, description).duplicate(source).build(parameters); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public Parameter createDiscreteStatistic(String name, String description) { |
| 218 |
return new Parameter.Builder(name, description).isDiscrete(true).isStatistic(true) |
| 219 |
.prior(PriorType.POISSON_PRIOR).mean(Math.log(2)).build(parameters); |
| 220 |
} |
| 221 |
|
| 222 |
public Parameter createStatistic(String name, String description) { |
| 223 |
return new Parameter.Builder(name, description).isStatistic(true).prior(PriorType.NONE_STATISTIC).build(parameters); |
| 224 |
} |
| 225 |
|
| 226 |
public Parameter createNonNegativeStatistic(String name, String description) { |
| 227 |
return new Parameter.Builder(name, description).isStatistic(true).prior(PriorType.NONE_STATISTIC).isNonNegative(true).build(parameters); |
| 228 |
} |
| 229 |
|
| 230 |
public Operator createOperator(String parameterName, OperatorType type, double tuning, double weight) { |
| 231 |
Parameter parameter = getParameter(parameterName); |
| 232 |
return new Operator.Builder(parameterName, parameterName, parameter, type, tuning, weight) |
| 233 |
.build(operators); |
| 234 |
} |
| 235 |
|
| 236 |
public Operator createOperator(String parameterName, OperatorType type, double tuning, double weight, boolean autoOptimize) { |
| 237 |
Parameter parameter = getParameter(parameterName); |
| 238 |
return new Operator.Builder(parameterName, parameterName, parameter, type, tuning, weight) |
| 239 |
.autoOptimize(autoOptimize) |
| 240 |
.build(operators); |
| 241 |
} |
| 242 |
|
| 243 |
public Operator createScaleOperator(String parameterName, double tuning, double weight) { |
| 244 |
return createScaleOperator(parameterName, null, tuning, weight); |
| 245 |
} |
| 246 |
|
| 247 |
public Operator createScaleOperator(String parameterName, String description, double tuning, double weight) { |
| 248 |
Parameter parameter = getParameter(parameterName); |
| 249 |
if (description == null) { |
| 250 |
description = (parameter.getDescription() == null ? parameterName : parameter.getDescription()); |
| 251 |
} |
| 252 |
return new Operator.Builder(parameterName, description, parameter, OperatorType.SCALE, tuning, weight).build(operators); |
| 253 |
} |
| 254 |
|
| 255 |
public Operator createOperator(String key, String name, String description, String parameterName, OperatorType type, |
| 256 |
double tuning, double weight) { |
| 257 |
Parameter parameter = null; |
| 258 |
if (parameterName != null) { |
| 259 |
parameter = getParameter(parameterName); |
| 260 |
} |
| 261 |
return operators.put(key, new Operator.Builder(name, description, parameter, type, tuning, weight).build()); |
| 262 |
} |
| 263 |
|
| 264 |
public Operator createOperatorUsing2Parameters(String key, String name, String description, String parameterName1, String parameterName2, |
| 265 |
OperatorType type, double tuning, double weight) { |
| 266 |
Parameter parameter1 = getParameter(parameterName1); |
| 267 |
Parameter parameter2 = getParameter(parameterName2); |
| 268 |
return operators.put(key, new Operator.Builder(name, description, parameter1, type, tuning, weight).parameter2(parameter2).build()); |
| 269 |
} |
| 270 |
|
| 271 |
public Operator createUpDownOperator(String key, String name, String description, Parameter parameter1, Parameter parameter2, |
| 272 |
OperatorType type, double tuning, double weight) { |
| 273 |
return operators.put(key, new Operator.Builder(name, description, parameter1, type, tuning, weight) |
| 274 |
.parameter2(parameter2).build()); |
| 275 |
} |
| 276 |
|
| 277 |
public Operator createBitFlipInSubstitutionModelOperator(String key, String name, String description, Parameter parameter, |
| 278 |
PartitionOptions options, double tuning, double weight) { |
| 279 |
|
| 280 |
return operators.put(key, new Operator.Builder(name, description, parameter, OperatorType.BITFLIP_IN_SUBST, tuning, weight) |
| 281 |
.partitionOptions(options).build()); |
| 282 |
} |
| 283 |
|
| 284 |
public Operator createDuplicate(String name, String description, Parameter parameter, Operator source) { |
| 285 |
return new Operator.Builder(name, description, parameter, source.getOperatorType(), source.getTuning(), source.getWeight()).build(operators); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public Parameter getParameter(String name) { |
| 291 |
Parameter parameter = parameters.get(name); |
| 292 |
if (parameter == null) { |
| 293 |
for (String key : parameters.keySet()) { |
| 294 |
System.err.println(key); |
| 295 |
} |
| 296 |
throw new IllegalArgumentException("Parameter with name, " + name + ", is unknown"); |
| 297 |
} |
| 298 |
return parameter; |
| 299 |
} |
| 300 |
|
| 301 |
public boolean parameterExists(String name) { |
| 302 |
Parameter parameter = parameters.get(name); |
| 303 |
return parameter != null; |
| 304 |
} |
| 305 |
|
| 306 |
public Parameter getStatistic(TaxonList taxonList) { |
| 307 |
Parameter parameter = statistics.get(taxonList); |
| 308 |
if (parameter == null) { |
| 309 |
for (TaxonList key : statistics.keySet()) { |
| 310 |
System.err.println("Taxon list: " + key.getId()); |
| 311 |
} |
| 312 |
throw new IllegalArgumentException("Statistic for taxon list, " + taxonList.getId() + ", is unknown"); |
| 313 |
} |
| 314 |
return parameter; |
| 315 |
} |
| 316 |
|
| 317 |
public Operator getOperator(String name) { |
| 318 |
Operator operator = operators.get(name); |
| 319 |
if (operator == null) { |
| 320 |
throw new IllegalArgumentException("Operator with name, " + name + ", is unknown"); |
| 321 |
} |
| 322 |
return operator; |
| 323 |
} |
| 324 |
|
| 325 |
protected void addComponent(ComponentOptions component) { |
| 326 |
components.add(component); |
| 327 |
component.createParameters(this); |
| 328 |
} |
| 329 |
|
| 330 |
protected boolean hasComponent(ComponentFactory factory) { |
| 331 |
for (ComponentOptions component : components) { |
| 332 |
if (factory.getOptionsClass().isAssignableFrom(component.getClass())) { |
| 333 |
return true; |
| 334 |
} |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
public ComponentOptions getComponentOptions(Class<?> theClass) { |
| 340 |
for (ComponentOptions component : components) { |
| 341 |
if (theClass.isAssignableFrom(component.getClass())) { |
| 342 |
return component; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
throw new IllegalArgumentException("A component options of class, " + theClass.toString() + ", has not been registered"); |
| 347 |
} |
| 348 |
|
| 349 |
protected void selectComponentParameters(ModelOptions options, List<Parameter> params) { |
| 350 |
for (ComponentOptions component : components) { |
| 351 |
component.selectParameters(options, params); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
protected void selectComponentStatistics(ModelOptions options, List<Parameter> stats) { |
| 356 |
for (ComponentOptions component : components) { |
| 357 |
component.selectStatistics(options, stats); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
protected void selectComponentOperators(ModelOptions options, List<Operator> ops) { |
| 362 |
for (ComponentOptions component : components) { |
| 363 |
component.selectOperators(options, ops); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
public Map<String, Parameter> getParameters() { |
| 368 |
return parameters; |
| 369 |
} |
| 370 |
|
| 371 |
public Map<TaxonList, Parameter> getStatistics() { |
| 372 |
return statistics; |
| 373 |
} |
| 374 |
|
| 375 |
public Map<String, Operator> getOperators() { |
| 376 |
return operators; |
| 377 |
} |
| 378 |
|
| 379 |
@Deprecated |
| 380 |
public String noDuplicatedPrefix1(String a , String b) { |
| 381 |
|
| 382 |
if (a.equals(b)) { |
| 383 |
return a; |
| 384 |
} else { |
| 385 |
return a + b; |
| 386 |
} |
| 387 |
} |
| 388 |
} |