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;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
33 * Takes in a vector or array of sequences and column start and column end and
34 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
35 * This class is used extensively in calculating alignment colourschemes that
36 * depend on the amount of conservation in each alignment column.
41 public class StructureFrequency
43 public static final int STRUCTURE_PROFILE_LENGTH = 74;
45 // No need to store 1000s of strings which are not
46 // visible to the user.
47 public static final String MAXCOUNT = "C";
49 public static final String MAXRESIDUE = "R";
51 public static final String PID_GAPS = "G";
53 public static final String PID_NOGAPS = "N";
55 public static final String PROFILE = "P";
57 public static final String PAIRPROFILE = "B";
60 * Returns the 3' position of a base pair
63 * Secondary structure annotation
65 * 5' position of a base pair
66 * @return 3' position of a base pair
68 public static int findPair(SequenceFeature[] pairs, int indice)
71 for (int i = 0; i < pairs.length; i++)
73 if (pairs[i].getBegin() == indice)
77 return pairs[i].getEnd();
85 * Method to calculate a 'base pair consensus row', very similar to nucleotide
86 * consensus but takes into account a given structure
95 public static final void calculate(SequenceI[] sequences, int start,
96 int end, Hashtable[] result, boolean profile,
97 AlignmentAnnotation rnaStruc)
100 Hashtable residueHash;
102 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;
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 != '[')
143 bpEnd = findPair(rna, i);
147 for (j = 0; j < jSize; j++) // foreach row
149 if (sequences[j] == null)
152 .println("WARNING: Consensus skipping null sequence - possible race condition.");
155 c = sequences[j].getCharAt(i);
156 // System.out.println("c="+c);
158 // standard representation for gaps in sequence and structure
159 if (c == '.' || c == ' ')
169 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)
193 // UPDATE this for new values
196 // TODO 1-dim array with jsize in [0], nongapped in [1]; or Pojo
197 residueHash.put(PROFILE, new int[][]
199 { jSize, (jSize - values['-']) } });
201 residueHash.put(PAIRPROFILE, pairs);
211 residueHash.put(MAXCOUNT, new Integer(count));
212 residueHash.put(MAXRESIDUE, maxResidue);
214 percentage = ((float) count * 100) / jSize;
215 residueHash.put(PID_GAPS, new Float(percentage));
217 // percentage = ((float) count * 100) / (float) nongap;
218 // residueHash.put(PID_NOGAPS, new Float(percentage));
219 if (result[i] == null)
221 result[i] = residueHash;
225 values[')'] = values['('];
226 values[']'] = values['['];
229 residueHash = new Hashtable();
232 // System.out.println(maxResidue+","+wooble);
237 // System.out.println(maxResidue+","+wooble);
242 residueHash.put(PROFILE, new int[][]
244 { jSize, (jSize - values['-']) } });
246 residueHash.put(PAIRPROFILE, pairs);
249 residueHash.put(MAXCOUNT, new Integer(count));
250 residueHash.put(MAXRESIDUE, maxResidue);
252 percentage = ((float) count * 100) / jSize;
253 residueHash.put(PID_GAPS, new Float(percentage));
255 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, long nseq)
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 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 int[][] ca = new int[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);
440 * profile[1] is {total, ungappedTotal}
442 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
444 for (int c = 624; c > 0; c--)
448 tval = (vl[c] * 100f / divisor);
449 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
450 + (char) ca[c][1] + " " + fmt.form(tval)
461 mouseOver += (fmt.form(value) + "%");
463 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
469 * get the sorted base-pair profile for the given position of the consensus
472 * @return profile of the given column
474 public static int[] extractProfile(Hashtable hconsensus,
475 boolean ignoreGapsInConsensusCalculation)
477 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
478 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
479 int[][] pairs = (int[][]) hconsensus
480 .get(StructureFrequency.PAIRPROFILE);
487 // TODO fix the object length, also do it in completeConsensus
488 // Object[] ca = new Object[625];
489 int[][] ca = new int[625][];
490 float[] vl = new float[625];
492 for (int c = 65; c < 90; c++)
494 for (int d = 65; d < 90; d++)
502 jalview.util.QuickSort.sort(vl, ca);
507 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
508 for (int c = 624; c > 0; c--)
512 rtnval[offset++] = ca[c][0];
513 rtnval[offset++] = ca[c][1];
514 rtnval[offset] = (int) (vl[c] * 100f / divisor);
515 rtnval[1] += rtnval[offset++];
519 rtnval[0] = valuesCount;
521 // insert profile type code in position 0
522 int[] result = new int[rtnval.length + 1];
523 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
524 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
528 public static void main(String args[])
530 // Short test to see if checkBpType works
531 ArrayList<String> test = new ArrayList<String>();
537 for (String i : test)
539 for (String j : test)
541 System.out.println(i + "-" + j + ": "
542 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));