Debian Codegraph @

src/dr/evomodel/siteratemodel/GammaSiteRateModel.java

raw symbols SCIP
1 /*
2 * GammaSiteRateModel.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.evomodel.siteratemodel;
27
28 import dr.inference.model.*;
29 import dr.math.distributions.GammaDistribution;
30 import dr.evomodel.substmodel.SubstitutionModel;
31 import dr.util.Author;
32 import dr.util.Citable;
33 import dr.util.Citation;
34
35 import java.util.Collections;
36 import java.util.List;
37
38 /**
39 * GammaSiteModel - A SiteModel that has a gamma distributed rates across sites.
40 *
41 * @author Andrew Rambaut
42 * @version $Id: GammaSiteModel.java,v 1.31 2005/09/26 14:27:38 rambaut Exp $
43 */
44
45 public class GammaSiteRateModel extends AbstractModel implements SiteRateModel, Citable {
46
47 public GammaSiteRateModel(String name) {
48 this( name,
49 null,
50 1.0,
51 null,
52 0,
53 null);
54 }
55
56 public GammaSiteRateModel(String name, double alpha, int categoryCount) {
57 this( name,
58 null,
59 1.0,
60 new Parameter.Default(alpha),
61 categoryCount,
62 null);
63 }
64
65 public GammaSiteRateModel(String name, double pInvar) {
66 this( name,
67 null,
68 1.0,
69 null,
70 0,
71 new Parameter.Default(pInvar));
72 }
73
74 public GammaSiteRateModel(String name, double alpha, int categoryCount, double pInvar) {
75 this( name,
76 null,
77 1.0,
78 new Parameter.Default(alpha),
79 categoryCount,
80 new Parameter.Default(pInvar));
81 }
82
83 public GammaSiteRateModel(
84 String name,
85 Parameter nuParameter,
86 Parameter shapeParameter, int gammaCategoryCount,
87 Parameter invarParameter) {
88 this(name, nuParameter, 1.0, shapeParameter, gammaCategoryCount, invarParameter);
89 }
90
91 /**
92 * Constructor for gamma+invar distributed sites. Either shapeParameter or
93 * invarParameter (or both) can be null to turn off that feature.
94 */
95 public GammaSiteRateModel(
96 String name,
97 Parameter nuParameter,
98 double muWeight,
99 Parameter shapeParameter, int gammaCategoryCount,
100 Parameter invarParameter) {
101
102 super(name);
103
104 this.nuParameter = nuParameter;
105 if (nuParameter != null) {
106 addVariable(nuParameter);
107 nuParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
108 }
109 this.muWeight = muWeight;
110
111 addStatistic(muStatistic);
112
113 this.shapeParameter = shapeParameter;
114 if (shapeParameter != null) {
115 this.categoryCount = gammaCategoryCount;
116
117 addVariable(shapeParameter);
118 // shapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 1E-3, 1));
119 // removing the bounds on the alpha parameter - to make the prior more explicit
120 shapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
121 } else {
122 this.categoryCount = 1;
123 }
124
125 this.invarParameter = invarParameter;
126 if (invarParameter != null) {
127 this.categoryCount += 1;
128
129 addVariable(invarParameter);
130 invarParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
131 }
132
133 categoryRates = new double[this.categoryCount];
134 categoryProportions = new double[this.categoryCount];
135
136 ratesKnown = false;
137 }
138
139 /**
140 * set mu
141 */
142 public void setMu(double mu) {
143 nuParameter.setParameterValue(0, mu / muWeight);
144 }
145
146 /**
147 * @return mu
148 */
149 public final double getMu() {
150 return nuParameter.getParameterValue(0) * muWeight;
151 }
152
153 /**
154 * set alpha
155 */
156 public void setAlpha(double alpha) {
157 shapeParameter.setParameterValue(0, alpha);
158 ratesKnown = false;
159 }
160
161 /**
162 * @return alpha
163 */
164 public final double getAlpha() {
165 return shapeParameter.getParameterValue(0);
166 }
167
168
169 public Parameter getAlphaParameter() {
170 return shapeParameter;
171 }
172
173 public Parameter getPInvParameter() {
174 return invarParameter;
175 }
176
177 public Parameter setRelativeRateParameter() {
178 return nuParameter;
179 }
180
181 public void setAlphaParameter(Parameter parameter) {
182 if (shapeParameter != null) removeVariable(shapeParameter);
183 shapeParameter = parameter;
184 if (shapeParameter != null) addVariable(shapeParameter);
185 }
186
187 public void setPInvParameter(Parameter parameter) {
188 if (invarParameter != null) removeVariable(invarParameter);
189 invarParameter = parameter;
190 if (invarParameter != null) addVariable(invarParameter);
191 }
192
193 public void setRelativeRateParameter(Parameter parameter) {
194 if (nuParameter != null) removeVariable(nuParameter);
195 nuParameter = parameter;
196 if (nuParameter != null) addVariable(nuParameter);
197 }
198
199 // *****************************************************************
200 // Interface SiteRateModel
201 // *****************************************************************
202
203 public int getCategoryCount() {
204 return categoryCount;
205 }
206
207 public double[] getCategoryRates() {
208 synchronized (this) {
209 if (!ratesKnown) {
210 calculateCategoryRates();
211 }
212 }
213
214 for (int i = (invarParameter != null ? 1 : 0); i < categoryRates.length; i++) {
215 // If a gamma rate is zero then the quantitization has failed numerically so return null.
216 // This allows the likelihood to return -Inf and reject this state.
217 if (categoryRates[i] == 0.0) {
218 return null;
219 }
220 }
221
222 return categoryRates;
223 }
224
225 public double[] getCategoryProportions() {
226 synchronized (this) {
227 if (!ratesKnown) {
228 calculateCategoryRates();
229 }
230 }
231
232 return categoryProportions;
233 }
234
235 public double getRateForCategory(int category) {
236 synchronized (this) {
237 if (!ratesKnown) {
238 calculateCategoryRates();
239 }
240 }
241
242 return categoryRates[category];
243 }
244
245 public double getProportionForCategory(int category) {
246 synchronized (this) {
247 if (!ratesKnown) {
248 calculateCategoryRates();
249 }
250 }
251
252 return categoryProportions[category];
253 }
254
255 /**
256 * discretization of gamma distribution with equal proportions in each
257 * category
258 */
259 private void calculateCategoryRates() {
260
261 double propVariable = 1.0;
262 int cat = 0;
263
264 if (invarParameter != null) {
265 categoryRates[0] = 0.0;
266 categoryProportions[0] = invarParameter.getParameterValue(0);
267
268 propVariable = 1.0 - categoryProportions[0];
269 cat = 1;
270 }
271
272 if (shapeParameter != null) {
273
274 final double a = shapeParameter.getParameterValue(0);
275 double mean = 0.0;
276 final int gammaCatCount = categoryCount - cat;
277
278 for (int i = 0; i < gammaCatCount; i++) {
279
280 categoryRates[i + cat] = GammaDistribution.quantile((2.0 * i + 1.0) / (2.0 * gammaCatCount), a, 1.0 / a);
281
282 // if (categoryRates[i + cat] == 0.0) {
283 // throw new RuntimeException("Alpha parameter for discrete gamma distribution is too small and causing numerical errors.");
284 // }
285
286 mean += categoryRates[i + cat];
287
288 categoryProportions[i + cat] = propVariable / gammaCatCount;
289 }
290
291 mean = (propVariable * mean) / gammaCatCount;
292
293 for (int i = 0; i < gammaCatCount; i++) {
294
295 categoryRates[i + cat] /= mean;
296 }
297 } else {
298 categoryRates[cat] = 1.0 / propVariable;
299 categoryProportions[cat] = propVariable;
300 }
301
302 if (nuParameter != null) { // Moved multiplication by mu to here; it also
303 // needed by double[] getCategoryRates() -- previously ignored
304 double mu = getMu();
305 for (int i=0; i < categoryCount; i++)
306 categoryRates[i] *= mu;
307 }
308
309 ratesKnown = true;
310 }
311
312 public boolean hasInvariantSites() {
313 return invarParameter != null;
314 }
315
316 // *****************************************************************
317 // Interface ModelComponent
318 // *****************************************************************
319
320 protected void handleModelChangedEvent(Model model, Object object, int index) {
321 // Substitution model has changed so fire model changed event
322 listenerHelper.fireModelChanged(this, object, index);
323 }
324
325 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
326 if (variable == shapeParameter) {
327 ratesKnown = false;
328 } else if (variable == invarParameter) {
329 ratesKnown = false;
330 } else if (variable == nuParameter) {
331 ratesKnown = false; // MAS: I changed this because the rate parameter can affect the categories if the parameter is in siteModel and not clockModel
332 } else {
333 throw new RuntimeException("Unknown variable in GammaSiteRateModel.handleVariableChangedEvent");
334 }
335 listenerHelper.fireModelChanged(this, variable, index);
336 }
337
338 protected void storeState() {
339 } // no additional state needs storing
340
341 protected void restoreState() {
342 ratesKnown = false;
343 }
344
345 protected void acceptState() {
346 } // no additional state needs accepting
347
348
349 private Statistic muStatistic = new Statistic.Abstract() {
350
351 public String getStatisticName() {
352 return "mu";
353 }
354
355 public int getDimension() {
356 return 1;
357 }
358
359 public String getDimensionName(int dim) {
360 return getId();
361 }
362
363 public double getStatisticValue(int dim) {
364 return getMu();
365 }
366
367 };
368
369
370 /**
371 * mutation rate parameter
372 */
373 private Parameter nuParameter;
374
375 private double muWeight;
376
377 /**
378 * shape parameter
379 */
380 private Parameter shapeParameter;
381
382 /**
383 * invariant sites parameter
384 */
385 private Parameter invarParameter;
386
387 private boolean ratesKnown;
388
389 private int categoryCount;
390
391 private double[] categoryRates;
392
393 private double[] categoryProportions;
394
395
396
397 // This is here solely to allow the GammaSiteModelParser to pass on the substitution model to the
398 // HomogenousBranchSubstitutionModel so that the XML will be compatible with older BEAST versions. To be removed
399 // at some point.
400 public SubstitutionModel getSubstitutionModel() {
401 return substitutionModel;
402 }
403
404 public void setSubstitutionModel(SubstitutionModel substitutionModel) {
405 this.substitutionModel = substitutionModel;
406 }
407
408
409 @Override
410 public Citation.Category getCategory() {
411 return Citation.Category.SUBSTITUTION_MODELS;
412 }
413
414 @Override
415 public String getDescription() {
416 return "Discrete gamma-distributed rate heterogeneity model";
417 }
418
419 public List<Citation> getCitations() {
420 if (getAlphaParameter() != null) {
421 return Collections.singletonList(CITATION);
422 } else {
423 return Collections.emptyList();
424 }
425 }
426
427 public final static Citation CITATION = new Citation(
428 new Author[]{
429 new Author("Z", "Yang")
430 },
431 "Maximum likelihood phylogenetic estimation from DNA sequences with variable rates over sites: approximate methods",
432 1994,
433 "J. Mol. Evol.",
434 39,
435 306, 314,
436 Citation.Status.PUBLISHED
437 );
438
439 private SubstitutionModel substitutionModel;
440 }