AAFrequency optimized
[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 of sequences and column start and column end\r
28  * and returns a vector of size (end-start+1). Each element of the\r
29  * vector contains a hashtable with the keys being residues and\r
30  * the values being the count of each residue in that column.\r
31  * This class is used extensively in calculating alignment colourschemes\r
32  * that depend on the amount of conservation in each alignment column.\r
33  * @author $author$\r
34  * @version $Revision$\r
35  */\r
36 public class AAFrequency\r
37 {\r
38     /** Takes in a !!ARRAY!! of sequences and column start and column end\r
39     * and fills given Vector of size (end-start+1). Each element of the\r
40     * vector contains a hashtable with the keys being residues and\r
41     * the values being the count of each residue in that column.\r
42     * This class is used extensively in calculating alignment colourschemes\r
43     * that depend on the amount of conservation in each alignment column. */\r
44 \r
45    public static final Hashtable [] calculate(Vector sequences, int start, int end)\r
46    {\r
47      SequenceI [] seqs = new SequenceI[sequences.size()];\r
48      int width = 0;\r
49      for(int i=0; i<sequences.size(); i++)\r
50      {\r
51        seqs[i] = (SequenceI) sequences.elementAt(i);\r
52        if(seqs[i].getLength()>width)\r
53          width = seqs[i].getLength();\r
54      }\r
55 \r
56      Hashtable [] reply = new Hashtable[width];\r
57 \r
58      calculate(seqs, start, end, reply);\r
59 \r
60      return reply;\r
61    }\r
62 \r
63 public static final void calculate(SequenceI[] sequences,\r
64                                   int start, int end,\r
65                                   Hashtable [] result)\r
66 {\r
67   Hashtable residueHash;\r
68   int maxCount, nongap, i, j, v, jSize = sequences.length;\r
69   String maxResidue;\r
70   char c;\r
71   float percentage;\r
72 \r
73   int[] values = new int[132];\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       if (sequences[j].getLength() > i)\r
86       {\r
87         c = sequences[j].getCharAt(i);\r
88 \r
89         if ('a' <= c && c <= 'z')\r
90         {\r
91           c -= ('a' - 'A');\r
92         }\r
93 \r
94         if (jalview.util.Comparison.isGap(c))\r
95         {\r
96           c = '-'; // we always use this for gaps in the property vectors\r
97         }\r
98         else\r
99         {\r
100           nongap++;\r
101         }\r
102 \r
103         values[c]++;\r
104 \r
105       }\r
106       else\r
107       {\r
108         values['-']++;\r
109       }\r
110     }\r
111 \r
112     for (v = 'A'; v < 'Z'; v++)\r
113     {\r
114       if (values[v] == 0 || values[v] < maxCount)\r
115         continue;\r
116 \r
117       if (values[v] > maxCount)\r
118       {\r
119         maxResidue = String.valueOf( (char) v);\r
120       }\r
121       else if (values[v] == maxCount)\r
122       {\r
123         maxResidue += String.valueOf( (char) v);\r
124       }\r
125       maxCount = values[v];\r
126     }\r
127 \r
128 \r
129     residueHash.put("maxCount", new Integer(maxCount));\r
130     residueHash.put("maxResidue", maxResidue);\r
131 \r
132     percentage = ( (float) maxCount * 100) / (float) jSize;\r
133     residueHash.put("pid_gaps", new Float(percentage));\r
134 \r
135     percentage = ( (float) maxCount * 100) / (float) nongap;\r
136     residueHash.put("pid_nogaps", new Float(percentage));\r
137     result[i] = residueHash;\r
138   }\r
139 }\r
140 }\r
141 \r
142 \r