Debian Codegraph @

src/dr/evomodel/substmodel/nucleotide/GTR.java

raw symbols SCIP
1 /*
2 * GTR.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.nucleotide;
27
28 import dr.evomodel.substmodel.BaseSubstitutionModel;
29 import dr.evomodel.substmodel.FrequencyModel;
30 import dr.evolution.datatype.Nucleotides;
31 import dr.inference.model.Parameter;
32 import dr.inference.model.Variable;
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 * General Time Reversible model of nucleotide evolution
42 * This is really just a place-holder because all the implementation
43 * already exists in NucleotideModel and GeneralModel, its base classes.
44 *
45 * @author Andrew Rambaut
46 * @author Alexei Drummond
47 * @version $Id: GTR.java,v 1.19 2005/05/24 20:25:58 rambaut Exp $
48 */
49 public class GTR extends BaseSubstitutionModel implements Citable {
50
51 public static final String A_TO_C = "AC";
52 public static final String A_TO_G = "AG";
53 public static final String A_TO_T = "AT";
54 public static final String C_TO_G = "CG";
55 public static final String C_TO_T = "CT";
56 public static final String G_TO_T = "GT";
57
58 private Parameter ratesParameter = null;
59
60 private Variable<Double> rateACVariable = null;
61 private Variable<Double> rateAGVariable = null;
62 private Variable<Double> rateATVariable = null;
63 private Variable<Double> rateCGVariable = null;
64 private Variable<Double> rateCTVariable = null;
65 private Variable<Double> rateGTVariable = null;
66
67 /**
68 * @param rates vector of all 6 rates
69 * @param freqModel frequencies
70 */
71 public GTR(
72 Parameter rates,
73 FrequencyModel freqModel) {
74 super("GTR", Nucleotides.INSTANCE, freqModel);
75 this.ratesParameter = rates;
76 this.ratesParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, rates.getDimension()));
77 addVariable(rates);
78 }
79
80 /**
81 * @param rateACVariable rate of A<->C substitutions
82 * @param rateAGVariable rate of A<->G substitutions
83 * @param rateATVariable rate of A<->T substitutions
84 * @param rateCGVariable rate of C<->G substitutions
85 * @param rateCTVariable rate of C<->T substitutions
86 * @param rateGTVariable rate of G<->T substitutions
87 * @param freqModel frequencies
88 */
89 public GTR(
90 Variable<Double> rateACVariable,
91 Variable<Double> rateAGVariable,
92 Variable<Double> rateATVariable,
93 Variable<Double> rateCGVariable,
94 Variable<Double> rateCTVariable,
95 Variable<Double> rateGTVariable,
96 FrequencyModel freqModel) {
97
98 super("GTR", Nucleotides.INSTANCE, freqModel);
99
100 if (rateACVariable != null) {
101 addVariable(rateACVariable);
102 rateACVariable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
103 this.rateACVariable = rateACVariable;
104 }
105
106 if (rateAGVariable != null) {
107 addVariable(rateAGVariable);
108 rateAGVariable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
109 this.rateAGVariable = rateAGVariable;
110 }
111
112 if (rateATVariable != null) {
113 addVariable(rateATVariable);
114 rateATVariable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
115 this.rateATVariable = rateATVariable;
116 }
117
118 if (rateCGVariable != null) {
119 addVariable(rateCGVariable);
120 rateCGVariable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
121 this.rateCGVariable = rateCGVariable;
122 }
123
124 if (rateCTVariable != null) {
125 addVariable(rateCTVariable);
126 rateCTVariable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
127 this.rateCTVariable = rateCTVariable;
128 }
129
130 if (rateGTVariable != null) {
131 addVariable(rateGTVariable);
132 rateGTVariable.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
133 this.rateGTVariable = rateGTVariable;
134 }
135
136 }
137
138 public void setAbsoluteRates(double[] rates, int relativeTo) {
139 for (int i = 0; i < relativeRates.length; i++) {
140 relativeRates[i] = rates[i] / rates[relativeTo];
141 }
142 updateMatrix = true;
143 fireModelChanged();
144 }
145
146 public void setRelativeRates(double[] rates) {
147 System.arraycopy(rates, 0, relativeRates, 0, relativeRates.length);
148 updateMatrix = true;
149 fireModelChanged();
150 }
151
152 protected void frequenciesChanged() {
153 // nothing to do...
154 }
155
156 protected void ratesChanged() {
157 // nothing to do...
158 }
159
160 protected void setupRelativeRates(double[] rates) {
161 if (ratesParameter == null) {
162 if (rateACVariable != null) {
163 rates[0] = rateACVariable.getValue(0);
164 }
165 if (rateAGVariable != null) {
166 rates[1] = rateAGVariable.getValue(0);
167 }
168 if (rateATVariable != null) {
169 rates[2] = rateATVariable.getValue(0);
170 }
171 if (rateCGVariable != null) {
172 rates[3] = rateCGVariable.getValue(0);
173 }
174 if (rateCTVariable != null) {
175 rates[4] = rateCTVariable.getValue(0);
176 }
177 if (rateGTVariable != null) {
178 rates[5] = rateGTVariable.getValue(0);
179 }
180 } else {
181 System.arraycopy(this.ratesParameter.getParameterValues(), 0, rates, 0, rates.length);
182 }
183 }
184
185 // **************************************************************
186 // XHTMLable IMPLEMENTATION
187 // **************************************************************
188
189 public String toXHTML() {
190 StringBuffer buffer = new StringBuffer();
191
192 buffer.append("<em>GTR Model</em> Instantaneous Rate Matrix = <table><tr><td></td><td>A</td><td>C</td><td>G</td><td>T</td></tr>");
193 buffer.append("<tr><td>A</td><td></td><td>");
194 buffer.append(relativeRates[0]);
195 buffer.append("</td><td>");
196 buffer.append(relativeRates[1]);
197 buffer.append("</td><td>");
198 buffer.append(relativeRates[2]);
199 buffer.append("</td></tr>");
200
201 buffer.append("<tr><td>C</td><td></td><td></td><td>");
202 buffer.append(relativeRates[3]);
203 buffer.append("</td><td>");
204 buffer.append(relativeRates[4]);
205 buffer.append("</td></tr>");
206
207 buffer.append("<tr><td>G</td><td></td><td></td><td></td><td>");
208 buffer.append(relativeRates[5]);
209 buffer.append("</td></tr>");
210
211 buffer.append("<tr><td>G</td><td></td><td></td><td></td><td></td></tr></table>");
212
213 return buffer.toString();
214 }
215
216 @Override
217 public Citation.Category getCategory() {
218 return Citation.Category.SUBSTITUTION_MODELS;
219 }
220
221 @Override
222 public String getDescription() {
223 return "GTR nucleotide substitution model";
224 }
225
226 @Override
227 public List<Citation> getCitations() {
228 return Collections.singletonList(CITATION);
229 }
230
231 public static Citation CITATION = new Citation(
232 new Author[]{
233 new Author("S", "Tavaré")
234 },
235 "Some probabilistic and statistical problems in the analysis of DNA sequences",
236 1985,
237 "In: Miura R. M., editor. Lectures on Mathematics in the Life Sciences",
238 17, 57, 86
239 );
240
241 }