Debian Codegraph @

src/dr/app/beauti/options/PartitionTreeModel.java

raw symbols SCIP
1 /*
2 * PartitionTreeModel.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.options;
27
28 import dr.app.beauti.types.*;
29 import dr.evolution.datatype.PloidyType;
30 import dr.evolution.tree.Tree;
31
32 import java.util.List;
33
34 /**
35 * @author Andrew Rambaut
36 * @author Walter Xie
37 * @version $Id$
38 */
39 public class PartitionTreeModel extends PartitionOptions {
40
41 private static final long serialVersionUID = 4829401415152235625L;
42
43 private PartitionTreePrior treePrior;
44
45 private StartingTreeType startingTreeType = StartingTreeType.RANDOM;
46 private Tree userStartingTree = null;
47
48 private boolean isNewick = true;
49
50 //TODO if use EBSP and *BEAST, validate Ploidy of every PD is same for each tree that the PD(s) belongs to
51 // BeastGenerator.checkOptions()
52 private PloidyType ploidyType = PloidyType.AUTOSOMAL_NUCLEAR;
53
54 private boolean hasTipCalibrations = false;
55 private boolean hasNodeCalibrations = false;
56
57
58 public PartitionTreeModel(BeautiOptions options, String name) {
59 super(options, name);
60
61 initModelParametersAndOpererators();
62 }
63
64 /**
65 * A copy constructor
66 *
67 * @param options the beauti options
68 * @param name the name of the new model
69 * @param source the source model
70 */
71 public PartitionTreeModel(BeautiOptions options, String name, PartitionTreeModel source) {
72 super(options, name);
73
74 treePrior = source.treePrior;
75 startingTreeType = source.startingTreeType;
76 userStartingTree = source.userStartingTree;
77
78 isNewick = source.isNewick;
79 // initialRootHeight = source.initialRootHeight;
80 ploidyType = source.ploidyType;
81
82 initModelParametersAndOpererators();
83 }
84
85 public void initModelParametersAndOpererators() {
86
87 createParameter("tree", "The tree");
88 createParameter("treeModel.internalNodeHeights", "internal node heights of the tree (except the root)");
89 createParameter("treeModel.allInternalNodeHeights", "internal node heights of the tree");
90 createParameterTree(this, "treeModel.rootHeight", "root height of the tree", true);
91
92 //TODO treeBitMove should move to PartitionClockModelTreeModelLink, after Alexei finish
93 createOperator("treeBitMove", "Tree", "Swaps the rates and change locations of local clocks", "tree",
94 OperatorType.TREE_BIT_MOVE, -1.0, treeWeights);
95
96 createScaleOperator("treeModel.rootHeight", demoTuning, demoWeights);
97 createOperator("uniformHeights", "Internal node heights", "Draws new internal node heights uniformally",
98 "treeModel.internalNodeHeights", OperatorType.UNIFORM, -1, branchWeights);
99
100 // This scale operator is used instead of the up/down if the rate is fixed.
101 new Operator.Builder("treeModel.allInternalNodeHeights", "Scales all internal node heights in tree", getParameter("treeModel.allInternalNodeHeights"), OperatorType.SCALE_ALL, 0.75, rateWeights).build(operators);
102
103 createOperator("subtreeSlide", "Tree", "Performs the subtree-slide rearrangement of the tree", "tree",
104 OperatorType.SUBTREE_SLIDE, 1.0, treeWeights);
105 createOperator("narrowExchange", "Tree", "Performs local rearrangements of the tree", "tree",
106 OperatorType.NARROW_EXCHANGE, -1, treeWeights);
107 createOperator("wideExchange", "Tree", "Performs global rearrangements of the tree", "tree",
108 OperatorType.WIDE_EXCHANGE, -1, demoWeights);
109 createOperator("wilsonBalding", "Tree", "Performs the Wilson-Balding rearrangement of the tree", "tree",
110 OperatorType.WILSON_BALDING, -1, demoWeights);
111
112 double weight = Math.max(options.taxonList.getTaxonCount(), 30);
113 createOperator("subtreeLeap", "Tree", "Performs the subtree-leap rearrangement of the tree", "tree",
114 OperatorType.SUBTREE_LEAP, 1.0, weight);
115
116 weight = Math.max(weight / 10, 3);
117 createOperator("subtreeJump", "Tree", "Performs the subtree-jump rearrangement of the tree", "tree",
118 OperatorType.SUBTREE_JUMP, 1.0, weight);
119
120 }
121
122 @Override
123 public List<Parameter> selectParameters(List<Parameter> parameters) {
124 // setAvgRootAndRate();
125
126 // Don't add these to the parameter list (as they don't appear in the table), but call
127 // get parameter so their id prefix can be set.
128 getParameter("tree");
129 getParameter("treeModel.internalNodeHeights");
130 getParameter("treeModel.allInternalNodeHeights");
131
132 Parameter rootHeightParameter = getParameter("treeModel.rootHeight");
133 if (rootHeightParameter.priorType == PriorType.NONE_TREE_PRIOR || !rootHeightParameter.isPriorEdited()) {
134 rootHeightParameter.setInitial(getInitialRootHeight());
135 rootHeightParameter.truncationLower = options.maximumTipHeight;
136 rootHeightParameter.uniformLower = options.maximumTipHeight;
137 rootHeightParameter.isTruncated = true;
138 }
139
140 rootHeightParameter.isCalibratedYule = treePrior.getNodeHeightPrior() == TreePriorType.YULE_CALIBRATION;
141 parameters.add(rootHeightParameter);
142
143 return parameters;
144 }
145
146 @Override
147 public List<Operator> selectOperators(List<Operator> operators) {
148 // setAvgRootAndRate();
149
150 Operator subtreeSlideOp = getOperator("subtreeSlide");
151 if (!subtreeSlideOp.isTuningEdited()) {
152 double tuning = 1.0;
153 if (!Double.isNaN(getInitialRootHeight()) && !Double.isInfinite(getInitialRootHeight())) {
154 tuning = getInitialRootHeight() / 10.0;
155 }
156 subtreeSlideOp.setTuning(tuning);
157 }
158
159 operators.add(subtreeSlideOp);
160 operators.add(getOperator("narrowExchange"));
161 operators.add(getOperator("wideExchange"));
162 operators.add(getOperator("wilsonBalding"));
163
164 operators.add(getOperator("treeModel.rootHeight"));
165 operators.add(getOperator("uniformHeights"));
166
167 operators.add(getOperator("subtreeLeap"));
168 operators.add(getOperator("subtreeJump"));
169
170 if (options.operatorSetType != OperatorSetType.CUSTOM) {
171 // do nothing
172 boolean defaultInUse = false;
173 boolean branchesInUse = false;
174 boolean newTreeOperatorsInUse = false;
175 boolean adaptiveMultivariateInUse = false;
176
177 // if not a fixed tree then sample tree space
178 if (options.operatorSetType == OperatorSetType.DEFAULT) {
179 defaultInUse = true;
180 branchesInUse = true;
181 } else if (options.operatorSetType == OperatorSetType.NEW_TREE_MIX) {
182 newTreeOperatorsInUse = true;
183 } else if (options.operatorSetType == OperatorSetType.FIXED_TREE_TOPOLOGY) {
184 branchesInUse = true;
185 } else if (options.operatorSetType == OperatorSetType.ADAPTIVE_MULTIVARIATE) {
186 newTreeOperatorsInUse = true;
187 adaptiveMultivariateInUse = true;
188 } else {
189 throw new IllegalArgumentException("Unknown operator set type");
190 }
191
192 getOperator("subtreeSlide").setUsed(defaultInUse);
193 getOperator("narrowExchange").setUsed(defaultInUse);
194 getOperator("wideExchange").setUsed(defaultInUse);
195 getOperator("wilsonBalding").setUsed(defaultInUse);
196
197 getOperator("treeModel.rootHeight").setUsed(branchesInUse);
198 getOperator("uniformHeights").setUsed(branchesInUse);
199
200 getOperator("subtreeLeap").setUsed(newTreeOperatorsInUse);
201 getOperator("subtreeJump").setUsed(newTreeOperatorsInUse);
202 }
203 return operators;
204 }
205
206 /////////////////////////////////////////////////////////////
207
208 public PartitionTreePrior getPartitionTreePrior() {
209 return treePrior;
210 }
211
212 public void setPartitionTreePrior(PartitionTreePrior treePrior) {
213 options.clearDataPartitionCaches();
214 this.treePrior = treePrior;
215 }
216
217 public StartingTreeType getStartingTreeType() {
218 return startingTreeType;
219 }
220
221 public void setStartingTreeType(StartingTreeType startingTreeType) {
222 this.startingTreeType = startingTreeType;
223 }
224
225 public Tree getUserStartingTree() {
226 return userStartingTree;
227 }
228
229 public void setUserStartingTree(Tree userStartingTree) {
230 this.userStartingTree = userStartingTree;
231 }
232
233 public boolean isNewick() {
234 return isNewick;
235 }
236
237 public void setNewick(boolean isNewick) {
238 this.isNewick = isNewick;
239 }
240
241 public void setTipCalibrations(boolean hasTipCalibrations) {
242 this.hasTipCalibrations = hasTipCalibrations;
243 }
244
245 public boolean hasTipCalibrations() {
246 return hasTipCalibrations;
247 }
248
249 public void setNodeCalibrations(boolean hasNodeCalibrations) {
250 this.hasNodeCalibrations = hasNodeCalibrations;
251 }
252
253 public boolean hasNodeCalibrations() {
254 return hasNodeCalibrations;
255 }
256
257 public void setPloidyType(PloidyType ploidyType) {
258 this.ploidyType = ploidyType;
259 }
260
261 public PloidyType getPloidyType() {
262 return ploidyType;
263 }
264
265 public double getInitialRootHeight() {
266 return Double.NaN;
267 // return getAvgRootAndRate()[0];
268 }
269
270 // public void setInitialRootHeight(double initialRootHeight) {
271 // this.initialRootHeight = initialRootHeight;
272 // }
273
274 // private void calculateInitialRootHeightPerTree() {
275 // initialRootHeight = options.clockModelOptions
276 // .calculateInitialRootHeightAndRate(options.getDataPartitions(this)) [0];
277 // }
278
279 public String getPrefix() {
280 String prefix = "";
281 if (options.getPartitionTreeModels().size() > 1) { //|| options.isSpeciesAnalysis()
282 // There is more than one active partition model
283 prefix += getName() + ".";
284 }
285 return prefix;
286 }
287
288 public int getDimension() { // n-1
289 return options.getTaxonCount(options.getDataPartitions(this)) - 1;
290 }
291
292 public int getTaxonCount() {
293 return options.getTaxonCount(options.getDataPartitions(this));
294 }
295 }