2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
26 * Takes in a vector or array of sequences and column start and column end and
27 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
28 * This class is used extensively in calculating alignment colourschemes that
29 * depend on the amount of conservation in each alignment column.
34 public class AAFrequency
36 // No need to store 1000s of strings which are not
37 // visible to the user.
38 public static final String MAXCOUNT = "C";
40 public static final String MAXRESIDUE = "R";
42 public static final String PID_GAPS = "G";
44 public static final String PID_NOGAPS = "N";
46 public static final String PROFILE = "P";
48 public static final Hashtable[] calculate(Vector sequences, int start,
51 return calculate(sequences, start, end, false);
54 public static final Hashtable[] calculate(Vector sequences, int start,
55 int end, boolean profile)
57 SequenceI[] seqs = new SequenceI[sequences.size()];
59 for (int i = 0; i < sequences.size(); i++)
61 seqs[i] = (SequenceI) sequences.elementAt(i);
62 if (seqs[i].getLength() > width)
64 width = seqs[i].getLength();
68 Hashtable[] reply = new Hashtable[width];
75 calculate(seqs, start, end, reply, profile);
80 public static final void calculate(SequenceI[] sequences, int start,
81 int end, Hashtable[] result)
83 calculate(sequences, start, end, result, false);
86 public static final void calculate(SequenceI[] sequences, int start,
87 int end, Hashtable[] result, boolean profile)
89 Hashtable residueHash;
90 int maxCount, nongap, i, j, v, jSize = sequences.length;
95 int[] values = new int[255];
99 for (i = start; i < end; i++)
101 residueHash = new Hashtable();
105 values = new int[255];
107 for (j = 0; j < jSize; j++)
109 seq = sequences[j].getSequence();
114 if (c == '.' || c == ' ')
124 else if ('a' <= c && c <= 'z')
126 c -= 32; // ('a' - 'A');
139 for (v = 'A'; v < 'Z'; v++)
141 if (values[v] < 2 || values[v] < maxCount)
146 if (values[v] > maxCount)
148 maxResidue = String.valueOf((char) v);
150 else if (values[v] == maxCount)
152 maxResidue += String.valueOf((char) v);
154 maxCount = values[v];
157 if (maxResidue.length() == 0)
163 residueHash.put(PROFILE, new int[][]
165 { jSize, nongap } });
167 residueHash.put(MAXCOUNT, new Integer(maxCount));
168 residueHash.put(MAXRESIDUE, maxResidue);
170 percentage = ((float) maxCount * 100) / (float) jSize;
171 residueHash.put(PID_GAPS, new Float(percentage));
173 percentage = ((float) maxCount * 100) / (float) nongap;
174 residueHash.put(PID_NOGAPS, new Float(percentage));
175 result[i] = residueHash;
180 * Compute all or part of the annotation row from the given consensus
184 * - pre-allocated annotation row
188 * @param ignoreGapsInConsensusCalculation
189 * @param includeAllConsSymbols
191 public static void completeConsensus(AlignmentAnnotation consensus,
192 Hashtable[] hconsensus, int iStart, int width,
193 boolean ignoreGapsInConsensusCalculation,
194 boolean includeAllConsSymbols)
196 completeConsensus(consensus, hconsensus, iStart, width,
197 ignoreGapsInConsensusCalculation, includeAllConsSymbols,
199 // { 'A', 'C', 'G', 'T', 'U' });
202 public static void completeConsensus(AlignmentAnnotation consensus,
203 Hashtable[] hconsensus, int iStart, int width,
204 boolean ignoreGapsInConsensusCalculation,
205 boolean includeAllConsSymbols, char[] alphabet)
208 for (int i = iStart; i < width; i++)
211 if (ignoreGapsInConsensusCalculation)
213 value = ((Float) hconsensus[i].get(AAFrequency.PID_NOGAPS))
218 value = ((Float) hconsensus[i].get(AAFrequency.PID_GAPS))
222 String maxRes = hconsensus[i].get(AAFrequency.MAXRESIDUE).toString();
223 String mouseOver = hconsensus[i].get(AAFrequency.MAXRESIDUE) + " ";
224 if (maxRes.length() > 1)
226 mouseOver = "[" + maxRes + "] ";
229 int[][] profile = (int[][]) hconsensus[i].get(AAFrequency.PROFILE);
230 if (profile != null && includeAllConsSymbols)
235 for (int c = 0; c < alphabet.length; c++)
237 tval = ((float) profile[0][alphabet[c]])
239 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
241 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
242 + ((int) tval) + "%";
247 Object[] ca = new Object[profile[0].length];
248 float[] vl = new float[profile[0].length];
249 for (int c = 0; c < ca.length; c++)
253 vl[c] = (float) profile[0][c];
256 jalview.util.QuickSort.sort(vl, ca);
257 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
259 if (((char[]) ca[c])[0] != '-')
261 tval = ((float) profile[0][((char[]) ca[c])[0]])
263 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
265 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
266 + " " + ((int) tval) + "%";
276 mouseOver += ((int) value + "%");
278 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
284 * get the sorted profile for the given position of the consensus
289 public static int[] extractProfile(Hashtable hconsensus,
290 boolean ignoreGapsInConsensusCalculation)
292 int[] rtnval = new int[64];
293 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
296 Object[] ca = new Object[profile[0].length];
297 float[] vl = new float[profile[0].length];
298 for (int c = 0; c < ca.length; c++)
302 vl[c] = (float) profile[0][c];
305 jalview.util.QuickSort.sort(vl, ca);
307 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
309 if (((char[]) ca[c])[0] != '-')
311 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
312 rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1