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/>.
19 package jalview.analysis;
23 import jalview.util.Format;
24 import jalview.datamodel.*;
27 * Takes in a vector or array of sequences and column start and column end and
28 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
29 * This class is used extensively in calculating alignment colourschemes that
30 * depend on the amount of conservation in each alignment column.
35 public class StructureFrequency
37 // No need to store 1000s of strings which are not
38 // visible to the user.
39 public static final String MAXCOUNT = "C";
41 public static final String MAXRESIDUE = "R";
43 public static final String PID_GAPS = "G";
45 public static final String PID_NOGAPS = "N";
47 public static final String PROFILE = "P";
49 public static final String PAIRPROFILE = "B";
52 * Returns the 3' position of a base pair
55 * Secondary structure annotation
57 * 5' position of a base pair
58 * @return 3' position of a base pair
60 public static int findPair(SequenceFeature[] pairs, int indice)
63 for (int i = 0; i < pairs.length; i++)
65 if (pairs[i].getBegin() == indice)
69 return pairs[i].getEnd();
77 * Method to calculate a 'base pair consensus row', very similar to nucleotide
78 * consensus but takes into account a given structure
87 public static final void calculate(SequenceI[] sequences, int start,
88 int end, Hashtable[] result, boolean profile,
89 AlignmentAnnotation rnaStruc)
92 Hashtable residueHash;
94 char[] struc = rnaStruc.getRNAStruc().toCharArray();
96 SequenceFeature[] rna = rnaStruc._rnasecstr;
98 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
102 boolean wooble = true;
103 for (i = start; i < end; i++) // foreach column
105 residueHash = new Hashtable();
107 values = new int[255];
108 pairs = new int[255][255];
110 // System.out.println("s="+struc[i]);
111 if (i < struc.length)
120 if (s == '.' || s == ' ')
125 if (s != '(' && s != '[')
135 bpEnd = findPair(rna, i);
139 for (j = 0; j < jSize; j++) // foreach row
141 if (sequences[j] == null)
144 .println("WARNING: Consensus skipping null sequence - possible race condition.");
147 c = sequences[j].getCharAt(i);
148 // System.out.println("c="+c);
150 // standard representation for gaps in sequence and structure
151 if (c == '.' || c == ' ')
161 cEnd = sequences[j].getCharAt(bpEnd);
163 // System.out.println("pairs ="+c+","+cEnd);
164 if (checkBpType(c, cEnd) == true)
166 values['(']++; // H means it's a helix (structured)
169 // System.out.println("It's a pair wc");
172 if (checkBpType(c, cEnd) == false)
175 values['[']++; // H means it's a helix (structured)
185 // UPDATE this for new values
188 residueHash.put(PROFILE, new int[][]
190 { jSize, (jSize - values['-']) } });
192 residueHash.put(PAIRPROFILE, pairs);
202 residueHash.put(MAXCOUNT, new Integer(count));
203 residueHash.put(MAXRESIDUE, maxResidue);
205 percentage = ((float) count * 100) / jSize;
206 residueHash.put(PID_GAPS, new Float(percentage));
208 // percentage = ((float) count * 100) / (float) nongap;
209 // residueHash.put(PID_NOGAPS, new Float(percentage));
210 if (result[i] == null)
212 result[i] = residueHash;
216 values[')'] = values['('];
217 values[']'] = values['['];
220 residueHash = new Hashtable();
223 // System.out.println(maxResidue+","+wooble);
228 // System.out.println(maxResidue+","+wooble);
233 residueHash.put(PROFILE, new int[][]
235 { jSize, (jSize - values['-']) } });
237 residueHash.put(PAIRPROFILE, pairs);
240 residueHash.put(MAXCOUNT, new Integer(count));
241 residueHash.put(MAXRESIDUE, maxResidue);
243 percentage = ((float) count * 100) / jSize;
244 residueHash.put(PID_GAPS, new Float(percentage));
246 result[bpEnd] = residueHash;
253 * Method to check if a base-pair is a canonical or a wobble bp
259 * @return True if it is a canonical/wobble bp
261 public static boolean checkBpType(char up, char down)
324 * Compute all or part of the annotation row from the given consensus
328 * - pre-allocated annotation row
332 * @param ignoreGapsInConsensusCalculation
333 * @param includeAllConsSymbols
335 public static void completeConsensus(AlignmentAnnotation consensus,
336 Hashtable[] hconsensus, int iStart, int width,
337 boolean ignoreGapsInConsensusCalculation,
338 boolean includeAllConsSymbols, long nseq)
341 if (consensus == null || consensus.annotations == null
342 || consensus.annotations.length < width)
344 // called with a bad alignment annotation row - wait for it to be
345 // initialised properly
348 String fmtstr="%3.1f";
356 fmtstr = "%"+(2+precision)+"."+precision+"f";
358 Format fmt = new Format(fmtstr);
360 for (int i = iStart; i < width; i++)
363 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
365 // happens if sequences calculated over were shorter than alignment
367 consensus.annotations[i] = null;
372 if (ignoreGapsInConsensusCalculation)
374 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
378 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
382 consensus.annotations[i] = null;
383 // data has changed below us .. give up and
386 value = fv.floatValue();
387 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
388 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
389 if (maxRes.length() > 1)
391 mouseOver = "[" + maxRes + "] ";
394 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
395 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
397 if (pairs != null && includeAllConsSymbols) // Just responsible for the
399 // TODO Update tooltips for Structure row
404 * TODO It's not sure what is the purpose of the alphabet and wheter it
405 * is useful for structure?
407 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
408 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
409 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
410 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
413 Object[] ca = new Object[625];
414 float[] vl = new float[625];
416 for (int c = 65; c < 90; c++)
418 for (int d = 65; d < 90; d++)
426 jalview.util.QuickSort.sort(vl, ca);
429 for (int c = 624; c > 0; c--)
433 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
435 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
436 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%";
446 mouseOver += (fmt.form(value) + "%");
448 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
454 * get the sorted base-pair profile for the given position of the consensus
457 * @return profile of the given column
459 public static int[] extractProfile(Hashtable hconsensus,
460 boolean ignoreGapsInConsensusCalculation)
462 int[] rtnval = new int[74]; // 2*(5*5)+2
463 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
464 int[][] pairs = (int[][]) hconsensus
465 .get(StructureFrequency.PAIRPROFILE);
470 // TODO fix the object length, also do it in completeConsensus
471 Object[] ca = new Object[625];
472 float[] vl = new float[625];
474 for (int c = 65; c < 90; c++)
476 for (int d = 65; d < 90; d++)
484 jalview.util.QuickSort.sort(vl, ca);
488 for (int c = 624; c > 0; c--)
492 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
493 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
494 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
496 rtnval[1] += rtnval[rtnval[0]++];
503 public static void main(String args[])
505 // Short test to see if checkBpType works
506 ArrayList<String> test = new ArrayList<String>();
512 for (String i : test)
514 for (String j : test)
516 System.out.println(i + "-" + j + ": "
517 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));