2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Format;
28 import jalview.util.QuickSort;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
34 * Takes in a vector or array of sequences and column start and column end and
35 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
36 * This class is used extensively in calculating alignment colourschemes that
37 * depend on the amount of conservation in each alignment column.
42 public class StructureFrequency
44 public static final int STRUCTURE_PROFILE_LENGTH = 74;
46 // No need to store 1000s of strings which are not
47 // visible to the user.
48 public static final String MAXCOUNT = "C";
50 public static final String MAXRESIDUE = "R";
52 public static final String PID_GAPS = "G";
54 public static final String PID_NOGAPS = "N";
56 public static final String PROFILE = "P";
58 public static final String PAIRPROFILE = "B";
61 * Returns the 3' position of a base pair
64 * Secondary structure annotation
66 * 5' position of a base pair
67 * @return 3' position of a base pair
69 public static int findPair(SequenceFeature[] pairs, int indice)
72 for (int i = 0; i < pairs.length; i++)
74 if (pairs[i].getBegin() == indice)
78 return pairs[i].getEnd();
86 * Method to calculate a 'base pair consensus row', very similar to nucleotide
87 * consensus but takes into account a given structure
96 public static final void calculate(SequenceI[] sequences, int start,
97 int end, Hashtable[] result, boolean profile,
98 AlignmentAnnotation rnaStruc)
101 Hashtable residueHash;
103 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;
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 != '[')
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);
159 // standard representation for gaps in sequence and structure
160 if (c == '.' || c == ' ')
170 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)
194 // UPDATE this for new values
197 // TODO 1-dim array with jsize in [0], nongapped in [1]; or Pojo
198 residueHash.put(PROFILE, new int[][]
200 { jSize, (jSize - values['-']) } });
202 residueHash.put(PAIRPROFILE, pairs);
212 residueHash.put(MAXCOUNT, new Integer(count));
213 residueHash.put(MAXRESIDUE, maxResidue);
215 percentage = ((float) count * 100) / jSize;
216 residueHash.put(PID_GAPS, new Float(percentage));
218 // percentage = ((float) count * 100) / (float) nongap;
219 // residueHash.put(PID_NOGAPS, new Float(percentage));
220 if (result[i] == null)
222 result[i] = residueHash;
226 values[')'] = values['('];
227 values[']'] = values['['];
230 residueHash = new Hashtable();
233 // 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, 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";
367 fmtstr = "%" + (2 + precision) + "." + precision + "f";
369 Format fmt = new Format(fmtstr);
371 for (int i = iStart; i < width; i++)
374 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
376 // happens if sequences calculated over were shorter than alignment
378 consensus.annotations[i] = null;
383 if (ignoreGapsInConsensusCalculation)
385 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
389 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
393 consensus.annotations[i] = null;
394 // data has changed below us .. give up and
397 value = fv.floatValue();
398 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
399 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
400 if (maxRes.length() > 1)
402 mouseOver = "[" + maxRes + "] ";
405 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
406 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
408 if (pairs != null && includeAllConsSymbols) // Just responsible for the
410 // TODO Update tooltips for Structure row
415 * TODO It's not sure what is the purpose of the alphabet and wheter it
416 * is useful for structure?
418 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
419 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
420 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
421 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
424 int[][] ca = javajs.util.AU.newInt2(625);// BH new int[625][];
425 float[] vl = new float[625];
427 for (int c = 65; c < 90; c++)
429 for (int d = 65; d < 90; d++)
437 QuickSort.sortFloatObject(vl, ca);
441 * profile[1] is {total, ungappedTotal}
443 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
445 for (int c = 624; c > 0; c--)
449 tval = (vl[c] * 100f / divisor);
450 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
451 + (char) ca[c][1] + " " + fmt.formDouble(tval)
462 mouseOver += (fmt.formDouble(value) + "%");
464 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
470 * get the sorted base-pair profile for the given position of the consensus
473 * @return profile of the given column
475 public static int[] extractProfile(Hashtable hconsensus,
476 boolean ignoreGapsInConsensusCalculation)
478 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
479 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
480 int[][] pairs = (int[][]) hconsensus
481 .get(StructureFrequency.PAIRPROFILE);
488 // TODO fix the object length, also do it in completeConsensus
489 // Object[] ca = new Object[625];
490 int[][] ca = javajs.util.AU.newInt2(625);// BH new int[625][];
491 float[] vl = new float[625];
493 for (int c = 65; c < 90; c++)
495 for (int d = 65; d < 90; d++)
503 QuickSort.sortFloatObject(vl, ca);
508 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
509 for (int c = 624; c > 0; c--)
513 rtnval[offset++] = ca[c][0];
514 rtnval[offset++] = ca[c][1];
515 rtnval[offset] = (int) (vl[c] * 100f / divisor);
516 rtnval[1] += rtnval[offset++];
520 rtnval[0] = valuesCount;
522 // insert profile type code in position 0
523 int[] result = new int[rtnval.length + 1];
524 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
525 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
534 public static void main(String args[])
536 // Short test to see if checkBpType works
537 ArrayList<String> test = new ArrayList<String>();
543 for (String i : test)
545 for (String j : test)
547 System.out.println(i + "-" + j + ": "
548 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));