JAL-1828 lookup table to convert MSE to MET when parsing PDB data
[jalview.git] / src / jalview / schemes / ResidueProperties.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.schemes;
22
23 import jalview.analysis.scoremodels.FeatureScoreModel;
24 import jalview.analysis.scoremodels.PIDScoreModel;
25 import jalview.api.analysis.ScoreModelI;
26
27 import java.awt.Color;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.Hashtable;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Vector;
35
36 public class ResidueProperties
37 {
38   public static Hashtable<String, ScoreModelI> scoreMatrices = new Hashtable();
39
40   // Stores residue codes/names and colours and other things
41   public static final int[] aaIndex; // aaHash version 2.1.1 and below
42
43   public static final int[] nucleotideIndex;
44
45   public static final int[] purinepyrimidineIndex;
46
47   public static final Map<String, Integer> aa3Hash = new HashMap<String, Integer>();
48
49   public static final Map<String, String> aa2Triplet = new HashMap<String, String>();
50
51   public static final Map<String, String> nucleotideName = new HashMap<String, String>();
52
53   // lookup from modified amino acid (e.g. MSE) to canonical form (e.g. MET)
54   public static final Map<String, String> modifications = new HashMap<String, String>();
55
56   static
57   {
58     aaIndex = new int[255];
59     for (int i = 0; i < 255; i++)
60     {
61       aaIndex[i] = 23;
62     }
63
64     aaIndex['A'] = 0;
65     aaIndex['R'] = 1;
66     aaIndex['N'] = 2;
67     aaIndex['D'] = 3;
68     aaIndex['C'] = 4;
69     aaIndex['Q'] = 5;
70     aaIndex['E'] = 6;
71     aaIndex['G'] = 7;
72     aaIndex['H'] = 8;
73     aaIndex['I'] = 9;
74     aaIndex['L'] = 10;
75     aaIndex['K'] = 11;
76     aaIndex['M'] = 12;
77     aaIndex['F'] = 13;
78     aaIndex['P'] = 14;
79     aaIndex['S'] = 15;
80     aaIndex['T'] = 16;
81     aaIndex['W'] = 17;
82     aaIndex['Y'] = 18;
83     aaIndex['V'] = 19;
84     aaIndex['B'] = 20;
85     aaIndex['Z'] = 21;
86     aaIndex['X'] = 22;
87     aaIndex['U'] = 22;
88     aaIndex['a'] = 0;
89     aaIndex['r'] = 1;
90     aaIndex['n'] = 2;
91     aaIndex['d'] = 3;
92     aaIndex['c'] = 4;
93     aaIndex['q'] = 5;
94     aaIndex['e'] = 6;
95     aaIndex['g'] = 7;
96     aaIndex['h'] = 8;
97     aaIndex['i'] = 9;
98     aaIndex['l'] = 10;
99     aaIndex['k'] = 11;
100     aaIndex['m'] = 12;
101     aaIndex['f'] = 13;
102     aaIndex['p'] = 14;
103     aaIndex['s'] = 15;
104     aaIndex['t'] = 16;
105     aaIndex['w'] = 17;
106     aaIndex['y'] = 18;
107     aaIndex['v'] = 19;
108     aaIndex['b'] = 20;
109     aaIndex['z'] = 21;
110     aaIndex['x'] = 22;
111     aaIndex['u'] = 22; // TODO: selenocystine triplet and codons needed. also
112     // extend subt. matrices
113   }
114
115   /**
116    * maximum (gap) index for matrices involving protein alphabet
117    */
118   public final static int maxProteinIndex = 23;
119
120   /**
121    * maximum (gap) index for matrices involving nucleotide alphabet
122    */
123   public final static int maxNucleotideIndex = 10;
124
125   static
126   {
127     nucleotideIndex = new int[255];
128     for (int i = 0; i < 255; i++)
129     {
130       nucleotideIndex[i] = 10; // non-nucleotide symbols are all non-gap gaps.
131     }
132
133     nucleotideIndex['A'] = 0;
134     nucleotideIndex['a'] = 0;
135     nucleotideIndex['C'] = 1;
136     nucleotideIndex['c'] = 1;
137     nucleotideIndex['G'] = 2;
138     nucleotideIndex['g'] = 2;
139     nucleotideIndex['T'] = 3;
140     nucleotideIndex['t'] = 3;
141     nucleotideIndex['U'] = 4;
142     nucleotideIndex['u'] = 4;
143     nucleotideIndex['I'] = 5;
144     nucleotideIndex['i'] = 5;
145     nucleotideIndex['X'] = 6;
146     nucleotideIndex['x'] = 6;
147     nucleotideIndex['R'] = 7;
148     nucleotideIndex['r'] = 7;
149     nucleotideIndex['Y'] = 8;
150     nucleotideIndex['y'] = 8;
151     nucleotideIndex['N'] = 9;
152     nucleotideIndex['n'] = 9;
153
154     nucleotideName.put("A", "Adenine");
155     nucleotideName.put("a", "Adenine");
156     nucleotideName.put("G", "Guanine");
157     nucleotideName.put("g", "Guanine");
158     nucleotideName.put("C", "Cytosine");
159     nucleotideName.put("c", "Cytosine");
160     nucleotideName.put("T", "Thymine");
161     nucleotideName.put("t", "Thymine");
162     nucleotideName.put("U", "Uracil");
163     nucleotideName.put("u", "Uracil");
164     nucleotideName.put("I", "Inosine");
165     nucleotideName.put("i", "Inosine");
166     nucleotideName.put("X", "Xanthine");
167     nucleotideName.put("x", "Xanthine");
168     nucleotideName.put("R", "Unknown Purine");
169     nucleotideName.put("r", "Unknown Purine");
170     nucleotideName.put("Y", "Unknown Pyrimidine");
171     nucleotideName.put("y", "Unknown Pyrimidine");
172     nucleotideName.put("N", "Unknown");
173     nucleotideName.put("n", "Unknown");
174     nucleotideName.put("W", "Weak nucleotide (A or T)");
175     nucleotideName.put("w", "Weak nucleotide (A or T)");
176     nucleotideName.put("S", "Strong nucleotide (G or C)");
177     nucleotideName.put("s", "Strong nucleotide (G or C)");
178     nucleotideName.put("M", "Amino (A or C)");
179     nucleotideName.put("m", "Amino (A or C)");
180     nucleotideName.put("K", "Keto (G or T)");
181     nucleotideName.put("k", "Keto (G or T)");
182     nucleotideName.put("B", "Not A (G or C or T)");
183     nucleotideName.put("b", "Not A (G or C or T)");
184     nucleotideName.put("H", "Not G (A or C or T)");
185     nucleotideName.put("h", "Not G (A or C or T)");
186     nucleotideName.put("D", "Not C (A or G or T)");
187     nucleotideName.put("d", "Not C (A or G or T)");
188     nucleotideName.put("V", "Not T (A or G or C");
189     nucleotideName.put("v", "Not T (A or G or C");
190
191   }
192
193   static
194   {
195     purinepyrimidineIndex = new int[255];
196     for (int i = 0; i < 255; i++)
197     {
198       purinepyrimidineIndex[i] = 3; // non-nucleotide symbols are all non-gap
199       // gaps.
200     }
201
202     purinepyrimidineIndex['A'] = 0;
203     purinepyrimidineIndex['a'] = 0;
204     purinepyrimidineIndex['C'] = 1;
205     purinepyrimidineIndex['c'] = 1;
206     purinepyrimidineIndex['G'] = 0;
207     purinepyrimidineIndex['g'] = 0;
208     purinepyrimidineIndex['T'] = 1;
209     purinepyrimidineIndex['t'] = 1;
210     purinepyrimidineIndex['U'] = 1;
211     purinepyrimidineIndex['u'] = 1;
212     purinepyrimidineIndex['I'] = 2;
213     purinepyrimidineIndex['i'] = 2;
214     purinepyrimidineIndex['X'] = 2;
215     purinepyrimidineIndex['x'] = 2;
216     purinepyrimidineIndex['R'] = 0;
217     purinepyrimidineIndex['r'] = 0;
218     purinepyrimidineIndex['Y'] = 1;
219     purinepyrimidineIndex['y'] = 1;
220     purinepyrimidineIndex['N'] = 2;
221     purinepyrimidineIndex['n'] = 2;
222   }
223
224   static
225   {
226     aa3Hash.put("ALA", new Integer(0));
227     aa3Hash.put("ARG", new Integer(1));
228     aa3Hash.put("ASN", new Integer(2));
229     aa3Hash.put("ASP", new Integer(3)); // D
230     aa3Hash.put("CYS", new Integer(4));
231     aa3Hash.put("GLN", new Integer(5)); // Q
232     aa3Hash.put("GLU", new Integer(6)); // E
233     aa3Hash.put("GLY", new Integer(7));
234     aa3Hash.put("HIS", new Integer(8));
235     aa3Hash.put("ILE", new Integer(9));
236     aa3Hash.put("LEU", new Integer(10));
237     aa3Hash.put("LYS", new Integer(11));
238     aa3Hash.put("MET", new Integer(12));
239     aa3Hash.put("PHE", new Integer(13));
240     aa3Hash.put("PRO", new Integer(14));
241     aa3Hash.put("SER", new Integer(15));
242     aa3Hash.put("THR", new Integer(16));
243     aa3Hash.put("TRP", new Integer(17));
244     aa3Hash.put("TYR", new Integer(18));
245     aa3Hash.put("VAL", new Integer(19));
246     // IUB Nomenclature for ambiguous peptides
247     aa3Hash.put("ASX", new Integer(20)); // "B";
248     aa3Hash.put("GLX", new Integer(21)); // X
249     aa3Hash.put("XAA", new Integer(22)); // X unknown
250     aa3Hash.put("-", new Integer(23));
251     aa3Hash.put("*", new Integer(23));
252     aa3Hash.put(".", new Integer(23));
253     aa3Hash.put(" ", new Integer(23));
254     aa3Hash.put("Gap", new Integer(23));
255   }
256
257   static
258   {
259     aa2Triplet.put("A", "ALA");
260     aa2Triplet.put("a", "ALA");
261     aa2Triplet.put("R", "ARG");
262     aa2Triplet.put("r", "ARG");
263     aa2Triplet.put("N", "ASN");
264     aa2Triplet.put("n", "ASN");
265     aa2Triplet.put("D", "ASP");
266     aa2Triplet.put("d", "ASP");
267     aa2Triplet.put("C", "CYS");
268     aa2Triplet.put("c", "CYS");
269     aa2Triplet.put("Q", "GLN");
270     aa2Triplet.put("q", "GLN");
271     aa2Triplet.put("E", "GLU");
272     aa2Triplet.put("e", "GLU");
273     aa2Triplet.put("G", "GLY");
274     aa2Triplet.put("g", "GLY");
275     aa2Triplet.put("H", "HIS");
276     aa2Triplet.put("h", "HIS");
277     aa2Triplet.put("I", "ILE");
278     aa2Triplet.put("i", "ILE");
279     aa2Triplet.put("L", "LEU");
280     aa2Triplet.put("l", "LEU");
281     aa2Triplet.put("K", "LYS");
282     aa2Triplet.put("k", "LYS");
283     aa2Triplet.put("M", "MET");
284     aa2Triplet.put("m", "MET");
285     aa2Triplet.put("F", "PHE");
286     aa2Triplet.put("f", "PHE");
287     aa2Triplet.put("P", "PRO");
288     aa2Triplet.put("p", "PRO");
289     aa2Triplet.put("S", "SER");
290     aa2Triplet.put("s", "SER");
291     aa2Triplet.put("T", "THR");
292     aa2Triplet.put("t", "THR");
293     aa2Triplet.put("W", "TRP");
294     aa2Triplet.put("w", "TRP");
295     aa2Triplet.put("Y", "TYR");
296     aa2Triplet.put("y", "TYR");
297     aa2Triplet.put("V", "VAL");
298     aa2Triplet.put("v", "VAL");
299   }
300
301   public static final String[] aa =
302   { "A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F",
303       "P", "S", "T", "W", "Y", "V", "B", "Z", "X", "_", "*", ".", " " };
304
305   public static final Color midBlue = new Color(100, 100, 255);
306
307   public static final Vector scaleColours = new Vector();
308
309   static
310   {
311     scaleColours.addElement(new Color(114, 0, 147));
312     scaleColours.addElement(new Color(156, 0, 98));
313     scaleColours.addElement(new Color(190, 0, 0));
314     scaleColours.addElement(Color.red);
315     scaleColours.addElement(new Color(255, 125, 0));
316     scaleColours.addElement(Color.orange);
317     scaleColours.addElement(new Color(255, 194, 85));
318     scaleColours.addElement(Color.yellow);
319     scaleColours.addElement(new Color(255, 255, 181));
320     scaleColours.addElement(Color.white);
321   }
322
323   public static final Color[] taylor =
324   { new Color(204, 255, 0), // A Greenish-yellowy-yellow
325       new Color(0, 0, 255), // R Blueish-bluey-blue
326       new Color(204, 0, 255), // N Blueish-reddy-blue
327       new Color(255, 0, 0), // D Reddish-reddy-red
328       new Color(255, 255, 0), // C Yellowish-yellowy-yellow
329       new Color(255, 0, 204), // Q Reddish-bluey-red
330       new Color(255, 0, 102), // E Blueish-reddy-red
331       new Color(255, 153, 0), // G Yellowy-reddy-yellow
332       new Color(0, 102, 255), // H Greenish-bluey-blue
333       new Color(102, 255, 0), // I Greenish-yellowy-green
334       new Color(51, 255, 0), // L Yellowish-greeny-green
335       new Color(102, 0, 255), // K Reddish-bluey-blue
336       new Color(0, 255, 0), // M Greenish-greeny-green
337       new Color(0, 255, 102), // F Blueish-greeny-green
338       new Color(255, 204, 0), // P Reddish-yellowy-yellow
339       new Color(255, 51, 0), // S Yellowish-reddy-red
340       new Color(255, 102, 0), // T Reddish-yellowy-red
341       new Color(0, 204, 255), // W Blueish-greeny-green
342       new Color(0, 255, 204), // Y Greenish-bluey-green
343       new Color(153, 255, 0), // V Yellowish-greeny-yellow
344       Color.white, // B
345       Color.white, // Z
346       Color.white, // X
347       Color.white, // -
348       Color.white, // *
349       Color.white // .
350   };
351
352   public static final Color[] nucleotide =
353   { new Color(100, 247, 63), // A
354       new Color(255, 179, 64), // C
355       new Color(235, 65, 60), // G
356       new Color(60, 136, 238), // T
357       new Color(60, 136, 238), // U
358       Color.white, // I (inosine)
359       Color.white, // X (xanthine)
360       Color.white, // R
361       Color.white, // Y
362       Color.white, // N
363       Color.white, // Gap
364   };
365
366   // Added for PurinePyrimidineColourScheme
367   public static final Color[] purinepyrimidine =
368   { new Color(255, 131, 250), // A, G, R purines purplish/orchid
369       new Color(64, 224, 208), // C,U, T, Y pyrimidines turquoise
370       Color.white, // all other nucleotides
371       Color.white // Gap
372   };
373
374   // Zappo
375   public static final Color[] zappo =
376   { Color.pink, // A
377       midBlue, // R
378       Color.green, // N
379       Color.red, // D
380       Color.yellow, // C
381       Color.green, // Q
382       Color.red, // E
383       Color.magenta, // G
384       midBlue,// Color.red, // H
385       Color.pink, // I
386       Color.pink, // L
387       midBlue, // K
388       Color.pink, // M
389       Color.orange, // F
390       Color.magenta, // P
391       Color.green, // S
392       Color.green, // T
393       Color.orange, // W
394       Color.orange, // Y
395       Color.pink, // V
396       Color.white, // B
397       Color.white, // Z
398       Color.white, // X
399       Color.white, // -
400       Color.white, // *
401       Color.white, // .
402       Color.white // ' '
403   };
404
405   // Dunno where I got these numbers from
406   public static final double[] hyd2 =
407   { 0.62, // A
408       0.29, // R
409       -0.90, // N
410       -0.74, // D
411       1.19, // C
412       0.48, // Q
413       -0.40, // E
414       1.38, // G
415       -1.50, // H
416       1.06, // I
417       0.64, // L
418       -0.78, // K
419       0.12, // M
420       -0.85, // F
421       -2.53, // P
422       -0.18, // S
423       -0.05, // T
424       1.08, // W
425       0.81, // Y
426       0.0, // V
427       0.26, // B
428       0.0, // Z
429       0.0 // X
430   };
431
432   public static final double[] helix =
433   { 1.42, 0.98, 0.67, 1.01, 0.70, 1.11, 1.51, 0.57, 1.00, 1.08, 1.21, 1.16,
434       1.45, 1.13, 0.57, 0.77, 0.83, 1.08, 0.69, 1.06, 0.84, 1.31, 1.00, 0.0 };
435
436   public static final double helixmin = 0.57;
437
438   public static final double helixmax = 1.51;
439
440   public static final double[] strand =
441   { 0.83, 0.93, 0.89, 0.54, 1.19, 1.10, 0.37, 0.75, 0.87, 1.60, 1.30, 0.74,
442       1.05, 1.38, 0.55, 0.75, 1.19, 1.37, 1.47, 1.70, 0.72, 0.74, 1.0, 0.0 };
443
444   public static final double strandmin = 0.37;
445
446   public static final double strandmax = 1.7;
447
448   public static final double[] turn =
449   { 0.66, 0.95, 1.56, 1.46, 1.19, 0.98, 0.74, 1.56, 0.95, 0.47, 0.59, 1.01,
450       0.60, 0.60, 1.52, 1.43, 0.96, 0.96, 1.14, 0.50, 1.51, 0.86, 1.00, 0,
451       0 };
452
453   public static final double turnmin = 0.47;
454
455   public static final double turnmax = 1.56;
456
457   public static final double[] buried =
458   { 1.7, 0.1, 0.4, 0.4, 4.6, 0.3, 0.3, 1.8, 0.8, 3.1, 2.4, 0.05, 1.9, 2.2,
459       0.6, 0.8, 0.7, 1.6, 0.5, 2.9, 0.4, 0.3, 1.358, 0.00 };
460
461   public static final double buriedmin = 0.05;
462
463   public static final double buriedmax = 4.6;
464
465   // This is hydropathy index
466   // Kyte, J., and Doolittle, R.F., J. Mol. Biol.
467   // 1157, 105-132, 1982
468   public static final double[] hyd =
469   { 1.8, -4.5, -3.5, -3.5, 2.5, -3.5, -3.5, -0.4, -3.2, 4.5, 3.8, -3.9,
470       1.9, 2.8, -1.6, -0.8, -0.7, -0.9, -1.3, 4.2, -3.5, -3.5, -0.49, 0.0 };
471
472   public static final double hydmax = 4.5;
473
474   public static final double hydmin = -3.9;
475
476   // public static final double hydmax = 1.38;
477   // public static final double hydmin = -2.53;
478   private static final int[][] BLOSUM62 =
479   {
480       { 4, -1, -2, -2, 0, -1, -1, 0, -2, -1, -1, -1, -1, -2, -1, 1, 0, -3,
481           -2, 0, -2, -1, 0, -4 },
482       { -1, 5, 0, -2, -3, 1, 0, -2, 0, -3, -2, 2, -1, -3, -2, -1, -1, -3,
483           -2, -3, -1, 0, -1, -4 },
484       { -2, 0, 6, 1, -3, 0, 0, 0, 1, -3, -3, 0, -2, -3, -2, 1, 0, -4, -2,
485           -3, 3, 0, -1, -4 },
486       { -2, -2, 1, 6, -3, 0, 2, -1, -1, -3, -4, -1, -3, -3, -1, 0, -1, -4,
487           -3, -3, 4, 1, -1, -4 },
488       { 0, 3, -3, -3, 9, -3, -4, -3, -3, -1, -1, -3, -1, -2, -3, -1, -1,
489           -2, -2, -1, -3, -3, -2, -4 },
490       { -1, 1, 0, 0, -3, 5, 2, -2, 0, -3, -2, 1, 0, -3, -1, 0, -1, -2, -1,
491           -2, 0, 3, -1, -4 },
492       { -1, 0, 0, 2, -4, 2, 5, -2, 0, -3, -3, 1, -2, -3, -1, 0, -1, -3, -2,
493           -2, 1, 4, -1, -4 },
494       { 0, -2, 0, -1, -3, -2, -2, 6, -2, -4, -4, -2, -3, -3, -2, 0, -2, -2,
495           -3, -3, -1, -2, -1, -4 },
496       { -2, 0, 1, -1, -3, 0, 0, -2, 8, -3, -3, -1, -2, -1, -2, -1, -2, -2,
497           2, -3, 0, 0, -1, -4 },
498       { -1, -3, -3, -3, -1, -3, -3, -4, -3, 4, 2, -3, 1, 0, -3, -2, -1, -3,
499           -1, 3, -3, -3, -1, -4 },
500       { -1, -2, -3, -4, -1, -2, -3, -4, -3, 2, 4, -2, 2, 0, -3, -2, -1, -2,
501           -1, 1, -4, -3, -1, -4 },
502       { -1, 2, 0, -1, -3, 1, 1, -2, -1, -3, -2, 5, -1, -3, -1, 0, -1, -3,
503           -2, -2, 0, 1, -1, -4 },
504       { -1, -1, -2, -3, -1, 0, -2, -3, -2, 1, 2, -1, 5, 0, -2, -1, -1, -1,
505           -1, 1, -3, -1, -1, -4 },
506       { -2, -3, -3, -3, -2, -3, -3, -3, -1, 0, 0, -3, 0, 6, -4, -2, -2, 1,
507           3, -1, -3, -3, -1, -4 },
508       { -1, -2, -2, -1, -3, -1, -1, -2, -2, -3, -3, -1, -2, -4, 7, -1, -1,
509           -4, -3, -2, -2, -1, -2, -4 },
510       { 1, -1, 1, 0, -1, 0, 0, 0, -1, -2, -2, 0, -1, -2, -1, 4, 1, -3, -2,
511           -2, 0, 0, 0, -4 },
512       { 0, -1, 0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -2, -1, 1, 5, -2,
513           -2, 0, -1, -1, 0, -4 },
514       { -3, -3, -4, -4, -2, -2, -3, -2, -2, -3, -2, -3, -1, 1, -4, -3, -2,
515           11, 2, -3, -4, -3, -2, -4 },
516       { -2, -2, -2, -3, -2, -1, -2, -3, 2, -1, -1, -2, -1, 3, -3, -2, -2,
517           2, 7, -1, -3, -2, -1, -4 },
518       { 0, -3, -3, -3, -1, -2, -2, -3, -3, 3, 1, -2, 1, -1, -2, -2, 0, -3,
519           -1, 4, -3, -2, -1, -4 },
520       { -2, -1, 3, 4, -3, 0, 1, -1, 0, -3, -4, 0, -3, -3, -2, 0, -1, -4,
521           -3, -3, 4, 1, -1, -4 },
522       { -1, 0, 0, 1, -3, 3, 4, -2, 0, -3, -3, 1, -1, -3, -1, 0, -1, -3, -2,
523           -2, 1, 4, -1, -4 },
524       { 0, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, 0, 0,
525           -2, -1, -1, -1, -1, -1, -4 },
526       { -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
527           -4, -4, -4, -4, -4, -4, 1 }, };
528
529   static final int[][] PAM250 =
530   {
531       { 2, -2, 0, 0, -2, 0, 0, 1, -1, -1, -2, -1, -1, -3, 1, 1, 1, -6, -3,
532           0, 0, 0, 0, -8 },
533       { -2, 6, 0, -1, -4, 1, -1, -3, 2, -2, -3, 3, 0, -4, 0, 0, -1, 2, -4,
534           -2, -1, 0, -1, -8 },
535       { 0, 0, 2, 2, -4, 1, 1, 0, 2, -2, -3, 1, -2, -3, 0, 1, 0, -4, -2, -2,
536           2, 1, 0, -8 },
537       { 0, -1, 2, 4, -5, 2, 3, 1, 1, -2, -4, 0, -3, -6, -1, 0, 0, -7, -4,
538           -2, 3, 3, -1, -8 },
539       { -2, -4, -4, -5, 12, -5, -5, -3, -3, -2, -6, -5, -5, -4, -3, 0, -2,
540           -8, 0, -2, -4, -5, -3, -8 },
541       { 0, 1, 1, 2, -5, 4, 2, -1, 3, -2, -2, 1, -1, -5, 0, -1, -1, -5, -4,
542           -2, 1, 3, -1, -8 },
543       { 0, -1, 1, 3, -5, 2, 4, 0, 1, -2, -3, 0, -2, -5, -1, 0, 0, -7, -4,
544           -2, 3, 3, -1, -8 },
545       { 1, -3, 0, 1, -3, -1, 0, 5, -2, -3, -4, -2, -3, -5, 0, 1, 0, -7, -5,
546           -1, 0, 0, -1, -8 },
547       { -1, 2, 2, 1, -3, 3, 1, -2, 6, -2, -2, 0, -2, -2, 0, -1, -1, -3, 0,
548           -2, 1, 2, -1, -8 },
549       { -1, -2, -2, -2, -2, -2, -2, -3, -2, 5, 2, -2, 2, 1, -2, -1, 0, -5,
550           -1, 4, -2, -2, -1, -8 },
551       { -2, -3, -3, -4, -6, -2, -3, -4, -2, 2, 6, -3, 4, 2, -3, -3, -2, -2,
552           -1, 2, -3, -3, -1, -8 },
553       { -1, 3, 1, 0, -5, 1, 0, -2, 0, -2, -3, 5, 0, -5, -1, 0, 0, -3, -4,
554           -2, 1, 0, -1, -8 },
555       { -1, 0, -2, -3, -5, -1, -2, -3, -2, 2, 4, 0, 6, 0, -2, -2, -1, -4,
556           -2, 2, -2, -2, -1, -8 },
557       { -3, -4, -3, -6, -4, -5, -5, -5, -2, 1, 2, -5, 0, 9, -5, -3, -3, 0,
558           7, -1, -4, -5, -2, -8 },
559       { 1, 0, 0, -1, -3, 0, -1, 0, 0, -2, -3, -1, -2, -5, 6, 1, 0, -6, -5,
560           -1, -1, 0, -1, -8 },
561       { 1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -3, 0, -2, -3, 1, 2, 1, -2, -3,
562           -1, 0, 0, 0, -8 },
563       { 1, -1, 0, 0, -2, -1, 0, 0, -1, 0, -2, 0, -1, -3, 0, 1, 3, -5, -3,
564           0, 0, -1, 0, -8 },
565       { -6, 2, -4, -7, -8, -5, -7, -7, -3, -5, -2, -3, -4, 0, -6, -2, -5,
566           17, 0, -6, -5, -6, -4, -8 },
567       { -3, -4, -2, -4, 0, -4, -4, -5, 0, -1, -1, -4, -2, 7, -5, -3, -3, 0,
568           10, -2, -3, -4, -2, -8 },
569       { 0, -2, -2, -2, -2, -2, -2, -1, -2, 4, 2, -2, 2, -1, -1, -1, 0, -6,
570           -2, 4, -2, -2, -1, -8 },
571       { 0, -1, 2, 3, -4, 1, 3, 0, 1, -2, -3, 1, -2, -4, -1, 0, 0, -5, -3,
572           -2, 3, 2, -1, -8 },
573       { 0, 0, 1, 3, -5, 3, 3, 0, 2, -2, -3, 0, -2, -5, 0, 0, -1, -6, -4,
574           -2, 2, 3, -1, -8 },
575       { 0, -1, 0, -1, -3, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, 0, 0, -4,
576           -2, -1, -1, -1, -1, -8 },
577       { -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,
578           -8, -8, -8, -8, -8, -8, 1 }, };
579
580   public static final Hashtable ssHash = new Hashtable(); // stores the number
581   // value of the aa
582
583   static
584   {
585     ssHash.put("H", Color.magenta);
586     ssHash.put("E", Color.yellow);
587     ssHash.put("-", Color.white);
588     ssHash.put(".", Color.white);
589     ssHash.put("S", Color.cyan);
590     ssHash.put("T", Color.blue);
591     ssHash.put("G", Color.pink);
592     ssHash.put("I", Color.pink);
593     ssHash.put("B", Color.yellow);
594   }
595
596   /*
597    * new Color(60, 136, 238), // U Color.white, // I Color.white, // X
598    * Color.white, // R Color.white, // Y Color.white, // N Color.white, // Gap
599    */
600
601   // JBPNote: patch matrix for T/U equivalence when working with DNA or RNA.
602   // Will equate sequences if working with mixed nucleotide sets.
603   // treats T and U identically. R and Y weak equivalence with AG and CTU.
604   // N matches any other base weakly
605   //
606   static final int[][] DNA =
607   {
608   { 10, -8, -8, -8, -8, 1, 1, 1, -8, 1, 1 }, // A
609       { -8, 10, -8, -8, -8, 1, 1, -8, 1, 1, 1 }, // C
610       { -8, -8, 10, -8, -8, 1, 1, 1, -8, 1, 1 }, // G
611       { -8, -8, -8, 10, 10, 1, 1, -8, 1, 1, 1 }, // T
612       { -8, -8, -8, 10, 10, 1, 1, -8, 1, 1, 1 }, // U
613       { 1, 1, 1, 1, 1, 10, 0, 0, 0, 1, 1 }, // I
614       { 1, 1, 1, 1, 1, 0, 10, 0, 0, 1, 1 }, // X
615       { 1, -8, 1, -8, -8, 0, 0, 10, -8, 1, 1 }, // R
616       { -8, 1, -8, 1, 1, 0, 0, -8, 10, 1, 1 }, // Y
617       { 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 1 }, // N
618       { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // -
619   };
620   /**
621    * register matrices in list
622    */
623   static
624   {
625     scoreMatrices.put("BLOSUM62", new ScoreMatrix("BLOSUM62", BLOSUM62, 0));
626     scoreMatrices.put("PAM250", new ScoreMatrix("PAM250", PAM250, 0));
627     scoreMatrices.put("DNA", new ScoreMatrix("DNA", DNA, 1));
628
629   }
630
631   public static final Color[] pidColours =
632   { midBlue, new Color(153, 153, 255),
633       // Color.lightGray,
634       new Color(204, 204, 255), };
635
636   public static final float[] pidThresholds =
637   { 80, 60, 40, };
638
639   public static Map<String, List<String>> codonHash = new HashMap<String, List<String>>();
640
641   private static List<String> Lys = new ArrayList<String>();
642
643   private static List<String> Asn = new ArrayList<String>();
644
645   private static List<String> Gln = new ArrayList<String>();
646
647   private static List<String> His = new ArrayList<String>();
648
649   private static List<String> Glu = new ArrayList<String>();
650
651   private static List<String> Asp = new ArrayList<String>();
652
653   private static List<String> Tyr = new ArrayList<String>();
654
655   private static List<String> Thr = new ArrayList<String>();
656
657   private static List<String> Pro = new ArrayList<String>();
658
659   private static List<String> Ala = new ArrayList<String>();
660
661   private static List<String> Ser = new ArrayList<String>();
662
663   private static List<String> Arg = new ArrayList<String>();
664
665   private static List<String> Gly = new ArrayList<String>();
666
667   private static List<String> Trp = new ArrayList<String>();
668
669   private static List<String> Cys = new ArrayList<String>();
670
671   private static List<String> Ile = new ArrayList<String>();
672
673   private static List<String> Met = new ArrayList<String>();
674
675   private static List<String> Leu = new ArrayList<String>();
676
677   private static List<String> Val = new ArrayList<String>();
678
679   private static List<String> Phe = new ArrayList<String>();
680
681   public static List<String> STOP = new ArrayList<String>();
682
683   public static String START = "ATG";
684
685   static
686   {
687     codonHash.put("K", Lys);
688     codonHash.put("N", Asn);
689     codonHash.put("Q", Gln);
690     codonHash.put("H", His);
691     codonHash.put("E", Glu);
692     codonHash.put("D", Asp);
693     codonHash.put("Y", Tyr);
694     codonHash.put("T", Thr);
695     codonHash.put("P", Pro);
696     codonHash.put("A", Ala);
697     codonHash.put("S", Ser);
698     codonHash.put("R", Arg);
699     codonHash.put("G", Gly);
700     codonHash.put("W", Trp);
701     codonHash.put("C", Cys);
702     codonHash.put("I", Ile);
703     codonHash.put("M", Met);
704     codonHash.put("L", Leu);
705     codonHash.put("V", Val);
706     codonHash.put("F", Phe);
707     codonHash.put("STOP", STOP);
708   }
709
710   /**
711    * Nucleotide Ambiguity Codes
712    */
713   public static final Map<String, String[]> ambiguityCodes = new Hashtable<String, String[]>();
714
715   /**
716    * Codon triplets with additional symbols for unambiguous codons that include
717    * ambiguity codes
718    */
719   public static final Hashtable<String, String> codonHash2 = new Hashtable<String, String>();
720
721   /**
722    * all ambiguity codes for a given base
723    */
724   public final static Hashtable<String, List<String>> _ambiguityCodes = new Hashtable<String, List<String>>();
725
726   static
727   {
728     /*
729      * Ambiguity codes as per http://www.chem.qmul.ac.uk/iubmb/misc/naseq.html
730      */
731     ambiguityCodes.put("R", new String[]
732     { "A", "G" });
733     ambiguityCodes.put("Y", new String[]
734     { "T", "C" });
735     ambiguityCodes.put("W", new String[]
736     { "A", "T" });
737     ambiguityCodes.put("S", new String[]
738     { "G", "C" });
739     ambiguityCodes.put("M", new String[]
740     { "A", "C" });
741     ambiguityCodes.put("K", new String[]
742     { "G", "T" });
743     ambiguityCodes.put("H", new String[]
744     { "A", "T", "C" });
745     ambiguityCodes.put("B", new String[]
746     { "G", "T", "C" });
747     ambiguityCodes.put("V", new String[]
748     { "G", "A", "C" });
749     ambiguityCodes.put("D", new String[]
750     { "G", "A", "T" });
751     ambiguityCodes.put("N", new String[]
752     { "G", "A", "T", "C" });
753
754     // Now build codon translation table
755     codonHash2.put("AAA", "K");
756     codonHash2.put("AAG", "K");
757     codonHash2.put("AAC", "N");
758     codonHash2.put("AAT", "N");
759
760     codonHash2.put("CAA", "Q");
761     codonHash2.put("CAG", "Q");
762     codonHash2.put("CAC", "H");
763     codonHash2.put("CAT", "H");
764
765     codonHash2.put("GAA", "E");
766     codonHash2.put("GAG", "E");
767     codonHash2.put("GAC", "D");
768     codonHash2.put("GAT", "D");
769
770     codonHash2.put("TAC", "Y");
771     codonHash2.put("TAT", "Y");
772
773     codonHash2.put("ACA", "T");
774     codonHash2.put("ACC", "T");
775     codonHash2.put("ACT", "T");
776     codonHash2.put("ACG", "T");
777
778     codonHash2.put("CCA", "P");
779     codonHash2.put("CCG", "P");
780     codonHash2.put("CCC", "P");
781     codonHash2.put("CCT", "P");
782
783     codonHash2.put("GCA", "A");
784     codonHash2.put("GCG", "A");
785     codonHash2.put("GCC", "A");
786     codonHash2.put("GCT", "A");
787
788     codonHash2.put("TCA", "S");
789     codonHash2.put("TCG", "S");
790     codonHash2.put("TCC", "S");
791     codonHash2.put("TCT", "S");
792     codonHash2.put("AGC", "S");
793     codonHash2.put("AGT", "S");
794
795     codonHash2.put("AGA", "R");
796     codonHash2.put("AGG", "R");
797     codonHash2.put("CGA", "R");
798     codonHash2.put("CGG", "R");
799     codonHash2.put("CGC", "R");
800     codonHash2.put("CGT", "R");
801
802     codonHash2.put("GGA", "G");
803     codonHash2.put("GGG", "G");
804     codonHash2.put("GGC", "G");
805     codonHash2.put("GGT", "G");
806
807     codonHash2.put("TGA", "*");
808     codonHash2.put("TAA", "*");
809     codonHash2.put("TAG", "*");
810
811     codonHash2.put("TGG", "W");
812
813     codonHash2.put("TGC", "C");
814     codonHash2.put("TGT", "C");
815
816     codonHash2.put("ATA", "I");
817     codonHash2.put("ATC", "I");
818     codonHash2.put("ATT", "I");
819
820     codonHash2.put("ATG", "M");
821
822     codonHash2.put("CTA", "L");
823     codonHash2.put("CTG", "L");
824     codonHash2.put("CTC", "L");
825     codonHash2.put("CTT", "L");
826     codonHash2.put("TTA", "L");
827     codonHash2.put("TTG", "L");
828
829     codonHash2.put("GTA", "V");
830     codonHash2.put("GTG", "V");
831     codonHash2.put("GTC", "V");
832     codonHash2.put("GTT", "V");
833
834     codonHash2.put("TTC", "F");
835     codonHash2.put("TTT", "F");
836
837     buildAmbiguityCodonSet();
838   }
839
840   /**
841    * programmatic generation of codons including ambiguity codes
842    */
843   public static void buildAmbiguityCodonSet()
844   {
845     if (_ambiguityCodes.size() > 0)
846     {
847       System.err
848               .println("Ignoring multiple calls to buildAmbiguityCodonSet");
849       return;
850     }
851     // Invert the ambiguity code set
852     for (Map.Entry<String, String[]> acode : ambiguityCodes.entrySet())
853     {
854       for (String r : acode.getValue())
855       {
856         List<String> codesfor = _ambiguityCodes.get(r);
857         if (codesfor == null)
858         {
859           _ambiguityCodes.put(r, codesfor = new ArrayList<String>());
860         }
861         if (!codesfor.contains(acode.getKey()))
862         {
863           codesfor.add(acode.getKey());
864         }
865         else
866         {
867           System.err
868                   .println("Inconsistency in the IUBMB ambiguity code nomenclature table: collision for "
869                           + acode.getKey() + " in residue " + r);
870         }
871       }
872     }
873     // and programmatically add in the ambiguity codes that yield the same amino
874     // acid
875     String[] unambcodons = codonHash2.keySet().toArray(
876             new String[codonHash2.size()]);
877     for (String codon : unambcodons)
878     {
879       String residue = codonHash2.get(codon);
880       String acodon[][] = new String[codon.length()][];
881       for (int i = 0, iSize = codon.length(); i < iSize; i++)
882       {
883         String _ac = "" + codon.charAt(i);
884         List<String> acodes = _ambiguityCodes.get(_ac);
885         if (acodes != null)
886         {
887           acodon[i] = acodes.toArray(new String[acodes.size()]);
888         }
889         else
890         {
891           acodon[i] = new String[]
892           {};
893         }
894       }
895       // enumerate all combinations and test for veracity of translation
896       int tpos[] = new int[codon.length()], cpos[] = new int[codon.length()];
897       for (int i = 0; i < tpos.length; i++)
898       {
899         tpos[i] = -1;
900       }
901       tpos[acodon.length - 1] = 0;
902       int ipos, j;
903       while (tpos[0] < acodon[0].length)
904       {
905         // make all codons for this combination
906         char allres[][] = new char[tpos.length][];
907         String _acodon = "";
908         char _anuc;
909         for (ipos = 0; ipos < tpos.length; ipos++)
910         {
911           if (acodon[ipos].length == 0 || tpos[ipos] < 0)
912           {
913             _acodon += codon.charAt(ipos);
914             allres[ipos] = new char[]
915             { codon.charAt(ipos) };
916           }
917           else
918           {
919             _acodon += acodon[ipos][tpos[ipos]];
920             String[] altbase = ambiguityCodes.get(acodon[ipos][tpos[ipos]]);
921             allres[ipos] = new char[altbase.length];
922             j = 0;
923             for (String ab : altbase)
924             {
925               allres[ipos][j++] = ab.charAt(0);
926             }
927           }
928         }
929         // test all codons for this combination
930         for (ipos = 0; ipos < cpos.length; ipos++)
931         {
932           cpos[ipos] = 0;
933         }
934         boolean valid = true;
935         do
936         {
937           String _codon = "";
938           for (j = 0; j < cpos.length; j++)
939           {
940             _codon += allres[j][cpos[j]];
941           }
942           String tr = codonHash2.get(_codon);
943           if (valid = (tr != null && tr.equals(residue)))
944           {
945             // advance to next combination
946             ipos = acodon.length - 1;
947             while (++cpos[ipos] >= allres[ipos].length && ipos > 0)
948             {
949               cpos[ipos] = 0;
950               ipos--;
951             }
952           }
953         } while (valid && cpos[0] < allres[0].length);
954         if (valid)
955         {
956           // Add this to the set of codons we will translate
957           // System.out.println("Adding ambiguity codon: " + _acodon + " for "
958           // + residue);
959           codonHash2.put(_acodon, residue);
960         }
961         else
962         {
963           // System.err.println("Rejecting ambiguity codon: " + _acodon
964           // + " for " + residue);
965         }
966         // next combination
967         ipos = acodon.length - 1;
968         while (++tpos[ipos] >= acodon[ipos].length && ipos > 0)
969         {
970           tpos[ipos] = -1;
971           ipos--;
972         }
973       }
974     }
975
976   }
977
978   static
979   {
980     Lys.add("AAA");
981     Lys.add("AAG");
982     Asn.add("AAC");
983     Asn.add("AAT");
984
985     Gln.add("CAA");
986     Gln.add("CAG");
987     His.add("CAC");
988     His.add("CAT");
989
990     Glu.add("GAA");
991     Glu.add("GAG");
992     Asp.add("GAC");
993     Asp.add("GAT");
994
995     Tyr.add("TAC");
996     Tyr.add("TAT");
997
998     Thr.add("ACA");
999     Thr.add("ACG");
1000     Thr.add("ACC");
1001     Thr.add("ACT");
1002
1003     Pro.add("CCA");
1004     Pro.add("CCG");
1005     Pro.add("CCC");
1006     Pro.add("CCT");
1007
1008     Ala.add("GCA");
1009     Ala.add("GCG");
1010     Ala.add("GCC");
1011     Ala.add("GCT");
1012
1013     Ser.add("TCA");
1014     Ser.add("TCG");
1015     Ser.add("TCC");
1016     Ser.add("TCT");
1017     Ser.add("AGC");
1018     Ser.add("AGT");
1019
1020     Arg.add("AGA");
1021     Arg.add("AGG");
1022     Arg.add("CGA");
1023     Arg.add("CGG");
1024     Arg.add("CGC");
1025     Arg.add("CGT");
1026
1027     Gly.add("GGA");
1028     Gly.add("GGG");
1029     Gly.add("GGC");
1030     Gly.add("GGT");
1031
1032     STOP.add("TGA");
1033     STOP.add("TAA");
1034     STOP.add("TAG");
1035
1036     Trp.add("TGG");
1037
1038     Cys.add("TGC");
1039     Cys.add("TGT");
1040
1041     Ile.add("ATA");
1042     Ile.add("ATC");
1043     Ile.add("ATT");
1044
1045     Met.add("ATG");
1046
1047     Leu.add("CTA");
1048     Leu.add("CTG");
1049     Leu.add("CTC");
1050     Leu.add("CTT");
1051     Leu.add("TTA");
1052     Leu.add("TTG");
1053
1054     Val.add("GTA");
1055     Val.add("GTG");
1056     Val.add("GTC");
1057     Val.add("GTT");
1058
1059     Phe.add("TTC");
1060     Phe.add("TTT");
1061   }
1062
1063   // Stores residue codes/names and colours and other things
1064   public static Hashtable propHash = new Hashtable();
1065
1066   public static Hashtable hydrophobic = new Hashtable();
1067
1068   public static Hashtable polar = new Hashtable();
1069
1070   public static Hashtable small = new Hashtable();
1071
1072   public static Hashtable positive = new Hashtable();
1073
1074   public static Hashtable negative = new Hashtable();
1075
1076   public static Hashtable charged = new Hashtable();
1077
1078   public static Hashtable aromatic = new Hashtable();
1079
1080   public static Hashtable aliphatic = new Hashtable();
1081
1082   public static Hashtable tiny = new Hashtable();
1083
1084   public static Hashtable proline = new Hashtable();
1085
1086   static
1087   {
1088     hydrophobic.put("I", new Integer(1));
1089     hydrophobic.put("L", new Integer(1));
1090     hydrophobic.put("V", new Integer(1));
1091     hydrophobic.put("C", new Integer(1));
1092     hydrophobic.put("A", new Integer(1));
1093     hydrophobic.put("G", new Integer(1));
1094     hydrophobic.put("M", new Integer(1));
1095     hydrophobic.put("F", new Integer(1));
1096     hydrophobic.put("Y", new Integer(1));
1097     hydrophobic.put("W", new Integer(1));
1098     hydrophobic.put("H", new Integer(1));
1099     hydrophobic.put("K", new Integer(1));
1100     hydrophobic.put("X", new Integer(1));
1101     hydrophobic.put("-", new Integer(1));
1102     hydrophobic.put("*", new Integer(1));
1103     hydrophobic.put("R", new Integer(0));
1104     hydrophobic.put("E", new Integer(0));
1105     hydrophobic.put("Q", new Integer(0));
1106     hydrophobic.put("D", new Integer(0));
1107     hydrophobic.put("N", new Integer(0));
1108     hydrophobic.put("S", new Integer(0));
1109     hydrophobic.put("T", new Integer(0));
1110     hydrophobic.put("P", new Integer(0));
1111   }
1112
1113   static
1114   {
1115     polar.put("Y", new Integer(1));
1116     polar.put("W", new Integer(1));
1117     polar.put("H", new Integer(1));
1118     polar.put("K", new Integer(1));
1119     polar.put("R", new Integer(1));
1120     polar.put("E", new Integer(1));
1121     polar.put("Q", new Integer(1));
1122     polar.put("D", new Integer(1));
1123     polar.put("N", new Integer(1));
1124     polar.put("S", new Integer(1));
1125     polar.put("T", new Integer(1));
1126     polar.put("X", new Integer(1));
1127     polar.put("-", new Integer(1));
1128     polar.put("*", new Integer(1));
1129     polar.put("I", new Integer(0));
1130     polar.put("L", new Integer(0));
1131     polar.put("V", new Integer(0));
1132     polar.put("C", new Integer(0));
1133     polar.put("A", new Integer(0));
1134     polar.put("G", new Integer(0));
1135     polar.put("M", new Integer(0));
1136     polar.put("F", new Integer(0));
1137     polar.put("P", new Integer(0));
1138   }
1139
1140   static
1141   {
1142     small.put("I", new Integer(0));
1143     small.put("L", new Integer(0));
1144     small.put("V", new Integer(1));
1145     small.put("C", new Integer(1));
1146     small.put("A", new Integer(1));
1147     small.put("G", new Integer(1));
1148     small.put("M", new Integer(0));
1149     small.put("F", new Integer(0));
1150     small.put("Y", new Integer(0));
1151     small.put("W", new Integer(0));
1152     small.put("H", new Integer(0));
1153     small.put("K", new Integer(0));
1154     small.put("R", new Integer(0));
1155     small.put("E", new Integer(0));
1156     small.put("Q", new Integer(0));
1157     small.put("D", new Integer(1));
1158     small.put("N", new Integer(1));
1159     small.put("S", new Integer(1));
1160     small.put("T", new Integer(1));
1161     small.put("P", new Integer(1));
1162     small.put("-", new Integer(1));
1163     small.put("*", new Integer(1));
1164   }
1165
1166   static
1167   {
1168     positive.put("I", new Integer(0));
1169     positive.put("L", new Integer(0));
1170     positive.put("V", new Integer(0));
1171     positive.put("C", new Integer(0));
1172     positive.put("A", new Integer(0));
1173     positive.put("G", new Integer(0));
1174     positive.put("M", new Integer(0));
1175     positive.put("F", new Integer(0));
1176     positive.put("Y", new Integer(0));
1177     positive.put("W", new Integer(0));
1178     positive.put("H", new Integer(1));
1179     positive.put("K", new Integer(1));
1180     positive.put("R", new Integer(1));
1181     positive.put("E", new Integer(0));
1182     positive.put("Q", new Integer(0));
1183     positive.put("D", new Integer(0));
1184     positive.put("N", new Integer(0));
1185     positive.put("S", new Integer(0));
1186     positive.put("T", new Integer(0));
1187     positive.put("P", new Integer(0));
1188     positive.put("-", new Integer(1));
1189     positive.put("*", new Integer(1));
1190   }
1191
1192   static
1193   {
1194     negative.put("I", new Integer(0));
1195     negative.put("L", new Integer(0));
1196     negative.put("V", new Integer(0));
1197     negative.put("C", new Integer(0));
1198     negative.put("A", new Integer(0));
1199     negative.put("G", new Integer(0));
1200     negative.put("M", new Integer(0));
1201     negative.put("F", new Integer(0));
1202     negative.put("Y", new Integer(0));
1203     negative.put("W", new Integer(0));
1204     negative.put("H", new Integer(0));
1205     negative.put("K", new Integer(0));
1206     negative.put("R", new Integer(0));
1207     negative.put("E", new Integer(1));
1208     negative.put("Q", new Integer(0));
1209     negative.put("D", new Integer(1));
1210     negative.put("N", new Integer(0));
1211     negative.put("S", new Integer(0));
1212     negative.put("T", new Integer(0));
1213     negative.put("P", new Integer(0));
1214     negative.put("-", new Integer(1));
1215     negative.put("*", new Integer(1));
1216   }
1217
1218   static
1219   {
1220     charged.put("I", new Integer(0));
1221     charged.put("L", new Integer(0));
1222     charged.put("V", new Integer(0));
1223     charged.put("C", new Integer(0));
1224     charged.put("A", new Integer(0));
1225     charged.put("G", new Integer(0));
1226     charged.put("M", new Integer(0));
1227     charged.put("F", new Integer(0));
1228     charged.put("Y", new Integer(0));
1229     charged.put("W", new Integer(0));
1230     charged.put("H", new Integer(1));
1231     charged.put("K", new Integer(1));
1232     charged.put("R", new Integer(1));
1233     charged.put("E", new Integer(1));
1234     charged.put("Q", new Integer(0));
1235     charged.put("D", new Integer(1));
1236     charged.put("N", new Integer(0)); // Asparagine is polar but not charged.
1237                                       // Alternative would be charged and
1238                                       // negative (in basic form)?
1239     charged.put("S", new Integer(0));
1240     charged.put("T", new Integer(0));
1241     charged.put("P", new Integer(0));
1242     charged.put("-", new Integer(1));
1243     charged.put("*", new Integer(1));
1244   }
1245
1246   static
1247   {
1248     aromatic.put("I", new Integer(0));
1249     aromatic.put("L", new Integer(0));
1250     aromatic.put("V", new Integer(0));
1251     aromatic.put("C", new Integer(0));
1252     aromatic.put("A", new Integer(0));
1253     aromatic.put("G", new Integer(0));
1254     aromatic.put("M", new Integer(0));
1255     aromatic.put("F", new Integer(1));
1256     aromatic.put("Y", new Integer(1));
1257     aromatic.put("W", new Integer(1));
1258     aromatic.put("H", new Integer(1));
1259     aromatic.put("K", new Integer(0));
1260     aromatic.put("R", new Integer(0));
1261     aromatic.put("E", new Integer(0));
1262     aromatic.put("Q", new Integer(0));
1263     aromatic.put("D", new Integer(0));
1264     aromatic.put("N", new Integer(0));
1265     aromatic.put("S", new Integer(0));
1266     aromatic.put("T", new Integer(0));
1267     aromatic.put("P", new Integer(0));
1268     aromatic.put("-", new Integer(1));
1269     aromatic.put("*", new Integer(1));
1270   }
1271
1272   static
1273   {
1274     aliphatic.put("I", new Integer(1));
1275     aliphatic.put("L", new Integer(1));
1276     aliphatic.put("V", new Integer(1));
1277     aliphatic.put("C", new Integer(0));
1278     aliphatic.put("A", new Integer(0));
1279     aliphatic.put("G", new Integer(0));
1280     aliphatic.put("M", new Integer(0));
1281     aliphatic.put("F", new Integer(0));
1282     aliphatic.put("Y", new Integer(0));
1283     aliphatic.put("W", new Integer(0));
1284     aliphatic.put("H", new Integer(0));
1285     aliphatic.put("K", new Integer(0));
1286     aliphatic.put("R", new Integer(0));
1287     aliphatic.put("E", new Integer(0));
1288     aliphatic.put("Q", new Integer(0));
1289     aliphatic.put("D", new Integer(0));
1290     aliphatic.put("N", new Integer(0));
1291     aliphatic.put("S", new Integer(0));
1292     aliphatic.put("T", new Integer(0));
1293     aliphatic.put("P", new Integer(0));
1294     aliphatic.put("-", new Integer(1));
1295     aliphatic.put("*", new Integer(1));
1296   }
1297
1298   static
1299   {
1300     tiny.put("I", new Integer(0));
1301     tiny.put("L", new Integer(0));
1302     tiny.put("V", new Integer(0));
1303     tiny.put("C", new Integer(0));
1304     tiny.put("A", new Integer(1));
1305     tiny.put("G", new Integer(1));
1306     tiny.put("M", new Integer(0));
1307     tiny.put("F", new Integer(0));
1308     tiny.put("Y", new Integer(0));
1309     tiny.put("W", new Integer(0));
1310     tiny.put("H", new Integer(0));
1311     tiny.put("K", new Integer(0));
1312     tiny.put("R", new Integer(0));
1313     tiny.put("E", new Integer(0));
1314     tiny.put("Q", new Integer(0));
1315     tiny.put("D", new Integer(0));
1316     tiny.put("N", new Integer(0));
1317     tiny.put("S", new Integer(1));
1318     tiny.put("T", new Integer(0));
1319     tiny.put("P", new Integer(0));
1320     tiny.put("-", new Integer(1));
1321     tiny.put("*", new Integer(1));
1322   }
1323
1324   static
1325   {
1326     proline.put("I", new Integer(0));
1327     proline.put("L", new Integer(0));
1328     proline.put("V", new Integer(0));
1329     proline.put("C", new Integer(0));
1330     proline.put("A", new Integer(0));
1331     proline.put("G", new Integer(0));
1332     proline.put("M", new Integer(0));
1333     proline.put("F", new Integer(0));
1334     proline.put("Y", new Integer(0));
1335     proline.put("W", new Integer(0));
1336     proline.put("H", new Integer(0));
1337     proline.put("K", new Integer(0));
1338     proline.put("R", new Integer(0));
1339     proline.put("E", new Integer(0));
1340     proline.put("Q", new Integer(0));
1341     proline.put("D", new Integer(0));
1342     proline.put("N", new Integer(0));
1343     proline.put("S", new Integer(0));
1344     proline.put("T", new Integer(0));
1345     proline.put("P", new Integer(1));
1346     proline.put("-", new Integer(1));
1347     proline.put("*", new Integer(1));
1348   }
1349
1350   static
1351   {
1352     propHash.put("hydrophobic", hydrophobic);
1353     propHash.put("small", small);
1354     propHash.put("positive", positive);
1355     propHash.put("negative", negative);
1356     propHash.put("charged", charged);
1357     propHash.put("aromatic", aromatic);
1358     propHash.put("aliphatic", aliphatic);
1359     propHash.put("tiny", tiny);
1360     propHash.put("proline", proline);
1361     propHash.put("polar", polar);
1362   }
1363   static
1364   {
1365     int[][] propMatrixF = new int[maxProteinIndex][maxProteinIndex], propMatrixPos = new int[maxProteinIndex][maxProteinIndex], propMatrixEpos = new int[maxProteinIndex][maxProteinIndex];
1366     for (int i = 0; i < maxProteinIndex; i++)
1367     {
1368       int maxF = 0, maxP = 0, maxEP = 0;
1369       String ic = "";
1370       if (aa.length > i)
1371       {
1372         ic += aa[i];
1373       }
1374       else
1375       {
1376         ic = "-";
1377       }
1378       for (int j = i + 1; j < maxProteinIndex; j++)
1379       {
1380         String jc = "";
1381         if (aa.length > j)
1382         {
1383           jc += aa[j];
1384         }
1385         else
1386         {
1387           jc = "-";
1388         }
1389         propMatrixF[i][j] = 0;
1390         propMatrixPos[i][j] = 0;
1391         propMatrixEpos[i][j] = 0;
1392         for (Enumeration<String> en = propHash.keys(); en
1393                 .hasMoreElements();)
1394         {
1395           String ph = en.nextElement();
1396           Map<String, Integer> pph = (Map<String, Integer>) propHash
1397                   .get(ph);
1398           if (pph.get(ic) != null && pph.get(jc) != null)
1399           {
1400             int icp = pph.get(ic).intValue(), jcp = pph.get(jc).intValue();
1401             // Still working on these definitions.
1402             propMatrixPos[i][j] += icp == jcp && icp > 0 ? 2 : 0;
1403             propMatrixPos[j][i] += icp == jcp && icp > 0 ? 2 : 0;
1404             propMatrixF[i][j] += icp == jcp ? 2 : 0;
1405             propMatrixF[j][i] += icp == jcp ? 2 : 0;
1406             propMatrixEpos[i][j] += icp == jcp ? (1 + icp * 2) : 0;
1407             propMatrixEpos[j][i] += icp == jcp ? (1 + icp * 2) : 0;
1408           }
1409         }
1410         if (maxF < propMatrixF[i][j])
1411         {
1412           maxF = propMatrixF[i][j];
1413         }
1414         if (maxP < propMatrixPos[i][j])
1415         {
1416           maxP = propMatrixPos[i][j];
1417         }
1418         if (maxEP < propMatrixEpos[i][j])
1419         {
1420           maxEP = propMatrixEpos[i][j];
1421         }
1422       }
1423       propMatrixF[i][i] = maxF;
1424       propMatrixPos[i][i] = maxP;
1425       propMatrixEpos[i][i] = maxEP;
1426     }
1427     // JAL-1512 comment out physicochemical score matrices for 2.8.1 release
1428     // scoreMatrices.put("Conservation Pos", new
1429     // ScoreMatrix("Conservation Pos",propMatrixPos,0));
1430     // scoreMatrices.put("Conservation Both", new
1431     // ScoreMatrix("Conservation Both",propMatrixF,0));
1432     // scoreMatrices.put("Conservation EnhPos", new
1433     // ScoreMatrix("Conservation EnhPos",propMatrixEpos,0));
1434     scoreMatrices.put("PID", new PIDScoreModel());
1435     scoreMatrices.put("Displayed Features", new FeatureScoreModel());
1436   }
1437
1438   private ResidueProperties()
1439   {
1440   }
1441
1442   public static double getHydmax()
1443   {
1444     return hydmax;
1445   }
1446
1447   public static double getHydmin()
1448   {
1449     return hydmin;
1450   }
1451
1452   public static double[] getHyd()
1453   {
1454     return hyd;
1455   }
1456
1457   public static Map<String, Integer> getAA3Hash()
1458   {
1459     return aa3Hash;
1460   }
1461
1462   public static int[][] getDNA()
1463   {
1464     return ResidueProperties.DNA;
1465   }
1466
1467   public static int[][] getBLOSUM62()
1468   {
1469     return ResidueProperties.BLOSUM62;
1470   }
1471
1472   public static int getPAM250(String A1, String A2)
1473   {
1474     return getPAM250(A1.charAt(0), A2.charAt(0));
1475   }
1476
1477   public static int getBLOSUM62(char c1, char c2)
1478   {
1479     int pog = 0;
1480
1481     try
1482     {
1483       int a = aaIndex[c1];
1484       int b = aaIndex[c2];
1485
1486       pog = ResidueProperties.BLOSUM62[a][b];
1487     } catch (Exception e)
1488     {
1489       // System.out.println("Unknown residue in " + A1 + " " + A2);
1490     }
1491
1492     return pog;
1493   }
1494
1495   public static Vector getCodons(String res)
1496   {
1497     if (codonHash.containsKey(res))
1498     {
1499       return (Vector) codonHash.get(res);
1500     }
1501
1502     return null;
1503   }
1504
1505   public static String codonTranslate(String lccodon)
1506   {
1507     if (false)
1508     {
1509       return _codonTranslate(lccodon);
1510     }
1511     String cdn = codonHash2.get(lccodon.toUpperCase());
1512     if (cdn != null && cdn.equals("*"))
1513     {
1514       return "STOP";
1515     }
1516     return cdn;
1517   }
1518
1519   public static String _codonTranslate(String lccodon)
1520   {
1521     String codon = lccodon.toUpperCase();
1522     // all base ambiguity codes yield an 'X' amino acid residue
1523     if (codon.indexOf('X') > -1 || codon.indexOf('N') > -1)
1524     {
1525       return "X";
1526     }
1527     for (String key : codonHash.keySet())
1528     {
1529       if (codonHash.get(key).contains(codon))
1530       {
1531         return key;
1532       }
1533     }
1534
1535     return null;
1536   }
1537
1538   public static int[][] getDefaultPeptideMatrix()
1539   {
1540     return ResidueProperties.getBLOSUM62();
1541   }
1542
1543   public static int[][] getDefaultDnaMatrix()
1544   {
1545     return ResidueProperties.getDNA();
1546   }
1547
1548   /**
1549    * get a ScoreMatrix based on its string name
1550    * 
1551    * @param pwtype
1552    * @return matrix in scoreMatrices with key pwtype or null
1553    */
1554   public static ScoreMatrix getScoreMatrix(String pwtype)
1555   {
1556     Object val = scoreMatrices.get(pwtype);
1557     if (val != null && val instanceof ScoreMatrix)
1558     {
1559       return (ScoreMatrix) val;
1560     }
1561     return null;
1562   }
1563
1564   /**
1565    * get a ScoreModel based on its string name
1566    * 
1567    * @param pwtype
1568    * @return scoremodel of type pwtype or null
1569    */
1570   public static ScoreModelI getScoreModel(String pwtype)
1571   {
1572     return scoreMatrices.get(pwtype);
1573   }
1574
1575   public static int getPAM250(char c, char d)
1576   {
1577     int a = aaIndex[c];
1578     int b = aaIndex[d];
1579
1580     int pog = ResidueProperties.PAM250[a][b];
1581
1582     return pog;
1583   }
1584
1585   public static Hashtable toDssp3State;
1586   static
1587   {
1588     toDssp3State = new Hashtable();
1589     toDssp3State.put("H", "H");
1590     toDssp3State.put("E", "E");
1591     toDssp3State.put("C", " ");
1592     toDssp3State.put(" ", " ");
1593     toDssp3State.put("T", " ");
1594     toDssp3State.put("B", "E");
1595     toDssp3State.put("G", "H");
1596     toDssp3State.put("I", "H");
1597     toDssp3State.put("X", " ");
1598   }
1599
1600   /**
1601    * translate from other dssp secondary structure alphabets to 3-state
1602    * 
1603    * @param ssstring
1604    * @return ssstring as a three-state secondary structure assignment.
1605    */
1606   public static String getDssp3state(String ssstring)
1607   {
1608     if (ssstring == null)
1609     {
1610       return null;
1611     }
1612     StringBuffer ss = new StringBuffer();
1613     for (int i = 0; i < ssstring.length(); i++)
1614     {
1615       String ssc = ssstring.substring(i, i + 1);
1616       if (toDssp3State.containsKey(ssc))
1617       {
1618         ss.append((String) toDssp3State.get(ssc));
1619       }
1620       else
1621       {
1622         ss.append(" ");
1623       }
1624     }
1625     return ss.toString();
1626   }
1627
1628   /**
1629    * Used by getRNASecStrucState
1630    * 
1631    */
1632   public static Hashtable<String, String> toRNAssState;
1633
1634   public static boolean RNAcloseParen[] = new boolean[255];
1635   static
1636   {
1637     toRNAssState = new Hashtable<String, String>();
1638     toRNAssState.put(")", "(");
1639     toRNAssState.put("(", "(");
1640     toRNAssState.put("]", "[");
1641     toRNAssState.put("[", "[");
1642     toRNAssState.put("{", "{");
1643     toRNAssState.put("}", "{");
1644     toRNAssState.put(">", ">");
1645     toRNAssState.put("<", ">");
1646     toRNAssState.put("A", "A");
1647     toRNAssState.put("a", "A");
1648     toRNAssState.put("B", "B");
1649     toRNAssState.put("b", "B");
1650     toRNAssState.put("C", "C");
1651     toRNAssState.put("c", "C");
1652     toRNAssState.put("D", "D");
1653     toRNAssState.put("d", "D");
1654     toRNAssState.put("E", "E");
1655     toRNAssState.put("e", "E");
1656     toRNAssState.put("F", "F");
1657     toRNAssState.put("f", "F");
1658     toRNAssState.put("G", "G");
1659     toRNAssState.put("g", "G");
1660     toRNAssState.put("H", "H");
1661     toRNAssState.put("h", "H");
1662     toRNAssState.put("I", "I");
1663     toRNAssState.put("i", "I");
1664     toRNAssState.put("J", "J");
1665     toRNAssState.put("j", "J");
1666     toRNAssState.put("K", "K");
1667     toRNAssState.put("k", "K");
1668     toRNAssState.put("L", "L");
1669     toRNAssState.put("l", "L");
1670     toRNAssState.put("M", "M");
1671     toRNAssState.put("m", "M");
1672     toRNAssState.put("N", "N");
1673     toRNAssState.put("n", "N");
1674     toRNAssState.put("O", "O");
1675     toRNAssState.put("o", "O");
1676     toRNAssState.put("P", "P");
1677     toRNAssState.put("p", "P");
1678     toRNAssState.put("Q", "Q");
1679     toRNAssState.put("q", "Q");
1680     toRNAssState.put("R", "R");
1681     toRNAssState.put("r", "R");
1682     toRNAssState.put("S", "S");
1683     toRNAssState.put("s", "S");
1684     toRNAssState.put("T", "T");
1685     toRNAssState.put("t", "T");
1686     toRNAssState.put("U", "U");
1687     toRNAssState.put("u", "U");
1688     toRNAssState.put("V", "V");
1689     toRNAssState.put("v", "V");
1690     toRNAssState.put("W", "W");
1691     toRNAssState.put("w", "W");
1692     toRNAssState.put("X", "X");
1693     toRNAssState.put("x", "X");
1694     toRNAssState.put("Y", "Y");
1695     toRNAssState.put("y", "Y");
1696     toRNAssState.put("Z", "Z");
1697     toRNAssState.put("z", "Z");
1698     for (int p = 0; p < RNAcloseParen.length; p++)
1699     {
1700       RNAcloseParen[p] = false;
1701     }
1702     for (String k : toRNAssState.keySet())
1703     {
1704       RNAcloseParen[k.charAt(0)] = k.charAt(0) != toRNAssState.get(k)
1705               .charAt(0);
1706     }
1707   }
1708
1709   static
1710   {
1711     modifications.put("MSE", "MET"); // Selenomethionine
1712     // the rest tbc; from
1713     // http://sourceforge.net/p/jmol/mailman/message/12833570/
1714     // modifications.put("CSE", "CYS"); // Selenocysteine
1715     // modifications.put("PTR", "TYR"); // Phosphotyrosine
1716     // modifications.put("SEP", "SER"); // Phosphoserine
1717     // modifications.put("HYP", "PRO"); // 4-hydroxyproline
1718     // modifications.put("5HP", "GLU"); // Pyroglutamic acid; 5-hydroxyproline
1719     // modifications.put("PCA", "GLU"); // Pyroglutamic acid
1720     // modifications.put("LYZ", "LYS"); // 5-hydroxylysine
1721   }
1722
1723   public static String getCanonicalAminoAcid(String aa)
1724   {
1725     String canonical = modifications.get(aa);
1726     return canonical == null ? aa : canonical;
1727   }
1728
1729   /**
1730    * translate to RNA secondary structure representation
1731    * 
1732    * @param ssstring
1733    * @return ssstring as a RNA-state secondary structure assignment.
1734    */
1735   public static String getRNASecStrucState(String ssstring)
1736   {
1737     if (ssstring == null)
1738     {
1739       return null;
1740     }
1741     StringBuffer ss = new StringBuffer();
1742     for (int i = 0; i < ssstring.length(); i++)
1743     {
1744       String ssc = ssstring.substring(i, i + 1);
1745       if (toRNAssState.containsKey(ssc))
1746       {
1747         // valid ss character - so return it
1748         ss.append(ssc); // (String) toRNAssState.get(ssc));
1749       }
1750       else
1751       {
1752         ss.append(" ");
1753       }
1754     }
1755     return ss.toString();
1756   }
1757
1758   public static boolean isCloseParenRNA(char dc)
1759   {
1760     return RNAcloseParen[dc];
1761   }
1762
1763   // main method generates perl representation of residue property hash
1764   // / cut here
1765   public static void main(String[] args)
1766   {
1767     Hashtable aa = new Hashtable();
1768     System.out.println("my %aa = {");
1769     // invert property hashes
1770     Enumeration prop = propHash.keys();
1771     while (prop.hasMoreElements())
1772     {
1773       String pname = (String) prop.nextElement();
1774       Hashtable phash = (Hashtable) propHash.get(pname);
1775       Enumeration res = phash.keys();
1776       while (res.hasMoreElements())
1777       {
1778         String rname = (String) res.nextElement();
1779         Vector aprops = (Vector) aa.get(rname);
1780         if (aprops == null)
1781         {
1782           aprops = new Vector();
1783           aa.put(rname, aprops);
1784         }
1785         Integer hasprop = (Integer) phash.get(rname);
1786         if (hasprop.intValue() == 1)
1787         {
1788           aprops.addElement(pname);
1789         }
1790       }
1791     }
1792     Enumeration res = aa.keys();
1793     while (res.hasMoreElements())
1794     {
1795       String rname = (String) res.nextElement();
1796
1797       System.out.print("'" + rname + "' => [");
1798       Enumeration props = ((Vector) aa.get(rname)).elements();
1799       while (props.hasMoreElements())
1800       {
1801         System.out.print("'" + (String) props.nextElement() + "'");
1802         if (props.hasMoreElements())
1803         {
1804           System.out.println(", ");
1805         }
1806       }
1807       System.out.println("]" + (res.hasMoreElements() ? "," : ""));
1808     }
1809     System.out.println("};");
1810   }
1811   // to here
1812
1813   /**
1814    * Returns a list of residue characters for the specified inputs
1815    * 
1816    * @param nucleotide
1817    * @param includeAmbiguous
1818    * @return
1819    */
1820   public static List<String> getResidues(boolean nucleotide,
1821           boolean includeAmbiguous)
1822   {
1823     List<String> result = new ArrayList<String>();
1824     if (nucleotide)
1825     {
1826       for (String nuc : nucleotideName.keySet())
1827       {
1828         int val = nucleotideIndex[nuc.charAt(0)];
1829         if ((!includeAmbiguous && val > 4) || (val >= maxNucleotideIndex))
1830         {
1831           continue;
1832         }
1833         nuc = nuc.toUpperCase();
1834         if (!result.contains(nuc))
1835         {
1836           result.add(nuc);
1837         }
1838       }
1839     } else {
1840       /*
1841        * Peptide
1842        */
1843       for (String res : aa3Hash.keySet())
1844       {
1845         int index = aa3Hash.get(res).intValue();
1846         if ((!includeAmbiguous && index >= 20) || index >= maxProteinIndex)
1847         {
1848           continue;
1849         }
1850         res = res.toUpperCase();
1851         if (!result.contains(res))
1852         {
1853           result.add(res);
1854         }
1855       }
1856     }
1857
1858     return result;
1859   }
1860
1861   /**
1862    * Returns the single letter code for a three letter code, or '0' if not known
1863    * 
1864    * @param threeLetterCode
1865    *          not case sensitive
1866    * @return
1867    */
1868   public static char getSingleCharacterCode(String threeLetterCode)
1869   {
1870     if (threeLetterCode == null)
1871     {
1872       return '0';
1873     }
1874     Integer index = ResidueProperties.aa3Hash.get(threeLetterCode
1875             .toUpperCase());
1876     return index == null ? '0' : aa[index].charAt(0);
1877   }
1878 }