Debian Codegraph @

src/dr/evolution/datatype/AminoAcids.java

raw symbols SCIP
1 /*
2 * AminoAcids.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 amino acids.
30 *
31 * @version $Id: AminoAcids.java,v 1.12 2005/06/22 14:48:19 beth Exp $
32 *
33 * @author Andrew Rambaut
34 * @author Korbinian Strimmer
35 * @author Alexei Drummond
36 * @author Matthew Goode
37 */
38 public class AminoAcids extends DataType
39 {
40 /**
41 * Name of data type. For XML and human reading of data type.
42 */
43 public static final String DESCRIPTION = "amino acid";
44
45 /**
46 * This integer is a unique identifier of this data type.
47 */
48 public static final int TYPE = AMINO_ACIDS;
49
50 /**
51 * The only instance of the AminoAcids class.
52 */
53 public static final AminoAcids INSTANCE = new AminoAcids();
54
55 /**
56 * This character represents the amino acid equivalent of a stop codon to cater for
57 * situations arising from converting coding DNA to an amino acid sequence.
58 */
59 public static final char STOP_CHARACTER = '*';
60
61 public static final int STOP_STATE = 23;
62
63 /**
64 * This state represents a amino acid residue of unknown type.
65 */
66 public static final int UNKNOWN_STATE = 24;
67
68 /**
69 * This state represents a gap in an amino acid sequence.
70 */
71 public static final int GAP_STATE = 25;
72
73 /**
74 * Unique integer identifier for the amino acid data type.
75 */
76 public static final int AMINOACIDS = 1;
77
78 /**
79 * A table to translate state numbers (0-25) into one letter codes.
80 */
81 public static final char[] AMINOACID_CHARS= {
82 'A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R',
83 'S','T','V','W','Y','B','Z','X',AminoAcids.STOP_CHARACTER,DataType.UNKNOWN_CHARACTER,DataType.GAP_CHARACTER
84 };
85
86 /**
87 * A table to map state numbers (0-25) to their three letter codes.
88 */
89 private static final String[] AMINOACID_TRIPLETS = {
90 // A C D E F G H I K
91 "Ala", "Cys", "Asp", "Glu", "Phe", "Gly", "His", "Ile", "Lys",
92 // L M N P Q R S T V
93 "Leu", "Met", "Asn", "Pro", "Gln", "Arg", "Ser", "Thr", "Val",
94 // W Y B Z X * ? -
95 "Trp", "Tyr", "Asx", "Glx", " X ", " * ", " ? ", " - "
96 };
97
98 /**
99 * This table maps amino acid characters into state codes (0-25).
100 * Amino Acids go ACDEFGHIKLMNPQRSTVWYBZX*?-,
101 * Other letters; j, o, and u are mapped to ?
102 * *, ? and - are mapped to themselves
103 * All other chars are mapped to -
104 */
105 public static final int[] AMINOACID_STATES = {
106 25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25, // 0-15
107 25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25, // 16-31
108 // * -
109 25,25,25,25,25,25,25,25,25,25,23,25,25,25,25,25, // 32-47
110 // ?
111 25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,24, // 48-63
112 // A B C D E F G H I j K L M N o
113 25, 0,20, 1, 2, 3, 4, 5, 6, 7,24, 8, 9,10,11,24, // 64-79
114 // P Q R S T u V W X Y Z
115 12,13,14,15,16,24,17,18,22,19,21,25,25,25,25,25, // 80-95
116 // A B C D E F G H I j K L M N o
117 25, 0,20, 1, 2, 3, 4, 5, 6, 7,24, 8, 9,10,11,24, // 96-111
118 // P Q R S T u V W X Y Z
119 12,13,14,15,16,24,17,18,22,19,21,25,25,25,25,25 // 112-127
120 };
121
122 /**
123 * A table to map state numbers (0-25) to their ambiguities.
124 */
125 private static final String[] AMINOACID_AMBIGUITIES = {
126 // A C D E F G H I K
127 "A", "C", "D", "E", "F", "G", "H", "I", "K",
128 // L M N P Q R S T V
129 "L", "M", "N", "P", "Q", "R", "S", "T", "V",
130 // W Y B Z
131 "W", "Y", "DN", "EQ",
132 // X * ? -
133 "ACDEFGHIKLMNPQRSTVWY", "*", "ACDEFGHIKLMNPQRSTVWY", "ACDEFGHIKLMNPQRSTVWY"
134 };
135
136 /**
137 * Private constructor - DEFAULT_INSTANCE provides the only instance.
138 */
139 protected AminoAcids() {
140 super();
141 stateCount = 20;
142 ambiguousStateCount = 26;
143 }
144
145 @Override
146 public char[] getValidChars() {
147 return AMINOACID_CHARS;
148 }
149
150 /**
151 * Get state corresponding to a character.
152 *
153 * @param c character
154 *
155 * @return state
156 */
157 public int getState(final char c) {
158
159 return AMINOACID_STATES[c];
160 }
161
162 /**
163 * Get state corresponding to a stop.
164 *
165 * @return state
166 */
167 public static int getStopState() {
168
169 return STOP_STATE;
170 }
171
172 /**
173 * Get state corresponding to an unknown.
174 *
175 * @return state
176 */
177 public int getUnknownState() {
178
179 return AminoAcids.UNKNOWN_STATE;
180 }
181
182 /**
183 * Get state corresponding to a gap.
184 *
185 * @return state
186 */
187 public int getGapState() {
188 return AminoAcids.GAP_STATE;
189 }
190
191 /**
192 * Get character corresponding to a given state.
193 *
194 * @param state state
195 *
196 * @return corresponding character
197 */
198 public char getChar(final int state) {
199 return AminoAcids.AMINOACID_CHARS[state];
200 }
201
202 /**
203 * Get triplet string corresponding to a given state.
204 *
205 * @param state state
206 *
207 * @return corresponding triplet string
208 */
209 public String getTriplet(final int state) {
210
211 return AminoAcids.AMINOACID_TRIPLETS[state];
212 }
213
214 /**
215 * @param state the state to return the state set of. If this state is an ambiguity
216 * code then the array returned will be size greater than 1.
217 * @return an array containing the non-ambiguous states
218 * that this state represents.
219 */
220 public int[] getStates(final int state) {
221
222 final String stateString = AminoAcids.AMINOACID_AMBIGUITIES[state];
223 final int[] states = new int[stateString.length()];
224 for (int i = 0; i < stateString.length(); i++) {
225 states[i] = getState(stateString.charAt(i));
226 }
227
228 return states;
229 }
230
231 /**
232 * @param state the state to return the state set of. If this state is an ambiguity
233 * code then the state set returned will have more than one true element in it.
234 * @return an array containing the non-ambiguous states that this state represents.
235 */
236 public boolean[] getStateSet(final int state) {
237
238 final boolean[] stateSet = new boolean[stateCount];
239 for (int i = 0; i < stateCount; i++) {
240 stateSet[i] = false;
241 }
242
243 final int len = AminoAcids.AMINOACID_AMBIGUITIES[state].length();
244 for (int i = 0; i < len; i++) {
245 stateSet[getState(AMINOACID_AMBIGUITIES[state].charAt(i))] = true;
246 }
247
248 return stateSet;
249 }
250
251 /**
252 * description of data type.
253 *
254 * @return string describing the data type
255 */
256 public String getDescription() {
257 return DESCRIPTION;
258 }
259
260 /**
261 * type of data type.
262 *
263 * @return integer code for the data type
264 */
265 public int getType() {
266 return TYPE;
267 }
268
269 /**
270 * @param c the char that is being tested to see if its a stop char.
271 * @return true if this character is a stop
272 */
273 public boolean isStopChar(final char c) {
274 return isStopState(getState(c));
275 }
276
277 /**
278 * @param state the state that is being tested to see if its a stop state.
279 * @return true if this state is a stop.
280 */
281 public boolean isStopState(final int state) {
282 return state == getStopState();
283 }
284 }