Debian Codegraph @

src/dr/evolution/datatype/Codons.java

raw symbols SCIP
1 /*
2 * Codons.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.evolution.datatype;
27
28 /**
29 * Implements DataType for codons
30 * <p/>
31 * Codons have tree different representations:
32 * State numbers - 0-63 + 64, 65 as unknown and gap
33 * State chars - the above converted into chars, starting at 'A'
34 * and '?' + '-' for unknown and gap
35 * Strings or chars of three nucleotide characters
36 *
37 * @author Andrew Rambaut
38 * @author Alexei Drummond
39 * @version $Id: Codons.java,v 1.15 2005/05/24 20:25:56 rambaut Exp $
40 */
41 public class Codons extends DataType {
42
43 /**
44 * Name of data type. For XML and human reading of data type.
45 */
46 public static final String DESCRIPTION = "codon";
47 public static final int TYPE = CODONS;
48
49 public static final Codons UNIVERSAL = new Codons(GeneticCode.UNIVERSAL);
50 public static final Codons VERTEBRATE_MT = new Codons(GeneticCode.VERTEBRATE_MT);
51 public static final Codons YEAST = new Codons(GeneticCode.YEAST);
52 public static final Codons MOLD_PROTOZOAN_MT = new Codons(GeneticCode.MOLD_PROTOZOAN_MT);
53 public static final Codons MYCOPLASMA = new Codons(GeneticCode.MYCOPLASMA);
54 public static final Codons INVERTEBRATE_MT = new Codons(GeneticCode.INVERTEBRATE_MT);
55 public static final Codons CILIATE = new Codons(GeneticCode.CILIATE);
56 public static final Codons ECHINODERM_MT = new Codons(GeneticCode.ECHINODERM_MT);
57 public static final Codons EUPLOTID_NUC = new Codons(GeneticCode.EUPLOTID_NUC);
58 public static final Codons BACTERIAL = new Codons(GeneticCode.BACTERIAL);
59 public static final Codons ALT_YEAST = new Codons(GeneticCode.ALT_YEAST);
60 public static final Codons ASCIDIAN_MT = new Codons(GeneticCode.ASCIDIAN_MT);
61 public static final Codons FLATWORM_MT = new Codons(GeneticCode.FLATWORM_MT);
62 public static final Codons BLEPHARISMA_NUC = new Codons(GeneticCode.BLEPHARISMA_NUC);
63 public static final Codons NO_STOPS = new Codons(GeneticCode.NO_STOPS);
64
65 public static final int UNKNOWN_STATE = 64;
66 public static final int GAP_STATE = 65;
67
68 public static final String[] CODON_TRIPLETS = {
69 "AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT",
70 "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT",
71 "CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT",
72 "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT",
73 "GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT",
74 "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT",
75 "TAA", "TAC", "TAG", "TAT", "TCA", "TCC", "TCG", "TCT",
76 "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT",
77 "???", "---"
78 };
79
80 /*
81 * Default unused constructor
82 */
83
84 public Codons() {
85 }
86
87 /**
88 * Protected constructor - DEFAULT_INSTANCE and descendents provide the only instance
89 */
90 protected Codons(GeneticCode geneticCode) {
91 this.geneticCode = geneticCode;
92
93 stateCount = 64 - geneticCode.getStopCodonCount();
94 ambiguousStateCount = 66;
95
96 stateMap = new int[ambiguousStateCount];
97 reverseMap = new int[ambiguousStateCount];
98
99 int j = 0;
100 int k = stateCount;
101 for (int i = 0; i < 64; i++) {
102 if (!geneticCode.isStopCodon(i)) {
103 stateMap[j] = i;
104 reverseMap[i] = j;
105 j++;
106 } else {
107 stateMap[k] = i;
108 reverseMap[i] = k;
109 k++;
110 }
111 }
112 for (int i = 64; i < ambiguousStateCount; i++) {
113 stateMap[i] = i;
114 reverseMap[i] = i;
115 }
116
117 }
118
119 @Override
120 public char[] getValidChars() {
121 return null;
122 }
123
124 /**
125 * Get state corresponding to a character
126 *
127 * @param c character
128 * @return state
129 */
130 public final int getState(char c) {
131 throw new IllegalArgumentException("Codons datatype cannot be expressed as char");
132 }
133
134 /**
135 * Get state corresponding to an unknown
136 *
137 * @return state
138 */
139 public int getUnknownState() {
140 return UNKNOWN_STATE;
141 }
142
143 /**
144 * Get state corresponding to a gap
145 *
146 * @return state
147 */
148 public int getGapState() {
149 return GAP_STATE;
150 }
151
152 /**
153 * Get state corresponding to a nucleotide triplet
154 *
155 * @param nuc1 the codon triplet as chars
156 * @param nuc2 the codon triplet as chars
157 * @param nuc3 the codon triplet as chars
158 * @return state
159 */
160 public final int getState(char nuc1, char nuc2, char nuc3) {
161 return getState(Nucleotides.INSTANCE.getState(nuc1),
162 Nucleotides.INSTANCE.getState(nuc2),
163 Nucleotides.INSTANCE.getState(nuc3));
164 }
165
166 /**
167 * @return the canonical state (in standard combinatorial order)
168 * of a funny codon state.
169 */
170 public final int getCanonicalState(int funnyState) {
171 return stateMap[funnyState];
172
173 }
174
175 /**
176 * Get state corresponding to a nucleotide triplet
177 *
178 * @param nuc1 the codon triplet as states
179 * @param nuc2 the codon triplet as states
180 * @param nuc3 the codon triplet as states
181 * @return state
182 */
183 public final int getState(int nuc1, int nuc2, int nuc3) {
184 if (Nucleotides.INSTANCE.isGapState(nuc1) ||
185 Nucleotides.INSTANCE.isGapState(nuc2) ||
186 Nucleotides.INSTANCE.isGapState(nuc3)) {
187 return GAP_STATE;
188 }
189
190 if (Nucleotides.INSTANCE.isAmbiguousState(nuc1) ||
191 Nucleotides.INSTANCE.isAmbiguousState(nuc2) ||
192 Nucleotides.INSTANCE.isAmbiguousState(nuc3)) {
193 return UNKNOWN_STATE;
194 }
195
196 int canonicalState = (nuc1 * 16) + (nuc2 * 4) + nuc3;
197
198 return reverseMap[canonicalState];
199 }
200
201 /**
202 * Get character corresponding to a given state
203 *
204 * @param state state
205 * <p/>
206 * return corresponding character
207 */
208 public final char getChar(int state) {
209 throw new IllegalArgumentException("Codons datatype cannot be expressed as char");
210 }
211
212 /**
213 * Get triplet string corresponding to a given state
214 *
215 * @param state state
216 * <p/>
217 * return corresponding triplet string
218 */
219 public final String getTriplet(int state) {
220 return CODON_TRIPLETS[stateMap[state]];
221 }
222
223 /**
224 * Get an array of three nucleotide states making this codon state
225 *
226 * @param state state
227 * <p/>
228 * return corresponding triplet string
229 */
230 public final int[] getTripletStates(int state) {
231 int[] triplet = new int[3];
232
233 triplet[0] = Nucleotides.INSTANCE.getState(CODON_TRIPLETS[stateMap[state]].charAt(0));
234 triplet[1] = Nucleotides.INSTANCE.getState(CODON_TRIPLETS[stateMap[state]].charAt(1));
235 triplet[2] = Nucleotides.INSTANCE.getState(CODON_TRIPLETS[stateMap[state]].charAt(2));
236
237 return triplet;
238 }
239
240 /**
241 * description of data type
242 *
243 * @return string describing the data type
244 */
245 public String getDescription() {
246 return DESCRIPTION;
247 }
248
249 /**
250 * type of data type
251 *
252 * @return integer code for the data type
253 */
254 public int getType() {
255 return TYPE;
256 }
257
258 /**
259 * @return the genetic code
260 */
261 public GeneticCode getGeneticCode() {
262 return geneticCode;
263 }
264
265 /**
266 * Takes non-canonical state and returns true if it represents stop codon
267 *
268 * @param state
269 * @return true if the given state is a stop codon
270 */
271 public final boolean isStopCodon(int state) {
272 return geneticCode.isStopCodon(stateMap[state]);
273 }
274
275 public static byte[] constructRateMap(Codons codonDataType) {
276 final int stateCount = codonDataType.getStateCount();
277 final int rateCount = (stateCount * (stateCount - 1)) / 2;
278 return constructRateMap(rateCount, stateCount, codonDataType, codonDataType.getGeneticCode());
279 }
280
281
282 /**
283 * Parse a text string to return a genetic code
284 */
285 public static Codons findByName(String codeStr) {
286 Codons codons = null;
287 if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) {
288 codons = Codons.UNIVERSAL;
289 } else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) {
290 codons = Codons.VERTEBRATE_MT;
291 } else if (codeStr.equals(GeneticCode.YEAST.getName())) {
292 codons = Codons.YEAST;
293 } else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) {
294 codons = Codons.MOLD_PROTOZOAN_MT;
295 } else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) {
296 codons = Codons.INVERTEBRATE_MT;
297 } else if (codeStr.equals(GeneticCode.CILIATE.getName())) {
298 codons = Codons.CILIATE;
299 } else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) {
300 codons = Codons.ECHINODERM_MT;
301 } else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) {
302 codons = Codons.EUPLOTID_NUC;
303 } else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) {
304 codons = Codons.BACTERIAL;
305 } else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) {
306 codons = Codons.ALT_YEAST;
307 } else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) {
308 codons = Codons.ASCIDIAN_MT;
309 } else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) {
310 codons = Codons.FLATWORM_MT;
311 } else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) {
312 codons = Codons.BLEPHARISMA_NUC;
313 } else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) {
314 codons = Codons.NO_STOPS;
315 } else {
316 throw new RuntimeException("Unknown genetics code");
317 }
318 return codons;
319 }
320
321 /**
322 * Construct a map of the rate classes in the rate matrix using the current
323 * genetic code. Classes:
324 * 0: codon changes in more than one codon position (or stop codons)
325 * 1: synonymous transition
326 * 2: synonymous transversion
327 * 3: non-synonymous transition
328 * 4: non-synonymous transversion
329 */
330 public static byte[] constructRateMap(int rateCount,
331 int stateCount,
332 Codons codonDataType,
333 GeneticCode geneticCode)
334 {
335 int u, v, i1, j1, k1, i2, j2, k2;
336 byte rateClass;
337 int[] codon;
338 int cs1, cs2, aa1, aa2;
339
340 int i = 0;
341
342 byte[] rateMap = new byte[rateCount];
343
344 for (u = 0; u < stateCount; u++) {
345
346 codon = codonDataType.getTripletStates(u);
347 i1 = codon[0];
348 j1 = codon[1];
349 k1 = codon[2];
350
351 cs1 = codonDataType.getState(i1, j1, k1);
352 aa1 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs1));
353
354 for (v = u + 1; v < stateCount; v++) {
355
356 codon = codonDataType.getTripletStates(v);
357 i2 = codon[0];
358 j2 = codon[1];
359 k2 = codon[2];
360
361 cs2 = codonDataType.getState(i2, j2, k2);
362 aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2));
363
364 rateClass = -1;
365 if (i1 != i2) {
366 if ( (i1 == 0 && i2 == 2) || (i1 == 2 && i2 == 0) || // A <-> G
367 (i1 == 1 && i2 == 3) || (i1 == 3 && i2 == 1) ) { // C <-> T
368 rateClass = 1; // Transition at position 1
369 } else {
370 rateClass = 2; // Transversion at position 1
371 }
372 }
373 if (j1 != j2) {
374 if (rateClass == -1) {
375 if ( (j1 == 0 && j2 == 2) || (j1 == 2 && j2 == 0) || // A <-> G
376 (j1 == 1 && j2 == 3) || (j1 == 3 && j2 == 1) ) { // C <-> T
377 rateClass = 1; // Transition
378 } else {
379 rateClass = 2; // Transversion
380 }
381 } else
382 rateClass = 0; // Codon changes at more than one position
383 }
384 if (k1 != k2) {
385 if (rateClass == -1) {
386 if ( (k1 == 0 && k2 == 2) || (k1 == 2 && k2 == 0) || // A <-> G
387 (k1 == 1 && k2 == 3) || (k1 == 3 && k2 == 1) ) { // C <-> T
388 rateClass = 1; // Transition
389 } else {
390 rateClass = 2; // Transversion
391 }
392 } else
393 rateClass = 0; // Codon changes at more than one position
394 }
395
396 if (rateClass != 0) {
397 if (aa1 != aa2) {
398 rateClass += 2; // Is a non-synonymous change
399 }
400 }
401
402 rateMap[i] = rateClass;
403 i++;
404 }
405
406 }
407 return rateMap;
408 }
409
410 // Private members
411
412 protected GeneticCode geneticCode;
413 protected int[] stateMap;
414 protected int[] reverseMap;
415 }