2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
25 * Takes in a vector or array of sequences and column start and column end and
26 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
27 * This class is used extensively in calculating alignment colourschemes that
28 * depend on the amount of conservation in each alignment column.
33 public class AAFrequency
35 // No need to store 1000s of strings which are not
36 // visible to the user.
37 public static final String MAXCOUNT = "C";
39 public static final String MAXRESIDUE = "R";
41 public static final String PID_GAPS = "G";
43 public static final String PID_NOGAPS = "N";
45 public static final String PROFILE = "P";
47 public static final Hashtable[] calculate(Vector sequences, int start,
50 return calculate(sequences, start, end, false);
53 public static final Hashtable[] calculate(Vector sequences, int start,
54 int end, boolean profile)
56 SequenceI[] seqs = new SequenceI[sequences.size()];
58 for (int i = 0; i < sequences.size(); i++)
60 seqs[i] = (SequenceI) sequences.elementAt(i);
61 if (seqs[i].getLength() > width)
63 width = seqs[i].getLength();
67 Hashtable[] reply = new Hashtable[width];
74 calculate(seqs, start, end, reply, profile);
79 public static final void calculate(SequenceI[] sequences, int start,
80 int end, Hashtable[] result)
82 calculate(sequences, start, end, result, false);
85 public static final void calculate(SequenceI[] sequences, int start,
86 int end, Hashtable[] result, boolean profile)
88 Hashtable residueHash;
89 int maxCount, nongap, i, j, v, jSize = sequences.length;
94 int[] values = new int[255];
98 for (i = start; i < end; i++)
100 residueHash = new Hashtable();
104 values = new int[255];
106 for (j = 0; j < jSize; j++)
108 seq = sequences[j].getSequence();
113 if (c == '.' || c == ' ')
123 else if ('a' <= c && c <= 'z')
125 c -= 32; // ('a' - 'A');
138 for (v = 'A'; v < 'Z'; v++)
140 if (values[v] < 2 || values[v] < maxCount)
145 if (values[v] > maxCount)
147 maxResidue = String.valueOf((char) v);
149 else if (values[v] == maxCount)
151 maxResidue += String.valueOf((char) v);
153 maxCount = values[v];
156 if (maxResidue.length() == 0)
162 residueHash.put(PROFILE, new int[][]
164 { jSize, nongap } });
166 residueHash.put(MAXCOUNT, new Integer(maxCount));
167 residueHash.put(MAXRESIDUE, maxResidue);
169 percentage = ((float) maxCount * 100) / (float) jSize;
170 residueHash.put(PID_GAPS, new Float(percentage));
172 percentage = ((float) maxCount * 100) / (float) nongap;
173 residueHash.put(PID_NOGAPS, new Float(percentage));
174 result[i] = residueHash;
179 * Compute all or part of the annotation row from the given consensus
183 * - pre-allocated annotation row
187 * @param ignoreGapsInConsensusCalculation
188 * @param includeAllConsSymbols
190 public static void completeConsensus(AlignmentAnnotation consensus,
191 Hashtable[] hconsensus, int iStart, int width,
192 boolean ignoreGapsInConsensusCalculation,
193 boolean includeAllConsSymbols)
195 completeConsensus(consensus, hconsensus, iStart, width,
196 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
198 // { 'A', 'C', 'G', 'T', 'U' });
201 public static void completeConsensus(AlignmentAnnotation consensus,
202 Hashtable[] hconsensus, int iStart, int width,
203 boolean ignoreGapsInConsensusCalculation,
204 boolean includeAllConsSymbols, char[] alphabet)
207 if (consensus==null || consensus.annotations==null || consensus.annotations.length<width)
209 // called with a bad alignment annotation row - wait for it to be initialised properly
212 for (int i = iStart; i < width; i++)
215 if (ignoreGapsInConsensusCalculation)
217 value = ((Float) hconsensus[i].get(AAFrequency.PID_NOGAPS))
222 value = ((Float) hconsensus[i].get(AAFrequency.PID_GAPS))
226 String maxRes = hconsensus[i].get(AAFrequency.MAXRESIDUE).toString();
227 String mouseOver = hconsensus[i].get(AAFrequency.MAXRESIDUE) + " ";
228 if (maxRes.length() > 1)
230 mouseOver = "[" + maxRes + "] ";
233 int[][] profile = (int[][]) hconsensus[i].get(AAFrequency.PROFILE);
234 if (profile != null && includeAllConsSymbols)
237 if (alphabet != null)
239 for (int c = 0; c < alphabet.length; c++)
241 tval = ((float) profile[0][alphabet[c]])
243 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
245 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
246 + ((int) tval) + "%";
251 Object[] ca = new Object[profile[0].length];
252 float[] vl = new float[profile[0].length];
253 for (int c = 0; c < ca.length; c++)
257 vl[c] = (float) profile[0][c];
260 jalview.util.QuickSort.sort(vl, ca);
261 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
263 if (((char[]) ca[c])[0] != '-')
265 tval = ((float) profile[0][((char[]) ca[c])[0]])
267 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
269 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
270 + " " + ((int) tval) + "%";
280 mouseOver += ((int) value + "%");
282 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
288 * get the sorted profile for the given position of the consensus
293 public static int[] extractProfile(Hashtable hconsensus,
294 boolean ignoreGapsInConsensusCalculation)
296 int[] rtnval = new int[64];
297 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
300 Object[] ca = new Object[profile[0].length];
301 float[] vl = new float[profile[0].length];
302 for (int c = 0; c < ca.length; c++)
306 vl[c] = (float) profile[0][c];
309 jalview.util.QuickSort.sort(vl, ca);
311 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
313 if (((char[]) ca[c])[0] != '-')
315 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
316 rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1