| 1 | /* |
| 2 | * PartitionOptions.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.PriorScaleType; |
| 29 | import dr.evolution.datatype.DataType; |
| 30 | import dr.evolution.datatype.Nucleotides; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * @author Alexei Drummond |
| 35 | * @author Andrew Rambaut |
| 36 | * @author Walter Xie |
| 37 | */ |
| 38 | public abstract class PartitionOptions extends ModelOptions { |
| 39 | |
| 40 | private String name; |
| 41 | protected final BeautiOptions options; |
| 42 | |
| 43 | // protected double[] avgRootAndRate = new double[]{1.0, 1.0}; |
| 44 | |
| 45 | public PartitionOptions(BeautiOptions options) { |
| 46 | this.options = options; |
| 47 | } |
| 48 | |
| 49 | public PartitionOptions(BeautiOptions options, String name) { |
| 50 | this.options = options; |
| 51 | this.name = name; |
| 52 | } |
| 53 | |
| 54 | protected void createParameterTree(PartitionOptions options, String name, String description, boolean isNodeHeight) { |
| 55 | new Parameter.Builder(name, description).isNodeHeight(isNodeHeight).scaleType(PriorScaleType.TIME_SCALE) |
| 56 | .isNonNegative(true).initial(Double.NaN).partitionOptions(options).build(parameters); |
| 57 | } |
| 58 | |
| 59 | public Parameter getParameter(String name) { |
| 60 | |
| 61 | Parameter parameter = parameters.get(name); |
| 62 | |
| 63 | if (parameter == null) { |
| 64 | throw new IllegalArgumentException("Parameter with name, " + name + ", is unknown"); |
| 65 | } |
| 66 | |
| 67 | parameter.setPrefix(getPrefix()); |
| 68 | |
| 69 | // autoScale(parameter); // not include clock rate, and treeModel.rootHeight |
| 70 | |
| 71 | return parameter; |
| 72 | } |
| 73 | |
| 74 | public boolean hasParameter(String name) { |
| 75 | return parameters.get(name) != null; |
| 76 | } |
| 77 | |
| 78 | public Operator getOperator(String name) { |
| 79 | |
| 80 | Operator operator = operators.get(name); |
| 81 | |
| 82 | if (operator == null) throw new IllegalArgumentException("Operator with name, " + name + ", is unknown"); |
| 83 | |
| 84 | operator.setPrefix(getPrefix()); |
| 85 | |
| 86 | return operator; |
| 87 | } |
| 88 | |
| 89 | public String getName() { |
| 90 | return name; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | public void setName(String name) { |
| 95 | this.name = name; |
| 96 | } |
| 97 | |
| 98 | public String toString() { |
| 99 | return getName(); |
| 100 | } |
| 101 | |
| 102 | public DataType getDataType() { |
| 103 | if (options.getDataPartitions(this).size() == 0) { |
| 104 | return Nucleotides.INSTANCE; |
| 105 | } |
| 106 | return options.getDataPartitions(this).get(0).getDataType(); |
| 107 | } |
| 108 | |
| 109 | // public double[] getAvgRootAndRate() { |
| 110 | // return avgRootAndRate; |
| 111 | // } |
| 112 | // |
| 113 | // public void setAvgRootAndRate() { |
| 114 | // this.avgRootAndRate = options.clockModelOptions.calculateInitialRootHeightAndRate(options.getDataPartitions(this)); |
| 115 | // } |
| 116 | |
| 117 | protected void autoScale(Parameter param) { |
| 118 | //// double avgInitialRootHeight = avgRootAndRate[0]; |
| 119 | //// double avgInitialRate = avgRootAndRate[1]; |
| 120 | // double avgInitialRootHeight = 1.0; |
| 121 | // double avgInitialRate = 0.1; |
| 122 | // |
| 123 | //// double growthRateMaximum = 1E6; |
| 124 | // double birthRateMaximum = 1E6; |
| 125 | //// double substitutionRateMaximum = 100; |
| 126 | //// double logStdevMaximum = 10; |
| 127 | //// double substitutionParameterMaximum = 100; |
| 128 | // |
| 129 | //// if (options.clockModelOptions.getRateOptionClockModel() == FixRateType.FIX_MEAN |
| 130 | //// || options.clockModelOptions.getRateOptionClockModel() == FixRateType.RELATIVE_TO) { |
| 131 | //// |
| 132 | //// growthRateMaximum = 1E6 * avgInitialRate; |
| 133 | // birthRateMaximum = 1E6 * avgInitialRate; |
| 134 | //// } |
| 135 | // |
| 136 | //// if (options.clockModelOptions.getRateOptionClockModel() == FixRateType.FIX_MEAN) { |
| 137 | //// double rate = options.clockModelOptions.getMeanRelativeRate(); |
| 138 | //// |
| 139 | //// growthRateMaximum = 1E6 * rate; |
| 140 | //// birthRateMaximum = 1E6 * rate; |
| 141 | //// |
| 142 | //// if (options.hasData()) { |
| 143 | //// initialRootHeight = meanDistance / rate; |
| 144 | //// |
| 145 | //// initialRootHeight = round(initialRootHeight, 2); |
| 146 | //// } |
| 147 | //// |
| 148 | //// } else { |
| 149 | //// if (options.maximumTipHeight > 0) { |
| 150 | //// initialRootHeight = options.maximumTipHeight * 10.0; |
| 151 | //// } |
| 152 | //// |
| 153 | //// initialRate = round((meanDistance * 0.2) / initialRootHeight, 2); |
| 154 | //// } |
| 155 | // |
| 156 | //// double timeScaleMaximum = MathUtils.round(avgInitialRootHeight * 1000.0, 2); |
| 157 | // |
| 158 | // |
| 159 | //// if (!options.hasData()) param.setPriorEdited(false); |
| 160 | // |
| 161 | // if (!param.isPriorEdited()) { |
| 162 | // switch (param.scaleType) { |
| 163 | // case TIME_SCALE: |
| 164 | //// param.lower = Math.max(0.0, param.lower); |
| 165 | //// param.upper = Math.min(timeScaleMaximum, param.upper); |
| 166 | //// if (param.isNodeHeight) { //TODO only affecting "treeModel.rootHeight", need to review |
| 167 | //// param.lower = options.maximumTipHeight; |
| 168 | ////// param.upper = timeScaleMaximum; |
| 169 | ////// param.initial = avgInitialRootHeight; |
| 170 | //// if (param.getOptions() instanceof PartitionTreeModel) { // move to PartitionTreeModel |
| 171 | //// param.initial = ((PartitionTreeModel) param.getOptions()).getInitialRootHeight(); |
| 172 | //// } |
| 173 | //// } else { |
| 174 | // param.initial = avgInitialRootHeight; |
| 175 | //// } |
| 176 | // |
| 177 | // break; |
| 178 | // case LOG_TIME_SCALE: |
| 179 | // param.initial = Math.log(avgInitialRootHeight); |
| 180 | // break; |
| 181 | // |
| 182 | // case T50_SCALE: |
| 183 | //// param.lower = Math.max(0.0, param.lower); |
| 184 | // //param.upper = Math.min(timeScaleMaximum, param.upper); |
| 185 | // param.initial = avgInitialRootHeight / 5.0; |
| 186 | // break; |
| 187 | // |
| 188 | // case GROWTH_RATE_SCALE: |
| 189 | // param.initial = avgInitialRootHeight / 1000; |
| 190 | // // use Laplace |
| 191 | // if (param.getBaseName().startsWith("logistic")) { |
| 192 | // param.scale = Math.log(1000) / avgInitialRootHeight; |
| 193 | //// System.out.println("logistic"); |
| 194 | // } else { |
| 195 | // param.scale = Math.log(10000) / avgInitialRootHeight; |
| 196 | //// System.out.println("not logistic"); |
| 197 | // } |
| 198 | // break; |
| 199 | // |
| 200 | // case BIRTH_RATE_SCALE: |
| 201 | //// param.uniformLower = Math.max(0.0, param.lower); |
| 202 | //// param.uniformUpper = Math.min(birthRateMaximum, param.upper); |
| 203 | // param.initial = MathUtils.round(1 / options.treeModelOptions.getExpectedAvgBranchLength(avgInitialRootHeight), 2); |
| 204 | // break; |
| 205 | // case ORIGIN_SCALE: |
| 206 | // param.initial = MathUtils.round(avgInitialRootHeight * 1.1, 2); |
| 207 | // break; |
| 208 | // |
| 209 | // case SUBSTITUTION_RATE_SCALE: |
| 210 | //// param.lower = Math.max(0.0, param.lower); |
| 211 | // //param.upper = Math.min(substitutionRateMaximum, param.upper); |
| 212 | // param.initial = avgInitialRate; |
| 213 | // break; |
| 214 | // |
| 215 | // case LOG_STDEV_SCALE: |
| 216 | //// param.lower = Math.max(0.0, param.lower); |
| 217 | // //param.upper = Math.min(logStdevMaximum, param.upper); |
| 218 | // break; |
| 219 | // |
| 220 | // case SUBSTITUTION_PARAMETER_SCALE: |
| 221 | //// param.lower = Math.max(0.0, param.lower); |
| 222 | // //param.upper = Math.min(substitutionParameterMaximum, param.upper); |
| 223 | // break; |
| 224 | // |
| 225 | // // Now have a field 'isZeroOne' |
| 226 | //// case UNITY_SCALE: |
| 227 | //// param.lower = 0.0; |
| 228 | //// param.upper = 1.0; |
| 229 | //// break; |
| 230 | // |
| 231 | // case ROOT_RATE_SCALE: |
| 232 | // param.initial = avgInitialRate; |
| 233 | // param.shape = 0.5; |
| 234 | // param.scale = param.initial / 0.5; |
| 235 | // break; |
| 236 | // |
| 237 | // case LOG_VAR_SCALE: |
| 238 | // param.initial = avgInitialRate; |
| 239 | // param.shape = 2.0; |
| 240 | // param.scale = param.initial / 2.0; |
| 241 | // break; |
| 242 | // |
| 243 | // } |
| 244 | // |
| 245 | // } |
| 246 | } |
| 247 | |
| 248 | public BeautiOptions getOptions() { |
| 249 | return options; |
| 250 | } |
| 251 | |
| 252 | } |