Debian Codegraph @

src/dr/oldevomodel/sitemodel/SampleStateAndCategoryModel.java

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