Debian Codegraph @

src/dr/app/beauti/components/tipdatesampling/TipDateSamplingComponentOptions.java

raw symbols SCIP
1 /*
2 * TipDateSamplingComponentOptions.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Andrew Rambaut
44 * @version $Id$
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 // no explicit priors when sampling from precisions - the random walk operators
72 // sample uniformly from within the bounds defined by the precision.
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 // no statistics
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 //OperatorType type = OperatorType.RANDOM_WALK_ABSORBING;
94 OperatorType type = OperatorType.UNIFORM;
95
96 if (tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) {
97 description = "Uniform sample from precision of age of this tip";
98 //type = OperatorType.UNIFORM;
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 // operator = new Operator("age(" + taxon.getId() + ")", "", parameter, OperatorType.SCALE, 0.75, 1.0);
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 }