2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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 StructureFrequency
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(Vector sequences, int start,
50 return calculate(sequences, start, end, false);
53 public static final Hashtable[] calculate(Vector sequences, int start,
54 int end, boolean profile)
56 SequenceI[] seqs = new SequenceI[sequences.size()];
58 for (int i = 0; i < sequences.size(); i++)
60 seqs[i] = (SequenceI) sequences.elementAt(i);
61 if (seqs[i].getLength() > width)
63 width = seqs[i].getLength();
67 Hashtable[] reply = new Hashtable[width];
74 calculate(seqs, start, end, reply, profile);
79 public static final void calculate(SequenceI[] sequences, int start,
80 int end, Hashtable[] result)
82 calculate(sequences, start, end, result, false);
85 public static final void calculate(SequenceI[] sequences, int start,
86 int end, Hashtable[] result, boolean profile)
88 Hashtable residueHash;
89 int maxCount, nongap, i, j, v, jSize = sequences.length;
94 int[] values = new int[255];
98 for (i = start; i < end; i++)
100 residueHash = new Hashtable();
104 values = new int[255];
106 for (j = 0; j < jSize; j++)
108 if (sequences[j]==null)
110 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
113 seq = sequences[j].getSequence();
118 if (c == '.' || c == ' ')
128 else if ('a' <= c && c <= 'z')
130 c -= 32; // ('a' - 'A');
143 for (v = 'A'; v < 'Z'; v++)
145 if (values[v] < 2 || values[v] < maxCount)
150 if (values[v] > maxCount)
152 maxResidue = String.valueOf((char) v);
154 else if (values[v] == maxCount)
156 maxResidue += String.valueOf((char) v);
158 maxCount = values[v];
161 if (maxResidue.length() == 0)
167 residueHash.put(PROFILE, new int[][]
169 { jSize, nongap } });
171 residueHash.put(MAXCOUNT, new Integer(maxCount));
172 residueHash.put(MAXRESIDUE, maxResidue);
174 percentage = ((float) maxCount * 100) / (float) jSize;
175 residueHash.put(PID_GAPS, new Float(percentage));
177 percentage = ((float) maxCount * 100) / (float) nongap;
178 residueHash.put(PID_NOGAPS, new Float(percentage));
179 result[i] = residueHash;
184 * Method to calculate a 'base pair consensus row', very similar
185 * to nucleotide consensus but takes into account a given structure
193 public static final void calculate(SequenceI[] sequences, int start,
194 int end, Hashtable[] result, boolean profile, AlignmentAnnotation rnaStruc){
195 //TODO Consider to use AlignmentAnnotation instead of structure string
197 Hashtable residueHash;
199 char[] seq, struc=rnaStruc.getRNAStruc().toCharArray();
200 SequenceFeature[] rna =rnaStruc._rnasecstr;
202 int count,nonGap,i,j,jSize = sequences.length;
203 int[] values = new int[255];
207 for (i = start; i < end; i++) //foreach column
209 residueHash = new Hashtable();
210 for (j = 0; j < jSize; j++) //foreach row
212 if (sequences[j]==null)
214 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
217 seq = sequences[j].getSequence();
224 //standard representation for gaps in sequence and structure
225 if (c == '.' || c == ' ')
229 if (s == '.' || s == ' ')
244 cEnd=seq[rna[i].getEnd()];
245 if(checkBpType(c,cEnd)){
246 values['H']++; //H means it's a helix (structured)
250 /*UPDATE this for new values
253 residueHash.put(PROFILE, new int[][]
255 { jSize, nongap } });
261 percentage = ((float) count * 100) / (float) jSize;
262 residueHash.put(PID_GAPS, new Float(percentage));
264 //percentage = ((float) count * 100) / (float) nongap;
265 //residueHash.put(PID_NOGAPS, new Float(percentage));
266 result[i] = residueHash;
273 * Method to check if a base-pair is a canonical or a wobble bp
275 * @param down 3' base
276 * @return True if it is a canonical/wobble bp
278 public static boolean checkBpType(char up, char down){
280 if(down>'Z'){down-=32;}
285 case 'T': return true;
286 case 'U': return true;
291 case 'G': return true;
296 case 'A': return true;
297 case 'G': return true;
302 case 'C': return true;
303 case 'T': return true;
304 case 'U': return true;
309 case 'A': return true;
310 case 'G': return true;
318 * Compute all or part of the annotation row from the given consensus
322 * - pre-allocated annotation row
326 * @param ignoreGapsInConsensusCalculation
327 * @param includeAllConsSymbols
329 public static void completeConsensus(AlignmentAnnotation consensus,
330 Hashtable[] hconsensus, int iStart, int width,
331 boolean ignoreGapsInConsensusCalculation,
332 boolean includeAllConsSymbols)
334 completeConsensus(consensus, hconsensus, iStart, width,
335 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
337 // { 'A', 'C', 'G', 'T', 'U' });
340 public static void completeConsensus(AlignmentAnnotation consensus,
341 Hashtable[] hconsensus, int iStart, int width,
342 boolean ignoreGapsInConsensusCalculation,
343 boolean includeAllConsSymbols, char[] alphabet)
346 if (consensus == null || consensus.annotations == null
347 || consensus.annotations.length < width)
349 // called with a bad alignment annotation row - wait for it to be
350 // initialised properly
353 for (int i = iStart; i < width; i++)
355 if (i >= hconsensus.length)
357 // happens if sequences calculated over were shorter than alignment
359 consensus.annotations[i] = null;
363 if (ignoreGapsInConsensusCalculation)
365 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
370 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
374 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE).toString();
375 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE) + " ";
376 if (maxRes.length() > 1)
378 mouseOver = "[" + maxRes + "] ";
381 int[][] profile = (int[][]) hconsensus[i].get(StructureFrequency.PROFILE);
382 if (profile != null && includeAllConsSymbols)
385 if (alphabet != null)
387 for (int c = 0; c < alphabet.length; c++)
389 tval = ((float) profile[0][alphabet[c]])
391 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
393 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
394 + ((int) tval) + "%";
399 Object[] ca = new Object[profile[0].length];
400 float[] vl = new float[profile[0].length];
401 for (int c = 0; c < ca.length; c++)
405 vl[c] = (float) profile[0][c];
408 jalview.util.QuickSort.sort(vl, ca);
409 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
411 if (((char[]) ca[c])[0] != '-')
413 tval = ((float) profile[0][((char[]) ca[c])[0]])
415 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
417 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
418 + " " + ((int) tval) + "%";
428 mouseOver += ((int) value + "%");
430 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
436 * get the sorted profile for the given position of the consensus
441 public static int[] extractProfile(Hashtable hconsensus,
442 boolean ignoreGapsInConsensusCalculation)
444 int[] rtnval = new int[64];
445 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
448 Object[] ca = new Object[profile[0].length];
449 float[] vl = new float[profile[0].length];
450 for (int c = 0; c < ca.length; c++)
454 vl[c] = (float) profile[0][c];
457 jalview.util.QuickSort.sort(vl, ca);
459 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
461 if (((char[]) ca[c])[0] != '-')
463 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
464 rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
474 public static void main(String args[]){
475 //Short test to see if checkBpType works
476 ArrayList<String> test = new ArrayList<String>();
482 for (String i : test) {
483 for (String j : test) {
484 System.out.println(i+"-"+j+": "+StructureFrequency.checkBpType(i.charAt(0),j.charAt(0)));