2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.util.Format;
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(List<SequenceI> list,
51 return calculate(list, start, end, false);
54 public static final Hashtable[] calculate(List<SequenceI> sequences,
55 int start, int end, boolean profile)
57 SequenceI[] seqs = new SequenceI[sequences.size()];
59 synchronized (sequences)
61 for (int i = 0; i < sequences.size(); i++)
63 seqs[i] = sequences.get(i);
64 if (seqs[i].getLength() > width)
66 width = seqs[i].getLength();
70 Hashtable[] reply = new Hashtable[width];
77 calculate(seqs, start, end, reply, profile);
82 public static final void calculate(SequenceI[] sequences, int start,
83 int end, Hashtable[] result)
85 calculate(sequences, start, end, result, false);
88 public static final void calculate(SequenceI[] sequences, int start,
89 int end, Hashtable[] result, boolean profile)
91 Hashtable residueHash;
92 int maxCount, nongap, i, j, v, jSize = sequences.length;
97 int[] values = new int[255];
101 for (i = start; i < end; i++)
103 residueHash = new Hashtable();
107 values = new int[255];
109 for (j = 0; j < jSize; j++)
111 if (sequences[j] == null)
114 .println("WARNING: Consensus skipping null sequence - possible race condition.");
117 seq = sequences[j].getSequence();
122 if (c == '.' || c == ' ')
132 else if ('a' <= c && c <= 'z')
134 c -= 32; // ('a' - 'A');
148 maxResidue = String.valueOf(c);
150 } else {for (v = 'A'; v < 'Z'; v++)
152 if (values[v] < 2 || values[v] < maxCount)
157 if (values[v] > maxCount)
159 maxResidue = String.valueOf((char) v);
161 else if (values[v] == maxCount)
163 maxResidue += String.valueOf((char) v);
165 maxCount = values[v];
168 if (maxResidue.length() == 0)
174 residueHash.put(PROFILE, new int[][]
176 { jSize, nongap } });
178 residueHash.put(MAXCOUNT, new Integer(maxCount));
179 residueHash.put(MAXRESIDUE, maxResidue);
181 percentage = ((float) maxCount * 100) / jSize;
182 residueHash.put(PID_GAPS, new Float(percentage));
184 percentage = ((float) maxCount * 100) / nongap;
185 residueHash.put(PID_NOGAPS, new Float(percentage));
186 result[i] = residueHash;
191 * Compute all or part of the annotation row from the given consensus
195 * - pre-allocated annotation row
199 * @param ignoreGapsInConsensusCalculation
200 * @param includeAllConsSymbols
203 public static void completeConsensus(AlignmentAnnotation consensus,
204 Hashtable[] hconsensus, int iStart, int width,
205 boolean ignoreGapsInConsensusCalculation,
206 boolean includeAllConsSymbols, long nseq)
208 completeConsensus(consensus, hconsensus, iStart, width,
209 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null, nseq); // new
211 // { 'A', 'C', 'G', 'T', 'U' });
214 public static void completeConsensus(AlignmentAnnotation consensus,
215 Hashtable[] hconsensus, int iStart, int width,
216 boolean ignoreGapsInConsensusCalculation,
217 boolean includeAllConsSymbols, char[] alphabet, long nseq)
220 if (consensus == null || consensus.annotations == null
221 || consensus.annotations.length < width)
223 // called with a bad alignment annotation row - wait for it to be
224 // initialised properly
227 String fmtstr="%3.1f";
238 fmtstr = "%"+(2+precision)+"."+(precision)+"f";
240 fmt = new Format(fmtstr);
244 for (int i = iStart; i < width; i++)
247 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
249 // happens if sequences calculated over were shorter than alignment
251 consensus.annotations[i] = null;
256 if (ignoreGapsInConsensusCalculation)
258 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
262 fv = (Float) hci.get(AAFrequency.PID_GAPS);
266 consensus.annotations[i] = null;
267 // data has changed below us .. give up and
270 value = fv.floatValue();
271 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
272 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
273 if (maxRes.length() > 1)
275 mouseOver = "[" + maxRes + "] ";
278 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
279 if (profile != null && includeAllConsSymbols)
282 if (alphabet != null)
284 for (int c = 0; c < alphabet.length; c++)
286 tval = profile[0][alphabet[c]] * 100f
287 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
288 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
289 + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
294 Object[] ca = new Object[profile[0].length];
295 float[] vl = new float[profile[0].length];
296 for (int c = 0; c < ca.length; c++)
300 vl[c] = profile[0][c];
303 jalview.util.QuickSort.sort(vl, ca);
304 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
306 if (((char[]) ca[c])[0] != '-')
308 tval = profile[0][((char[]) ca[c])[0]]
310 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
311 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
312 + " " + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
322 mouseOver += ((fmt!=null) ? fmt.form(value) : ((int) value)) + "%";
324 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
330 * get the sorted profile for the given position of the consensus
335 public static int[] extractProfile(Hashtable hconsensus,
336 boolean ignoreGapsInConsensusCalculation)
338 int[] rtnval = new int[64];
339 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
342 Object[] ca = new Object[profile[0].length];
343 float[] vl = new float[profile[0].length];
344 for (int c = 0; c < ca.length; c++)
348 vl[c] = profile[0][c];
351 jalview.util.QuickSort.sort(vl, ca);
354 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
356 if (((char[]) ca[c])[0] != '-')
358 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
359 rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
361 rtnval[1] += rtnval[rtnval[0]++];