2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
25 import jalview.util.Format;
26 import jalview.datamodel.*;
29 * Takes in a vector or array of sequences and column start and column end and
30 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
31 * This class is used extensively in calculating alignment colourschemes that
32 * depend on the amount of conservation in each alignment column.
37 public class AAFrequency
39 // No need to store 1000s of strings which are not
40 // visible to the user.
41 public static final String MAXCOUNT = "C";
43 public static final String MAXRESIDUE = "R";
45 public static final String PID_GAPS = "G";
47 public static final String PID_NOGAPS = "N";
49 public static final String PROFILE = "P";
51 public static final Hashtable[] calculate(List<SequenceI> list,
54 return calculate(list, start, end, false);
57 public static final Hashtable[] calculate(List<SequenceI> sequences,
58 int start, int end, boolean profile)
60 SequenceI[] seqs = new SequenceI[sequences.size()];
62 synchronized (sequences)
64 for (int i = 0; i < sequences.size(); i++)
66 seqs[i] = sequences.get(i);
67 if (seqs[i].getLength() > width)
69 width = seqs[i].getLength();
73 Hashtable[] reply = new Hashtable[width];
80 calculate(seqs, start, end, reply, profile);
85 public static final void calculate(SequenceI[] sequences, int start,
86 int end, Hashtable[] result)
88 calculate(sequences, start, end, result, false);
91 public static final void calculate(SequenceI[] sequences, int start,
92 int end, Hashtable[] result, boolean profile)
94 Hashtable residueHash;
95 int maxCount, nongap, i, j, v, jSize = sequences.length;
100 int[] values = new int[255];
104 for (i = start; i < end; i++)
106 residueHash = new Hashtable();
110 values = new int[255];
112 for (j = 0; j < jSize; j++)
114 if (sequences[j] == null)
117 .println("WARNING: Consensus skipping null sequence - possible race condition.");
120 seq = sequences[j].getSequence();
125 if (c == '.' || c == ' ')
135 else if ('a' <= c && c <= 'z')
137 c -= 32; // ('a' - 'A');
151 maxResidue = String.valueOf(c);
156 for (v = 'A'; v < 'Z'; v++)
158 if (values[v] < 2 || values[v] < maxCount)
163 if (values[v] > maxCount)
165 maxResidue = String.valueOf((char) v);
167 else if (values[v] == maxCount)
169 maxResidue += String.valueOf((char) v);
171 maxCount = values[v];
174 if (maxResidue.length() == 0)
180 residueHash.put(PROFILE, new int[][]
182 { jSize, nongap } });
184 residueHash.put(MAXCOUNT, new Integer(maxCount));
185 residueHash.put(MAXRESIDUE, maxResidue);
187 percentage = ((float) maxCount * 100) / jSize;
188 residueHash.put(PID_GAPS, new Float(percentage));
192 // calculate for non-gapped too
193 percentage = ((float) maxCount * 100) / nongap;
195 residueHash.put(PID_NOGAPS, new Float(percentage));
197 result[i] = residueHash;
202 * Compute all or part of the annotation row from the given consensus
206 * - pre-allocated annotation row
210 * @param ignoreGapsInConsensusCalculation
211 * @param includeAllConsSymbols
214 public static void completeConsensus(AlignmentAnnotation consensus,
215 Hashtable[] hconsensus, int iStart, int width,
216 boolean ignoreGapsInConsensusCalculation,
217 boolean includeAllConsSymbols, long nseq)
219 completeConsensus(consensus, hconsensus, iStart, width,
220 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null,
223 // { 'A', 'C', 'G', 'T', 'U' });
226 public static void completeConsensus(AlignmentAnnotation consensus,
227 Hashtable[] hconsensus, int iStart, int width,
228 boolean ignoreGapsInConsensusCalculation,
229 boolean includeAllConsSymbols, char[] alphabet, long nseq)
232 if (consensus == null || consensus.annotations == null
233 || consensus.annotations.length < width)
235 // called with a bad alignment annotation row - wait for it to be
236 // initialised properly
239 String fmtstr = "%3.1f";
251 fmtstr = "%" + (2 + precision) + "." + (precision) + "f";
253 fmt = new Format(fmtstr);
259 for (int i = iStart; i < width; i++)
262 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
264 // happens if sequences calculated over were shorter than alignment
266 consensus.annotations[i] = null;
271 if (ignoreGapsInConsensusCalculation)
273 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
277 fv = (Float) hci.get(AAFrequency.PID_GAPS);
281 consensus.annotations[i] = null;
282 // data has changed below us .. give up and
285 value = fv.floatValue();
286 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
287 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
288 if (maxRes.length() > 1)
290 mouseOver = "[" + maxRes + "] ";
293 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
294 if (profile != null && includeAllConsSymbols)
297 if (alphabet != null)
299 for (int c = 0; c < alphabet.length; c++)
301 tval = profile[0][alphabet[c]] * 100f
302 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
303 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
304 + ((fmt != null) ? fmt.form(tval) : ((int) tval)) + "%";
309 Object[] ca = new Object[profile[0].length];
310 float[] vl = new float[profile[0].length];
311 for (int c = 0; c < ca.length; c++)
315 vl[c] = profile[0][c];
318 jalview.util.QuickSort.sort(vl, ca);
319 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
321 if (((char[]) ca[c])[0] != '-')
323 tval = profile[0][((char[]) ca[c])[0]]
325 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
326 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
328 + ((fmt != null) ? fmt.form(tval) : ((int) tval))
339 mouseOver += ((fmt != null) ? fmt.form(value) : ((int) value))
342 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
348 * get the sorted profile for the given position of the consensus
353 public static int[] extractProfile(Hashtable hconsensus,
354 boolean ignoreGapsInConsensusCalculation)
356 int[] rtnval = new int[64];
357 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
360 Object[] ca = new Object[profile[0].length];
361 float[] vl = new float[profile[0].length];
362 for (int c = 0; c < ca.length; c++)
366 vl[c] = profile[0][c];
369 jalview.util.QuickSort.sort(vl, ca);
372 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
374 if (((char[]) ca[c])[0] != '-')
376 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
377 rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
379 rtnval[1] += rtnval[rtnval[0]++];