2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
25 import jalview.util.Format;
26 import jalview.datamodel.*;
29 * Takes in a vector or array of sequences and column start and column end and
30 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
31 * This class is used extensively in calculating alignment colourschemes that
32 * depend on the amount of conservation in each alignment column.
37 public class StructureFrequency
39 // No need to store 1000s of strings which are not
40 // visible to the user.
41 public static final String MAXCOUNT = "C";
43 public static final String MAXRESIDUE = "R";
45 public static final String PID_GAPS = "G";
47 public static final String PID_NOGAPS = "N";
49 public static final String PROFILE = "P";
51 public static final String PAIRPROFILE = "B";
54 * Returns the 3' position of a base pair
57 * Secondary structure annotation
59 * 5' position of a base pair
60 * @return 3' position of a base pair
62 public static int findPair(SequenceFeature[] pairs, int indice)
65 for (int i = 0; i < pairs.length; i++)
67 if (pairs[i].getBegin() == indice)
71 return pairs[i].getEnd();
79 * Method to calculate a 'base pair consensus row', very similar to nucleotide
80 * consensus but takes into account a given structure
89 public static final void calculate(SequenceI[] sequences, int start,
90 int end, Hashtable[] result, boolean profile,
91 AlignmentAnnotation rnaStruc)
94 Hashtable residueHash;
96 char[] struc = rnaStruc.getRNAStruc().toCharArray();
98 SequenceFeature[] rna = rnaStruc._rnasecstr;
100 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
104 boolean wooble = true;
105 for (i = start; i < end; i++) // foreach column
107 residueHash = new Hashtable();
109 values = new int[255];
110 pairs = new int[255][255];
112 // System.out.println("s="+struc[i]);
113 if (i < struc.length)
122 if (s == '.' || s == ' ')
127 if (s != '(' && s != '[')
137 bpEnd = findPair(rna, i);
141 for (j = 0; j < jSize; j++) // foreach row
143 if (sequences[j] == null)
146 .println("WARNING: Consensus skipping null sequence - possible race condition.");
149 c = sequences[j].getCharAt(i);
150 // System.out.println("c="+c);
152 // standard representation for gaps in sequence and structure
153 if (c == '.' || c == ' ')
163 cEnd = sequences[j].getCharAt(bpEnd);
165 // System.out.println("pairs ="+c+","+cEnd);
166 if (checkBpType(c, cEnd) == true)
168 values['(']++; // H means it's a helix (structured)
171 // System.out.println("It's a pair wc");
174 if (checkBpType(c, cEnd) == false)
177 values['[']++; // H means it's a helix (structured)
187 // UPDATE this for new values
190 residueHash.put(PROFILE, new int[][]
192 { jSize, (jSize - values['-']) } });
194 residueHash.put(PAIRPROFILE, pairs);
204 residueHash.put(MAXCOUNT, new Integer(count));
205 residueHash.put(MAXRESIDUE, maxResidue);
207 percentage = ((float) count * 100) / jSize;
208 residueHash.put(PID_GAPS, new Float(percentage));
210 // percentage = ((float) count * 100) / (float) nongap;
211 // residueHash.put(PID_NOGAPS, new Float(percentage));
212 if (result[i] == null)
214 result[i] = residueHash;
218 values[')'] = values['('];
219 values[']'] = values['['];
222 residueHash = new Hashtable();
225 // System.out.println(maxResidue+","+wooble);
230 // System.out.println(maxResidue+","+wooble);
235 residueHash.put(PROFILE, new int[][]
237 { jSize, (jSize - values['-']) } });
239 residueHash.put(PAIRPROFILE, pairs);
242 residueHash.put(MAXCOUNT, new Integer(count));
243 residueHash.put(MAXRESIDUE, maxResidue);
245 percentage = ((float) count * 100) / jSize;
246 residueHash.put(PID_GAPS, new Float(percentage));
248 result[bpEnd] = residueHash;
255 * Method to check if a base-pair is a canonical or a wobble bp
261 * @return True if it is a canonical/wobble bp
263 public static boolean checkBpType(char up, char down)
326 * Compute all or part of the annotation row from the given consensus
330 * - pre-allocated annotation row
334 * @param ignoreGapsInConsensusCalculation
335 * @param includeAllConsSymbols
337 public static void completeConsensus(AlignmentAnnotation consensus,
338 Hashtable[] hconsensus, int iStart, int width,
339 boolean ignoreGapsInConsensusCalculation,
340 boolean includeAllConsSymbols, long nseq)
343 if (consensus == null || consensus.annotations == null
344 || consensus.annotations.length < width)
346 // called with a bad alignment annotation row - wait for it to be
347 // initialised properly
350 String fmtstr="%3.1f";
358 fmtstr = "%"+(2+precision)+"."+precision+"f";
360 Format fmt = new Format(fmtstr);
362 for (int i = iStart; i < width; i++)
365 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
367 // happens if sequences calculated over were shorter than alignment
369 consensus.annotations[i] = null;
374 if (ignoreGapsInConsensusCalculation)
376 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
380 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
384 consensus.annotations[i] = null;
385 // data has changed below us .. give up and
388 value = fv.floatValue();
389 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
390 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
391 if (maxRes.length() > 1)
393 mouseOver = "[" + maxRes + "] ";
396 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
397 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
399 if (pairs != null && includeAllConsSymbols) // Just responsible for the
401 // TODO Update tooltips for Structure row
406 * TODO It's not sure what is the purpose of the alphabet and wheter it
407 * is useful for structure?
409 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
410 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
411 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
412 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
415 Object[] ca = new Object[625];
416 float[] vl = new float[625];
418 for (int c = 65; c < 90; c++)
420 for (int d = 65; d < 90; d++)
428 jalview.util.QuickSort.sort(vl, ca);
431 for (int c = 624; c > 0; c--)
435 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
437 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
438 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%";
448 mouseOver += (fmt.form(value) + "%");
450 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
456 * get the sorted base-pair profile for the given position of the consensus
459 * @return profile of the given column
461 public static int[] extractProfile(Hashtable hconsensus,
462 boolean ignoreGapsInConsensusCalculation)
464 int[] rtnval = new int[74]; // 2*(5*5)+2
465 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
466 int[][] pairs = (int[][]) hconsensus
467 .get(StructureFrequency.PAIRPROFILE);
472 // TODO fix the object length, also do it in completeConsensus
473 Object[] ca = new Object[625];
474 float[] vl = new float[625];
476 for (int c = 65; c < 90; c++)
478 for (int d = 65; d < 90; d++)
486 jalview.util.QuickSort.sort(vl, ca);
490 for (int c = 624; c > 0; c--)
494 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
495 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
496 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
498 rtnval[1] += rtnval[rtnval[0]++];
505 public static void main(String args[])
507 // Short test to see if checkBpType works
508 ArrayList<String> test = new ArrayList<String>();
514 for (String i : test)
516 for (String j : test)
518 System.out.println(i + "-" + j + ": "
519 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));