| 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.components.tipdatesampling; |
| 27 |
|
| 28 |
import dr.app.beauti.options.*; |
| 29 |
import dr.app.beauti.types.OperatorType; |
| 30 |
import dr.app.beauti.types.PriorScaleType; |
| 31 |
import dr.app.beauti.types.PriorType; |
| 32 |
import dr.app.beauti.types.TipDateSamplingType; |
| 33 |
import dr.evolution.util.Date; |
| 34 |
import dr.evolution.util.Taxa; |
| 35 |
import dr.evolution.util.Taxon; |
| 36 |
import dr.evolution.util.TaxonList; |
| 37 |
|
| 38 |
import java.util.HashMap; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Map; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class TipDateSamplingComponentOptions implements ComponentOptions { |
| 47 |
|
| 48 |
private final BeautiOptions options; |
| 49 |
|
| 50 |
public TipDateSamplingComponentOptions(final BeautiOptions options) { |
| 51 |
this.options = options; |
| 52 |
} |
| 53 |
|
| 54 |
public void createParameters(final ModelOptions modelOptions) { |
| 55 |
modelOptions.createNonNegativeParameterInfinitePrior("treeModel.tipDates", "date of specified tips", |
| 56 |
PriorScaleType.TIME_SCALE, 1.0); |
| 57 |
|
| 58 |
modelOptions.createScaleOperator("treeModel.tipDates", modelOptions.demoTuning, 3.0); |
| 59 |
} |
| 60 |
|
| 61 |
public void selectParameters(final ModelOptions modelOptions, final List<Parameter> params) { |
| 62 |
if (tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY || |
| 63 |
tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) { |
| 64 |
TaxonList taxa = getTaxonSet(); |
| 65 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 66 |
Taxon taxon = taxa.getTaxon(i); |
| 67 |
|
| 68 |
Parameter parameter = getTipDateParameter(taxon); |
| 69 |
|
| 70 |
if (tipDateSamplingType != TipDateSamplingType.SAMPLE_PRECISION) { |
| 71 |
|
| 72 |
|
| 73 |
params.add(parameter); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
} else if (tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) { |
| 78 |
params.add(modelOptions.getParameter("treeModel.tipDates")); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public void selectStatistics(final ModelOptions modelOptions, final List<Parameter> stats) { |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
public void selectOperators(final ModelOptions modelOptions, final List<Operator> ops) { |
| 87 |
if (tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY || |
| 88 |
tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) { |
| 89 |
TaxonList taxa = getTaxonSet(); |
| 90 |
|
| 91 |
String description = "Random walk for the age of this tip"; |
| 92 |
|
| 93 |
|
| 94 |
OperatorType type = OperatorType.UNIFORM; |
| 95 |
|
| 96 |
if (tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) { |
| 97 |
description = "Uniform sample from precision of age of this tip"; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 102 |
Taxon taxon = taxa.getTaxon(i); |
| 103 |
|
| 104 |
Operator operator = tipDateOperators.get(taxon); |
| 105 |
|
| 106 |
if (operator == null) { |
| 107 |
Parameter parameter = getTipDateParameter(taxon); |
| 108 |
|
| 109 |
|
| 110 |
operator = new Operator.Builder("age(" + taxon.getId() + ")", description, parameter, type, 1.0, 1.0).build(); |
| 111 |
if (tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY) { |
| 112 |
operator.setWeight(2.0); |
| 113 |
} |
| 114 |
tipDateOperators.put(taxon, operator); |
| 115 |
} |
| 116 |
ops.add(operator); |
| 117 |
} |
| 118 |
} else if (tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) { |
| 119 |
ops.add(modelOptions.getOperator("treeModel.tipDates")); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
private Parameter getTipDateParameter(Taxon taxon) { |
| 124 |
Parameter parameter = tipDateParameters.get(taxon); |
| 125 |
|
| 126 |
if (parameter == null) { |
| 127 |
double height = 0.0; |
| 128 |
if (taxon.getAttribute("height") != null) { |
| 129 |
height = (Double)taxon.getAttribute("height"); |
| 130 |
} |
| 131 |
|
| 132 |
parameter = new Parameter.Builder("age(" + taxon.getId() + ")", "sampled age of taxon, " + taxon.getId()) |
| 133 |
.scaleType(PriorScaleType.TIME_SCALE).prior(PriorType.UNIFORM_PRIOR).initial(height).isNonNegative(true).build(); |
| 134 |
parameter.setPriorEdited(true); |
| 135 |
tipDateParameters.put(taxon, parameter); |
| 136 |
} |
| 137 |
|
| 138 |
return parameter; |
| 139 |
} |
| 140 |
|
| 141 |
public TaxonList getTaxonSet() { |
| 142 |
TaxonList taxa = options.taxonList; |
| 143 |
|
| 144 |
if (tipDateSamplingTaxonSet != null) { |
| 145 |
taxa = tipDateSamplingTaxonSet; |
| 146 |
} |
| 147 |
|
| 148 |
if (tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) { |
| 149 |
Taxa precisionTaxonList = new Taxa(); |
| 150 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 151 |
Taxon taxon = taxa.getTaxon(i); |
| 152 |
Date date = taxon.getDate(); |
| 153 |
if (date.getUncertainty() > 0.0) { |
| 154 |
precisionTaxonList.addTaxon(taxon); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
taxa = precisionTaxonList; |
| 159 |
} |
| 160 |
|
| 161 |
return taxa; |
| 162 |
} |
| 163 |
|
| 164 |
public TipDateSamplingType tipDateSamplingType = TipDateSamplingType.NO_SAMPLING; |
| 165 |
public TaxonList tipDateSamplingTaxonSet = null; |
| 166 |
|
| 167 |
private Map<Taxon, Parameter> tipDateParameters = new HashMap<Taxon, Parameter>(); |
| 168 |
private Map<Taxon, Operator> tipDateOperators = new HashMap<Taxon, Operator>(); |
| 169 |
} |