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 String PAIRPROFILE = "B";
50 * Returns the 3' position of a base pair
51 * @param pairs Secondary structure annotation
52 * @param indice 5' position of a base pair
53 * @return 3' position of a base pair
55 public static int findPair(SequenceFeature[] pairs, int indice)
57 for (int i = 0; i < pairs.length; i++)
59 if (pairs[i].getBegin() == indice)
61 return pairs[i].getEnd();
68 * Method to calculate a 'base pair consensus row', very similar to nucleotide
69 * consensus but takes into account a given structure
78 public static final void calculate(SequenceI[] sequences, int start,
79 int end, Hashtable[] result, boolean profile,
80 AlignmentAnnotation rnaStruc)
82 Hashtable residueHash;
84 char[] seq, struc = rnaStruc.getRNAStruc().toCharArray();
85 SequenceFeature[] rna = rnaStruc._rnasecstr;
87 int count, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
92 for (i = start; i < end; i++) // foreach column
94 residueHash = new Hashtable();
96 values = new int[255];
97 pairs = new int[255][255];
104 if (s == '.' || s == ' ')
118 for (j = 0; j < jSize; j++) // foreach row
120 if (sequences[j] == null)
123 .println("WARNING: Consensus skipping null sequence - possible race condition.");
126 seq = sequences[j].getSequence();
132 // standard representation for gaps in sequence and structure
133 if (c == '.' || c == ' ')
143 bpEnd = findPair(rna, i);
145 if (checkBpType(c, cEnd))
147 values['(']++; // H means it's a helix (structured)
156 // UPDATE this for new values
159 residueHash.put(PROFILE, new int[][]
161 { jSize, (jSize - values['-']) } });
163 residueHash.put(PAIRPROFILE, pairs);
168 residueHash.put(MAXCOUNT, new Integer(count));
169 residueHash.put(MAXRESIDUE, maxResidue);
171 percentage = ((float) count * 100) / (float) jSize;
172 residueHash.put(PID_GAPS, new Float(percentage));
174 // percentage = ((float) count * 100) / (float) nongap;
175 // residueHash.put(PID_NOGAPS, new Float(percentage));
176 if (result[i] == null)
178 result[i] = residueHash;
182 values[')']=values['('];
185 residueHash = new Hashtable();
190 residueHash.put(PROFILE, new int[][]
192 { jSize, (jSize - values['-']) } });
194 residueHash.put(PAIRPROFILE, pairs);
197 residueHash.put(MAXCOUNT, new Integer(count));
198 residueHash.put(MAXRESIDUE, maxResidue);
200 percentage = ((float) count * 100) / (float) jSize;
201 residueHash.put(PID_GAPS, new Float(percentage));
204 result[bpEnd] = residueHash;
210 * Method to check if a base-pair is a canonical or a wobble bp
216 * @return True if it is a canonical/wobble bp
218 public static boolean checkBpType(char up, char down)
281 * Compute all or part of the annotation row from the given consensus
285 * - pre-allocated annotation row
289 * @param ignoreGapsInConsensusCalculation
290 * @param includeAllConsSymbols
292 public static void completeConsensus(AlignmentAnnotation consensus,
293 Hashtable[] hconsensus, int iStart, int width,
294 boolean ignoreGapsInConsensusCalculation,
295 boolean includeAllConsSymbols)
297 completeConsensus(consensus, hconsensus, iStart, width,
298 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
300 // { 'A', 'C', 'G', 'T', 'U' });
303 public static void completeConsensus(AlignmentAnnotation consensus,
304 Hashtable[] hconsensus, int iStart, int width,
305 boolean ignoreGapsInConsensusCalculation,
306 boolean includeAllConsSymbols, char[] alphabet)
309 if (consensus == null || consensus.annotations == null
310 || consensus.annotations.length < width)
312 // called with a bad alignment annotation row - wait for it to be
313 // initialised properly
316 for (int i = iStart; i < width; i++)
318 if (i >= hconsensus.length)
320 // happens if sequences calculated over were shorter than alignment
322 consensus.annotations[i] = null;
326 if (ignoreGapsInConsensusCalculation)
328 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
333 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
337 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
339 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
341 if (maxRes.length() > 1)
343 mouseOver = "[" + maxRes + "] ";
346 int[][] profile = (int[][]) hconsensus[i]
347 .get(StructureFrequency.PROFILE);
348 if (profile != null && includeAllConsSymbols) // Just responsible for the
350 //TODO Update tooltips for Structure row
353 if (alphabet != null)
355 for (int c = 0; c < alphabet.length; c++)
357 tval = ((float) profile[0][alphabet[c]])
359 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
361 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
362 + ((int) tval) + "%";
367 Object[] ca = new Object[profile[0].length];
368 float[] vl = new float[profile[0].length];
369 for (int c = 0; c < ca.length; c++)
373 vl[c] = (float) profile[0][c];
376 jalview.util.QuickSort.sort(vl, ca);
377 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
379 if (((char[]) ca[c])[0] != '-')
381 tval = ((float) profile[0][((char[]) ca[c])[0]])
383 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
385 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
386 + " " + ((int) tval) + "%";
396 mouseOver += ((int) value + "%");
398 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
404 * get the sorted base-pair profile for the given position of the consensus
407 * @return profile of the given column
409 public static int[] extractProfile(Hashtable hconsensus,
410 boolean ignoreGapsInConsensusCalculation, int column)
412 // TODO is there a more elegant way to acces the column number?
414 * calculate the frequence of the 16 bp variations for this column 'somehow'
415 * transfer this via getProfile and let it correctly draw
417 int[] rtnval = new int[51]; // 2*(5*5)+1
418 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
419 int[][] pairs = (int[][]) hconsensus
420 .get(StructureFrequency.PAIRPROFILE);
425 Object[] ca = new Object[625];
426 float[] vl = new float[625];
428 for (int c = 65; c < 90; c++)
430 for(int d = 65; d< 90; d++)
432 ca[x] = new int[]{ c, d};
433 vl[x] = (float) pairs[c][d];
437 jalview.util.QuickSort.sort(vl, ca);
440 for (int c=624; c>0; c--)
444 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
445 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
446 rtnval[rtnval[0]++] = (int) ((float) vl[c] * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
454 public static void main(String args[])
456 // Short test to see if checkBpType works
457 ArrayList<String> test = new ArrayList<String>();
463 for (String i : test)
465 for (String j : test)
467 System.out.println(i + "-" + j + ": "
468 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));