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.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)
62 System.out.print("indice"+indice+" ");
63 for (int i = 0; i < pairs.length; i++)
65 if (pairs[i].getBegin() == indice)
68 System.out.println(pairs[i].getEnd());
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)
91 // System.out.println("longueur="+sequences.length);
92 // for(int l=0;l<=(sequences.length-1);l++){
93 // System.out.println("sequences "+l+":"+sequences[l].getSequenceAsString());
95 // System.out.println("start="+start);
96 System.out.println("end="+end);
97 // System.out.println("result="+result.length);
99 // System.out.println("profile="+profile);
100 // System.out.println("rnaStruc="+rnaStruc);
101 Hashtable residueHash;
103 char[] struc = rnaStruc.getRNAStruc().toCharArray();
104 SequenceFeature[] rna = rnaStruc._rnasecstr;
106 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
110 boolean wooble = true;
112 for (i = start; i < end; i++) // foreach column
114 residueHash = new Hashtable();
116 values = new int[255];
117 pairs = new int[255][255];
119 //System.out.println("s="+struc[i]);
120 if (i < struc.length)
129 if (s == '.' || s == ' ')
134 if (s != '(' && s != '[')
145 bpEnd = findPair(rna, i);
149 for (j = 0; j < jSize; j++) // foreach row
151 if (sequences[j] == null)
154 .println("WARNING: Consensus skipping null sequence - possible race condition.");
157 c = sequences[j].getCharAt(i);
158 //System.out.println("c="+c);
161 // standard representation for gaps in sequence and structure
162 if (c == '.' || c == ' ')
165 .println("WARNING: Consensus skipping null sequence - possible race condition.");
168 cEnd = sequences[j].getCharAt(bpEnd);
171 System.out.println("pairs ="+c+","+cEnd);
172 if (checkBpType(c, cEnd)==true)
174 values['(']++; // H means it's a helix (structured)
177 System.out.println("It's a pair wc");
180 if (checkBpType(c, cEnd)==false)
183 values['[']++; // H means it's a helix (structured)
185 System.out.println("It's an pair non canonic");
186 System.out.println(sequences[j].getRNA());
187 System.out.println(rnaStruc.getRNAStruc().charAt(i));
197 // UPDATE this for new values
200 residueHash.put(PROFILE, new int[][]
202 { jSize, (jSize - values['-']) } });
204 residueHash.put(PAIRPROFILE, pairs);
214 residueHash.put(MAXCOUNT, new Integer(count));
215 residueHash.put(MAXRESIDUE, maxResidue);
217 percentage = ((float) count * 100) / jSize;
218 residueHash.put(PID_GAPS, new Float(percentage));
220 // percentage = ((float) count * 100) / (float) nongap;
221 // residueHash.put(PID_NOGAPS, new Float(percentage));
222 if (result[i] == null)
224 result[i] = residueHash;
228 values[')'] = values['('];
229 values[']'] = values['['];
232 residueHash = new Hashtable();
234 System.out.println(maxResidue+","+wooble);
238 System.out.println(maxResidue+","+wooble);
243 residueHash.put(PROFILE, new int[][]
245 { jSize, (jSize - values['-']) } });
247 residueHash.put(PAIRPROFILE, pairs);
250 residueHash.put(MAXCOUNT, new Integer(count));
251 residueHash.put(MAXRESIDUE, maxResidue);
253 percentage = ((float) count * 100) / jSize;
254 residueHash.put(PID_GAPS, new Float(percentage));
256 result[bpEnd] = residueHash;
262 * Method to check if a base-pair is a canonical or a wobble bp
268 * @return True if it is a canonical/wobble bp
270 public static boolean checkBpType(char up, char down)
333 * Compute all or part of the annotation row from the given consensus
337 * - pre-allocated annotation row
341 * @param ignoreGapsInConsensusCalculation
342 * @param includeAllConsSymbols
344 public static void completeConsensus(AlignmentAnnotation consensus,
345 Hashtable[] hconsensus, int iStart, int width,
346 boolean ignoreGapsInConsensusCalculation,
347 boolean includeAllConsSymbols)
350 if (consensus == null || consensus.annotations == null
351 || consensus.annotations.length < width)
353 // called with a bad alignment annotation row - wait for it to be
354 // initialised properly
357 for (int i = iStart; i < width; i++)
360 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
362 // happens if sequences calculated over were shorter than alignment
364 consensus.annotations[i] = null;
369 if (ignoreGapsInConsensusCalculation)
371 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
375 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
379 consensus.annotations[i] = null;
380 // data has changed below us .. give up and
383 value = fv.floatValue();
384 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
385 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
386 if (maxRes.length() > 1)
388 mouseOver = "[" + maxRes + "] ";
391 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
392 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
394 if (pairs != null && includeAllConsSymbols) // Just responsible for the
396 // TODO Update tooltips for Structure row
401 * TODO It's not sure what is the purpose of the alphabet and wheter it
402 * is useful for structure?
404 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
405 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
406 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
407 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
410 Object[] ca = new Object[625];
411 float[] vl = new float[625];
413 for (int c = 65; c < 90; c++)
415 for (int d = 65; d < 90; d++)
423 jalview.util.QuickSort.sort(vl, ca);
426 for (int c = 624; c > 0; c--)
430 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
432 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
433 + (char) ((int[]) ca[c])[1] + " " + ((int) tval) + "%";
443 mouseOver += ((int) value + "%");
445 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
451 * get the sorted base-pair profile for the given position of the consensus
454 * @return profile of the given column
456 public static int[] extractProfile(Hashtable hconsensus,
457 boolean ignoreGapsInConsensusCalculation)
459 int[] rtnval = new int[74]; // 2*(5*5)+2
460 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
461 int[][] pairs = (int[][]) hconsensus
462 .get(StructureFrequency.PAIRPROFILE);
467 // TODO fix the object length, also do it in completeConsensus
468 Object[] ca = new Object[625];
469 float[] vl = new float[625];
471 for (int c = 65; c < 90; c++)
473 for (int d = 65; d < 90; d++)
481 jalview.util.QuickSort.sort(vl, ca);
485 for (int c = 624; c > 0; c--)
489 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
490 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
491 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
493 rtnval[1] += rtnval[rtnval[0]++];
500 public static void main(String args[])
502 // Short test to see if checkBpType works
503 ArrayList<String> test = new ArrayList<String>();
509 for (String i : test)
511 for (String j : test)
513 System.out.println(i + "-" + j + ": "
514 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));