461b5455d2bbaa8fc8ef454d55852ada0fdf9e98
[jalview.git] / src / jalview / analysis / AAFrequency.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 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.analysis.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 import java.util.*;\r
26 \r
27 \r
28 /**\r
29  * Takes in a vector of sequences and column start and column end\r
30  * and returns a vector of size (end-start+1). Each element of the\r
31  * vector contains a hashtable with the keys being residues and\r
32  * the values being the count of each residue in that column.\r
33  * This class is used extensively in calculating alignment colourschemes\r
34  * that depend on the amount of conservation in each alignment column.\r
35  * @author $author$\r
36  * @version $Revision$\r
37  */\r
38 public class AAFrequency\r
39 {\r
40     /** Takes in a vector of sequences and column start and column end\r
41     * and returns a vector of size (end-start+1). Each element of the\r
42     * vector contains a hashtable with the keys being residues and\r
43     * the values being the count of each residue in that column.\r
44     * This class is used extensively in calculating alignment colourschemes\r
45     * that depend on the amount of conservation in each alignment column. */\r
46     public static final Vector calculate(Vector sequences, int start, int end)\r
47     {\r
48       Vector result = new Vector();\r
49       Hashtable residueHash;\r
50       int count, maxCount, nongap, i, j, jSize = sequences.size();\r
51       String maxResidue, sequence, res;\r
52       float percentage;\r
53 \r
54         for (i = start; i <= end; i++)\r
55         {\r
56             residueHash = new Hashtable();\r
57             maxCount = 0;\r
58             maxResidue = "-";\r
59             nongap = 0;\r
60 \r
61             for (j = 0; j < jSize; j++)\r
62             {\r
63                 if (sequences.elementAt(j) instanceof Sequence)\r
64                 {\r
65                     sequence = ((Sequence) sequences.elementAt(j)).getSequence();\r
66 \r
67                     if (sequence.length() > i)\r
68                     {\r
69                         res = String.valueOf(Character.toUpperCase(sequence.charAt(i)));\r
70 \r
71                         if (jalview.util.Comparison.isGap(res.charAt(0)))\r
72                         {\r
73                             res = "-"; // we always use this for gaps in the property vectors\r
74                         }\r
75                         else\r
76                         {   nongap++;    }\r
77 \r
78                         if (residueHash.containsKey(res))\r
79                         {\r
80                             count = ((Integer) residueHash.get(res)).intValue();\r
81                             count++;\r
82 \r
83                             if (!jalview.util.Comparison.isGap(res.charAt(0)) &&\r
84                                     (count >= maxCount))\r
85                             {\r
86                                 if (count > maxCount)\r
87                                 {\r
88                                     maxResidue = res;\r
89                                 }\r
90                                 else if (maxResidue.indexOf(res) == -1)\r
91                                 {\r
92                                     maxResidue += res;\r
93                                 }\r
94 \r
95                                 maxCount = count;\r
96                             }\r
97 \r
98                             residueHash.put(res, new Integer(count));\r
99                         }\r
100                         else\r
101                         {\r
102                             residueHash.put(res, new Integer(1));\r
103                         }\r
104                     }\r
105                     else\r
106                     {\r
107                         if (residueHash.containsKey("-"))\r
108                         {\r
109                             count = ((Integer) residueHash.get("-")).intValue();\r
110                             count++;\r
111                             residueHash.put("-", new Integer(count));\r
112                         }\r
113                         else\r
114                         {\r
115                             residueHash.put("-", new Integer(1));\r
116                         }\r
117                     }\r
118                 }\r
119             }\r
120 \r
121             residueHash.put("maxCount", new Integer(maxCount));\r
122             residueHash.put("maxResidue", maxResidue);\r
123 \r
124 \r
125             //Size is redundant at present if we calculate percentage here\r
126             //residueHash.put("size", new Integer(jSize));\r
127             //residueHash.put("nogaps", new Integer(nongap));\r
128 \r
129             percentage = ((float)maxCount*100) / (float)jSize;\r
130             residueHash.put("pid_gaps", new Float(percentage) );\r
131 \r
132             percentage = ((float)maxCount*100) / (float)nongap;\r
133             residueHash.put("pid_nogaps", new Float(percentage) );\r
134             result.addElement(residueHash);\r
135         }\r
136 \r
137 \r
138 \r
139         return result;\r
140     }\r
141 }\r