2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 package jalview.analysis;
24 import jalview.util.Format;
25 import jalview.datamodel.*;
28 * Takes in a vector or array of sequences and column start and column end and
29 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
30 * This class is used extensively in calculating alignment colourschemes that
31 * depend on the amount of conservation in each alignment column.
36 public class StructureFrequency
38 // No need to store 1000s of strings which are not
39 // visible to the user.
40 public static final String MAXCOUNT = "C";
42 public static final String MAXRESIDUE = "R";
44 public static final String PID_GAPS = "G";
46 public static final String PID_NOGAPS = "N";
48 public static final String PROFILE = "P";
50 public static final String PAIRPROFILE = "B";
53 * Returns the 3' position of a base pair
56 * Secondary structure annotation
58 * 5' position of a base pair
59 * @return 3' position of a base pair
61 public static int findPair(SequenceFeature[] pairs, int indice)
64 for (int i = 0; i < pairs.length; i++)
66 if (pairs[i].getBegin() == indice)
70 return pairs[i].getEnd();
78 * Method to calculate a 'base pair consensus row', very similar to nucleotide
79 * consensus but takes into account a given structure
88 public static final void calculate(SequenceI[] sequences, int start,
89 int end, Hashtable[] result, boolean profile,
90 AlignmentAnnotation rnaStruc)
93 Hashtable residueHash;
95 char[] struc = rnaStruc.getRNAStruc().toCharArray();
97 SequenceFeature[] rna = rnaStruc._rnasecstr;
99 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
103 boolean wooble = true;
104 for (i = start; i < end; i++) // foreach column
106 residueHash = new Hashtable();
108 values = new int[255];
109 pairs = new int[255][255];
111 // System.out.println("s="+struc[i]);
112 if (i < struc.length)
121 if (s == '.' || s == ' ')
126 if (s != '(' && s != '[')
136 bpEnd = findPair(rna, i);
140 for (j = 0; j < jSize; j++) // foreach row
142 if (sequences[j] == null)
145 .println("WARNING: Consensus skipping null sequence - possible race condition.");
148 c = sequences[j].getCharAt(i);
149 // System.out.println("c="+c);
151 // standard representation for gaps in sequence and structure
152 if (c == '.' || c == ' ')
162 cEnd = sequences[j].getCharAt(bpEnd);
164 // System.out.println("pairs ="+c+","+cEnd);
165 if (checkBpType(c, cEnd) == true)
167 values['(']++; // H means it's a helix (structured)
170 // System.out.println("It's a pair wc");
173 if (checkBpType(c, cEnd) == false)
176 values['[']++; // H means it's a helix (structured)
186 // UPDATE this for new values
189 residueHash.put(PROFILE, new int[][]
191 { jSize, (jSize - values['-']) } });
193 residueHash.put(PAIRPROFILE, pairs);
203 residueHash.put(MAXCOUNT, new Integer(count));
204 residueHash.put(MAXRESIDUE, maxResidue);
206 percentage = ((float) count * 100) / jSize;
207 residueHash.put(PID_GAPS, new Float(percentage));
209 // percentage = ((float) count * 100) / (float) nongap;
210 // residueHash.put(PID_NOGAPS, new Float(percentage));
211 if (result[i] == null)
213 result[i] = residueHash;
217 values[')'] = values['('];
218 values[']'] = values['['];
221 residueHash = new Hashtable();
224 // System.out.println(maxResidue+","+wooble);
229 // System.out.println(maxResidue+","+wooble);
234 residueHash.put(PROFILE, new int[][]
236 { jSize, (jSize - values['-']) } });
238 residueHash.put(PAIRPROFILE, pairs);
241 residueHash.put(MAXCOUNT, new Integer(count));
242 residueHash.put(MAXRESIDUE, maxResidue);
244 percentage = ((float) count * 100) / jSize;
245 residueHash.put(PID_GAPS, new Float(percentage));
247 result[bpEnd] = residueHash;
254 * Method to check if a base-pair is a canonical or a wobble bp
260 * @return True if it is a canonical/wobble bp
262 public static boolean checkBpType(char up, char down)
325 * Compute all or part of the annotation row from the given consensus
329 * - pre-allocated annotation row
333 * @param ignoreGapsInConsensusCalculation
334 * @param includeAllConsSymbols
336 public static void completeConsensus(AlignmentAnnotation consensus,
337 Hashtable[] hconsensus, int iStart, int width,
338 boolean ignoreGapsInConsensusCalculation,
339 boolean includeAllConsSymbols, long nseq)
342 if (consensus == null || consensus.annotations == null
343 || consensus.annotations.length < width)
345 // called with a bad alignment annotation row - wait for it to be
346 // initialised properly
349 String fmtstr="%3.1f";
357 fmtstr = "%"+(2+precision)+"."+precision+"f";
359 Format fmt = new Format(fmtstr);
361 for (int i = iStart; i < width; i++)
364 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
366 // happens if sequences calculated over were shorter than alignment
368 consensus.annotations[i] = null;
373 if (ignoreGapsInConsensusCalculation)
375 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
379 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
383 consensus.annotations[i] = null;
384 // data has changed below us .. give up and
387 value = fv.floatValue();
388 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
389 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
390 if (maxRes.length() > 1)
392 mouseOver = "[" + maxRes + "] ";
395 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
396 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
398 if (pairs != null && includeAllConsSymbols) // Just responsible for the
400 // TODO Update tooltips for Structure row
405 * TODO It's not sure what is the purpose of the alphabet and wheter it
406 * is useful for structure?
408 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
409 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
410 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
411 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
414 Object[] ca = new Object[625];
415 float[] vl = new float[625];
417 for (int c = 65; c < 90; c++)
419 for (int d = 65; d < 90; d++)
427 jalview.util.QuickSort.sort(vl, ca);
430 for (int c = 624; c > 0; c--)
434 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
436 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
437 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%";
447 mouseOver += (fmt.form(value) + "%");
449 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
455 * get the sorted base-pair profile for the given position of the consensus
458 * @return profile of the given column
460 public static int[] extractProfile(Hashtable hconsensus,
461 boolean ignoreGapsInConsensusCalculation)
463 int[] rtnval = new int[74]; // 2*(5*5)+2
464 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
465 int[][] pairs = (int[][]) hconsensus
466 .get(StructureFrequency.PAIRPROFILE);
471 // TODO fix the object length, also do it in completeConsensus
472 Object[] ca = new Object[625];
473 float[] vl = new float[625];
475 for (int c = 65; c < 90; c++)
477 for (int d = 65; d < 90; d++)
485 jalview.util.QuickSort.sort(vl, ca);
489 for (int c = 624; c > 0; c--)
493 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
494 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
495 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
497 rtnval[1] += rtnval[rtnval[0]++];
504 public static void main(String args[])
506 // Short test to see if checkBpType works
507 ArrayList<String> test = new ArrayList<String>();
513 for (String i : test)
515 for (String j : test)
517 System.out.println(i + "-" + j + ": "
518 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));