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
53 * Secondary structure annotation
55 * 5' position of a base pair
56 * @return 3' position of a base pair
58 public static int findPair(SequenceFeature[] pairs, int indice)
60 for (int i = 0; i < pairs.length; i++)
62 if (pairs[i].getBegin() == indice)
64 return pairs[i].getEnd();
71 * Method to calculate a 'base pair consensus row', very similar to nucleotide
72 * consensus but takes into account a given structure
81 public static final void calculate(SequenceI[] sequences, int start,
82 int end, Hashtable[] result, boolean profile,
83 AlignmentAnnotation rnaStruc)
85 Hashtable residueHash;
87 char[] seq, struc = rnaStruc.getRNAStruc().toCharArray();
88 SequenceFeature[] rna = rnaStruc._rnasecstr;
90 int count, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
95 for (i = start; i < end; i++) // foreach column
97 residueHash = new Hashtable();
99 values = new int[255];
100 pairs = new int[255][255];
102 if (i < struc.length)
110 if (s == '.' || s == ' ')
124 for (j = 0; j < jSize; j++) // foreach row
126 if (sequences[j] == null)
129 .println("WARNING: Consensus skipping null sequence - possible race condition.");
132 seq = sequences[j].getSequence();
138 // standard representation for gaps in sequence and structure
139 if (c == '.' || c == ' ')
149 bpEnd = findPair(rna, i);
151 if (checkBpType(c, cEnd))
153 values['(']++; // H means it's a helix (structured)
162 // UPDATE this for new values
165 residueHash.put(PROFILE, new int[][]
167 { jSize, (jSize - values['-']) } });
169 residueHash.put(PAIRPROFILE, pairs);
174 residueHash.put(MAXCOUNT, new Integer(count));
175 residueHash.put(MAXRESIDUE, maxResidue);
177 percentage = ((float) count * 100) / (float) jSize;
178 residueHash.put(PID_GAPS, new Float(percentage));
180 // percentage = ((float) count * 100) / (float) nongap;
181 // residueHash.put(PID_NOGAPS, new Float(percentage));
182 if (result[i] == null)
184 result[i] = residueHash;
188 values[')'] = values['('];
191 residueHash = new Hashtable();
196 residueHash.put(PROFILE, new int[][]
198 { jSize, (jSize - values['-']) } });
200 residueHash.put(PAIRPROFILE, pairs);
203 residueHash.put(MAXCOUNT, new Integer(count));
204 residueHash.put(MAXRESIDUE, maxResidue);
206 percentage = ((float) count * 100) / (float) jSize;
207 residueHash.put(PID_GAPS, new Float(percentage));
209 result[bpEnd] = residueHash;
215 * Method to check if a base-pair is a canonical or a wobble bp
221 * @return True if it is a canonical/wobble bp
223 public static boolean checkBpType(char up, char down)
286 * Compute all or part of the annotation row from the given consensus
290 * - pre-allocated annotation row
294 * @param ignoreGapsInConsensusCalculation
295 * @param includeAllConsSymbols
297 public static void completeConsensus(AlignmentAnnotation consensus,
298 Hashtable[] hconsensus, int iStart, int width,
299 boolean ignoreGapsInConsensusCalculation,
300 boolean includeAllConsSymbols)
303 if (consensus == null || consensus.annotations == null
304 || consensus.annotations.length < width)
306 // called with a bad alignment annotation row - wait for it to be
307 // initialised properly
310 for (int i = iStart; i < width; i++)
312 if (i >= hconsensus.length)
314 // happens if sequences calculated over were shorter than alignment
316 consensus.annotations[i] = null;
320 if (ignoreGapsInConsensusCalculation)
322 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
327 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
331 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
333 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
335 if (maxRes.length() > 1)
337 mouseOver = "[" + maxRes + "] ";
340 int[][] profile = (int[][]) hconsensus[i]
341 .get(StructureFrequency.PROFILE);
342 int[][] pairs = (int[][]) hconsensus[i]
343 .get(StructureFrequency.PAIRPROFILE);
345 if (pairs != null && includeAllConsSymbols) // Just responsible for the
347 // TODO Update tooltips for Structure row
351 /* TODO It's not sure what is the purpose of the alphabet and wheter it is useful for structure?
353 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
354 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
355 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
356 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
359 Object[] ca = new Object[625];
360 float[] vl = new float[625];
362 for (int c = 65; c < 90; c++)
364 for (int d = 65; d < 90; d++)
368 vl[x] = (float) pairs[c][d];
372 jalview.util.QuickSort.sort(vl, ca);
375 for (int c = 624; c > 0; c--)
379 tval = ((float) vl[c] * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
381 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
382 + (char) ((int[]) ca[c])[1] + " " + ((int) tval) + "%";
392 mouseOver += ((int) value + "%");
394 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
400 * get the sorted base-pair profile for the given position of the consensus
403 * @return profile of the given column
405 public static int[] extractProfile(Hashtable hconsensus,
406 boolean ignoreGapsInConsensusCalculation)
408 int[] rtnval = new int[51]; // 2*(5*5)+1
409 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
410 int[][] pairs = (int[][]) hconsensus
411 .get(StructureFrequency.PAIRPROFILE);
416 // TODO fix the object length, also do it in completeConsensus
417 Object[] ca = new Object[625];
418 float[] vl = new float[625];
420 for (int c = 65; c < 90; c++)
422 for (int d = 65; d < 90; d++)
426 vl[x] = (float) pairs[c][d];
430 jalview.util.QuickSort.sort(vl, ca);
433 for (int c = 624; c > 0; c--)
437 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
438 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
439 rtnval[rtnval[0]++] = (int) ((float) vl[c] * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
447 public static void main(String args[])
449 // Short test to see if checkBpType works
450 ArrayList<String> test = new ArrayList<String>();
456 for (String i : test)
458 for (String j : test)
460 System.out.println(i + "-" + j + ": "
461 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));