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