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 System.out.print("indice"+indice+" ");
64 for (int i = 0; i < pairs.length; i++)
66 if (pairs[i].getBegin() == indice)
69 System.out.println(pairs[i].getEnd());
70 return pairs[i].getEnd();
78 * Method to calculate a 'base pair consensus row', very similar to nucleotide
79 * consensus but takes into account a given structure
88 public static final void calculate(SequenceI[] sequences, int start,
89 int end, Hashtable[] result, boolean profile,
90 AlignmentAnnotation rnaStruc)
92 // System.out.println("longueur="+sequences.length);
93 // for(int l=0;l<=(sequences.length-1);l++){
94 // System.out.println("sequences "+l+":"+sequences[l].getSequenceAsString());
96 // System.out.println("start="+start);
97 System.out.println("end="+end);
98 // System.out.println("result="+result.length);
100 // System.out.println("profile="+profile);
101 // System.out.println("rnaStruc="+rnaStruc);
102 Hashtable residueHash;
104 char[] struc = rnaStruc.getRNAStruc().toCharArray();
105 SequenceFeature[] rna = rnaStruc._rnasecstr;
107 int count = 0, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
111 boolean wooble = true;
113 for (i = start; i < end; i++) // foreach column
115 residueHash = new Hashtable();
117 values = new int[255];
118 pairs = new int[255][255];
120 //System.out.println("s="+struc[i]);
121 if (i < struc.length)
130 if (s == '.' || s == ' ')
135 if (s != '(' && s != '[')
146 bpEnd = findPair(rna, i);
150 for (j = 0; j < jSize; j++) // foreach row
152 if (sequences[j] == null)
155 .println("WARNING: Consensus skipping null sequence - possible race condition.");
158 c = sequences[j].getCharAt(i);
159 //System.out.println("c="+c);
162 // standard representation for gaps in sequence and structure
163 if (c == '.' || c == ' ')
166 .println("WARNING: Consensus skipping null sequence - possible race condition.");
169 cEnd = sequences[j].getCharAt(bpEnd);
172 System.out.println("pairs ="+c+","+cEnd);
173 if (checkBpType(c, cEnd)==true)
175 values['(']++; // H means it's a helix (structured)
178 System.out.println("It's a pair wc");
181 if (checkBpType(c, cEnd)==false)
184 values['[']++; // H means it's a helix (structured)
186 System.out.println("It's an pair non canonic");
187 System.out.println(sequences[j].getRNA());
188 System.out.println(rnaStruc.getRNAStruc().charAt(i));
198 // UPDATE this for new values
201 residueHash.put(PROFILE, new int[][]
203 { jSize, (jSize - values['-']) } });
205 residueHash.put(PAIRPROFILE, pairs);
215 residueHash.put(MAXCOUNT, new Integer(count));
216 residueHash.put(MAXRESIDUE, maxResidue);
218 percentage = ((float) count * 100) / jSize;
219 residueHash.put(PID_GAPS, new Float(percentage));
221 // percentage = ((float) count * 100) / (float) nongap;
222 // residueHash.put(PID_NOGAPS, new Float(percentage));
223 if (result[i] == null)
225 result[i] = residueHash;
229 values[')'] = values['('];
230 values[']'] = values['['];
233 residueHash = new Hashtable();
235 System.out.println(maxResidue+","+wooble);
239 System.out.println(maxResidue+","+wooble);
244 residueHash.put(PROFILE, new int[][]
246 { jSize, (jSize - values['-']) } });
248 residueHash.put(PAIRPROFILE, pairs);
251 residueHash.put(MAXCOUNT, new Integer(count));
252 residueHash.put(MAXRESIDUE, maxResidue);
254 percentage = ((float) count * 100) / jSize;
255 residueHash.put(PID_GAPS, new Float(percentage));
257 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, long nseq)
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 String fmtstr="%3.1f";
366 fmtstr = "%"+(2+precision)+"."+precision+"f";
368 Format fmt = new Format(fmtstr);
370 for (int i = iStart; i < width; i++)
373 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
375 // happens if sequences calculated over were shorter than alignment
377 consensus.annotations[i] = null;
382 if (ignoreGapsInConsensusCalculation)
384 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
388 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
392 consensus.annotations[i] = null;
393 // data has changed below us .. give up and
396 value = fv.floatValue();
397 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
398 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
399 if (maxRes.length() > 1)
401 mouseOver = "[" + maxRes + "] ";
404 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
405 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
407 if (pairs != null && includeAllConsSymbols) // Just responsible for the
409 // TODO Update tooltips for Structure row
414 * TODO It's not sure what is the purpose of the alphabet and wheter it
415 * is useful for structure?
417 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
418 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
419 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
420 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
423 Object[] ca = new Object[625];
424 float[] vl = new float[625];
426 for (int c = 65; c < 90; c++)
428 for (int d = 65; d < 90; d++)
436 jalview.util.QuickSort.sort(vl, ca);
439 for (int c = 624; c > 0; c--)
443 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
445 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
446 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%";
456 mouseOver += (fmt.form(value) + "%");
458 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
464 * get the sorted base-pair profile for the given position of the consensus
467 * @return profile of the given column
469 public static int[] extractProfile(Hashtable hconsensus,
470 boolean ignoreGapsInConsensusCalculation)
472 int[] rtnval = new int[74]; // 2*(5*5)+2
473 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
474 int[][] pairs = (int[][]) hconsensus
475 .get(StructureFrequency.PAIRPROFILE);
480 // TODO fix the object length, also do it in completeConsensus
481 Object[] ca = new Object[625];
482 float[] vl = new float[625];
484 for (int c = 65; c < 90; c++)
486 for (int d = 65; d < 90; d++)
494 jalview.util.QuickSort.sort(vl, ca);
498 for (int c = 624; c > 0; c--)
502 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
503 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
504 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
506 rtnval[1] += rtnval[rtnval[0]++];
513 public static void main(String args[])
515 // Short test to see if checkBpType works
516 ArrayList<String> test = new ArrayList<String>();
522 for (String i : test)
524 for (String j : test)
526 System.out.println(i + "-" + j + ": "
527 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));