Debian Codegraph @

src/dr/oldevomodel/sitemodel/SampleStateModel.java

raw symbols SCIP
1 /*
2 * SampleStateModel.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.oldevomodel.sitemodel;
27
28 import dr.oldevomodel.substmodel.FrequencyModel;
29 import dr.oldevomodel.substmodel.SubstitutionModel;
30 import dr.oldevomodel.substmodel.YangCodonModel;
31 import dr.oldevomodelxml.sitemodel.SampleStateModelParser;
32 import dr.inference.model.*;
33
34 import java.util.Vector;
35
36
37 /**
38 * DiscreteSampleModel - A SiteModel that has a discrete distribution of substitutionmodels over sites
39 * designed for sampling of rate categories.
40 *
41 * @author Roald Forsberg
42 */
43
44 public class SampleStateModel extends AbstractModel implements SiteModel {
45 public static final double OMEGA_MAX_VALUE = 100.0;
46 public static final double OMEGA_MIN_VALUE = 0.0;
47
48 /**
49 * Constructor
50 */
51 public SampleStateModel(Parameter muParameter,
52 Parameter proportionParameter,
53 Vector substitutionModels) {
54
55 super(SampleStateModelParser.SAMPLE_STATE_MODEL);
56
57
58 this.substitutionModels = substitutionModels;
59
60 for (int i = 0; i < substitutionModels.size(); i++) {
61 addModel((SubstitutionModel) substitutionModels.elementAt(i));
62 }
63
64 this.categoryCount = substitutionModels.size();
65
66 // stateCount = ((SubstitutionModel)substitutionModels.elementAt(0)).getDataType().getStateCount();
67
68 addVariable(muParameter);
69 muParameter.addBounds(new Parameter.DefaultBounds(1000.0, 0.0, 1));
70
71 this.proportionParameter = proportionParameter;
72 addVariable(proportionParameter);
73 proportionParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, proportionParameter.getDimension()));
74
75 proportionParameter.setParameterValue(0, (0.5));
76 for (int i = 1; i < categoryCount; i++) {
77 proportionParameter.setParameterValue(i, (0.5 / (categoryCount - 1)));
78 }
79
80 if (categoryCount > 1) {
81 for (int i = 0; i < categoryCount; i++) {
82 Parameter p = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i)).getVariable(0);
83 Parameter lower = null;
84 Parameter upper = null;
85
86 if (i == 0) {
87 upper = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i + 1)).getVariable(0);
88 p.addBounds(new omegaBounds(lower, upper));
89 } else {
90 if (i == (categoryCount - 1)) {
91 lower = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i - 1)).getVariable(0);
92 p.addBounds(new omegaBounds(lower, upper));
93 } else {
94 upper = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i + 1)).getVariable(0);
95 lower = (Parameter) ((YangCodonModel) substitutionModels.elementAt(i - 1)).getVariable(0);
96 p.addBounds(new omegaBounds(lower, upper));
97 }
98 }
99 }
100 }
101 }
102
103 // *****************************************************************
104 // Interface SiteModel
105 // *****************************************************************
106
107 public SubstitutionModel getSubstitutionModel() {
108 return null;
109 }
110
111 public boolean integrateAcrossCategories() {
112 return true;
113 }
114
115 public int getCategoryCount() {
116 return categoryCount;
117 }
118
119 public int getCategoryOfSite(int site) {
120 throw new IllegalArgumentException("Integrating across categories");
121 }
122
123 public double[] getCategoryRates() {
124 throw new RuntimeException("getCategoryRates not available in this siteModel");
125 }
126
127 public double getRateForCategory(int category) {
128 throw new RuntimeException("getRateForCategory not available in this siteModel");
129 }
130
131 public double getSubstitutionsForCategory(int category, double time) {
132 throw new RuntimeException("getSubstitutionsForCategory not available in this siteModel");
133 }
134
135 public void getTransitionProbabilities(double substitutions, double[] matrix) {
136 throw new RuntimeException("getTransitionProbabilities not available in this siteModel");
137 }
138
139 /**
140 * Get the frequencyModel for this SiteModel.
141 *
142 * @return the frequencyModel.
143 */
144 public FrequencyModel getFrequencyModel() {
145 return ((SubstitutionModel) substitutionModels.elementAt(0)).getFrequencyModel();
146 }
147
148 /**
149 * Get the expected proportion of sites in this category.
150 *
151 * @param category the category number
152 * @return the proportion.
153 */
154 public double getProportionForCategory(int category) {
155 return proportionParameter.getParameterValue(category);
156 }
157
158 /**
159 * Get an array of the expected proportion of sites in this category.
160 *
161 * @return an array of the proportion.
162 */
163 public double[] getCategoryProportions() {
164 double[] probs = new double[categoryCount];
165
166 for (int i = 0; i < categoryCount; i++) {
167 probs[i] = proportionParameter.getParameterValue(i);
168 }
169
170 return probs;
171 }
172
173 // *****************************************************************
174 // Interface ModelComponent
175 // *****************************************************************
176
177 protected void handleModelChangedEvent(Model model, Object object, int index) {
178 // Substitution model has changed so fire model changed event
179 listenerHelper.fireModelChanged(this, object, index);
180 }
181
182
183 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
184 /*if(categoryCount > 1){
185 for(int i = 0; i < categoryCount; i++){
186 if(parameter == classSizes[i] && i == 0) // proportions have changed
187 resetProportions(i+1);// adjust the proportion of class above
188 if(parameter == classSizes[i] && i != 0) // proportions have changed
189 resetProportions(i-1);// adjust the proportion of class below
190 }
191 } else{}*/
192 }
193
194 protected void storeState() {
195 }
196
197 protected void restoreState() {
198 }
199
200 protected void acceptState() {
201 } // no additional state needs accepting
202
203 public String toString() {
204 StringBuffer s = new StringBuffer();
205
206 return s.toString();
207 }
208
209 /*private void resetProportions(int classToAlter){
210 double[] probs = getCategoryProportions();
211 double sum = 0.0;
212 double current = classSizes[classToAlter].getParameterValue(0);
213 for(int i = 0; i < categoryCount; i++){
214 sum += probs[i];
215 }
216 sum = 1 - sum;
217 classSizes[classToAlter].setParameterValue(0,(current+sum));
218 }*/
219
220 private class omegaBounds implements Bounds<Double> {
221
222 private final Parameter lowerOmega, upperOmega;
223
224 public omegaBounds(Parameter lowerOmega, Parameter upperOmega) {
225
226 this.lowerOmega = lowerOmega;
227 this.upperOmega = upperOmega;
228 }
229
230 public omegaBounds(Parameter nearestOmega, boolean isUpper) {
231
232 if (isUpper) {
233 lowerOmega = nearestOmega;
234 upperOmega = null;
235 } else {
236 lowerOmega = null;
237 upperOmega = nearestOmega;
238 }
239 }
240
241 /**
242 * @return the upper limit of this hypervolume in the given dimension.
243 */
244 public Double getUpperLimit(int dimension) {
245
246 if (dimension != 0)
247 throw new RuntimeException("omega parameters have wrong dimension " + dimension);
248
249 if (upperOmega == null)
250 return OMEGA_MAX_VALUE;
251 else
252 return upperOmega.getParameterValue(dimension);
253
254 }
255
256 /**
257 * @return the lower limit of this hypervolume in the given dimension.
258 */
259 public Double getLowerLimit(int dimension) {
260
261 if (dimension != 0)
262 throw new RuntimeException("omega parameters have wrong dimension " + dimension);
263
264 if (lowerOmega == null)
265 return OMEGA_MIN_VALUE;
266
267 else
268 return lowerOmega.getParameterValue(dimension);
269 }
270
271 /**
272 * @return the dimensionality of this hypervolume.
273 */
274 public int getBoundsDimension() {
275 return 1;
276 }
277
278 }
279
280 class classSizeBounds implements Bounds {
281
282 private final Parameter catProportionUnder;
283 private final Parameter catProportionAbove;
284 private final Parameter catProportion;
285
286 public classSizeBounds(Parameter catProportion, Parameter catProportionUnder, Parameter catProportionAbove) {
287
288 this.catProportionUnder = catProportionUnder;
289 this.catProportionAbove = catProportionAbove;
290 this.catProportion = catProportion;
291 }
292
293 /**
294 * @return the upper limit of this hypervolume in the given dimension.
295 */
296 public Double getUpperLimit(int dimension) {
297
298 if (dimension != 0)
299 throw new RuntimeException("class size parameters have wrong dimension " + dimension);
300 if (catProportionUnder == null)
301 return catProportion.getParameterValue(dimension) + catProportionAbove.getParameterValue(dimension);
302 else
303 return catProportion.getParameterValue(dimension) + catProportionUnder.getParameterValue(dimension);
304 }
305
306 /**
307 * @return the lower limit of this hypervolume in the given dimension.
308 */
309 public Double getLowerLimit(int dimension) {
310
311 if (dimension != 0)
312 throw new RuntimeException("class size parameters have wrong dimension " + dimension);
313
314 return 0.0;
315 }
316
317 /**
318 * @return the dimensionality of this hypervolume.
319 */
320 public int getBoundsDimension() {
321 return 1;
322 }
323
324 }
325
326 private final Vector substitutionModels;
327
328 /**
329 * class size parameters
330 */
331 private final Parameter proportionParameter;
332
333 private final int categoryCount;
334
335 // private int stateCount;
336 }