2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
23 import jalview.util.Format;
24 import jalview.datamodel.*;
27 * Takes in a vector or array of sequences and column start and column end and
28 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
29 * This class is used extensively in calculating alignment colourschemes that
30 * depend on the amount of conservation in each alignment column.
35 public class AAFrequency
37 // No need to store 1000s of strings which are not
38 // visible to the user.
39 public static final String MAXCOUNT = "C";
41 public static final String MAXRESIDUE = "R";
43 public static final String PID_GAPS = "G";
45 public static final String PID_NOGAPS = "N";
47 public static final String PROFILE = "P";
49 public static final Hashtable[] calculate(List<SequenceI> list,
52 return calculate(list, start, end, false);
55 public static final Hashtable[] calculate(List<SequenceI> sequences,
56 int start, int end, boolean profile)
58 SequenceI[] seqs = new SequenceI[sequences.size()];
60 synchronized (sequences)
62 for (int i = 0; i < sequences.size(); i++)
64 seqs[i] = sequences.get(i);
65 if (seqs[i].getLength() > width)
67 width = seqs[i].getLength();
71 Hashtable[] reply = new Hashtable[width];
78 calculate(seqs, start, end, reply, profile);
83 public static final void calculate(SequenceI[] sequences, int start,
84 int end, Hashtable[] result)
86 calculate(sequences, start, end, result, false);
89 public static final void calculate(SequenceI[] sequences, int start,
90 int end, Hashtable[] result, boolean profile)
92 Hashtable residueHash;
93 int maxCount, nongap, i, j, v, jSize = sequences.length;
98 int[] values = new int[255];
102 for (i = start; i < end; i++)
104 residueHash = new Hashtable();
108 values = new int[255];
110 for (j = 0; j < jSize; j++)
112 if (sequences[j] == null)
115 .println("WARNING: Consensus skipping null sequence - possible race condition.");
118 seq = sequences[j].getSequence();
123 if (c == '.' || c == ' ')
133 else if ('a' <= c && c <= 'z')
135 c -= 32; // ('a' - 'A');
149 maxResidue = String.valueOf(c);
151 } else {for (v = 'A'; v < 'Z'; v++)
153 if (values[v] < 2 || values[v] < maxCount)
158 if (values[v] > maxCount)
160 maxResidue = String.valueOf((char) v);
162 else if (values[v] == maxCount)
164 maxResidue += String.valueOf((char) v);
166 maxCount = values[v];
169 if (maxResidue.length() == 0)
175 residueHash.put(PROFILE, new int[][]
177 { jSize, nongap } });
179 residueHash.put(MAXCOUNT, new Integer(maxCount));
180 residueHash.put(MAXRESIDUE, maxResidue);
182 percentage = ((float) maxCount * 100) / jSize;
183 residueHash.put(PID_GAPS, new Float(percentage));
186 // calculate for non-gapped too
187 percentage = ((float) maxCount * 100) / nongap;
189 residueHash.put(PID_NOGAPS, new Float(percentage));
191 result[i] = residueHash;
196 * Compute all or part of the annotation row from the given consensus
200 * - pre-allocated annotation row
204 * @param ignoreGapsInConsensusCalculation
205 * @param includeAllConsSymbols
208 public static void completeConsensus(AlignmentAnnotation consensus,
209 Hashtable[] hconsensus, int iStart, int width,
210 boolean ignoreGapsInConsensusCalculation,
211 boolean includeAllConsSymbols, long nseq)
213 completeConsensus(consensus, hconsensus, iStart, width,
214 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null, nseq); // new
216 // { 'A', 'C', 'G', 'T', 'U' });
219 public static void completeConsensus(AlignmentAnnotation consensus,
220 Hashtable[] hconsensus, int iStart, int width,
221 boolean ignoreGapsInConsensusCalculation,
222 boolean includeAllConsSymbols, char[] alphabet, long nseq)
225 if (consensus == null || consensus.annotations == null
226 || consensus.annotations.length < width)
228 // called with a bad alignment annotation row - wait for it to be
229 // initialised properly
232 String fmtstr="%3.1f";
243 fmtstr = "%"+(2+precision)+"."+(precision)+"f";
245 fmt = new Format(fmtstr);
249 for (int i = iStart; i < width; i++)
252 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
254 // happens if sequences calculated over were shorter than alignment
256 consensus.annotations[i] = null;
261 if (ignoreGapsInConsensusCalculation)
263 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
267 fv = (Float) hci.get(AAFrequency.PID_GAPS);
271 consensus.annotations[i] = null;
272 // data has changed below us .. give up and
275 value = fv.floatValue();
276 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
277 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
278 if (maxRes.length() > 1)
280 mouseOver = "[" + maxRes + "] ";
283 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
284 if (profile != null && includeAllConsSymbols)
287 if (alphabet != null)
289 for (int c = 0; c < alphabet.length; c++)
291 tval = profile[0][alphabet[c]] * 100f
292 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
293 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
294 + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
299 Object[] ca = new Object[profile[0].length];
300 float[] vl = new float[profile[0].length];
301 for (int c = 0; c < ca.length; c++)
305 vl[c] = profile[0][c];
308 jalview.util.QuickSort.sort(vl, ca);
309 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
311 if (((char[]) ca[c])[0] != '-')
313 tval = profile[0][((char[]) ca[c])[0]]
315 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
316 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
317 + " " + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
327 mouseOver += ((fmt!=null) ? fmt.form(value) : ((int) value)) + "%";
329 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
335 * get the sorted profile for the given position of the consensus
340 public static int[] extractProfile(Hashtable hconsensus,
341 boolean ignoreGapsInConsensusCalculation)
343 int[] rtnval = new int[64];
344 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
347 Object[] ca = new Object[profile[0].length];
348 float[] vl = new float[profile[0].length];
349 for (int c = 0; c < ca.length; c++)
353 vl[c] = profile[0][c];
356 jalview.util.QuickSort.sort(vl, ca);
359 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
361 if (((char[]) ca[c])[0] != '-')
363 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
364 rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
366 rtnval[1] += rtnval[rtnval[0]++];