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";
359 fmtstr = "%" + (2 + precision) + "." + precision + "f";
361 Format fmt = new Format(fmtstr);
363 for (int i = iStart; i < width; i++)
366 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
368 // happens if sequences calculated over were shorter than alignment
370 consensus.annotations[i] = null;
375 if (ignoreGapsInConsensusCalculation)
377 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
381 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
385 consensus.annotations[i] = null;
386 // data has changed below us .. give up and
389 value = fv.floatValue();
390 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
391 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
392 if (maxRes.length() > 1)
394 mouseOver = "[" + maxRes + "] ";
397 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
398 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
400 if (pairs != null && includeAllConsSymbols) // Just responsible for the
402 // TODO Update tooltips for Structure row
407 * TODO It's not sure what is the purpose of the alphabet and wheter it
408 * is useful for structure?
410 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
411 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
412 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
413 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
416 Object[] ca = new Object[625];
417 float[] vl = new float[625];
419 for (int c = 65; c < 90; c++)
421 for (int d = 65; d < 90; d++)
429 jalview.util.QuickSort.sort(vl, ca);
432 for (int c = 624; c > 0; c--)
436 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
438 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
439 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval)
450 mouseOver += (fmt.form(value) + "%");
452 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
458 * get the sorted base-pair profile for the given position of the consensus
461 * @return profile of the given column
463 public static int[] extractProfile(Hashtable hconsensus,
464 boolean ignoreGapsInConsensusCalculation)
466 int[] rtnval = new int[74]; // 2*(5*5)+2
467 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
468 int[][] pairs = (int[][]) hconsensus
469 .get(StructureFrequency.PAIRPROFILE);
474 // TODO fix the object length, also do it in completeConsensus
475 Object[] ca = new Object[625];
476 float[] vl = new float[625];
478 for (int c = 65; c < 90; c++)
480 for (int d = 65; d < 90; d++)
488 jalview.util.QuickSort.sort(vl, ca);
492 for (int c = 624; c > 0; c--)
496 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
497 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
498 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
500 rtnval[1] += rtnval[rtnval[0]++];
507 public static void main(String args[])
509 // Short test to see if checkBpType works
510 ArrayList<String> test = new ArrayList<String>();
516 for (String i : test)
518 for (String j : test)
520 System.out.println(i + "-" + j + ": "
521 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));