Formatting
[jalview.git] / src / jalview / analysis / AAFrequency.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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 java.util.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 /**\r
26  * Takes in a vector or array of sequences and column start and column end\r
27  * and returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.\r
28  * This class is used extensively in calculating alignment colourschemes\r
29  * that depend on the amount of conservation in each alignment column.\r
30  * @author $author$\r
31  * @version $Revision$\r
32  */\r
33 public class AAFrequency\r
34 {\r
35   //No need to store 1000s of strings which are not\r
36   //visible to the user.\r
37   public static final String MAXCOUNT = "C";\r
38   public static final String MAXRESIDUE = "R";\r
39   public static final String PID_GAPS = "G";\r
40   public static final String PID_NOGAPS = "N";\r
41 \r
42   public static final Hashtable[] calculate(Vector sequences, int start,\r
43                                             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       {\r
52         width = seqs[i].getLength();\r
53       }\r
54     }\r
55 \r
56     Hashtable[] reply = new Hashtable[width];\r
57 \r
58     if (end >= width)\r
59     {\r
60       end = width;\r
61     }\r
62 \r
63     calculate(seqs, start, end, reply);\r
64 \r
65     return reply;\r
66   }\r
67 \r
68   public static final void calculate(SequenceI[] sequences,\r
69                                      int start, int end,\r
70                                      Hashtable[] result)\r
71   {\r
72     Hashtable residueHash;\r
73     int maxCount, nongap, i, j, v, jSize = sequences.length;\r
74     String maxResidue;\r
75     char c;\r
76     float percentage;\r
77 \r
78     int[] values = new int[255];\r
79 \r
80     char[] seq;\r
81 \r
82     for (i = start; i < end; i++)\r
83     {\r
84       residueHash = new Hashtable();\r
85       maxCount = 0;\r
86       maxResidue = "";\r
87       nongap = 0;\r
88       values = new int[255];\r
89 \r
90       for (j = 0; j < jSize; j++)\r
91       {\r
92         seq = sequences[j].getSequence();\r
93         if (seq.length > i)\r
94         {\r
95           c = seq[i];\r
96 \r
97           if (c == '.' || c == ' ')\r
98           {\r
99             c = '-';\r
100           }\r
101 \r
102           if (c == '-')\r
103           {\r
104             values['-']++;\r
105             continue;\r
106           }\r
107           else if ('a' <= c && c <= 'z')\r
108           {\r
109             c -= 32; //('a' - 'A');\r
110           }\r
111 \r
112           nongap++;\r
113           values[c]++;\r
114 \r
115         }\r
116         else\r
117         {\r
118           values['-']++;\r
119         }\r
120       }\r
121 \r
122       for (v = 'A'; v < 'Z'; v++)\r
123       {\r
124         if (values[v] < 2 || values[v] < maxCount)\r
125         {\r
126           continue;\r
127         }\r
128 \r
129         if (values[v] > maxCount)\r
130         {\r
131           maxResidue = String.valueOf( (char) v);\r
132         }\r
133         else if (values[v] == maxCount)\r
134         {\r
135           maxResidue += String.valueOf( (char) v);\r
136         }\r
137         maxCount = values[v];\r
138       }\r
139 \r
140       if (maxResidue.length() == 0)\r
141       {\r
142         maxResidue = "-";\r
143       }\r
144 \r
145       residueHash.put(MAXCOUNT, new Integer(maxCount));\r
146       residueHash.put(MAXRESIDUE, maxResidue);\r
147 \r
148       percentage = ( (float) maxCount * 100) / (float) jSize;\r
149       residueHash.put(PID_GAPS, new Float(percentage));\r
150 \r
151       percentage = ( (float) maxCount * 100) / (float) nongap;\r
152       residueHash.put(PID_NOGAPS, new Float(percentage));\r
153       result[i] = residueHash;\r
154     }\r
155   }\r
156 }\r