2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.datamodel.*;
26 * Takes in a vector or array of sequences and column start and column end and
27 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
28 * This class is used extensively in calculating alignment colourschemes that
29 * depend on the amount of conservation in each alignment column.
34 public class StructureFrequency
36 // No need to store 1000s of strings which are not
37 // visible to the user.
38 public static final String MAXCOUNT = "C";
40 public static final String MAXRESIDUE = "R";
42 public static final String PID_GAPS = "G";
44 public static final String PID_NOGAPS = "N";
46 public static final String PROFILE = "P";
48 public static final String PAIRPROFILE = "B";
51 * Returns the 3' position of a base pair
54 * Secondary structure annotation
56 * 5' position of a base pair
57 * @return 3' position of a base pair
59 public static int findPair(SequenceFeature[] pairs, int indice)
61 System.out.print("indice"+indice+" ");
62 for (int i = 0; i < pairs.length; i++)
64 if (pairs[i].getBegin() == indice)
67 System.out.println(pairs[i].getEnd());
68 return pairs[i].getEnd();
76 * Method to calculate a 'base pair consensus row', very similar to nucleotide
77 * consensus but takes into account a given structure
86 public static final void calculate(SequenceI[] sequences, int start,
87 int end, Hashtable[] result, boolean profile,
88 AlignmentAnnotation rnaStruc)
90 // System.out.println("longueur="+sequences.length);
91 // for(int l=0;l<=(sequences.length-1);l++){
92 // System.out.println("sequences "+l+":"+sequences[l].getSequenceAsString());
94 // System.out.println("start="+start);
95 System.out.println("end="+end);
96 // System.out.println("result="+result.length);
98 // System.out.println("profile="+profile);
99 // System.out.println("rnaStruc="+rnaStruc);
100 Hashtable residueHash;
102 char[] struc = rnaStruc.getRNAStruc().toCharArray();
103 SequenceFeature[] rna = rnaStruc._rnasecstr;
105 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
109 boolean wooble = true;
111 for (i = start; i < end; i++) // foreach column
113 residueHash = new Hashtable();
115 values = new int[255];
116 pairs = new int[255][255];
118 //System.out.println("s="+struc[i]);
119 if (i < struc.length)
128 if (s == '.' || s == ' ')
133 if (s != '(' && s != '[')
144 bpEnd = findPair(rna, i);
148 for (j = 0; j < jSize; j++) // foreach row
150 if (sequences[j] == null)
153 .println("WARNING: Consensus skipping null sequence - possible race condition.");
156 c = sequences[j].getCharAt(i);
157 //System.out.println("c="+c);
160 // standard representation for gaps in sequence and structure
161 if (c == '.' || c == ' ')
171 cEnd = sequences[j].getCharAt(bpEnd);
174 System.out.println("pairs ="+c+","+cEnd);
175 if (checkBpType(c, cEnd)==true)
177 values['(']++; // H means it's a helix (structured)
180 System.out.println("It's a pair wc");
183 if (checkBpType(c, cEnd)==false)
186 values['[']++; // H means it's a helix (structured)
188 System.out.println("It's an pair non canonic");
189 System.out.println(sequences[j].getRNA());
190 System.out.println(rnaStruc.getRNAStruc().charAt(i));
199 // UPDATE this for new values
202 residueHash.put(PROFILE, new int[][]
204 { jSize, (jSize - values['-']) } });
206 residueHash.put(PAIRPROFILE, pairs);
216 residueHash.put(MAXCOUNT, new Integer(count));
217 residueHash.put(MAXRESIDUE, maxResidue);
219 percentage = ((float) count * 100) / jSize;
220 residueHash.put(PID_GAPS, new Float(percentage));
222 // percentage = ((float) count * 100) / (float) nongap;
223 // residueHash.put(PID_NOGAPS, new Float(percentage));
224 if (result[i] == null)
226 result[i] = residueHash;
230 values[')'] = values['('];
231 values[']'] = values['['];
234 residueHash = new Hashtable();
236 System.out.println(maxResidue+","+wooble);
240 System.out.println(maxResidue+","+wooble);
245 residueHash.put(PROFILE, new int[][]
247 { jSize, (jSize - values['-']) } });
249 residueHash.put(PAIRPROFILE, pairs);
252 residueHash.put(MAXCOUNT, new Integer(count));
253 residueHash.put(MAXRESIDUE, maxResidue);
255 percentage = ((float) count * 100) / jSize;
256 residueHash.put(PID_GAPS, new Float(percentage));
258 result[bpEnd] = residueHash;
265 * Method to check if a base-pair is a canonical or a wobble bp
271 * @return True if it is a canonical/wobble bp
273 public static boolean checkBpType(char up, char down)
336 * Compute all or part of the annotation row from the given consensus
340 * - pre-allocated annotation row
344 * @param ignoreGapsInConsensusCalculation
345 * @param includeAllConsSymbols
347 public static void completeConsensus(AlignmentAnnotation consensus,
348 Hashtable[] hconsensus, int iStart, int width,
349 boolean ignoreGapsInConsensusCalculation,
350 boolean includeAllConsSymbols)
353 if (consensus == null || consensus.annotations == null
354 || consensus.annotations.length < width)
356 // called with a bad alignment annotation row - wait for it to be
357 // initialised properly
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)
389 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE)
391 if (maxRes.length() > 1)
393 mouseOver = "[" + maxRes + "] ";
396 int[][] profile = (int[][]) hci
397 .get(StructureFrequency.PROFILE);
398 int[][] pairs = (int[][]) hci
399 .get(StructureFrequency.PAIRPROFILE);
401 if (pairs != null && includeAllConsSymbols) // Just responsible for the
403 // TODO Update tooltips for Structure row
407 /* TODO It's not sure what is the purpose of the alphabet and wheter it 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] + " " + ((int) tval) + "%";
448 mouseOver += ((int) 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)));