2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.datamodel.*;
25 * Takes in a vector or array of sequences and column start and column end and
26 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
27 * This class is used extensively in calculating alignment colourschemes that
28 * depend on the amount of conservation in each alignment column.
33 public class AAFrequency
35 // No need to store 1000s of strings which are not
36 // visible to the user.
37 public static final String MAXCOUNT = "C";
39 public static final String MAXRESIDUE = "R";
41 public static final String PID_GAPS = "G";
43 public static final String PID_NOGAPS = "N";
45 public static final String PROFILE = "P";
47 public static final Hashtable[] calculate(List<SequenceI> list, int start,
50 return calculate(list, start, end, false);
53 public static final Hashtable[] calculate(List<SequenceI> sequences, int start,
54 int end, boolean profile)
56 SequenceI[] seqs = new SequenceI[sequences.size()];
58 synchronized (sequences) {
59 for (int i = 0; i < sequences.size(); i++)
61 seqs[i] = sequences.get(i);
62 if (seqs[i].getLength() > width)
64 width = seqs[i].getLength();
68 Hashtable[] reply = new Hashtable[width];
75 calculate(seqs, start, end, reply, profile);
80 public static final void calculate(SequenceI[] sequences, int start,
81 int end, Hashtable[] result)
83 calculate(sequences, start, end, result, false);
86 public static final void calculate(SequenceI[] sequences, int start,
87 int end, Hashtable[] result, boolean profile)
89 Hashtable residueHash;
90 int maxCount, nongap, i, j, v, jSize = sequences.length;
95 int[] values = new int[255];
99 for (i = start; i < end; i++)
101 residueHash = new Hashtable();
105 values = new int[255];
107 for (j = 0; j < jSize; j++)
109 if (sequences[j]==null)
111 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
114 seq = sequences[j].getSequence();
119 if (c == '.' || c == ' ')
129 else if ('a' <= c && c <= 'z')
131 c -= 32; // ('a' - 'A');
144 for (v = 'A'; v < 'Z'; v++)
146 if (values[v] < 2 || values[v] < maxCount)
151 if (values[v] > maxCount)
153 maxResidue = String.valueOf((char) v);
155 else if (values[v] == maxCount)
157 maxResidue += String.valueOf((char) v);
159 maxCount = values[v];
162 if (maxResidue.length() == 0)
168 residueHash.put(PROFILE, new int[][]
170 { jSize, nongap } });
172 residueHash.put(MAXCOUNT, new Integer(maxCount));
173 residueHash.put(MAXRESIDUE, maxResidue);
175 percentage = ((float) maxCount * 100) / jSize;
176 residueHash.put(PID_GAPS, new Float(percentage));
178 percentage = ((float) maxCount * 100) / nongap;
179 residueHash.put(PID_NOGAPS, new Float(percentage));
180 result[i] = residueHash;
185 * Compute all or part of the annotation row from the given consensus
189 * - pre-allocated annotation row
193 * @param ignoreGapsInConsensusCalculation
194 * @param includeAllConsSymbols
196 public static void completeConsensus(AlignmentAnnotation consensus,
197 Hashtable[] hconsensus, int iStart, int width,
198 boolean ignoreGapsInConsensusCalculation,
199 boolean includeAllConsSymbols)
201 completeConsensus(consensus, hconsensus, iStart, width,
202 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
204 // { 'A', 'C', 'G', 'T', 'U' });
207 public static void completeConsensus(AlignmentAnnotation consensus,
208 Hashtable[] hconsensus, int iStart, int width,
209 boolean ignoreGapsInConsensusCalculation,
210 boolean includeAllConsSymbols, char[] alphabet)
213 if (consensus == null || consensus.annotations == null
214 || consensus.annotations.length < width)
216 // called with a bad alignment annotation row - wait for it to be
217 // initialised properly
220 for (int i = iStart; i < width; i++)
223 if (i >= hconsensus.length || ((hci=hconsensus[i])==null))
225 // happens if sequences calculated over were shorter than alignment
227 consensus.annotations[i] = null;
233 if (ignoreGapsInConsensusCalculation)
235 fv = (Float) hci.get(AAFrequency.PID_NOGAPS);
239 fv = (Float) hci.get(AAFrequency.PID_GAPS);
243 consensus.annotations[i] = null;
244 // data has changed below us .. give up and
247 value = fv.floatValue();
248 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
249 String mouseOver = hci.get(AAFrequency.MAXRESIDUE) + " ";
250 if (maxRes.length() > 1)
252 mouseOver = "[" + maxRes + "] ";
255 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
256 if (profile != null && includeAllConsSymbols)
259 if (alphabet != null)
261 for (int c = 0; c < alphabet.length; c++)
263 tval = profile[0][alphabet[c]]
265 / profile[1][ignoreGapsInConsensusCalculation ? 1
267 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
268 + ((int) tval) + "%";
273 Object[] ca = new Object[profile[0].length];
274 float[] vl = new float[profile[0].length];
275 for (int c = 0; c < ca.length; c++)
279 vl[c] = profile[0][c];
282 jalview.util.QuickSort.sort(vl, ca);
283 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
285 if (((char[]) ca[c])[0] != '-')
287 tval = profile[0][((char[]) ca[c])[0]]
289 / profile[1][ignoreGapsInConsensusCalculation ? 1
291 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
292 + " " + ((int) tval) + "%";
302 mouseOver += ((int) value + "%");
304 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
310 * get the sorted profile for the given position of the consensus
315 public static int[] extractProfile(Hashtable hconsensus,
316 boolean ignoreGapsInConsensusCalculation)
318 int[] rtnval = new int[64];
319 int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
322 Object[] ca = new Object[profile[0].length];
323 float[] vl = new float[profile[0].length];
324 for (int c = 0; c < ca.length; c++)
328 vl[c] = profile[0][c];
331 jalview.util.QuickSort.sort(vl, ca);
334 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
336 if (((char[]) ca[c])[0] != '-')
338 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
339 rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
341 rtnval[1]+=rtnval[rtnval[0]++];