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/>.
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)
61 for (int i = 0; i < pairs.length; i++)
63 if (pairs[i].getBegin() == indice)
65 return pairs[i].getEnd();
72 * Method to calculate a 'base pair consensus row', very similar to nucleotide
73 * consensus but takes into account a given structure
82 public static final void calculate(SequenceI[] sequences, int start,
83 int end, Hashtable[] result, boolean profile,
84 AlignmentAnnotation rnaStruc)
86 Hashtable residueHash;
88 char[] seq, struc = rnaStruc.getRNAStruc().toCharArray();
89 SequenceFeature[] rna = rnaStruc._rnasecstr;
91 int count, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
96 for (i = start; i < end; i++) // foreach column
98 residueHash = new Hashtable();
100 values = new int[255];
101 pairs = new int[255][255];
103 if (i < struc.length)
111 if (s == '.' || s == ' ')
125 bpEnd = findPair(rna, i);
128 for (j = 0; j < jSize; j++) // foreach row
130 if (sequences[j] == null)
133 .println("WARNING: Consensus skipping null sequence - possible race condition.");
136 c = sequences[j].getCharAt(i);
139 // standard representation for gaps in sequence and structure
140 if (c == '.' || c == ' ')
150 cEnd = sequences[j].getCharAt(bpEnd);
151 if (checkBpType(c, cEnd))
153 values['(']++; // H means it's a helix (structured)
163 // UPDATE this for new values
166 residueHash.put(PROFILE, new int[][]
168 { jSize, (jSize - values['-']) } });
170 residueHash.put(PAIRPROFILE, pairs);
175 residueHash.put(MAXCOUNT, new Integer(count));
176 residueHash.put(MAXRESIDUE, maxResidue);
178 percentage = ((float) count * 100) / (float) jSize;
179 residueHash.put(PID_GAPS, new Float(percentage));
181 // percentage = ((float) count * 100) / (float) nongap;
182 // residueHash.put(PID_NOGAPS, new Float(percentage));
183 if (result[i] == null)
185 result[i] = residueHash;
189 values[')'] = values['('];
192 residueHash = new Hashtable();
197 residueHash.put(PROFILE, new int[][]
199 { jSize, (jSize - values['-']) } });
201 residueHash.put(PAIRPROFILE, pairs);
204 residueHash.put(MAXCOUNT, new Integer(count));
205 residueHash.put(MAXRESIDUE, maxResidue);
207 percentage = ((float) count * 100) / (float) jSize;
208 residueHash.put(PID_GAPS, new Float(percentage));
210 result[bpEnd] = residueHash;
216 * Method to check if a base-pair is a canonical or a wobble bp
222 * @return True if it is a canonical/wobble bp
224 public static boolean checkBpType(char up, char down)
287 * Compute all or part of the annotation row from the given consensus
291 * - pre-allocated annotation row
295 * @param ignoreGapsInConsensusCalculation
296 * @param includeAllConsSymbols
298 public static void completeConsensus(AlignmentAnnotation consensus,
299 Hashtable[] hconsensus, int iStart, int width,
300 boolean ignoreGapsInConsensusCalculation,
301 boolean includeAllConsSymbols)
304 if (consensus == null || consensus.annotations == null
305 || consensus.annotations.length < width)
307 // called with a bad alignment annotation row - wait for it to be
308 // initialised properly
311 for (int i = iStart; i < width; i++)
313 if (i >= hconsensus.length)
315 // happens if sequences calculated over were shorter than alignment
317 consensus.annotations[i] = null;
321 if (ignoreGapsInConsensusCalculation)
323 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
328 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
332 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
334 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
336 if (maxRes.length() > 1)
338 mouseOver = "[" + maxRes + "] ";
341 int[][] profile = (int[][]) hconsensus[i]
342 .get(StructureFrequency.PROFILE);
343 int[][] pairs = (int[][]) hconsensus[i]
344 .get(StructureFrequency.PAIRPROFILE);
346 if (pairs != null && includeAllConsSymbols) // Just responsible for the
348 // TODO Update tooltips for Structure row
352 /* TODO It's not sure what is the purpose of the alphabet and wheter it is useful for structure?
354 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
355 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
356 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
357 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
360 Object[] ca = new Object[625];
361 float[] vl = new float[625];
363 for (int c = 65; c < 90; c++)
365 for (int d = 65; d < 90; d++)
369 vl[x] = (float) pairs[c][d];
373 jalview.util.QuickSort.sort(vl, ca);
376 for (int c = 624; c > 0; c--)
380 tval = ((float) vl[c] * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
382 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
383 + (char) ((int[]) ca[c])[1] + " " + ((int) tval) + "%";
393 mouseOver += ((int) value + "%");
395 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
401 * get the sorted base-pair profile for the given position of the consensus
404 * @return profile of the given column
406 public static int[] extractProfile(Hashtable hconsensus,
407 boolean ignoreGapsInConsensusCalculation)
409 int[] rtnval = new int[74]; // 2*(5*5)+2
410 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
411 int[][] pairs = (int[][]) hconsensus
412 .get(StructureFrequency.PAIRPROFILE);
417 // TODO fix the object length, also do it in completeConsensus
418 Object[] ca = new Object[625];
419 float[] vl = new float[625];
421 for (int c = 65; c < 90; c++)
423 for (int d = 65; d < 90; d++)
427 vl[x] = (float) pairs[c][d];
431 jalview.util.QuickSort.sort(vl, ca);
435 for (int c = 624; c > 0; c--)
439 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
440 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
441 rtnval[rtnval[0]] = (int) ((float) vl[c] * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
443 rtnval[1]+=rtnval[rtnval[0]++];
450 public static void main(String args[])
452 // Short test to see if checkBpType works
453 ArrayList<String> test = new ArrayList<String>();
459 for (String i : test)
461 for (String j : test)
463 System.out.println(i + "-" + j + ": "
464 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));