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;
263 * Method to check if a base-pair is a canonical or a wobble bp
269 * @return True if it is a canonical/wobble bp
271 public static boolean checkBpType(char up, char down)
334 * Compute all or part of the annotation row from the given consensus
338 * - pre-allocated annotation row
342 * @param ignoreGapsInConsensusCalculation
343 * @param includeAllConsSymbols
345 public static void completeConsensus(AlignmentAnnotation consensus,
346 Hashtable[] hconsensus, int iStart, int width,
347 boolean ignoreGapsInConsensusCalculation,
348 boolean includeAllConsSymbols)
351 if (consensus == null || consensus.annotations == null
352 || consensus.annotations.length < width)
354 // called with a bad alignment annotation row - wait for it to be
355 // initialised properly
358 for (int i = iStart; i < width; i++)
361 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
363 // happens if sequences calculated over were shorter than alignment
365 consensus.annotations[i] = null;
370 if (ignoreGapsInConsensusCalculation)
372 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
376 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
380 consensus.annotations[i] = null;
381 // data has changed below us .. give up and
384 value = fv.floatValue();
385 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
386 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
387 if (maxRes.length() > 1)
389 mouseOver = "[" + maxRes + "] ";
392 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
393 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
395 if (pairs != null && includeAllConsSymbols) // Just responsible for the
397 // TODO Update tooltips for Structure row
402 * TODO It's not sure what is the purpose of the alphabet and wheter it
403 * is useful for structure?
405 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
406 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
407 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
408 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
411 Object[] ca = new Object[625];
412 float[] vl = new float[625];
414 for (int c = 65; c < 90; c++)
416 for (int d = 65; d < 90; d++)
424 jalview.util.QuickSort.sort(vl, ca);
427 for (int c = 624; c > 0; c--)
431 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
433 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
434 + (char) ((int[]) ca[c])[1] + " " + ((int) tval) + "%";
444 mouseOver += ((int) value + "%");
446 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
452 * get the sorted base-pair profile for the given position of the consensus
455 * @return profile of the given column
457 public static int[] extractProfile(Hashtable hconsensus,
458 boolean ignoreGapsInConsensusCalculation)
460 int[] rtnval = new int[74]; // 2*(5*5)+2
461 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
462 int[][] pairs = (int[][]) hconsensus
463 .get(StructureFrequency.PAIRPROFILE);
468 // TODO fix the object length, also do it in completeConsensus
469 Object[] ca = new Object[625];
470 float[] vl = new float[625];
472 for (int c = 65; c < 90; c++)
474 for (int d = 65; d < 90; d++)
482 jalview.util.QuickSort.sort(vl, ca);
486 for (int c = 624; c > 0; c--)
490 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
491 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
492 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
494 rtnval[1] += rtnval[rtnval[0]++];
501 public static void main(String args[])
503 // Short test to see if checkBpType works
504 ArrayList<String> test = new ArrayList<String>();
510 for (String i : test)
512 for (String j : test)
514 System.out.println(i + "-" + j + ": "
515 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));