Merge remote-tracking branch 'origin/develop' into
[jalview.git] / src / jalview / schemes / ResidueProperties.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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 Hashtable codonHash = new Hashtable();
637
638   public static Vector Lys = new Vector();
639
640   public static Vector Asn = new Vector();
641
642   public static Vector Gln = new Vector();
643
644   public static Vector His = new Vector();
645
646   public static Vector Glu = new Vector();
647
648   public static Vector Asp = new Vector();
649
650   public static Vector Tyr = new Vector();
651
652   public static Vector Thr = new Vector();
653
654   public static Vector Pro = new Vector();
655
656   public static Vector Ala = new Vector();
657
658   public static Vector Ser = new Vector();
659
660   public static Vector Arg = new Vector();
661
662   public static Vector Gly = new Vector();
663
664   public static Vector Trp = new Vector();
665
666   public static Vector Cys = new Vector();
667
668   public static Vector Ile = new Vector();
669
670   public static Vector Met = new Vector();
671
672   public static Vector Leu = new Vector();
673
674   public static Vector Val = new Vector();
675
676   public static Vector Phe = new Vector();
677
678   public static Vector STOP = new Vector();
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 Hashtable<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      * 3.2. Purine (adenine or guanine): R
727      * 
728      * R is the symbol previously recommended [1].
729      */
730     ambiguityCodes.put("R", new String[]
731     { "A", "G" });
732
733     /**
734      * 3.3. Pyrimidine (thymine or cytosine): Y
735      * 
736      * Y is the symbol previously recommended [1].
737      */
738     ambiguityCodes.put("Y", new String[]
739     { "T", "C" });
740     /**
741      * 3.4. Adenine or thymine: W
742      * 
743      * Although several diverse symbols have been used for this pair, (and for
744      * the reciprocal pair G+C), only two symbols have a rational basis, L and
745      * W: L derives from DNA density (light; G+C - heavy - would thus be H); W
746      * derives from the strength of the hydrogen bonding interaction between the
747      * base pairs (weak for A+T: G +C - strong - would thus be S). However, the
748      * system recommended for the three-base series (not-A = B, etc., see below,
749      * section 3.8) rules out H as this would be not-G. W is thus recommended.
750      */
751     ambiguityCodes.put("W", new String[]
752     { "A", "T" });
753     /**
754      * 3.5. Guanine or cytosine: S
755      * 
756      * The choice of this symbol is discussed above in section 3.4.
757      */
758     ambiguityCodes.put("S", new String[]
759     { "G", "C" });
760     /**
761      * 3.6. Adenine or cytosine: M
762      * 
763      * There are few common features between A and C. The presence of an NH2
764      * group in similar positions on both bases (Fig. 1) makes possible a
765      * logically derived symbol. A and N being ruled out, M (from aMino) is
766      * recommended.
767      * 
768      * 
769      * Fig. 1. Origin of the symbols M and K The four bases are drawn so as to
770      * show the relationship between adenine and cytosine on the one hand, which
771      * both have aMino groups at the ring position most distant from the point
772      * of attachment to the sugar, and between guanine and thymine on the other,
773      * which both have Keto groups at the corresponding position. The ring atoms
774      * are numbered as recommended [24-26], although for the present purpose
775      * this has the disadvantage of giving discordant numbers to the
776      * corresponding positions.
777      */
778     ambiguityCodes.put("M", new String[]
779     { "A", "C" });
780     /**
781      * 3.7. Guanine or thymine: K By analogy with A and C (section 3.6), both G
782      * and T have Keto groups in similar positions (Fig. 1).
783      */
784     ambiguityCodes.put("K", new String[]
785     { "G", "T" });
786     /**
787      * 3.8. Adenine or thymine or cytosine: H
788      * 
789      * Not-G is the most simple means of memorising this combination and symbols
790      * logically related to G were examined. F and H would both be suitable, as
791      * the letters before and after G in the alphabet, but A would have no
792      * equivalent to F. The use of H has historical precedence [2].
793      */
794     ambiguityCodes.put("H", new String[]
795     { "A", "T", "C" });
796     /**
797      * 3.9. Guanine or cytosine or thymine: B
798      * 
799      * Not-A as above (section 3.8).
800      */
801     ambiguityCodes.put("B", new String[]
802     { "G", "T", "C" });
803     /**
804      * 3.10. Guanine or adenine or cytosine: V
805      * 
806      * Not-T by analogy with not-G (section 3.8) would be U but this is ruled
807      * out to eliminate confusion with uracil. V is the next logical choice.
808      * Note that T and U may in some cases be considered to be synonyms.
809      */
810     ambiguityCodes.put("V", new String[]
811     { "G", "A", "C" });
812     /**
813      * 3.11. Guanine or adenine or thymine: D
814      * 
815      * Not-C as above (section 3.8).
816      */
817     ambiguityCodes.put("D", new String[]
818     { "G", "A", "T" });
819     /**
820      * 3.12. Guanine or adenine or thymine or cytosine: N
821      */
822     ambiguityCodes.put("R", new String[]
823     { "G", "A", "T", "C" });
824     // Now build codon translation table
825     codonHash2.put("AAA", "K");
826     codonHash2.put("AAG", "K");
827     codonHash2.put("AAC", "N");
828     codonHash2.put("AAT", "N");
829
830     codonHash2.put("CAA", "Q");
831     codonHash2.put("CAG", "Q");
832     codonHash2.put("CAC", "H");
833     codonHash2.put("CAT", "H");
834
835     codonHash2.put("GAA", "E");
836     codonHash2.put("GAG", "E");
837     codonHash2.put("GAC", "D");
838     codonHash2.put("GAT", "D");
839
840     codonHash2.put("TAC", "Y");
841     codonHash2.put("TAT", "Y");
842
843     codonHash2.put("ACA", "T");
844     codonHash2.put("ACC", "T");
845     codonHash2.put("ACT", "T");
846     codonHash2.put("ACG", "T");
847
848     codonHash2.put("CCA", "P");
849     codonHash2.put("CCG", "P");
850     codonHash2.put("CCC", "P");
851     codonHash2.put("CCT", "P");
852
853     codonHash2.put("GCA", "A");
854     codonHash2.put("GCG", "A");
855     codonHash2.put("GCC", "A");
856     codonHash2.put("GCT", "A");
857
858     codonHash2.put("TCA", "S");
859     codonHash2.put("TCG", "S");
860     codonHash2.put("TCC", "S");
861     codonHash2.put("TCT", "S");
862     codonHash2.put("AGC", "S");
863     codonHash2.put("AGT", "S");
864
865     codonHash2.put("AGA", "R");
866     codonHash2.put("AGG", "R");
867     codonHash2.put("CGA", "R");
868     codonHash2.put("CGG", "R");
869     codonHash2.put("CGC", "R");
870     codonHash2.put("CGT", "R");
871
872     codonHash2.put("GGA", "G");
873     codonHash2.put("GGG", "G");
874     codonHash2.put("GGC", "G");
875     codonHash2.put("GGT", "G");
876
877     codonHash2.put("TGA", "*");
878     codonHash2.put("TAA", "*");
879     codonHash2.put("TAG", "*");
880
881     codonHash2.put("TGG", "W");
882
883     codonHash2.put("TGC", "C");
884     codonHash2.put("TGT", "C");
885
886     codonHash2.put("ATA", "I");
887     codonHash2.put("ATC", "I");
888     codonHash2.put("ATT", "I");
889
890     codonHash2.put("ATG", "M");
891
892     codonHash2.put("CTA", "L");
893     codonHash2.put("CTG", "L");
894     codonHash2.put("CTC", "L");
895     codonHash2.put("CTT", "L");
896     codonHash2.put("TTA", "L");
897     codonHash2.put("TTG", "L");
898
899     codonHash2.put("GTA", "V");
900     codonHash2.put("GTG", "V");
901     codonHash2.put("GTC", "V");
902     codonHash2.put("GTT", "V");
903
904     codonHash2.put("TTC", "F");
905     codonHash2.put("TTT", "F");
906
907     buildAmbiguityCodonSet();
908   }
909
910   /**
911    * programmatic generation of codons including ambiguity codes
912    */
913   public static void buildAmbiguityCodonSet()
914   {
915     if (_ambiguityCodes.size() > 0)
916     {
917       System.err
918               .println("Ignoring multiple calls to buildAmbiguityCodonSet");
919       return;
920     }
921     // Invert the ambiguity code set
922     for (Map.Entry<String, String[]> acode : ambiguityCodes.entrySet())
923     {
924       for (String r : acode.getValue())
925       {
926         List<String> codesfor = _ambiguityCodes.get(r);
927         if (codesfor == null)
928         {
929           _ambiguityCodes.put(r, codesfor = new ArrayList<String>());
930         }
931         if (!codesfor.contains(acode.getKey()))
932         {
933           codesfor.add(acode.getKey());
934         }
935         else
936         {
937           System.err
938                   .println("Inconsistency in the IUBMB ambiguity code nomenclature table: collision for "
939                           + acode.getKey() + " in residue " + r);
940         }
941       }
942     }
943     // and programmatically add in the ambiguity codes that yield the same amino
944     // acid
945     String[] unambcodons = codonHash2.keySet().toArray(
946             new String[codonHash2.size()]);
947     for (String codon : unambcodons)
948     {
949       String residue = codonHash2.get(codon);
950       String acodon[][] = new String[codon.length()][];
951       for (int i = 0, iSize = codon.length(); i < iSize; i++)
952       {
953         String _ac = "" + codon.charAt(i);
954         List<String> acodes = _ambiguityCodes.get(_ac);
955         if (acodes != null)
956         {
957           acodon[i] = acodes.toArray(new String[acodes.size()]);
958         }
959         else
960         {
961           acodon[i] = new String[]
962           {};
963         }
964       }
965       // enumerate all combinations and test for veracity of translation
966       int tpos[] = new int[codon.length()], cpos[] = new int[codon.length()];
967       for (int i = 0; i < tpos.length; i++)
968       {
969         tpos[i] = -1;
970       }
971       tpos[acodon.length - 1] = 0;
972       int ipos, j;
973       while (tpos[0] < acodon[0].length)
974       {
975         // make all codons for this combination
976         char allres[][] = new char[tpos.length][];
977         String _acodon = "";
978         char _anuc;
979         for (ipos = 0; ipos < tpos.length; ipos++)
980         {
981           if (acodon[ipos].length == 0 || tpos[ipos] < 0)
982           {
983             _acodon += codon.charAt(ipos);
984             allres[ipos] = new char[]
985             { codon.charAt(ipos) };
986           }
987           else
988           {
989             _acodon += acodon[ipos][tpos[ipos]];
990             String[] altbase = ambiguityCodes.get(acodon[ipos][tpos[ipos]]);
991             allres[ipos] = new char[altbase.length];
992             j = 0;
993             for (String ab : altbase)
994             {
995               allres[ipos][j++] = ab.charAt(0);
996             }
997           }
998         }
999         // test all codons for this combination
1000         for (ipos = 0; ipos < cpos.length; ipos++)
1001         {
1002           cpos[ipos] = 0;
1003         }
1004         boolean valid = true;
1005         do
1006         {
1007           String _codon = "";
1008           for (j = 0; j < cpos.length; j++)
1009           {
1010             _codon += allres[j][cpos[j]];
1011           }
1012           String tr = codonHash2.get(_codon);
1013           if (valid = (tr != null && tr.equals(residue)))
1014           {
1015             // advance to next combination
1016             ipos = acodon.length - 1;
1017             while (++cpos[ipos] >= allres[ipos].length && ipos > 0)
1018             {
1019               cpos[ipos] = 0;
1020               ipos--;
1021             }
1022           }
1023         } while (valid && cpos[0] < allres[0].length);
1024         if (valid)
1025         {
1026           // Add this to the set of codons we will translate
1027           // System.out.println("Adding ambiguity codon: " + _acodon + " for "
1028           // + residue);
1029           codonHash2.put(_acodon, residue);
1030         }
1031         else
1032         {
1033           // System.err.println("Rejecting ambiguity codon: " + _acodon
1034           // + " for " + residue);
1035         }
1036         // next combination
1037         ipos = acodon.length - 1;
1038         while (++tpos[ipos] >= acodon[ipos].length && ipos > 0)
1039         {
1040           tpos[ipos] = -1;
1041           ipos--;
1042         }
1043       }
1044     }
1045
1046   }
1047
1048   static
1049   {
1050     Lys.addElement("AAA");
1051     Lys.addElement("AAG");
1052     Asn.addElement("AAC");
1053     Asn.addElement("AAT");
1054
1055     Gln.addElement("CAA");
1056     Gln.addElement("CAG");
1057     His.addElement("CAC");
1058     His.addElement("CAT");
1059
1060     Glu.addElement("GAA");
1061     Glu.addElement("GAG");
1062     Asp.addElement("GAC");
1063     Asp.addElement("GAT");
1064
1065     Tyr.addElement("TAC");
1066     Tyr.addElement("TAT");
1067
1068     Thr.addElement("ACA");
1069     Thr.addElement("ACG");
1070     Thr.addElement("ACC");
1071     Thr.addElement("ACT");
1072
1073     Pro.addElement("CCA");
1074     Pro.addElement("CCG");
1075     Pro.addElement("CCC");
1076     Pro.addElement("CCT");
1077
1078     Ala.addElement("GCA");
1079     Ala.addElement("GCG");
1080     Ala.addElement("GCC");
1081     Ala.addElement("GCT");
1082
1083     Ser.addElement("TCA");
1084     Ser.addElement("TCG");
1085     Ser.addElement("TCC");
1086     Ser.addElement("TCT");
1087     Ser.addElement("AGC");
1088     Ser.addElement("AGT");
1089
1090     Arg.addElement("AGA");
1091     Arg.addElement("AGG");
1092     Arg.addElement("CGA");
1093     Arg.addElement("CGG");
1094     Arg.addElement("CGC");
1095     Arg.addElement("CGT");
1096
1097     Gly.addElement("GGA");
1098     Gly.addElement("GGG");
1099     Gly.addElement("GGC");
1100     Gly.addElement("GGT");
1101
1102     STOP.addElement("TGA");
1103     STOP.addElement("TAA");
1104     STOP.addElement("TAG");
1105
1106     Trp.addElement("TGG");
1107
1108     Cys.addElement("TGC");
1109     Cys.addElement("TGT");
1110
1111     Ile.addElement("ATA");
1112     Ile.addElement("ATC");
1113     Ile.addElement("ATT");
1114
1115     Met.addElement("ATG");
1116
1117     Leu.addElement("CTA");
1118     Leu.addElement("CTG");
1119     Leu.addElement("CTC");
1120     Leu.addElement("CTT");
1121     Leu.addElement("TTA");
1122     Leu.addElement("TTG");
1123
1124     Val.addElement("GTA");
1125     Val.addElement("GTG");
1126     Val.addElement("GTC");
1127     Val.addElement("GTT");
1128
1129     Phe.addElement("TTC");
1130     Phe.addElement("TTT");
1131   }
1132
1133   // Stores residue codes/names and colours and other things
1134   public static Hashtable propHash = new Hashtable();
1135
1136   public static Hashtable hydrophobic = new Hashtable();
1137
1138   public static Hashtable polar = new Hashtable();
1139
1140   public static Hashtable small = new Hashtable();
1141
1142   public static Hashtable positive = new Hashtable();
1143
1144   public static Hashtable negative = new Hashtable();
1145
1146   public static Hashtable charged = new Hashtable();
1147
1148   public static Hashtable aromatic = new Hashtable();
1149
1150   public static Hashtable aliphatic = new Hashtable();
1151
1152   public static Hashtable tiny = new Hashtable();
1153
1154   public static Hashtable proline = new Hashtable();
1155
1156   static
1157   {
1158     hydrophobic.put("I", new Integer(1));
1159     hydrophobic.put("L", new Integer(1));
1160     hydrophobic.put("V", new Integer(1));
1161     hydrophobic.put("C", new Integer(1));
1162     hydrophobic.put("A", new Integer(1));
1163     hydrophobic.put("G", new Integer(1));
1164     hydrophobic.put("M", new Integer(1));
1165     hydrophobic.put("F", new Integer(1));
1166     hydrophobic.put("Y", new Integer(1));
1167     hydrophobic.put("W", new Integer(1));
1168     hydrophobic.put("H", new Integer(1));
1169     hydrophobic.put("K", new Integer(1));
1170     hydrophobic.put("X", new Integer(1));
1171     hydrophobic.put("-", new Integer(1));
1172     hydrophobic.put("*", new Integer(1));
1173     hydrophobic.put("R", new Integer(0));
1174     hydrophobic.put("E", new Integer(0));
1175     hydrophobic.put("Q", new Integer(0));
1176     hydrophobic.put("D", new Integer(0));
1177     hydrophobic.put("N", new Integer(0));
1178     hydrophobic.put("S", new Integer(0));
1179     hydrophobic.put("T", new Integer(0));
1180     hydrophobic.put("P", new Integer(0));
1181   }
1182
1183   static
1184   {
1185     polar.put("Y", new Integer(1));
1186     polar.put("W", new Integer(1));
1187     polar.put("H", new Integer(1));
1188     polar.put("K", new Integer(1));
1189     polar.put("R", new Integer(1));
1190     polar.put("E", new Integer(1));
1191     polar.put("Q", new Integer(1));
1192     polar.put("D", new Integer(1));
1193     polar.put("N", new Integer(1));
1194     polar.put("S", new Integer(1));
1195     polar.put("T", new Integer(1));
1196     polar.put("X", new Integer(1));
1197     polar.put("-", new Integer(1));
1198     polar.put("*", new Integer(1));
1199     polar.put("I", new Integer(0));
1200     polar.put("L", new Integer(0));
1201     polar.put("V", new Integer(0));
1202     polar.put("C", new Integer(0));
1203     polar.put("A", new Integer(0));
1204     polar.put("G", new Integer(0));
1205     polar.put("M", new Integer(0));
1206     polar.put("F", new Integer(0));
1207     polar.put("P", new Integer(0));
1208   }
1209
1210   static
1211   {
1212     small.put("I", new Integer(0));
1213     small.put("L", new Integer(0));
1214     small.put("V", new Integer(1));
1215     small.put("C", new Integer(1));
1216     small.put("A", new Integer(1));
1217     small.put("G", new Integer(1));
1218     small.put("M", new Integer(0));
1219     small.put("F", new Integer(0));
1220     small.put("Y", new Integer(0));
1221     small.put("W", new Integer(0));
1222     small.put("H", new Integer(0));
1223     small.put("K", new Integer(0));
1224     small.put("R", new Integer(0));
1225     small.put("E", new Integer(0));
1226     small.put("Q", new Integer(0));
1227     small.put("D", new Integer(1));
1228     small.put("N", new Integer(1));
1229     small.put("S", new Integer(1));
1230     small.put("T", new Integer(1));
1231     small.put("P", new Integer(1));
1232     small.put("-", new Integer(1));
1233     small.put("*", new Integer(1));
1234   }
1235
1236   static
1237   {
1238     positive.put("I", new Integer(0));
1239     positive.put("L", new Integer(0));
1240     positive.put("V", new Integer(0));
1241     positive.put("C", new Integer(0));
1242     positive.put("A", new Integer(0));
1243     positive.put("G", new Integer(0));
1244     positive.put("M", new Integer(0));
1245     positive.put("F", new Integer(0));
1246     positive.put("Y", new Integer(0));
1247     positive.put("W", new Integer(0));
1248     positive.put("H", new Integer(1));
1249     positive.put("K", new Integer(1));
1250     positive.put("R", new Integer(1));
1251     positive.put("E", new Integer(0));
1252     positive.put("Q", new Integer(0));
1253     positive.put("D", new Integer(0));
1254     positive.put("N", new Integer(0));
1255     positive.put("S", new Integer(0));
1256     positive.put("T", new Integer(0));
1257     positive.put("P", new Integer(0));
1258     positive.put("-", new Integer(1));
1259     positive.put("*", new Integer(1));
1260   }
1261
1262   static
1263   {
1264     negative.put("I", new Integer(0));
1265     negative.put("L", new Integer(0));
1266     negative.put("V", new Integer(0));
1267     negative.put("C", new Integer(0));
1268     negative.put("A", new Integer(0));
1269     negative.put("G", new Integer(0));
1270     negative.put("M", new Integer(0));
1271     negative.put("F", new Integer(0));
1272     negative.put("Y", new Integer(0));
1273     negative.put("W", new Integer(0));
1274     negative.put("H", new Integer(0));
1275     negative.put("K", new Integer(0));
1276     negative.put("R", new Integer(0));
1277     negative.put("E", new Integer(1));
1278     negative.put("Q", new Integer(0));
1279     negative.put("D", new Integer(1));
1280     negative.put("N", new Integer(0));
1281     negative.put("S", new Integer(0));
1282     negative.put("T", new Integer(0));
1283     negative.put("P", new Integer(0));
1284     negative.put("-", new Integer(1));
1285     negative.put("*", new Integer(1));
1286   }
1287
1288   static
1289   {
1290     charged.put("I", new Integer(0));
1291     charged.put("L", new Integer(0));
1292     charged.put("V", new Integer(0));
1293     charged.put("C", new Integer(0));
1294     charged.put("A", new Integer(0));
1295     charged.put("G", new Integer(0));
1296     charged.put("M", new Integer(0));
1297     charged.put("F", new Integer(0));
1298     charged.put("Y", new Integer(0));
1299     charged.put("W", new Integer(0));
1300     charged.put("H", new Integer(1));
1301     charged.put("K", new Integer(1));
1302     charged.put("R", new Integer(1));
1303     charged.put("E", new Integer(1));
1304     charged.put("Q", new Integer(0));
1305     charged.put("D", new Integer(1));
1306     charged.put("N", new Integer(0)); // Asparagine is polar but not charged.
1307                                       // Alternative would be charged and
1308                                       // negative (in basic form)?
1309     charged.put("S", new Integer(0));
1310     charged.put("T", new Integer(0));
1311     charged.put("P", new Integer(0));
1312     charged.put("-", new Integer(1));
1313     charged.put("*", new Integer(1));
1314   }
1315
1316   static
1317   {
1318     aromatic.put("I", new Integer(0));
1319     aromatic.put("L", new Integer(0));
1320     aromatic.put("V", new Integer(0));
1321     aromatic.put("C", new Integer(0));
1322     aromatic.put("A", new Integer(0));
1323     aromatic.put("G", new Integer(0));
1324     aromatic.put("M", new Integer(0));
1325     aromatic.put("F", new Integer(1));
1326     aromatic.put("Y", new Integer(1));
1327     aromatic.put("W", new Integer(1));
1328     aromatic.put("H", new Integer(1));
1329     aromatic.put("K", new Integer(0));
1330     aromatic.put("R", new Integer(0));
1331     aromatic.put("E", new Integer(0));
1332     aromatic.put("Q", new Integer(0));
1333     aromatic.put("D", new Integer(0));
1334     aromatic.put("N", new Integer(0));
1335     aromatic.put("S", new Integer(0));
1336     aromatic.put("T", new Integer(0));
1337     aromatic.put("P", new Integer(0));
1338     aromatic.put("-", new Integer(1));
1339     aromatic.put("*", new Integer(1));
1340   }
1341
1342   static
1343   {
1344     aliphatic.put("I", new Integer(1));
1345     aliphatic.put("L", new Integer(1));
1346     aliphatic.put("V", new Integer(1));
1347     aliphatic.put("C", new Integer(0));
1348     aliphatic.put("A", new Integer(0));
1349     aliphatic.put("G", new Integer(0));
1350     aliphatic.put("M", new Integer(0));
1351     aliphatic.put("F", new Integer(0));
1352     aliphatic.put("Y", new Integer(0));
1353     aliphatic.put("W", new Integer(0));
1354     aliphatic.put("H", new Integer(0));
1355     aliphatic.put("K", new Integer(0));
1356     aliphatic.put("R", new Integer(0));
1357     aliphatic.put("E", new Integer(0));
1358     aliphatic.put("Q", new Integer(0));
1359     aliphatic.put("D", new Integer(0));
1360     aliphatic.put("N", new Integer(0));
1361     aliphatic.put("S", new Integer(0));
1362     aliphatic.put("T", new Integer(0));
1363     aliphatic.put("P", new Integer(0));
1364     aliphatic.put("-", new Integer(1));
1365     aliphatic.put("*", new Integer(1));
1366   }
1367
1368   static
1369   {
1370     tiny.put("I", new Integer(0));
1371     tiny.put("L", new Integer(0));
1372     tiny.put("V", new Integer(0));
1373     tiny.put("C", new Integer(0));
1374     tiny.put("A", new Integer(1));
1375     tiny.put("G", new Integer(1));
1376     tiny.put("M", new Integer(0));
1377     tiny.put("F", new Integer(0));
1378     tiny.put("Y", new Integer(0));
1379     tiny.put("W", new Integer(0));
1380     tiny.put("H", new Integer(0));
1381     tiny.put("K", new Integer(0));
1382     tiny.put("R", new Integer(0));
1383     tiny.put("E", new Integer(0));
1384     tiny.put("Q", new Integer(0));
1385     tiny.put("D", new Integer(0));
1386     tiny.put("N", new Integer(0));
1387     tiny.put("S", new Integer(1));
1388     tiny.put("T", new Integer(0));
1389     tiny.put("P", new Integer(0));
1390     tiny.put("-", new Integer(1));
1391     tiny.put("*", new Integer(1));
1392   }
1393
1394   static
1395   {
1396     proline.put("I", new Integer(0));
1397     proline.put("L", new Integer(0));
1398     proline.put("V", new Integer(0));
1399     proline.put("C", new Integer(0));
1400     proline.put("A", new Integer(0));
1401     proline.put("G", new Integer(0));
1402     proline.put("M", new Integer(0));
1403     proline.put("F", new Integer(0));
1404     proline.put("Y", new Integer(0));
1405     proline.put("W", new Integer(0));
1406     proline.put("H", new Integer(0));
1407     proline.put("K", new Integer(0));
1408     proline.put("R", new Integer(0));
1409     proline.put("E", new Integer(0));
1410     proline.put("Q", new Integer(0));
1411     proline.put("D", new Integer(0));
1412     proline.put("N", new Integer(0));
1413     proline.put("S", new Integer(0));
1414     proline.put("T", new Integer(0));
1415     proline.put("P", new Integer(1));
1416     proline.put("-", new Integer(1));
1417     proline.put("*", new Integer(1));
1418   }
1419
1420   static
1421   {
1422     propHash.put("hydrophobic", hydrophobic);
1423     propHash.put("small", small);
1424     propHash.put("positive", positive);
1425     propHash.put("negative", negative);
1426     propHash.put("charged", charged);
1427     propHash.put("aromatic", aromatic);
1428     propHash.put("aliphatic", aliphatic);
1429     propHash.put("tiny", tiny);
1430     propHash.put("proline", proline);
1431     propHash.put("polar", polar);
1432   }
1433   static
1434   {
1435     int[][] propMatrixF = new int[maxProteinIndex][maxProteinIndex], propMatrixPos = new int[maxProteinIndex][maxProteinIndex], propMatrixEpos = new int[maxProteinIndex][maxProteinIndex];
1436     for (int i = 0; i < maxProteinIndex; i++)
1437     {
1438       int maxF = 0, maxP = 0, maxEP = 0;
1439       String ic = "";
1440       if (aa.length > i)
1441       {
1442         ic += aa[i];
1443       }
1444       else
1445       {
1446         ic = "-";
1447       }
1448       for (int j = i + 1; j < maxProteinIndex; j++)
1449       {
1450         String jc = "";
1451         if (aa.length > j)
1452         {
1453           jc += aa[j];
1454         }
1455         else
1456         {
1457           jc = "-";
1458         }
1459         propMatrixF[i][j] = 0;
1460         propMatrixPos[i][j] = 0;
1461         propMatrixEpos[i][j] = 0;
1462         for (Enumeration<String> en = propHash.keys(); en
1463                 .hasMoreElements();)
1464         {
1465           String ph = en.nextElement();
1466           Map<String, Integer> pph = (Map<String, Integer>) propHash
1467                   .get(ph);
1468           if (pph.get(ic) != null && pph.get(jc) != null)
1469           {
1470             int icp = pph.get(ic).intValue(), jcp = pph.get(jc).intValue();
1471             // Still working on these definitions.
1472             propMatrixPos[i][j] += icp == jcp && icp > 0 ? 2 : 0;
1473             propMatrixPos[j][i] += icp == jcp && icp > 0 ? 2 : 0;
1474             propMatrixF[i][j] += icp == jcp ? 2 : 0;
1475             propMatrixF[j][i] += icp == jcp ? 2 : 0;
1476             propMatrixEpos[i][j] += icp == jcp ? (1 + icp * 2) : 0;
1477             propMatrixEpos[j][i] += icp == jcp ? (1 + icp * 2) : 0;
1478           }
1479         }
1480         if (maxF < propMatrixF[i][j])
1481         {
1482           maxF = propMatrixF[i][j];
1483         }
1484         if (maxP < propMatrixPos[i][j])
1485         {
1486           maxP = propMatrixPos[i][j];
1487         }
1488         if (maxEP < propMatrixEpos[i][j])
1489         {
1490           maxEP = propMatrixEpos[i][j];
1491         }
1492       }
1493       propMatrixF[i][i] = maxF;
1494       propMatrixPos[i][i] = maxP;
1495       propMatrixEpos[i][i] = maxEP;
1496     }
1497     // JAL-1512 comment out physicochemical score matrices for 2.8.1 release
1498     // scoreMatrices.put("Conservation Pos", new
1499     // ScoreMatrix("Conservation Pos",propMatrixPos,0));
1500     // scoreMatrices.put("Conservation Both", new
1501     // ScoreMatrix("Conservation Both",propMatrixF,0));
1502     // scoreMatrices.put("Conservation EnhPos", new
1503     // ScoreMatrix("Conservation EnhPos",propMatrixEpos,0));
1504     scoreMatrices.put("PID", new PIDScoreModel());
1505     scoreMatrices.put("Displayed Features", new FeatureScoreModel());
1506   }
1507
1508   private ResidueProperties()
1509   {
1510   }
1511
1512   public static double getHydmax()
1513   {
1514     return hydmax;
1515   }
1516
1517   public static double getHydmin()
1518   {
1519     return hydmin;
1520   }
1521
1522   public static double[] getHyd()
1523   {
1524     return hyd;
1525   }
1526
1527   public static Map<String, Integer> getAA3Hash()
1528   {
1529     return aa3Hash;
1530   }
1531
1532   public static int[][] getDNA()
1533   {
1534     return ResidueProperties.DNA;
1535   }
1536
1537   public static int[][] getBLOSUM62()
1538   {
1539     return ResidueProperties.BLOSUM62;
1540   }
1541
1542   public static int getPAM250(String A1, String A2)
1543   {
1544     return getPAM250(A1.charAt(0), A2.charAt(0));
1545   }
1546
1547   public static int getBLOSUM62(char c1, char c2)
1548   {
1549     int pog = 0;
1550
1551     try
1552     {
1553       int a = aaIndex[c1];
1554       int b = aaIndex[c2];
1555
1556       pog = ResidueProperties.BLOSUM62[a][b];
1557     } catch (Exception e)
1558     {
1559       // System.out.println("Unknown residue in " + A1 + " " + A2);
1560     }
1561
1562     return pog;
1563   }
1564
1565   public static Vector getCodons(String res)
1566   {
1567     if (codonHash.containsKey(res))
1568     {
1569       return (Vector) codonHash.get(res);
1570     }
1571
1572     return null;
1573   }
1574
1575   public static String codonTranslate(String lccodon)
1576   {
1577     if (false)
1578     {
1579       return _codonTranslate(lccodon);
1580     }
1581     String cdn = codonHash2.get(lccodon.toUpperCase());
1582     if (cdn != null && cdn.equals("*"))
1583     {
1584       return "STOP";
1585     }
1586     return cdn;
1587   }
1588
1589   public static String _codonTranslate(String lccodon)
1590   {
1591     String codon = lccodon.toUpperCase();
1592     // all base ambiguity codes yield an 'X' amino acid residue
1593     if (codon.indexOf('X') > -1 || codon.indexOf('N') > -1)
1594     {
1595       return "X";
1596     }
1597     Enumeration e = codonHash.keys();
1598
1599     while (e.hasMoreElements())
1600     {
1601       String key = (String) e.nextElement();
1602       Vector tmp = (Vector) codonHash.get(key);
1603
1604       if (tmp.contains(codon))
1605       {
1606         return key;
1607       }
1608     }
1609
1610     return null;
1611   }
1612
1613   public static int[][] getDefaultPeptideMatrix()
1614   {
1615     return ResidueProperties.getBLOSUM62();
1616   }
1617
1618   public static int[][] getDefaultDnaMatrix()
1619   {
1620     return ResidueProperties.getDNA();
1621   }
1622
1623   /**
1624    * get a ScoreMatrix based on its string name
1625    * 
1626    * @param pwtype
1627    * @return matrix in scoreMatrices with key pwtype or null
1628    */
1629   public static ScoreMatrix getScoreMatrix(String pwtype)
1630   {
1631     Object val = scoreMatrices.get(pwtype);
1632     if (val != null && val instanceof ScoreMatrix)
1633     {
1634       return (ScoreMatrix) val;
1635     }
1636     return null;
1637   }
1638
1639   /**
1640    * get a ScoreModel based on its string name
1641    * 
1642    * @param pwtype
1643    * @return scoremodel of type pwtype or null
1644    */
1645   public static ScoreModelI getScoreModel(String pwtype)
1646   {
1647     return scoreMatrices.get(pwtype);
1648   }
1649
1650   public static int getPAM250(char c, char d)
1651   {
1652     int a = aaIndex[c];
1653     int b = aaIndex[d];
1654
1655     int pog = ResidueProperties.PAM250[a][b];
1656
1657     return pog;
1658   }
1659
1660   public static Hashtable toDssp3State;
1661   static
1662   {
1663     toDssp3State = new Hashtable();
1664     toDssp3State.put("H", "H");
1665     toDssp3State.put("E", "E");
1666     toDssp3State.put("C", " ");
1667     toDssp3State.put(" ", " ");
1668     toDssp3State.put("T", " ");
1669     toDssp3State.put("B", "E");
1670     toDssp3State.put("G", "H");
1671     toDssp3State.put("I", "H");
1672     toDssp3State.put("X", " ");
1673   }
1674
1675   /**
1676    * translate from other dssp secondary structure alphabets to 3-state
1677    * 
1678    * @param ssstring
1679    * @return ssstring as a three-state secondary structure assignment.
1680    */
1681   public static String getDssp3state(String ssstring)
1682   {
1683     if (ssstring == null)
1684     {
1685       return null;
1686     }
1687     StringBuffer ss = new StringBuffer();
1688     for (int i = 0; i < ssstring.length(); i++)
1689     {
1690       String ssc = ssstring.substring(i, i + 1);
1691       if (toDssp3State.containsKey(ssc))
1692       {
1693         ss.append((String) toDssp3State.get(ssc));
1694       }
1695       else
1696       {
1697         ss.append(" ");
1698       }
1699     }
1700     return ss.toString();
1701   }
1702
1703   /**
1704    * Used by getRNASecStrucState
1705    * 
1706    */
1707   public static Hashtable<String, String> toRNAssState;
1708
1709   public static boolean RNAcloseParen[] = new boolean[255];
1710   static
1711   {
1712     toRNAssState = new Hashtable<String, String>();
1713     toRNAssState.put(")", "(");
1714     toRNAssState.put("(", "(");
1715     toRNAssState.put("]", "[");
1716     toRNAssState.put("[", "[");
1717     toRNAssState.put("{", "{");
1718     toRNAssState.put("}", "{");
1719     toRNAssState.put(">", ">");
1720     toRNAssState.put("<", ">");
1721     toRNAssState.put("A", "A");
1722     toRNAssState.put("a", "A");
1723     toRNAssState.put("B", "B");
1724     toRNAssState.put("b", "B");
1725     toRNAssState.put("C", "C");
1726     toRNAssState.put("c", "C");
1727     toRNAssState.put("D", "D");
1728     toRNAssState.put("d", "D");
1729     toRNAssState.put("E", "E");
1730     toRNAssState.put("e", "E");
1731     toRNAssState.put("F", "F");
1732     toRNAssState.put("f", "F");
1733     toRNAssState.put("G", "G");
1734     toRNAssState.put("g", "G");
1735     toRNAssState.put("H", "H");
1736     toRNAssState.put("h", "H");
1737     toRNAssState.put("I", "I");
1738     toRNAssState.put("i", "I");
1739     toRNAssState.put("J", "J");
1740     toRNAssState.put("j", "J");
1741     toRNAssState.put("K", "K");
1742     toRNAssState.put("k", "K");
1743     toRNAssState.put("L", "L");
1744     toRNAssState.put("l", "L");
1745     toRNAssState.put("M", "M");
1746     toRNAssState.put("m", "M");
1747     toRNAssState.put("N", "N");
1748     toRNAssState.put("n", "N");
1749     toRNAssState.put("O", "O");
1750     toRNAssState.put("o", "O");
1751     toRNAssState.put("P", "P");
1752     toRNAssState.put("p", "P");
1753     toRNAssState.put("Q", "Q");
1754     toRNAssState.put("q", "Q");
1755     toRNAssState.put("R", "R");
1756     toRNAssState.put("r", "R");
1757     toRNAssState.put("S", "S");
1758     toRNAssState.put("s", "S");
1759     toRNAssState.put("T", "T");
1760     toRNAssState.put("t", "T");
1761     toRNAssState.put("U", "U");
1762     toRNAssState.put("u", "U");
1763     toRNAssState.put("V", "V");
1764     toRNAssState.put("v", "V");
1765     toRNAssState.put("W", "W");
1766     toRNAssState.put("w", "W");
1767     toRNAssState.put("X", "X");
1768     toRNAssState.put("x", "X");
1769     toRNAssState.put("Y", "Y");
1770     toRNAssState.put("y", "Y");
1771     toRNAssState.put("Z", "Z");
1772     toRNAssState.put("z", "Z");
1773     for (int p = 0; p < RNAcloseParen.length; p++)
1774     {
1775       RNAcloseParen[p] = false;
1776     }
1777     for (String k : toRNAssState.keySet())
1778     {
1779       RNAcloseParen[k.charAt(0)] = k.charAt(0) != toRNAssState.get(k)
1780               .charAt(0);
1781     }
1782   }
1783
1784   /**
1785    * translate to RNA secondary structure representation
1786    * 
1787    * @param ssstring
1788    * @return ssstring as a RNA-state secondary structure assignment.
1789    */
1790   public static String getRNASecStrucState(String ssstring)
1791   {
1792     if (ssstring == null)
1793     {
1794       return null;
1795     }
1796     StringBuffer ss = new StringBuffer();
1797     for (int i = 0; i < ssstring.length(); i++)
1798     {
1799       String ssc = ssstring.substring(i, i + 1);
1800       if (toRNAssState.containsKey(ssc))
1801       {
1802         // valid ss character - so return it
1803         ss.append(ssc); // (String) toRNAssState.get(ssc));
1804       }
1805       else
1806       {
1807         ss.append(" ");
1808       }
1809     }
1810     return ss.toString();
1811   }
1812
1813   public static boolean isCloseParenRNA(char dc)
1814   {
1815     return RNAcloseParen[dc];
1816   }
1817
1818   // main method generates perl representation of residue property hash
1819   // / cut here
1820   public static void main(String[] args)
1821   {
1822     Hashtable aa = new Hashtable();
1823     System.out.println("my %aa = {");
1824     // invert property hashes
1825     Enumeration prop = propHash.keys();
1826     while (prop.hasMoreElements())
1827     {
1828       String pname = (String) prop.nextElement();
1829       Hashtable phash = (Hashtable) propHash.get(pname);
1830       Enumeration res = phash.keys();
1831       while (res.hasMoreElements())
1832       {
1833         String rname = (String) res.nextElement();
1834         Vector aprops = (Vector) aa.get(rname);
1835         if (aprops == null)
1836         {
1837           aprops = new Vector();
1838           aa.put(rname, aprops);
1839         }
1840         Integer hasprop = (Integer) phash.get(rname);
1841         if (hasprop.intValue() == 1)
1842         {
1843           aprops.addElement(pname);
1844         }
1845       }
1846     }
1847     Enumeration res = aa.keys();
1848     while (res.hasMoreElements())
1849     {
1850       String rname = (String) res.nextElement();
1851
1852       System.out.print("'" + rname + "' => [");
1853       Enumeration props = ((Vector) aa.get(rname)).elements();
1854       while (props.hasMoreElements())
1855       {
1856         System.out.print("'" + (String) props.nextElement() + "'");
1857         if (props.hasMoreElements())
1858         {
1859           System.out.println(", ");
1860         }
1861       }
1862       System.out.println("]" + (res.hasMoreElements() ? "," : ""));
1863     }
1864     System.out.println("};");
1865   }
1866   // to here
1867
1868 }