/
GammaSiteRateModel.java
554 lines (459 loc) · 17.3 KB
/
GammaSiteRateModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* GammaSiteRateModelParser.java
*
* Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
*
* This file is part of BEAST.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership and licensing.
*
* BEAST is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* BEAST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BEAST; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package dr.evomodel.siteratemodel;
import dr.inference.model.*;
import dr.math.GeneralisedGaussLaguerreQuadrature;
import dr.math.distributions.GammaDistribution;
import dr.evomodel.substmodel.SubstitutionModel;
import dr.math.functionEval.GammaFunction;
import dr.util.Author;
import dr.util.Citable;
import dr.util.Citation;
import java.util.ArrayList;
import java.util.List;
/**
* GammaSiteModel - A SiteModel that has a gamma distributed rates across sites.
*
* @author Andrew Rambaut
* @version $Id: GammaSiteModel.java,v 1.31 2005/09/26 14:27:38 rambaut Exp $
*/
public class GammaSiteRateModel extends AbstractModel implements SiteRateModel, Citable {
public static final DiscretizationType DEFAULT_DISCRETIZATION = DiscretizationType.EQUAL;
public enum DiscretizationType {
EQUAL,
QUADRATURE
};
public GammaSiteRateModel(String name) {
this(name,
null,
1.0,
null,
0, DiscretizationType.EQUAL,
null);
}
public GammaSiteRateModel(String name, double alpha, int categoryCount) {
this(name,
null,
1.0,
new Parameter.Default(alpha),
categoryCount,
DEFAULT_DISCRETIZATION,
null);
}
public GammaSiteRateModel(String name, double alpha, int categoryCount, double pInvar) {
this(name,
null,
1.0,
new Parameter.Default(alpha),
categoryCount,
DEFAULT_DISCRETIZATION,
new Parameter.Default(pInvar));
}
public GammaSiteRateModel(String name, Parameter nu) {
this(name,
nu,
1.0,
null,
-1,
null,
null);
}
/**
* Constructor for gamma+invar distributed sites. Either shapeParameter or
* invarParameter (or both) can be null to turn off that feature.
*/
public GammaSiteRateModel(
String name,
Parameter nuParameter,
double muWeight,
Parameter shapeParameter, int gammaCategoryCount,
DiscretizationType discretizationType,
Parameter invarParameter) {
super(name);
this.nuParameter = nuParameter;
if (nuParameter != null) {
addVariable(nuParameter);
nuParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
}
this.muWeight = muWeight;
addStatistic(muStatistic);
this.shapeParameter = shapeParameter;
if (shapeParameter != null) {
this.categoryCount = gammaCategoryCount;
addVariable(shapeParameter);
// shapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 1E-3, 1));
// removing the bounds on the alpha parameter - to make the prior more explicit
shapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
} else {
this.categoryCount = 1;
}
this.invarParameter = invarParameter;
if (invarParameter != null) {
this.categoryCount += 1;
addVariable(invarParameter);
invarParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
}
this.discretizationType = discretizationType;
categoryRates = new double[this.categoryCount];
categoryProportions = new double[this.categoryCount];
ratesKnown = false;
}
/**
* set mu
*/
public void setMu(double mu) {
nuParameter.setParameterValue(0, mu / muWeight);
}
/**
* @return mu
*/
public final double getMu() {
return nuParameter.getParameterValue(0) * muWeight;
}
/**
* set alpha
*/
public void setAlpha(double alpha) {
shapeParameter.setParameterValue(0, alpha);
ratesKnown = false;
}
/**
* @return alpha
*/
public final double getAlpha() {
return shapeParameter.getParameterValue(0);
}
public void setRelativeRateParameter(Parameter nu) {
this.nuParameter = nu;
}
// *****************************************************************
// Interface SiteRateModel
// *****************************************************************
public int getCategoryCount() {
return categoryCount;
}
public double[] getCategoryRates() {
synchronized (this) {
if (!ratesKnown) {
calculateCategoryRates();
}
}
for (int i = (invarParameter != null ? 1 : 0); i < categoryRates.length; i++) {
// If a gamma rate is zero then the quantitization has failed numerically so return null.
// This allows the likelihood to return -Inf and reject this state.
if (categoryRates[i] == 0.0) {
return null;
}
}
return categoryRates;
}
public double[] getCategoryProportions() {
synchronized (this) {
if (!ratesKnown) {
calculateCategoryRates();
}
}
return categoryProportions;
}
public double getRateForCategory(int category) {
synchronized (this) {
if (!ratesKnown) {
calculateCategoryRates();
}
}
return categoryRates[category];
}
public double getProportionForCategory(int category) {
synchronized (this) {
if (!ratesKnown) {
calculateCategoryRates();
}
}
return categoryProportions[category];
}
/**
* discretization of gamma distribution with equal proportions in each
* category
*/
private void calculateCategoryRates() {
int offset = 0;
if (invarParameter != null) {
categoryRates[0] = 0.0;
categoryProportions[0] = invarParameter.getParameterValue(0);
offset = 1;
}
if (shapeParameter != null) {
double alpha = shapeParameter.getParameterValue(0);
final int gammaCatCount = categoryCount - offset;
if (discretizationType == DiscretizationType.QUADRATURE) {
setQuadratureRates(categoryRates, categoryProportions, alpha, gammaCatCount, offset);
} else {
setEqualRates(categoryRates, categoryProportions, alpha, gammaCatCount, offset);
}
} else if (offset > 0) {
// just the invariant rate and variant rate
categoryRates[offset] = 2.0;
categoryProportions[offset] = 1.0 - categoryProportions[0];
} else {
categoryRates[0] = 1.0;
categoryProportions[0] = 1.0;
}
if (nuParameter != null) {
double mu = getMu();
for (int i = 0; i < categoryCount; i++)
categoryRates[i] *= mu;
}
ratesKnown = true;
}
// *****************************************************************
// Interface ModelComponent
// *****************************************************************
protected void handleModelChangedEvent(Model model, Object object, int index) {
// Substitution model has changed so fire model changed event
listenerHelper.fireModelChanged(this, object, index);
}
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
if (variable == shapeParameter) {
ratesKnown = false;
} else if (variable == invarParameter) {
ratesKnown = false;
} else if (variable == nuParameter) {
ratesKnown = false; // MAS: I changed this because the rate parameter can affect the categories if the parameter is in siteModel and not clockModel
} else {
throw new RuntimeException("Unknown variable in GammaSiteRateModelParser.handleVariableChangedEvent");
}
listenerHelper.fireModelChanged(this, variable, index);
}
protected void storeState() {
} // no additional state needs storing
protected void restoreState() {
ratesKnown = false;
}
protected void acceptState() {
} // no additional state needs accepting
private Statistic muStatistic = new Statistic.Abstract() {
public String getStatisticName() {
return "mu";
}
public int getDimension() {
return 1;
}
public String getDimensionName(int dim) {
return getId();
}
public double getStatisticValue(int dim) {
return getMu();
}
};
/**
* mutation rate parameter
*/
private Parameter nuParameter;
private double muWeight;
/**
* shape parameter
*/
private Parameter shapeParameter;
/**
* invariant sites parameter
*/
private Parameter invarParameter;
private DiscretizationType discretizationType;
private boolean ratesKnown;
private int categoryCount;
private double[] categoryRates;
private double[] categoryProportions;
// This is here solely to allow the GammaSiteModelParser to pass on the substitution model to the
// HomogenousBranchSubstitutionModel so that the XML will be compatible with older BEAST versions. To be removed
// at some point.
public SubstitutionModel getSubstitutionModel() {
return substitutionModel;
}
public void setSubstitutionModel(SubstitutionModel substitutionModel) {
this.substitutionModel = substitutionModel;
}
@Override
public Citation.Category getCategory() {
return Citation.Category.SUBSTITUTION_MODELS;
}
@Override
public String getDescription() {
return "Discrete gamma-distributed rate heterogeneity model";
}
public List<Citation> getCitations() {
List<Citation> citations = new ArrayList<>();
if (shapeParameter != null) {
citations.add(CITATION_YANG94);
if (discretizationType == DiscretizationType.QUADRATURE) {
citations.add(CITATION_FELSENSTEIN01);
}
}
return citations;
}
public final static Citation CITATION_YANG94 = new Citation(
new Author[]{
new Author("Z", "Yang")
},
"Maximum likelihood phylogenetic estimation from DNA sequences with variable rates over sites: approximate methods",
1994,
"J. Mol. Evol.",
39,
306, 314,
Citation.Status.PUBLISHED
);
public final static Citation CITATION_FELSENSTEIN01 = new Citation(
new Author[]{
new Author("J", "Felsenstein")
},
"Taking Variation of Evolutionary Rates Between Sites into Account in Inferring Phylogenies",
2001,
"J. Mol. Evol.",
53,
447, 455,
Citation.Status.PUBLISHED
);
private SubstitutionModel substitutionModel;
private static GeneralisedGaussLaguerreQuadrature quadrature = null;
/**
* Set the rates and proportions using a Gauss-Laguerre Quadrature, as proposed by Felsenstein 2001, JME
*
* @param categoryRates
* @param categoryProportions
* @param alpha
* @param catCount
* @param offset
*/
public static void setQuadratureRates(double[] categoryRates, double[] categoryProportions, double alpha, int catCount, int offset) {
if (quadrature == null) {
quadrature = new GeneralisedGaussLaguerreQuadrature(catCount);
}
quadrature.setAlpha(alpha-1);
double[] abscissae = quadrature.getAbscissae();
double[] coefficients = quadrature.getCoefficients();
for (int i = 0; i < catCount; i++) {
categoryRates[i + offset] = abscissae[i] / alpha;
categoryProportions[i + offset] = coefficients[i]/GammaFunction.gamma(alpha);
}
}
/**
* set the rates as equally spaced quantiles represented by the mean as proposed by Yang 1994
* @param categoryRates
* @param categoryProportions
* @param alpha
* @param catCount
* @param offset
*/
public static void setEqualRates(double[] categoryRates, double[] categoryProportions, double alpha, int catCount, int offset) {
for (int i = 0; i < catCount; i++) {
categoryRates[i + offset] = GammaDistribution.quantile((2.0 * i + 1.0) / (2.0 * catCount), alpha, 1.0 / alpha);
categoryProportions[i + offset] = 1.0;
}
normalize(categoryRates, categoryProportions);
}
/**
* Gives the category rates a mean of 1.0 and the proportions sum to 1.0
* @param categoryRates
* @param categoryProportions
*/
public static void normalize(double[] categoryRates, double[] categoryProportions) {
double mean = 0.0;
double sum = 0.0;
for (int i = 0; i < categoryRates.length; i++) {
mean += categoryRates[i];
sum += categoryProportions[i];
}
mean /= categoryRates.length;
for(int i = 0; i < categoryRates.length; i++) {
categoryRates[i] /= mean;
categoryProportions[i] /= sum;
}
}
public static void main(String[] argv) {
final int catCount = 6;
double[] categoryRates = new double[catCount];
double[] categoryProportions = new double[catCount];
setEqualRates(categoryRates, categoryProportions, 1.0, catCount, 0);
System.out.println();
System.out.println("Equal, alpha = 1.0");
System.out.println("cat\trate\tproportion");
for (int i = 0; i < catCount; i++) {
System.out.println(i + "\t"+ categoryRates[i] +"\t" + categoryProportions[i]);
}
setQuadratureRates(categoryRates, categoryProportions, 1.0, catCount, 0);
System.out.println();
System.out.println("Quadrature, alpha = 1.0");
System.out.println("cat\trate\tproportion");
for (int i = 0; i < catCount; i++) {
System.out.println(i + "\t"+ categoryRates[i] +"\t" + categoryProportions[i]);
}
// Table 3 from Felsenstein 2001, JME
// Rates and probabilities chosen by the quadrature method for six rates and coefficient of
// variation of rates among sites 1 (a 4 1)
// Rate Probability
// 0.264 0.278
// 0.898 0.494
// 1.938 0.203
// 3.459 0.025
// 5.617 0.00076
// 8.823 0.000003
// Output
// Quadrature, alpha = 1.0
// cat rate proportion
// 0 0.26383406085556455 0.27765014202987454
// 1 0.8981499048217043 0.49391058305035496
// 2 1.938320760238456 0.20300429674372977
// 3 3.459408283352361 0.02466882036918974
// 4 5.617305214541558 7.6304276746353E-4
// 5 8.822981776190357 3.1150393875275343E-6
setEqualRates(categoryRates, categoryProportions, 0.1, catCount, 0);
System.out.println();
System.out.println("Equal, alpha = 0.1");
System.out.println("cat\trate\tproportion");
for (int i = 0; i < catCount; i++) {
System.out.println(i + "\t"+ categoryRates[i] +"\t" + categoryProportions[i]);
}
setQuadratureRates(categoryRates, categoryProportions, 0.1, catCount, 0);
System.out.println();
System.out.println("Quadrature, alpha = 0.1");
System.out.println("cat\trate\tproportion");
for (int i = 0; i < catCount; i++) {
System.out.println(i + "\t"+ categoryRates[i] +"\t" + categoryProportions[i]);
}
setEqualRates(categoryRates, categoryProportions, 10.0, catCount, 0);
System.out.println();
System.out.println("Equal, alpha = 10.0");
System.out.println("cat\trate\tproportion");
for (int i = 0; i < catCount; i++) {
System.out.println(i + "\t"+ categoryRates[i] +"\t" + categoryProportions[i]);
}
setQuadratureRates(categoryRates, categoryProportions, 10.0, catCount, 0);
System.out.println();
System.out.println("Quadrature, alpha = 10.0");
System.out.println("cat\trate\tproportion");
for (int i = 0; i < catCount; i++) {
System.out.println(i + "\t"+ categoryRates[i] +"\t" + categoryProportions[i]);
}
}
}