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