Debian Codegraph @

src/dr/app/beauti/components/hpm/HierarchicalModelComponentOptions.java

raw symbols SCIP
1 /*
2 * HierarchicalModelComponentOptions.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.hpm;
27
28 import dr.app.beauti.options.*;
29 import dr.app.beauti.priorspanel.PriorsPanel;
30 import dr.app.beauti.types.PriorScaleType;
31 import dr.app.beauti.types.PriorType;
32
33 import javax.swing.*;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * @author Marc A. Suchard
39 * @version $Id$
40 */
41 public class HierarchicalModelComponentOptions implements ComponentOptions {
42
43 public HierarchicalModelComponentOptions(final BeautiOptions options) {
44 this.options = options;
45 hpmList = new ArrayList<HierarchicalPhylogeneticModel>();
46 }
47
48 public void createParameters(final ModelOptions modelOptions) {
49 // Do nothing; this is only called at launch
50 }
51
52 public void selectParameters(final ModelOptions modelOptions, final List<Parameter> params) {
53 // Do nothing because priors are previously defined for idref in operator schedule
54 // for (HierarchicalPhylogeneticModel hpm : hpmList) {
55 // if (!hpm.isEmpty()) {
56 // List<Parameter> hpmParameterList = hpm.getConditionalParameterList();
57 // params.addAll(hpmParameterList);
58 // }
59 // }
60 }
61
62 public void selectStatistics(final ModelOptions modelOptions, final List<Parameter> stats) {
63 // No statistics
64 }
65
66 public void selectOperators(final ModelOptions modelOptions, final List<Operator> ops) {
67 // Do nothing because Gibbs operator format do not fit into the current Operator implementation
68 // for (HierarchicalPhylogeneticModel hpm : hpmList) {
69 // if (!hpm.isEmpty()) {
70 // hpm.selectOperators(modelOptions, ops);
71 // }
72 // }
73 }
74
75 public boolean modelExists(String name) {
76 boolean found = false;
77 for (HierarchicalPhylogeneticModel hpm : hpmList) {
78 if (hpm.getName().compareTo(name) == 0) {
79 found = true;
80 break;
81 }
82 }
83 return found;
84 }
85
86 public HierarchicalPhylogeneticModel addHPM(String text, List<Parameter> parameterList, PriorType priorType) {
87 List<Parameter> argumentList = new ArrayList<Parameter>();
88
89 // TODO May have to remove these constructors
90 String meanName = text + HierarchicalModelComponentGenerator.MEAN_SUFFIX;
91 Parameter mean = options.parameterExists(meanName) ?
92 options.getParameter(meanName) :
93 options.createParameterNormalPrior(meanName, "Unknown mean of HPM",
94 PriorScaleType.NONE, 0.0, 0.0, 1.0, 0.0);
95 argumentList.add(mean);
96
97 String precisionName = text + HierarchicalModelComponentGenerator.PRECISION_SUFFIX;
98 Parameter precision = options.parameterExists(precisionName) ?
99 options.getParameter(precisionName) :
100 options.createParameterGammaPrior(precisionName, "Unknown precision of HPM",
101 PriorScaleType.NONE, 1.0, 0.001, 1000.0, true);
102 argumentList.add(precision);
103
104 HierarchicalPhylogeneticModel hpm = new HierarchicalPhylogeneticModel(text, parameterList, argumentList, priorType);
105 hpmList.add(hpm);
106
107 for (Parameter parameter : parameterList) {
108 parameter.linkedName = hpm.getName();
109 }
110 return hpm;
111 }
112
113 public boolean isHierarchicalParameter(Parameter parameter) {
114 boolean found = false;
115 for (HierarchicalPhylogeneticModel hpm : hpmList) {
116 if (hpm.getArgumentParameterList().contains(parameter)) {
117 found = true;
118 break;
119 }
120 }
121 return found;
122 }
123
124 public int removeParameter(PriorsPanel priorsPanel, Parameter parameter, boolean caution) {
125 HierarchicalPhylogeneticModel toRemove = null;
126 for (HierarchicalPhylogeneticModel hpm : hpmList) {
127 List<Parameter> parameterList = hpm.getArgumentParameterList();
128 if (parameterList.contains(parameter)) {
129 if (caution && parameterList.size() == 2 && priorsPanel != null) {
130 String modelName = hpm.getName();
131 // Throw special warning
132 int option = JOptionPane.showConfirmDialog(priorsPanel,
133 "Removing this parameter from HPM '" + modelName + "' will result in only one\n" +
134 "parameter remaining the HPM. Single parameter models are not recommended.\n" +
135 "Continue?",
136 "HPM warning",
137 JOptionPane.YES_NO_OPTION,
138 JOptionPane.WARNING_MESSAGE);
139 if (option == JOptionPane.NO_OPTION) {
140 return JOptionPane.NO_OPTION;
141 }
142 }
143 parameterList.remove(parameter);
144 }
145 if (hpm.isEmpty()) {
146 toRemove = hpm;
147 }
148 }
149 if (toRemove != null) {
150 hpmList.remove(toRemove);
151 }
152 return JOptionPane.YES_OPTION;
153 }
154
155 public List<HierarchicalPhylogeneticModel> getHPMList() {
156 return hpmList;
157 }
158
159 // public void generateDistributions(final XMLWriter writer) {
160 // for (HierarchicalPhylogeneticModel hpm : hpmList) {
161 // hpm.generateDistribution(writer);
162 // }
163 // }
164
165 final private BeautiOptions options;
166 final private List<HierarchicalPhylogeneticModel> hpmList;
167
168
169 // public void generatePriors(final XMLWriter writer) {
170 // for (HierarchicalPhylogeneticModel hpm : hpmList) {
171 // hpm.generatePriors(writer);
172 // }
173 // }
174
175 public boolean isEmpty() {
176 return hpmList.isEmpty();
177 }
178 }