2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 percentage = ((float) maxCount * 100) / nongap;
187 residueHash.put(PID_NOGAPS, new Float(percentage));
189 result[i] = residueHash;
194 * Compute all or part of the annotation row from the given consensus
198 * - pre-allocated annotation row
202 * @param ignoreGapsInConsensusCalculation
203 * @param includeAllConsSymbols
206 public static void completeConsensus(AlignmentAnnotation consensus,
207 Hashtable[] hconsensus, int iStart, int width,
208 boolean ignoreGapsInConsensusCalculation,
209 boolean includeAllConsSymbols, long nseq)
211 completeConsensus(consensus, hconsensus, iStart, width,
212 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null, nseq); // new
214 // { 'A', 'C', 'G', 'T', 'U' });
217 public static void completeConsensus(AlignmentAnnotation consensus,
218 Hashtable[] hconsensus, int iStart, int width,
219 boolean ignoreGapsInConsensusCalculation,
220 boolean includeAllConsSymbols, char[] alphabet, long nseq)
223 if (consensus == null || consensus.annotations == null
224 || consensus.annotations.length < width)
226 // called with a bad alignment annotation row - wait for it to be
227 // initialised properly
230 String fmtstr="%3.1f";
241 fmtstr = "%"+(2+precision)+"."+(precision)+"f";
243 fmt = new Format(fmtstr);
247 for (int i = iStart; i < width; i++)
250 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
252 // happens if sequences calculated over were shorter than alignment
254 consensus.annotations[i] = null;
259 if (ignoreGapsInConsensusCalculation)
261 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
265 fv = (Float) hci.get(AAFrequency.PID_GAPS);
269 consensus.annotations[i] = null;
270 // data has changed below us .. give up and
273 value = fv.floatValue();
274 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
275 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
276 if (maxRes.length() > 1)
278 mouseOver = "[" + maxRes + "] ";
281 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
282 if (profile != null && includeAllConsSymbols)
285 if (alphabet != null)
287 for (int c = 0; c < alphabet.length; c++)
289 tval = profile[0][alphabet[c]] * 100f
290 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
291 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
292 + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
297 Object[] ca = new Object[profile[0].length];
298 float[] vl = new float[profile[0].length];
299 for (int c = 0; c < ca.length; c++)
303 vl[c] = profile[0][c];
306 jalview.util.QuickSort.sort(vl, ca);
307 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
309 if (((char[]) ca[c])[0] != '-')
311 tval = profile[0][((char[]) ca[c])[0]]
313 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
314 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
315 + " " + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
325 mouseOver += ((fmt!=null) ? fmt.form(value) : ((int) value)) + "%";
327 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
333 * get the sorted profile for the given position of the consensus
338 public static int[] extractProfile(Hashtable hconsensus,
339 boolean ignoreGapsInConsensusCalculation)
341 int[] rtnval = new int[64];
342 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
345 Object[] ca = new Object[profile[0].length];
346 float[] vl = new float[profile[0].length];
347 for (int c = 0; c < ca.length; c++)
351 vl[c] = profile[0][c];
354 jalview.util.QuickSort.sort(vl, ca);
357 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
359 if (((char[]) ca[c])[0] != '-')
361 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
362 rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
364 rtnval[1] += rtnval[rtnval[0]++];