static final strings for consensus keys
[jalview.git] / src / jalview / analysis / AAFrequency.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.analysis;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import java.util.*;\r
24 \r
25 \r
26 /**\r
27  * Takes in a vector or array of sequences and column start and column end\r
28  * and returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.\r
29  * This class is used extensively in calculating alignment colourschemes\r
30  * that depend on the amount of conservation in each alignment column.\r
31  * @author $author$\r
32  * @version $Revision$\r
33  */\r
34 public class AAFrequency\r
35 {\r
36   //No need to store 1000s of strings which are not\r
37   //visible to the user.\r
38   public static final String MAXCOUNT = "C";\r
39   public static final String MAXRESIDUE="R";\r
40   public static final String PID_GAPS = "G";\r
41   public static final String PID_NOGAPS="N";\r
42 \r
43    public static final Hashtable [] calculate(Vector sequences, int start, int end)\r
44    {\r
45      SequenceI [] seqs = new SequenceI[sequences.size()];\r
46      int width = 0;\r
47      for(int i=0; i<sequences.size(); i++)\r
48      {\r
49        seqs[i] = (SequenceI) sequences.elementAt(i);\r
50        if(seqs[i].getLength()>width)\r
51          width = seqs[i].getLength();\r
52      }\r
53 \r
54      Hashtable [] reply = new Hashtable[width];\r
55 \r
56      calculate(seqs, start, end, reply);\r
57 \r
58      return reply;\r
59    }\r
60 \r
61 public static final void calculate(SequenceI[] sequences,\r
62                                   int start, int end,\r
63                                   Hashtable [] result)\r
64 {\r
65   Hashtable residueHash;\r
66   int maxCount, nongap, i, j, v, jSize = sequences.length;\r
67   String maxResidue;\r
68   char c;\r
69   float percentage;\r
70 \r
71   int[] values = new int[132];\r
72 \r
73   String seq;\r
74 \r
75   for (i = start; i < end; i++)\r
76   {\r
77     residueHash = new Hashtable();\r
78     maxCount = 0;\r
79     maxResidue = "-";\r
80     nongap = 0;\r
81     values = new int[132];\r
82 \r
83     for (j = 0; j < jSize; j++)\r
84     {\r
85       seq = sequences[j].getSequence();\r
86       if (seq.length() > i)\r
87       {\r
88         c = seq.charAt(i);\r
89 \r
90         if(c == '.' || c==' ')\r
91           c = '-';\r
92 \r
93         if(c=='-')\r
94         {\r
95           values['-']++;\r
96           continue;\r
97         }\r
98         else if ('a' <= c && c <= 'z')\r
99         {\r
100           c -= 32 ;//('a' - 'A');\r
101         }\r
102 \r
103         nongap++;\r
104         values[c]++;\r
105 \r
106       }\r
107       else\r
108       {\r
109         values['-']++;\r
110       }\r
111     }\r
112 \r
113     for (v = 'A'; v < 'Z'; v++)\r
114     {\r
115       if (values[v] == 0 || values[v] < maxCount)\r
116         continue;\r
117 \r
118       if (values[v] > maxCount)\r
119       {\r
120         maxResidue = String.valueOf( (char) v);\r
121       }\r
122       else if (values[v] == maxCount)\r
123       {\r
124         maxResidue += String.valueOf( (char) v);\r
125       }\r
126       maxCount = values[v];\r
127     }\r
128 \r
129 \r
130     residueHash.put(MAXCOUNT, new Integer(maxCount));\r
131     residueHash.put(MAXRESIDUE, maxResidue);\r
132 \r
133     percentage = ( (float) maxCount * 100) / (float) jSize;\r
134     residueHash.put(PID_GAPS, new Float(percentage));\r
135 \r
136     percentage = ( (float) maxCount * 100) / (float) nongap;\r
137     residueHash.put(PID_NOGAPS, new Float(percentage));\r
138     result[i] = residueHash;\r
139   }\r
140 }\r
141 }\r
142 \r
143 \r