Debian Codegraph @

src/dr/oldevomodel/sitemodel/DirichletSiteModel.java

raw symbols SCIP
1 /*
2 * DirichletSiteModel.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 import dr.xml.*;
35
36 import java.util.logging.Logger;
37
38 /**
39 * DirichletSiteModel - A SiteModel that has a free rate parameter for each category.
40 * These rate parameters are constrained to sum to 1.
41 *
42 * @author Benjamin Redelings
43 */
44
45 public class DirichletSiteModel extends AbstractModel implements SiteModel {
46
47 public static final String SUBSTITUTION_MODEL = "substitutionModel";
48 public static final String DIRICHLET_SITE_MODEL = "DirichletSiteModel";
49 public static final String CATEGORIES = "categories";
50 public static final String RATES = "rates";
51
52 /**
53 * Constructor for gamma+invar distributed sites. Either shapeParameter or
54 * invarParameter (or both) can be null to turn off that feature.
55 */
56 public DirichletSiteModel(SubstitutionModel substitutionModel, int n_categories) {
57 this(substitutionModel, new Parameter.Default(new double[n_categories]));
58 }
59
60 public DirichletSiteModel(SubstitutionModel substitutionModel, Parameter ratesParameter) {
61 super(DIRICHLET_SITE_MODEL);
62
63 // Set substitution model
64 this.substitutionModel = substitutionModel;
65 addModel(substitutionModel);
66
67 // Allocate array for categories and proportions
68 int n_categories = ratesParameter.getDimension();
69 categoryRates = new double[n_categories];
70 categoryProportions = new double[n_categories];
71
72 // We haven't calculated any rates yet.
73 ratesKnown = false;
74
75 // Initialize the rate parameter value
76 for(int i=0;i<n_categories;i++) {
77 ratesParameter.setParameterValue(i, 1.0/n_categories);
78 categoryProportions[i] = 1.0/n_categories;
79 }
80
81 // Add the rate parameters to the model
82 this.ratesParameter = ratesParameter;
83 addVariable(this.ratesParameter);
84 this.ratesParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, ratesParameter.getDimension()));
85 }
86
87 public Parameter getRatesParameter() {
88 return ratesParameter;
89 }
90
91 // public void setRatesParameter(Parameter parameter) {
92 // removeVariable(ratesParameter);
93 // ratesParameter = parameter;
94 // addVariable(ratesParameter);
95 // }
96
97 // *****************************************************************
98 // Interface SiteModel
99 // *****************************************************************
100
101 public boolean integrateAcrossCategories() {
102 return true;
103 }
104
105 public int getCategoryCount() {
106 return categoryRates.length;
107 }
108
109 public int getCategoryOfSite(int site) {
110 throw new IllegalArgumentException("Integrating across categories");
111 }
112
113 public double getRateForCategory(int category) {
114 calculateCategoryRates();
115
116 return categoryRates[category];
117 }
118
119 public double[] getCategoryRates() {
120 calculateCategoryRates();
121
122 double[] rates = new double[categoryRates.length];
123
124 for (int i = 0; i < rates.length; i++) {
125 rates[i] = categoryRates[i];
126 }
127
128 return rates;
129 }
130
131 public void getTransitionProbabilities(double substitutions, double[] matrix) {
132 substitutionModel.getTransitionProbabilities(substitutions, matrix);
133 }
134
135 /**
136 * Get the expected proportion of sites in this category.
137 *
138 * @param category the category number
139 * @return the proportion.
140 */
141 public double getProportionForCategory(int category) {
142 calculateCategoryRates();
143
144 return categoryProportions[category];
145 }
146
147 /**
148 * Get an array of the expected proportion of sites in this category.
149 *
150 * @return an array of the proportion.
151 */
152 public double[] getCategoryProportions() {
153 calculateCategoryRates();
154
155 return categoryProportions;
156 }
157
158 private double get_substitution_scale()
159 {
160 double scale=0;
161 for(int i=0;i<categoryRates.length;i++) {
162 // System.out.println(" get_substitution_scale: i = " + i);
163 scale += categoryProportions[i]*ratesParameter.getParameterValue(i);
164 }
165 return scale;
166 }
167
168 /**
169 * Calculate the category rates from the unscaled rate variables.
170 */
171 private void calculateCategoryRates()
172 {
173 synchronized (this) {
174 if (ratesKnown)
175 return;
176 }
177
178 double scale = get_substitution_scale();
179
180 double temp = 1.0/scale;
181
182 for(int i=0;i<categoryRates.length;i++)
183 categoryRates[i] = ratesParameter.getParameterValue(i)*temp;
184
185 ratesKnown = true;
186 }
187
188 /**
189 * Get the frequencyModel for this SiteModel.
190 *
191 * @return the frequencyModel.
192 */
193 public FrequencyModel getFrequencyModel() {
194 return substitutionModel.getFrequencyModel();
195 }
196
197 /**
198 * Get the substitutionModel for this SiteModel.
199 *
200 * @return the substitutionModel.
201 */
202 public SubstitutionModel getSubstitutionModel() {
203 return substitutionModel;
204 }
205
206 // *****************************************************************
207 // Interface ModelComponent
208 // *****************************************************************
209
210 protected void handleModelChangedEvent(Model model, Object object, int index) {
211 // Substitution model has changed so fire model changed event
212 listenerHelper.fireModelChanged(this, object, index);
213 }
214
215 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
216 if (variable == ratesParameter) {
217 ratesKnown = false;
218 } else {
219 // This should not happen
220 }
221 listenerHelper.fireModelChanged(this, variable, index);
222 }
223
224 protected void storeState() {
225 } // no additional state needs storing
226
227 protected void restoreState() {
228 ratesKnown = false;
229 }
230
231 protected void acceptState() {
232 } // no additional state needs accepting
233
234 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
235
236 public String[] getParserNames() {
237 return new String[] {
238 getParserName(), "beast_"+getParserName()
239 };
240 }
241
242 public String getParserName() {
243 return DIRICHLET_SITE_MODEL;
244 }
245
246 public Object parseXMLObject(XMLObject xo) throws XMLParseException
247 {
248 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getElementFirstChild(SUBSTITUTION_MODEL);
249
250 String msg = "";
251
252 Parameter ratesParameter = null;
253 if (xo.hasChildNamed("rates")) {
254 XMLObject cxo = (XMLObject) xo.getChild("rates");
255 ratesParameter = (Parameter) cxo.getChild(Parameter.class);
256 msg += "\n with " + ratesParameter.getDimension() + " categories.";
257 }
258
259
260 if (msg.length() > 0) {
261 Logger.getLogger("dr.evomodel").info("Creating site model: " + msg);
262 } else {
263 Logger.getLogger("dr.evomodel").info("Creating site model.");
264 }
265
266 return new DirichletSiteModel(substitutionModel, ratesParameter);
267 }
268
269 //************************************************************************
270 // AbstractXMLObjectParser implementation
271 //************************************************************************
272
273 public String getParserDescription() {
274 return "A SiteModel that has Dirichlet distributed rates across sites";
275 }
276
277 public Class getReturnType() {
278 return DirichletSiteModel.class;
279 }
280
281 public XMLSyntaxRule[] getSyntaxRules() {
282 return rules;
283 }
284
285 private final XMLSyntaxRule[] rules = {
286 new ElementRule(SUBSTITUTION_MODEL, new XMLSyntaxRule[]{
287 new ElementRule(SubstitutionModel.class)
288 }),
289 new ElementRule(RATES, new XMLSyntaxRule[]{
290 new ElementRule(Parameter.class)
291 })
292 };
293 };
294
295 /**
296 * the substitution model for these sites
297 */
298 private SubstitutionModel substitutionModel = null;
299
300 /**
301 * the substitution rates: these sum to 1
302 */
303 private Parameter ratesParameter;
304
305 private boolean ratesKnown;
306
307 private double[] categoryRates;
308
309 private double[] categoryProportions;
310 }