Check if groupWidth is greater than sequence width
[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      if(end>=width)\r
57      {\r
58        end = width-1;\r
59      }\r
60 \r
61      calculate(seqs, start, end, reply);\r
62 \r
63      return reply;\r
64    }\r
65 \r
66 public static final void calculate(SequenceI[] sequences,\r
67                                   int start, int end,\r
68                                   Hashtable [] result)\r
69 {\r
70   Hashtable residueHash;\r
71   int maxCount, nongap, i, j, v, jSize = sequences.length;\r
72   String maxResidue;\r
73   char c;\r
74   float percentage;\r
75 \r
76   int[] values = new int[132];\r
77 \r
78   String seq;\r
79 \r
80   for (i = start; i < end; i++)\r
81   {\r
82     residueHash = new Hashtable();\r
83     maxCount = 0;\r
84     maxResidue = "";\r
85     nongap = 0;\r
86     values = new int[132];\r
87 \r
88     for (j = 0; j < jSize; j++)\r
89     {\r
90       seq = sequences[j].getSequence();\r
91       if (seq.length() > i)\r
92       {\r
93         c = seq.charAt(i);\r
94 \r
95         if(c == '.' || c==' ')\r
96           c = '-';\r
97 \r
98         if(c=='-')\r
99         {\r
100           values['-']++;\r
101           continue;\r
102         }\r
103         else if ('a' <= c && c <= 'z')\r
104         {\r
105           c -= 32 ;//('a' - 'A');\r
106         }\r
107 \r
108         nongap++;\r
109         values[c]++;\r
110 \r
111       }\r
112       else\r
113       {\r
114         values['-']++;\r
115       }\r
116     }\r
117 \r
118     for (v = 'A'; v < 'Z'; v++)\r
119     {\r
120       if (values[v] < 2 || values[v] < maxCount)\r
121         continue;\r
122 \r
123       if (values[v] > maxCount)\r
124       {\r
125         maxResidue = String.valueOf( (char) v);\r
126       }\r
127       else if (values[v] == maxCount)\r
128       {\r
129         maxResidue += String.valueOf( (char) v);\r
130       }\r
131       maxCount = values[v];\r
132     }\r
133 \r
134     if(maxResidue.length()==0)\r
135       maxResidue = "-";\r
136 \r
137     residueHash.put(MAXCOUNT, new Integer(maxCount));\r
138     residueHash.put(MAXRESIDUE, maxResidue);\r
139 \r
140     percentage = ( (float) maxCount * 100) / (float) jSize;\r
141     residueHash.put(PID_GAPS, new Float(percentage));\r
142 \r
143     percentage = ( (float) maxCount * 100) / (float) nongap;\r
144     residueHash.put(PID_NOGAPS, new Float(percentage));\r
145     result[i] = residueHash;\r
146   }\r
147 }\r
148 }\r
149 \r
150 \r