Debian Codegraph @

src/dr/oldevomodel/sitemodel/CategorySiteModel.java

raw symbols SCIP
1 /*
2 * CategorySiteModel.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.inference.model.AbstractModel;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 /**
36 * CategorySiteModel - A SiteModel that has a different rate for each category;
37 *
38 * @author Alexei Drummond
39 * @version $Id: CategorySiteModel.java,v 1.3 2004/10/01 22:40:04 alexei Exp $
40 */
41
42 public class CategorySiteModel extends AbstractModel implements SiteModel {
43
44 /**
45 * @param rateParameter (relative to the rate of category 1)
46 */
47 public CategorySiteModel(SubstitutionModel substitutionModel,
48 Parameter muParameter,
49 Parameter rateParameter,
50 String categoryString,
51 String stateString,
52 int relativeTo) {
53
54 super(SITE_MODEL);
55
56 this.substitutionModel = substitutionModel;
57 addModel(substitutionModel);
58
59 this.muParameter = muParameter;
60 addVariable(muParameter);
61 muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
62
63 this.rateParameter = rateParameter;
64 addVariable(rateParameter);
65 rateParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, Double.MIN_VALUE, rateParameter.getDimension()));
66
67 states = stateString;
68
69 if (states.length() != (rateParameter.getDimension() + 1)) {
70 throw new IllegalArgumentException("States must have one more dimension than rate parameter!");
71 }
72 categoryCount = states.length();
73
74 categories = new int[categoryString.length()];
75 categoryWeights = new int[categoryCount];
76 categoryRates = new double[categoryCount];
77 for (int i = 0; i < categories.length; i++) {
78 char state = categoryString.charAt(i);
79 categories[i] = states.indexOf(state);
80 categoryWeights[categories[i]] += 1;
81 }
82
83 siteCount = categories.length;
84 this.relativeTo = relativeTo;
85 ratesKnown = false;
86 }
87
88 /**
89 * set mu
90 */
91 public void setMu(double mu) {
92 muParameter.setParameterValue(0, mu);
93 }
94
95 /**
96 * @return mu
97 */
98 public final double getMu() {
99 return muParameter.getParameterValue(0);
100 }
101
102
103 public Parameter getMutationRateParameter() {
104 return muParameter;
105 }
106
107 // *****************************************************************
108 // Interface SiteModel
109 // *****************************************************************
110
111 public boolean integrateAcrossCategories() {
112 return false;
113 }
114
115 public int getCategoryCount() {
116 return categoryCount;
117 }
118
119 public int getCategoryOfSite(int site) {
120
121 return categories[site];
122 }
123
124 public double getRateForCategory(int category) {
125 synchronized (this) {
126 if (!ratesKnown) {
127 calculateCategoryRates();
128 }
129 }
130 double mu = 1.0;
131 if (muParameter != null) {
132 mu = muParameter.getParameterValue(0);
133 }
134
135 return categoryRates[category] * mu;
136 }
137
138 public double[] getCategoryRates() {
139 synchronized (this) {
140 if (!ratesKnown) {
141 calculateCategoryRates();
142 }
143 }
144 double[] rates = new double[categoryRates.length];
145 double mu = 1.0;
146 if (muParameter != null) {
147 mu = muParameter.getParameterValue(0);
148 }
149 for (int i = 0; i < rates.length; i++) {
150 rates[i] = categoryRates[i] * mu;
151 }
152
153 return rates;
154 }
155
156 public void getTransitionProbabilities(double substitutions, double[] matrix) {
157 substitutionModel.getTransitionProbabilities(substitutions, matrix);
158 }
159
160 /**
161 * Get the expected proportion of sites in this category.
162 *
163 * @param category the category number
164 * @return the proportion.
165 */
166 public double getProportionForCategory(int category) {
167 throw new UnsupportedOperationException();
168 }
169
170 /**
171 * Get an array of the expected proportion of sites in this category.
172 *
173 * @return an array of the proportion.
174 */
175 public double[] getCategoryProportions() {
176 throw new UnsupportedOperationException();
177 }
178
179 /**
180 *
181 */
182 private void calculateCategoryRates() {
183
184 categoryRates[relativeTo] = 1.0;
185 double total = 1.0;
186 int count = 0;
187 for (int i = 0; i < categoryRates.length; i++) {
188 if (i != relativeTo) {
189 categoryRates[i] = rateParameter.getParameterValue(count);
190 total = categoryRates[i] * categoryWeights[i];
191 count += 1;
192 }
193 }
194 total /= (double) siteCount;
195
196 // normalize so that total output rate is 1.0
197 for (int i = 0; i < categoryRates.length; i++) {
198 categoryRates[i] /= total;
199 }
200
201 ratesKnown = true;
202 }
203
204 /**
205 * Get the frequencyModel for this SiteModel.
206 *
207 * @return the frequencyModel.
208 */
209 public FrequencyModel getFrequencyModel() {
210 return substitutionModel.getFrequencyModel();
211 }
212
213 /**
214 * Get the substitutionModel for this SiteModel.
215 *
216 * @return the substitutionModel.
217 */
218 public SubstitutionModel getSubstitutionModel() {
219 return substitutionModel;
220 }
221
222 // *****************************************************************
223 // Interface ModelComponent
224 // *****************************************************************
225
226 protected void handleModelChangedEvent(Model model, Object object, int index) {
227 // Substitution model has changed so fire model changed event
228 listenerHelper.fireModelChanged(this, object, index);
229 }
230
231 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
232 if (variable == rateParameter) {
233 ratesKnown = false;
234 }
235 listenerHelper.fireModelChanged(this, variable, index);
236 }
237
238 protected void storeState() {
239 } // no additional state needs storing
240
241 protected void restoreState() {
242 ratesKnown = false;
243 }
244
245 protected void acceptState() {
246 } // no additional state needs accepting
247
248 /**
249 * the substitution model for these sites
250 */
251 private SubstitutionModel substitutionModel = null;
252
253 /**
254 * mutation rate parameter
255 */
256 private final Parameter muParameter;
257
258 /**
259 * rates parameter
260 */
261 private final Parameter rateParameter;
262
263 private boolean ratesKnown;
264
265 private final int categoryCount;
266
267 private final double[] categoryRates;
268
269
270 private final int[] categoryWeights;
271 private final int[] categories;
272 private final String states;
273 private final int siteCount;
274 private int relativeTo = 0;
275
276 }