2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.Format;
28 import java.util.Hashtable;
29 import java.util.List;
32 * Takes in a vector or array of sequences and column start and column end and
33 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
34 * This class is used extensively in calculating alignment colourschemes that
35 * depend on the amount of conservation in each alignment column.
40 public class AAFrequency
42 private static final int TO_UPPER_CASE = 'A' - 'a'; // -32
44 public static final String MAXCOUNT = "C";
46 public static final String MAXRESIDUE = "R";
48 public static final String PID_GAPS = "G";
50 public static final String PID_NOGAPS = "N";
52 public static final String PROFILE = "P";
55 * Quick look-up of String value of char 'A' to 'Z'
57 private static final String[] CHARS = new String['Z' - 'A' + 1];
61 for (char c = 'A'; c <= 'Z'; c++)
63 CHARS[c - 'A'] = String.valueOf(c);
67 public static final Hashtable[] calculate(List<SequenceI> list,
70 return calculate(list, start, end, false);
73 public static final Hashtable[] calculate(List<SequenceI> sequences,
74 int start, int end, boolean profile)
76 SequenceI[] seqs = new SequenceI[sequences.size()];
78 synchronized (sequences)
80 for (int i = 0; i < sequences.size(); i++)
82 seqs[i] = sequences.get(i);
83 if (seqs[i].getLength() > width)
85 width = seqs[i].getLength();
89 Hashtable[] reply = new Hashtable[width];
96 calculate(seqs, start, end, reply, profile);
101 public static final void calculate(SequenceI[] sequences, int start,
102 int end, Hashtable[] result, boolean profile)
104 Hashtable residueHash;
105 int maxCount, nongap, i, j, v;
106 int jSize = sequences.length;
111 int[] values = new int[255];
115 for (i = start; i < end; i++)
117 residueHash = new Hashtable();
121 values = new int[255];
123 for (j = 0; j < jSize; j++)
125 if (sequences[j] == null)
128 .println("WARNING: Consensus skipping null sequence - possible race condition.");
131 seq = sequences[j].getSequence();
136 if (c == '.' || c == ' ')
146 else if ('a' <= c && c <= 'z')
162 maxResidue = String.valueOf(c);
167 for (v = 'A'; v <= 'Z'; v++)
169 if (values[v] < 2 || values[v] < maxCount)
174 if (values[v] > maxCount)
176 maxResidue = CHARS[v - 'A'];
178 else if (values[v] == maxCount)
180 maxResidue += CHARS[v - 'A'];
182 maxCount = values[v];
185 if (maxResidue.length() == 0)
191 residueHash.put(PROFILE, new int[][]
193 { jSize, nongap } });
195 residueHash.put(MAXCOUNT, new Integer(maxCount));
196 residueHash.put(MAXRESIDUE, maxResidue);
198 percentage = ((float) maxCount * 100) / jSize;
199 residueHash.put(PID_GAPS, new Float(percentage));
203 // calculate for non-gapped too
204 percentage = ((float) maxCount * 100) / nongap;
206 residueHash.put(PID_NOGAPS, new Float(percentage));
208 result[i] = residueHash;
213 * Compute all or part of the annotation row from the given consensus
217 * - pre-allocated annotation row
221 * @param ignoreGapsInConsensusCalculation
222 * @param includeAllConsSymbols
225 public static void completeConsensus(AlignmentAnnotation consensus,
226 Hashtable[] hconsensus, int iStart, int width,
227 boolean ignoreGapsInConsensusCalculation,
228 boolean includeAllConsSymbols, long nseq)
230 completeConsensus(consensus, hconsensus, iStart, width,
231 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null,
235 public static void completeConsensus(AlignmentAnnotation consensus,
236 Hashtable[] hconsensus, int iStart, int width,
237 boolean ignoreGapsInConsensusCalculation,
238 boolean includeAllConsSymbols, char[] alphabet, long nseq)
241 if (consensus == null || consensus.annotations == null
242 || consensus.annotations.length < width)
244 // called with a bad alignment annotation row - wait for it to be
245 // initialised properly
248 String fmtstr = "%3.1f";
260 fmtstr = "%" + (2 + precision) + "." + (precision) + "f";
262 fmt = new Format(fmtstr);
268 for (int i = iStart; i < width; i++)
271 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
273 // happens if sequences calculated over were shorter than alignment
275 consensus.annotations[i] = null;
280 if (ignoreGapsInConsensusCalculation)
282 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
286 fv = (Float) hci.get(AAFrequency.PID_GAPS);
290 consensus.annotations[i] = null;
291 // data has changed below us .. give up and
294 value = fv.floatValue();
295 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
296 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
297 if (maxRes.length() > 1)
299 mouseOver = "[" + maxRes + "] ";
302 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
303 if (profile != null && includeAllConsSymbols)
306 if (alphabet != null)
308 for (int c = 0; c < alphabet.length; c++)
310 tval = profile[0][alphabet[c]] * 100f
311 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
312 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
313 + ((fmt != null) ? fmt.form(tval) : ((int) tval)) + "%";
318 Object[] ca = new Object[profile[0].length];
319 float[] vl = new float[profile[0].length];
320 for (int c = 0; c < ca.length; c++)
324 vl[c] = profile[0][c];
327 jalview.util.QuickSort.sort(vl, ca);
328 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
330 if (((char[]) ca[c])[0] != '-')
332 tval = profile[0][((char[]) ca[c])[0]]
334 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
335 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
337 + ((fmt != null) ? fmt.form(tval) : ((int) tval))
348 mouseOver += ((fmt != null) ? fmt.form(value) : ((int) value))
351 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
357 * get the sorted profile for the given position of the consensus
362 public static int[] extractProfile(Hashtable hconsensus,
363 boolean ignoreGapsInConsensusCalculation)
365 int[] rtnval = new int[64];
366 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
371 char[][] ca = new char[profile[0].length][];
372 float[] vl = new float[profile[0].length];
373 for (int c = 0; c < ca.length; c++)
377 vl[c] = profile[0][c];
379 jalview.util.QuickSort.sort(vl, ca);
382 for (int c = ca.length - 1; profile[0][ca[c][0]] > 0; c--)
386 rtnval[rtnval[0]++] = ca[c][0];
387 rtnval[rtnval[0]] = (int) (profile[0][ca[c][0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
389 rtnval[1] += rtnval[rtnval[0]++];