2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
19 package jalview.analysis;
23 import jalview.datamodel.*;
26 * Takes in a vector or array of sequences and column start and column end and
27 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
28 * This class is used extensively in calculating alignment colourschemes that
29 * depend on the amount of conservation in each alignment column.
34 public class StructureFrequency
36 // No need to store 1000s of strings which are not
37 // visible to the user.
38 public static final String MAXCOUNT = "C";
40 public static final String MAXRESIDUE = "R";
42 public static final String PID_GAPS = "G";
44 public static final String PID_NOGAPS = "N";
46 public static final String PROFILE = "P";
48 public static final String PAIRPROFILE = "B";
51 * Returns the 3' position of a base pair
54 * Secondary structure annotation
56 * 5' position of a base pair
57 * @return 3' position of a base pair
59 public static int findPair(SequenceFeature[] pairs, int indice)
62 for (int i = 0; i < pairs.length; i++)
64 if (pairs[i].getBegin() == indice)
68 return pairs[i].getEnd();
76 * Method to calculate a 'base pair consensus row', very similar to nucleotide
77 * consensus but takes into account a given structure
86 public static final void calculate(SequenceI[] sequences, int start,
87 int end, Hashtable[] result, boolean profile,
88 AlignmentAnnotation rnaStruc)
91 Hashtable residueHash;
93 char[] struc = rnaStruc.getRNAStruc().toCharArray();
95 SequenceFeature[] rna = rnaStruc._rnasecstr;
97 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
101 boolean wooble = true;
102 for (i = start; i < end; i++) // foreach column
104 residueHash = new Hashtable();
106 values = new int[255];
107 pairs = new int[255][255];
109 // System.out.println("s="+struc[i]);
110 if (i < struc.length)
119 if (s == '.' || s == ' ')
124 if (s != '(' && s != '[')
134 bpEnd = findPair(rna, i);
138 for (j = 0; j < jSize; j++) // foreach row
140 if (sequences[j] == null)
143 .println("WARNING: Consensus skipping null sequence - possible race condition.");
146 c = sequences[j].getCharAt(i);
147 // System.out.println("c="+c);
149 // standard representation for gaps in sequence and structure
150 if (c == '.' || c == ' ')
160 cEnd = sequences[j].getCharAt(bpEnd);
162 // System.out.println("pairs ="+c+","+cEnd);
163 if (checkBpType(c, cEnd) == true)
165 values['(']++; // H means it's a helix (structured)
168 // System.out.println("It's a pair wc");
171 if (checkBpType(c, cEnd) == false)
174 values['[']++; // H means it's a helix (structured)
184 // UPDATE this for new values
187 residueHash.put(PROFILE, new int[][]
189 { jSize, (jSize - values['-']) } });
191 residueHash.put(PAIRPROFILE, pairs);
201 residueHash.put(MAXCOUNT, new Integer(count));
202 residueHash.put(MAXRESIDUE, maxResidue);
204 percentage = ((float) count * 100) / jSize;
205 residueHash.put(PID_GAPS, new Float(percentage));
207 // percentage = ((float) count * 100) / (float) nongap;
208 // residueHash.put(PID_NOGAPS, new Float(percentage));
209 if (result[i] == null)
211 result[i] = residueHash;
215 values[')'] = values['('];
216 values[']'] = values['['];
219 residueHash = new Hashtable();
222 // System.out.println(maxResidue+","+wooble);
227 // System.out.println(maxResidue+","+wooble);
232 residueHash.put(PROFILE, new int[][]
234 { jSize, (jSize - values['-']) } });
236 residueHash.put(PAIRPROFILE, pairs);
239 residueHash.put(MAXCOUNT, new Integer(count));
240 residueHash.put(MAXRESIDUE, maxResidue);
242 percentage = ((float) count * 100) / jSize;
243 residueHash.put(PID_GAPS, new Float(percentage));
245 result[bpEnd] = residueHash;
252 * Method to check if a base-pair is a canonical or a wobble bp
258 * @return True if it is a canonical/wobble bp
260 public static boolean checkBpType(char up, char down)
323 * Compute all or part of the annotation row from the given consensus
327 * - pre-allocated annotation row
331 * @param ignoreGapsInConsensusCalculation
332 * @param includeAllConsSymbols
334 public static void completeConsensus(AlignmentAnnotation consensus,
335 Hashtable[] hconsensus, int iStart, int width,
336 boolean ignoreGapsInConsensusCalculation,
337 boolean includeAllConsSymbols)
340 if (consensus == null || consensus.annotations == null
341 || consensus.annotations.length < width)
343 // called with a bad alignment annotation row - wait for it to be
344 // initialised properly
347 for (int i = iStart; i < width; i++)
350 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
352 // happens if sequences calculated over were shorter than alignment
354 consensus.annotations[i] = null;
359 if (ignoreGapsInConsensusCalculation)
361 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
365 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
369 consensus.annotations[i] = null;
370 // data has changed below us .. give up and
373 value = fv.floatValue();
374 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
375 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
376 if (maxRes.length() > 1)
378 mouseOver = "[" + maxRes + "] ";
381 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
382 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
384 if (pairs != null && includeAllConsSymbols) // Just responsible for the
386 // TODO Update tooltips for Structure row
391 * TODO It's not sure what is the purpose of the alphabet and wheter it
392 * is useful for structure?
394 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
395 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
396 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
397 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
400 Object[] ca = new Object[625];
401 float[] vl = new float[625];
403 for (int c = 65; c < 90; c++)
405 for (int d = 65; d < 90; d++)
413 jalview.util.QuickSort.sort(vl, ca);
416 for (int c = 624; c > 0; c--)
420 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
422 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
423 + (char) ((int[]) ca[c])[1] + " " + ((int) tval) + "%";
433 mouseOver += ((int) value + "%");
435 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
441 * get the sorted base-pair profile for the given position of the consensus
444 * @return profile of the given column
446 public static int[] extractProfile(Hashtable hconsensus,
447 boolean ignoreGapsInConsensusCalculation)
449 int[] rtnval = new int[74]; // 2*(5*5)+2
450 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
451 int[][] pairs = (int[][]) hconsensus
452 .get(StructureFrequency.PAIRPROFILE);
457 // TODO fix the object length, also do it in completeConsensus
458 Object[] ca = new Object[625];
459 float[] vl = new float[625];
461 for (int c = 65; c < 90; c++)
463 for (int d = 65; d < 90; d++)
471 jalview.util.QuickSort.sort(vl, ca);
475 for (int c = 624; c > 0; c--)
479 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
480 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
481 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
483 rtnval[1] += rtnval[rtnval[0]++];
490 public static void main(String args[])
492 // Short test to see if checkBpType works
493 ArrayList<String> test = new ArrayList<String>();
499 for (String i : test)
501 for (String j : test)
503 System.out.println(i + "-" + j + ": "
504 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));