2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
23 import jalview.util.Format;
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 for (int i = 0; i < pairs.length; i++)
64 if (pairs[i].getBegin() == indice)
66 return pairs[i].getEnd();
73 * Method to calculate a 'base pair consensus row', very similar to nucleotide
74 * consensus but takes into account a given structure
83 public static final void calculate(SequenceI[] sequences, int start,
84 int end, Hashtable[] result, boolean profile,
85 AlignmentAnnotation rnaStruc)
87 Hashtable residueHash;
89 char[] seq, struc = rnaStruc.getRNAStruc().toCharArray();
90 SequenceFeature[] rna = rnaStruc._rnasecstr;
92 int count, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
97 for (i = start; i < end; i++) // foreach column
99 residueHash = new Hashtable();
101 values = new int[255];
102 pairs = new int[255][255];
104 if (i < struc.length)
112 if (s == '.' || s == ' ')
126 bpEnd = findPair(rna, i);
129 for (j = 0; j < jSize; j++) // foreach row
131 if (sequences[j] == null)
134 .println("WARNING: Consensus skipping null sequence - possible race condition.");
137 c = sequences[j].getCharAt(i);
140 // standard representation for gaps in sequence and structure
141 if (c == '.' || c == ' ')
151 cEnd = sequences[j].getCharAt(bpEnd);
152 if (checkBpType(c, cEnd))
154 values['(']++; // H means it's a helix (structured)
164 // UPDATE this for new values
167 residueHash.put(PROFILE, new int[][]
169 { jSize, (jSize - values['-']) } });
171 residueHash.put(PAIRPROFILE, pairs);
176 residueHash.put(MAXCOUNT, new Integer(count));
177 residueHash.put(MAXRESIDUE, maxResidue);
179 percentage = ((float) count * 100) / jSize;
180 residueHash.put(PID_GAPS, new Float(percentage));
182 // percentage = ((float) count * 100) / (float) nongap;
183 // residueHash.put(PID_NOGAPS, new Float(percentage));
184 if (result[i] == null)
186 result[i] = residueHash;
190 values[')'] = values['('];
193 residueHash = new Hashtable();
198 residueHash.put(PROFILE, new int[][]
200 { jSize, (jSize - values['-']) } });
202 residueHash.put(PAIRPROFILE, pairs);
205 residueHash.put(MAXCOUNT, new Integer(count));
206 residueHash.put(MAXRESIDUE, maxResidue);
208 percentage = ((float) count * 100) / jSize;
209 residueHash.put(PID_GAPS, new Float(percentage));
211 result[bpEnd] = residueHash;
217 * Method to check if a base-pair is a canonical or a wobble bp
223 * @return True if it is a canonical/wobble bp
225 public static boolean checkBpType(char up, char down)
288 * Compute all or part of the annotation row from the given consensus
292 * - pre-allocated annotation row
296 * @param ignoreGapsInConsensusCalculation
297 * @param includeAllConsSymbols
299 public static void completeConsensus(AlignmentAnnotation consensus,
300 Hashtable[] hconsensus, int iStart, int width,
301 boolean ignoreGapsInConsensusCalculation,
302 boolean includeAllConsSymbols, long nseq)
305 if (consensus == null || consensus.annotations == null
306 || consensus.annotations.length < width)
308 // called with a bad alignment annotation row - wait for it to be
309 // initialised properly
312 String fmtstr="%3.1f";
320 fmtstr = "%"+(2+precision)+"."+precision+"f";
322 Format fmt = new Format(fmtstr);
324 for (int i = iStart; i < width; i++)
327 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
329 // happens if sequences calculated over were shorter than alignment
331 consensus.annotations[i] = null;
336 if (ignoreGapsInConsensusCalculation)
338 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
342 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
346 consensus.annotations[i] = null;
347 // data has changed below us .. give up and
350 value = fv.floatValue();
351 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
352 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
353 if (maxRes.length() > 1)
355 mouseOver = "[" + maxRes + "] ";
358 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
359 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
361 if (pairs != null && includeAllConsSymbols) // Just responsible for the
363 // TODO Update tooltips for Structure row
368 * TODO It's not sure what is the purpose of the alphabet and wheter it
369 * is useful for structure?
371 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
372 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
373 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
374 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
377 Object[] ca = new Object[625];
378 float[] vl = new float[625];
380 for (int c = 65; c < 90; c++)
382 for (int d = 65; d < 90; d++)
390 jalview.util.QuickSort.sort(vl, ca);
393 for (int c = 624; c > 0; c--)
397 tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
399 mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0]
400 + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%";
410 mouseOver += (fmt.form(value) + "%");
412 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
418 * get the sorted base-pair profile for the given position of the consensus
421 * @return profile of the given column
423 public static int[] extractProfile(Hashtable hconsensus,
424 boolean ignoreGapsInConsensusCalculation)
426 int[] rtnval = new int[74]; // 2*(5*5)+2
427 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
428 int[][] pairs = (int[][]) hconsensus
429 .get(StructureFrequency.PAIRPROFILE);
434 // TODO fix the object length, also do it in completeConsensus
435 Object[] ca = new Object[625];
436 float[] vl = new float[625];
438 for (int c = 65; c < 90; c++)
440 for (int d = 65; d < 90; d++)
448 jalview.util.QuickSort.sort(vl, ca);
452 for (int c = 624; c > 0; c--)
456 rtnval[rtnval[0]++] = ((int[]) ca[c])[0];
457 rtnval[rtnval[0]++] = ((int[]) ca[c])[1];
458 rtnval[rtnval[0]] = (int) (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1
460 rtnval[1] += rtnval[rtnval[0]++];
467 public static void main(String args[])
469 // Short test to see if checkBpType works
470 ArrayList<String> test = new ArrayList<String>();
476 for (String i : test)
478 for (String j : test)
480 System.out.println(i + "-" + j + ": "
481 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));