header updated
[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 vector of sequences and column start and column end\r
39     * and returns a 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     public static final Vector calculate(Vector sequences, int start, int end)\r
45     {\r
46       Vector result = new Vector();\r
47       Hashtable residueHash;\r
48       int count, maxCount, nongap, i, j, jSize = sequences.size();\r
49       String maxResidue, sequence, res;\r
50       float percentage;\r
51 \r
52         for (i = start; i <= end; i++)\r
53         {\r
54             residueHash = new Hashtable();\r
55             maxCount = 0;\r
56             maxResidue = "-";\r
57             nongap = 0;\r
58 \r
59             for (j = 0; j < jSize; j++)\r
60             {\r
61                 if (sequences.elementAt(j) instanceof Sequence)\r
62                 {\r
63                     sequence = ((Sequence) sequences.elementAt(j)).getSequence();\r
64 \r
65                     if (sequence.length() > i)\r
66                     {\r
67                         res = String.valueOf(Character.toUpperCase(sequence.charAt(i)));\r
68 \r
69                         if (jalview.util.Comparison.isGap(res.charAt(0)))\r
70                         {\r
71                             res = "-"; // we always use this for gaps in the property vectors\r
72                         }\r
73                         else\r
74                         {   nongap++;    }\r
75 \r
76                         if (residueHash.containsKey(res))\r
77                         {\r
78                             count = ((Integer) residueHash.get(res)).intValue();\r
79                             count++;\r
80 \r
81                             if (!jalview.util.Comparison.isGap(res.charAt(0)) &&\r
82                                     (count >= maxCount))\r
83                             {\r
84                                 if (count > maxCount)\r
85                                 {\r
86                                     maxResidue = res;\r
87                                 }\r
88                                 else if (maxResidue.indexOf(res) == -1)\r
89                                 {\r
90                                     maxResidue += res;\r
91                                 }\r
92 \r
93                                 maxCount = count;\r
94                             }\r
95 \r
96                             residueHash.put(res, new Integer(count));\r
97                         }\r
98                         else\r
99                         {\r
100                             residueHash.put(res, new Integer(1));\r
101                         }\r
102                     }\r
103                     else\r
104                     {\r
105                         if (residueHash.containsKey("-"))\r
106                         {\r
107                             count = ((Integer) residueHash.get("-")).intValue();\r
108                             count++;\r
109                             residueHash.put("-", new Integer(count));\r
110                         }\r
111                         else\r
112                         {\r
113                             residueHash.put("-", new Integer(1));\r
114                         }\r
115                     }\r
116                 }\r
117             }\r
118 \r
119             residueHash.put("maxCount", new Integer(maxCount));\r
120             residueHash.put("maxResidue", maxResidue);\r
121 \r
122 \r
123             //Size is redundant at present if we calculate percentage here\r
124             //residueHash.put("size", new Integer(jSize));\r
125             //residueHash.put("nogaps", new Integer(nongap));\r
126 \r
127             percentage = ((float)maxCount*100) / (float)jSize;\r
128             residueHash.put("pid_gaps", new Float(percentage) );\r
129 \r
130             percentage = ((float)maxCount*100) / (float)nongap;\r
131             residueHash.put("pid_nogaps", new Float(percentage) );\r
132             result.addElement(residueHash);\r
133         }\r
134 \r
135 \r
136 \r
137         return result;\r
138     }\r
139 }\r