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);
153 } else {for (v = 'A'; v < 'Z'; v++)
155 if (values[v] < 2 || values[v] < maxCount)
160 if (values[v] > maxCount)
162 maxResidue = String.valueOf((char) v);
164 else if (values[v] == maxCount)
166 maxResidue += String.valueOf((char) v);
168 maxCount = values[v];
171 if (maxResidue.length() == 0)
177 residueHash.put(PROFILE, new int[][]
179 { jSize, nongap } });
181 residueHash.put(MAXCOUNT, new Integer(maxCount));
182 residueHash.put(MAXRESIDUE, maxResidue);
184 percentage = ((float) maxCount * 100) / jSize;
185 residueHash.put(PID_GAPS, new Float(percentage));
188 // calculate for non-gapped too
189 percentage = ((float) maxCount * 100) / nongap;
191 residueHash.put(PID_NOGAPS, new Float(percentage));
193 result[i] = residueHash;
198 * Compute all or part of the annotation row from the given consensus
202 * - pre-allocated annotation row
206 * @param ignoreGapsInConsensusCalculation
207 * @param includeAllConsSymbols
210 public static void completeConsensus(AlignmentAnnotation consensus,
211 Hashtable[] hconsensus, int iStart, int width,
212 boolean ignoreGapsInConsensusCalculation,
213 boolean includeAllConsSymbols, long nseq)
215 completeConsensus(consensus, hconsensus, iStart, width,
216 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null, nseq); // new
218 // { 'A', 'C', 'G', 'T', 'U' });
221 public static void completeConsensus(AlignmentAnnotation consensus,
222 Hashtable[] hconsensus, int iStart, int width,
223 boolean ignoreGapsInConsensusCalculation,
224 boolean includeAllConsSymbols, char[] alphabet, long nseq)
227 if (consensus == null || consensus.annotations == null
228 || consensus.annotations.length < width)
230 // called with a bad alignment annotation row - wait for it to be
231 // initialised properly
234 String fmtstr="%3.1f";
245 fmtstr = "%"+(2+precision)+"."+(precision)+"f";
247 fmt = new Format(fmtstr);
251 for (int i = iStart; i < width; i++)
254 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
256 // happens if sequences calculated over were shorter than alignment
258 consensus.annotations[i] = null;
263 if (ignoreGapsInConsensusCalculation)
265 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
269 fv = (Float) hci.get(AAFrequency.PID_GAPS);
273 consensus.annotations[i] = null;
274 // data has changed below us .. give up and
277 value = fv.floatValue();
278 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
279 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
280 if (maxRes.length() > 1)
282 mouseOver = "[" + maxRes + "] ";
285 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
286 if (profile != null && includeAllConsSymbols)
289 if (alphabet != null)
291 for (int c = 0; c < alphabet.length; c++)
293 tval = profile[0][alphabet[c]] * 100f
294 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
295 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
296 + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
301 Object[] ca = new Object[profile[0].length];
302 float[] vl = new float[profile[0].length];
303 for (int c = 0; c < ca.length; c++)
307 vl[c] = profile[0][c];
310 jalview.util.QuickSort.sort(vl, ca);
311 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
313 if (((char[]) ca[c])[0] != '-')
315 tval = profile[0][((char[]) ca[c])[0]]
317 / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
318 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
319 + " " + ((fmt!=null) ? fmt.form(tval) : ((int) tval)) + "%";
329 mouseOver += ((fmt!=null) ? fmt.form(value) : ((int) value)) + "%";
331 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
337 * get the sorted profile for the given position of the consensus
342 public static int[] extractProfile(Hashtable hconsensus,
343 boolean ignoreGapsInConsensusCalculation)
345 int[] rtnval = new int[64];
346 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
349 Object[] ca = new Object[profile[0].length];
350 float[] vl = new float[profile[0].length];
351 for (int c = 0; c < ca.length; c++)
355 vl[c] = profile[0][c];
358 jalview.util.QuickSort.sort(vl, ca);
361 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
363 if (((char[]) ca[c])[0] != '-')
365 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
366 rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
368 rtnval[1] += rtnval[rtnval[0]++];