2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
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)
63 for (int i = 0; i < pairs.length; i++)
65 if (pairs[i].getBegin() == indice)
67 return pairs[i].getEnd();
74 * Method to calculate a 'base pair consensus row', very similar to nucleotide
75 * consensus but takes into account a given structure
84 public static final void calculate(SequenceI[] sequences, int start,
85 int end, Hashtable[] result, boolean profile,
86 AlignmentAnnotation rnaStruc)
88 Hashtable residueHash;
90 char[] seq, struc = rnaStruc.getRNAStruc().toCharArray();
91 SequenceFeature[] rna = rnaStruc._rnasecstr;
93 int count, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
98 for (i = start; i < end; i++) // foreach column
100 residueHash = new Hashtable();
102 values = new int[255];
103 pairs = new int[255][255];
105 if (i < struc.length)
113 if (s == '.' || s == ' ')
127 bpEnd = findPair(rna, i);
130 for (j = 0; j < jSize; j++) // foreach row
132 if (sequences[j] == null)
135 .println("WARNING: Consensus skipping null sequence - possible race condition.");
138 c = sequences[j].getCharAt(i);
141 // standard representation for gaps in sequence and structure
142 if (c == '.' || c == ' ')
152 cEnd = sequences[j].getCharAt(bpEnd);
153 if (checkBpType(c, cEnd))
155 values['(']++; // H means it's a helix (structured)
165 // UPDATE this for new values
168 residueHash.put(PROFILE, new int[][]
170 { jSize, (jSize - values['-']) } });
172 residueHash.put(PAIRPROFILE, pairs);
177 residueHash.put(MAXCOUNT, new Integer(count));
178 residueHash.put(MAXRESIDUE, maxResidue);
180 percentage = ((float) count * 100) / jSize;
181 residueHash.put(PID_GAPS, new Float(percentage));
183 // percentage = ((float) count * 100) / (float) nongap;
184 // residueHash.put(PID_NOGAPS, new Float(percentage));
185 if (result[i] == null)
187 result[i] = residueHash;
191 values[')'] = values['('];
194 residueHash = new Hashtable();
199 residueHash.put(PROFILE, new int[][]
201 { jSize, (jSize - values['-']) } });
203 residueHash.put(PAIRPROFILE, pairs);
206 residueHash.put(MAXCOUNT, new Integer(count));
207 residueHash.put(MAXRESIDUE, maxResidue);
209 percentage = ((float) count * 100) / jSize;
210 residueHash.put(PID_GAPS, new Float(percentage));
212 result[bpEnd] = residueHash;
218 * Method to check if a base-pair is a canonical or a wobble bp
224 * @return True if it is a canonical/wobble bp
226 public static boolean checkBpType(char up, char down)
289 * Compute all or part of the annotation row from the given consensus
293 * - pre-allocated annotation row
297 * @param ignoreGapsInConsensusCalculation
298 * @param includeAllConsSymbols
300 public static void completeConsensus(AlignmentAnnotation consensus,
301 Hashtable[] hconsensus, int iStart, int width,
302 boolean ignoreGapsInConsensusCalculation,
303 boolean includeAllConsSymbols, long nseq)
306 if (consensus == null || consensus.annotations == null
307 || consensus.annotations.length < width)
309 // called with a bad alignment annotation row - wait for it to be
310 // initialised properly
313 String fmtstr="%3.1f";
321 fmtstr = "%"+(2+precision)+"."+precision+"f";
323 Format fmt = new Format(fmtstr);
325 for (int i = iStart; i < width; i++)
328 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
330 // happens if sequences calculated over were shorter than alignment
332 consensus.annotations[i] = null;
337 if (ignoreGapsInConsensusCalculation)
339 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
343 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
347 consensus.annotations[i] = null;
348 // data has changed below us .. give up and
351 value = fv.floatValue();
352 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
353 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
354 if (maxRes.length() > 1)
356 mouseOver = "[" + maxRes + "] ";
359 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
360 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
362 if (pairs != null && includeAllConsSymbols) // Just responsible for the
364 // TODO Update tooltips for Structure row
369 * TODO It's not sure what is the purpose of the alphabet and wheter it
370 * is useful for structure?
372 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
373 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
374 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
375 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
378 Object[] ca = new Object[625];
379 float[] vl = new float[625];
381 for (int c = 65; c < 90; c++)
383 for (int d = 65; d < 90; d++)
391 jalview.util.QuickSort.sort(vl, ca);
394 for (int c = 624; c > 0; c--)
398 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
400 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
401 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%";
411 mouseOver += (fmt.form(value) + "%");
413 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
419 * get the sorted base-pair profile for the given position of the consensus
422 * @return profile of the given column
424 public static int[] extractProfile(Hashtable hconsensus,
425 boolean ignoreGapsInConsensusCalculation)
427 int[] rtnval = new int[74]; // 2*(5*5)+2
428 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
429 int[][] pairs = (int[][]) hconsensus
430 .get(StructureFrequency.PAIRPROFILE);
435 // TODO fix the object length, also do it in completeConsensus
436 Object[] ca = new Object[625];
437 float[] vl = new float[625];
439 for (int c = 65; c < 90; c++)
441 for (int d = 65; d < 90; d++)
449 jalview.util.QuickSort.sort(vl, ca);
453 for (int c = 624; c > 0; c--)
457 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
458 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
459 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
461 rtnval[1] += rtnval[rtnval[0]++];
468 public static void main(String args[])
470 // Short test to see if checkBpType works
471 ArrayList<String> test = new ArrayList<String>();
477 for (String i : test)
479 for (String j : test)
481 System.out.println(i + "-" + j + ": "
482 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));