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