Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * AbstractCodonModel.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.BaseSubstitutionModel;
29 import dr.evomodel.substmodel.EigenSystem;
30 import dr.evomodel.substmodel.FrequencyModel;
31 import dr.evolution.datatype.AminoAcids;
32 import dr.evolution.datatype.Codons;
33 import dr.evolution.datatype.GeneticCode;
34 import dr.evolution.datatype.Nucleotides;
35
36 /**
37 * @author Marc A. Suchard
38 */
39 public abstract class AbstractCodonModel extends BaseSubstitutionModel {
40 /** kappa */
41 // protected Parameter kappaParameter;
42
43 /**
44 * omega
45 */
46 // protected Parameter omegaParameter;
47
48 protected byte[] rateMap;
49
50 protected Codons codonDataType;
51 protected GeneticCode geneticCode;
52
53
54 // public AbstractCodonModel(Codons codonDataType, Parameter omegaParameter, Parameter kappaParameter,
55 // FrequencyModel freqModel) {
56 // this(codonDataType, omegaParameter, kappaParameter, freqModel,
57 // new DefaultEigenSystem(codonDataType.getStateCount()));
58 // }
59
60 public AbstractCodonModel(String name, Codons codonDataType, FrequencyModel freqModel, EigenSystem eigenSystem) {
61 super(name, codonDataType, freqModel, eigenSystem);
62
63 this.codonDataType = codonDataType;
64 this.geneticCode = codonDataType.getGeneticCode();
65
66 constructRateMap();
67
68 updateMatrix = true;
69 }
70
71 /*public AbstractCodonModel(Codons codonDataType,
72 FrequencyModel freqModel, EigenSystem eigenSystem) {
73 super("GY94", codonDataType, freqModel, eigenSystem);
74
75 this.codonDataType = codonDataType;
76 this.geneticCode = codonDataType.getGeneticCode();
77
78 constructRateMap();
79
80 updateMatrix = true;
81 }*/
82
83 protected void frequenciesChanged() {
84 }
85
86 protected void ratesChanged() {
87 }
88
89 abstract protected void setupRelativeRates(double[] rates);
90
91 /**
92 * Construct a map of the rate classes in the rate matrix using the current
93 * genetic code. Classes:
94 * 0: codon changes in more than one codon position (or stop codons)
95 * 1: synonymous transition
96 * 2: synonymous transversion
97 * 3: non-synonymous transition
98 * 4: non-synonymous transversion
99 */
100 protected void constructRateMap() {
101 // Refactored into static function, since CodonProductChains need this functionality
102 rateMap = Codons.constructRateMap(rateCount, stateCount, codonDataType, geneticCode);
103 }
104
105 public void printRateMap() {
106 int u, v, i1, j1, k1, i2, j2, k2;
107 byte rateClass;
108 int[] codon;
109 int cs1, cs2, aa1, aa2;
110
111 System.out.print("\t");
112 for (v = 0; v < stateCount; v++) {
113 codon = codonDataType.getTripletStates(v);
114 i2 = codon[0];
115 j2 = codon[1];
116 k2 = codon[2];
117
118 System.out.print("\t" + Nucleotides.INSTANCE.getChar(i2));
119 System.out.print(Nucleotides.INSTANCE.getChar(j2));
120 System.out.print(Nucleotides.INSTANCE.getChar(k2));
121 }
122 System.out.println();
123
124 System.out.print("\t");
125 for (v = 0; v < stateCount; v++) {
126 codon = codonDataType.getTripletStates(v);
127 i2 = codon[0];
128 j2 = codon[1];
129 k2 = codon[2];
130
131 cs2 = codonDataType.getState(i2, j2, k2);
132 aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2));
133 System.out.print("\t" + AminoAcids.INSTANCE.getChar(aa2));
134 }
135 System.out.println();
136
137 for (u = 0; u < stateCount; u++) {
138
139 codon = codonDataType.getTripletStates(u);
140 i1 = codon[0];
141 j1 = codon[1];
142 k1 = codon[2];
143
144 System.out.print(Nucleotides.INSTANCE.getChar(i1));
145 System.out.print(Nucleotides.INSTANCE.getChar(j1));
146 System.out.print(Nucleotides.INSTANCE.getChar(k1));
147
148 cs1 = codonDataType.getState(i1, j1, k1);
149 aa1 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs1));
150
151 System.out.print("\t" + AminoAcids.INSTANCE.getChar(aa1));
152
153 for (v = 0; v < stateCount; v++) {
154
155 codon = codonDataType.getTripletStates(v);
156 i2 = codon[0];
157 j2 = codon[1];
158 k2 = codon[2];
159
160 cs2 = codonDataType.getState(i2, j2, k2);
161 aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2));
162
163 rateClass = -1;
164 if (i1 != i2) {
165 if ((i1 == 0 && i2 == 2) || (i1 == 2 && i2 == 0) || // A <-> G
166 (i1 == 1 && i2 == 3) || (i1 == 3 && i2 == 1)) { // C <-> T
167 rateClass = 1; // Transition at position 1
168 } else {
169 rateClass = 2; // Transversion at position 1
170 }
171 }
172 if (j1 != j2) {
173 if (rateClass == -1) {
174 if ((j1 == 0 && j2 == 2) || (j1 == 2 && j2 == 0) || // A <-> G
175 (j1 == 1 && j2 == 3) || (j1 == 3 && j2 == 1)) { // C <-> T
176 rateClass = 1; // Transition
177 } else {
178 rateClass = 2; // Transversion
179 }
180 } else
181 rateClass = 0; // Codon changes at more than one position
182 }
183 if (k1 != k2) {
184 if (rateClass == -1) {
185 if ((k1 == 0 && k2 == 2) || (k1 == 2 && k2 == 0) || // A <-> G
186 (k1 == 1 && k2 == 3) || (k1 == 3 && k2 == 1)) { // C <-> T
187 rateClass = 1; // Transition
188 } else {
189 rateClass = 2; // Transversion
190 }
191 } else
192 rateClass = 0; // Codon changes at more than one position
193 }
194
195 if (rateClass != 0) {
196 if (aa1 != aa2) {
197 rateClass += 2; // Is a non-synonymous change
198 }
199 }
200
201 System.out.print("\t" + rateClass);
202
203 }
204 System.out.println();
205
206 }
207 }
208 }