Debian Codegraph @

src/dr/evomodel/substmodel/codon/MG94CodonModel.java

raw symbols SCIP
1 /*
2 * MG94CodonModel.java
3 *
4 * Copyright (c) 2002-2016 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.substmodel.codon;
27
28 import dr.evomodel.substmodel.DefaultEigenSystem;
29 import dr.evomodel.substmodel.EigenSystem;
30 import dr.evomodel.substmodel.FrequencyModel;
31 import dr.evolution.datatype.Codons;
32 import dr.inference.model.Parameter;
33 import dr.util.Author;
34 import dr.util.Citable;
35 import dr.util.Citation;
36
37 import java.util.Collections;
38 import java.util.List;
39
40 /**
41 * Muse-Gaut model of codon evolution
42 *
43 * @author Marc A. Suchard
44 * @author Guy Baele
45 * @author Philippe lemey
46 */
47 public class MG94CodonModel extends AbstractCodonModel implements Citable {
48
49 protected Parameter alphaParameter;
50 protected Parameter betaParameter;
51
52 protected final int numSynTransitions;
53 protected final int numNonsynTransitions;
54
55 public MG94CodonModel(Codons codonDataType, Parameter alphaParameter, Parameter betaParameter,
56 FrequencyModel freqModel) {
57 this(codonDataType, alphaParameter, betaParameter, freqModel,
58 new DefaultEigenSystem(codonDataType.getStateCount()));
59 }
60
61 public MG94CodonModel(Codons codonDataType,
62 Parameter alphaParameter,
63 Parameter betaParameter,
64 FrequencyModel freqModel, EigenSystem eigenSystem) {
65 super("MG94", codonDataType, freqModel, eigenSystem);
66
67 this.alphaParameter = alphaParameter;
68 addVariable(alphaParameter);
69 alphaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
70 alphaParameter.getDimension()));
71
72 this.betaParameter = betaParameter;
73 addVariable(betaParameter);
74 betaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
75 betaParameter.getDimension()));
76
77 numSynTransitions = getNumSynTransitions();
78 numNonsynTransitions = getNumNonsynTransitions();
79 }
80
81 private int countRates(int i, int j) {
82 int count = 0;
83
84 for (byte rate : rateMap) {
85 if (rate == i || rate == j) {
86 count++;
87 }
88 }
89 return count;
90 }
91
92 protected int getNumSynTransitions() {
93 return 2 * countRates(1, 2);
94 }
95
96 protected int getNumNonsynTransitions() {
97 return 2 * countRates(3, 4);
98 }
99
100 protected double getNormalizationValue(double[][] matrix, double[] pi) {
101 double norm = 1.0;
102 if (doNormalization) {
103 double ratio =
104 getAlpha() + getBeta();
105 //(numSynTransitions * getAlpha() + numNonsynTransitions * getBeta()) / (numSynTransitions + numNonsynTransitions);
106 norm = super.getNormalizationValue(matrix, pi) / ratio;
107 }
108 return norm;
109 }
110
111 public double getAlpha() {
112 return alphaParameter.getParameterValue(0);
113 }
114
115 public double getBeta() {
116 return betaParameter.getParameterValue(0);
117 }
118
119 protected void setupRelativeRates(double[] rates) {
120
121 double alpha = getAlpha() / numSynTransitions;
122 double beta = getBeta() / numNonsynTransitions;
123 for (int i = 0; i < rateCount; i++) {
124 switch (rateMap[i]) {
125 case 0:
126 rates[i] = 0.0;
127 break; // codon changes in more than one codon position
128 case 1:
129 rates[i] = alpha;
130 break; // synonymous transition
131 case 2:
132 rates[i] = alpha;
133 break; // synonymous transversion
134 case 3:
135 rates[i] = beta;
136 break; // non-synonymous transition
137 case 4:
138 rates[i] = beta;
139 break; // non-synonymous transversion
140 }
141 }
142 }
143
144 public void setNormalization(boolean normalize) {
145 this.doNormalization = normalize;
146 }
147
148 private boolean doNormalization = true;
149
150 @Override
151 public Citation.Category getCategory() {
152 return Citation.Category.SUBSTITUTION_MODELS;
153 }
154
155 @Override
156 public String getDescription() {
157 return "Muse-Gaut codon substitution model";
158 }
159
160 @Override
161 public List<Citation> getCitations() {
162 return Collections.singletonList(CITATION);
163 }
164
165 public static Citation CITATION = new Citation(
166 new Author[]{
167 new Author("SV", "Muse"),
168 new Author("BS", "Gaut")
169 },
170 "A likelihood approach for comparing synonymous and nonsynonymous nucleotide substitution rates, with application to the chloroplast genome",
171 1994,
172 "Mol Biol Evol",
173 11, 715, 724
174 );
175 }