2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
25 * Takes in a vector or array of sequences and column start and column end and
26 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
27 * This class is used extensively in calculating alignment colourschemes that
28 * depend on the amount of conservation in each alignment column.
33 public class StructureFrequency
35 // No need to store 1000s of strings which are not
36 // visible to the user.
37 public static final String MAXCOUNT = "C";
39 public static final String MAXRESIDUE = "R";
41 public static final String PID_GAPS = "G";
43 public static final String PID_NOGAPS = "N";
45 public static final String PROFILE = "P";
47 public static final String PAIRPROFILE = "B";
49 public static final Hashtable[] calculate(Vector sequences, int start,
52 return calculate(sequences, start, end, false);
55 public static final Hashtable[] calculate(Vector sequences, int start,
56 int end, boolean profile)
58 SequenceI[] seqs = new SequenceI[sequences.size()];
60 for (int i = 0; i < sequences.size(); i++)
62 seqs[i] = (SequenceI) sequences.elementAt(i);
63 if (seqs[i].getLength() > width)
65 width = seqs[i].getLength();
69 Hashtable[] reply = new Hashtable[width];
76 calculate(seqs, start, end, reply, profile);
81 public static final void calculate(SequenceI[] sequences, int start,
82 int end, Hashtable[] result)
84 calculate(sequences, start, end, result, false);
87 public static final void calculate(SequenceI[] sequences, int start,
88 int end, Hashtable[] result, boolean profile)
90 Hashtable residueHash;
91 int maxCount, nongap, i, j, v, jSize = sequences.length;
96 int[] values = new int[255];
100 for (i = start; i < end; i++)
102 residueHash = new Hashtable();
106 values = new int[255];
108 for (j = 0; j < jSize; j++)
110 if (sequences[j] == null)
113 .println("WARNING: Consensus skipping null sequence - possible race condition.");
116 seq = sequences[j].getSequence();
121 if (c == '.' || c == ' ')
131 else if ('a' <= c && c <= 'z')
133 c -= 32; // ('a' - 'A');
146 for (v = 'A'; v < 'Z'; v++)
148 if (values[v] < 2 || values[v] < maxCount)
153 if (values[v] > maxCount)
155 maxResidue = String.valueOf((char) v);
157 else if (values[v] == maxCount)
159 maxResidue += String.valueOf((char) v);
161 maxCount = values[v];
164 if (maxResidue.length() == 0)
170 residueHash.put(PROFILE, new int[][]
172 { jSize, nongap } });
174 residueHash.put(MAXCOUNT, new Integer(maxCount));
175 residueHash.put(MAXRESIDUE, maxResidue);
177 percentage = ((float) maxCount * 100) / (float) jSize;
178 residueHash.put(PID_GAPS, new Float(percentage));
180 percentage = ((float) maxCount * 100) / (float) nongap;
181 residueHash.put(PID_NOGAPS, new Float(percentage));
182 result[i] = residueHash;
186 public static int findPair(SequenceFeature[] pairs, int indice)
188 for (int i = 0; i < pairs.length; i++)
190 if (pairs[i].getBegin() == indice)
192 return pairs[i].getEnd();
199 * Method to calculate a 'base pair consensus row', very similar to nucleotide
200 * consensus but takes into account a given structure
209 public static final void calculate(SequenceI[] sequences, int start,
210 int end, Hashtable[] result, boolean profile,
211 AlignmentAnnotation rnaStruc)
213 // System.out.println("StructureFrequency.calculateNEW4");
214 Hashtable residueHash;
216 char[] seq, struc = rnaStruc.getRNAStruc().toCharArray();
217 SequenceFeature[] rna = rnaStruc._rnasecstr;
219 int count, nonGap = 0, i, bpEnd = -1, j, jSize = sequences.length;
224 for (i = start; i < end; i++) // foreach column
226 residueHash = new Hashtable();
228 values = new int[255];
229 pairs = new int[255][255];
233 if (s == '.' || s == ' ')
247 for (j = 0; j < jSize; j++) // foreach row
249 if (sequences[j] == null)
252 .println("WARNING: Consensus skipping null sequence - possible race condition.");
255 seq = sequences[j].getSequence();
261 // standard representation for gaps in sequence and structure
262 if (c == '.' || c == ' ')
272 bpEnd = findPair(rna, i);
274 if (checkBpType(c, cEnd))
276 values['H']++; // H means it's a helix (structured)
278 // System.out.println("pair: "+c+","+cEnd);
280 // System.out.println("pairs: "+c+","+cEnd+" - "+pairs[c][cEnd]);
287 // UPDATE this for new values
290 // System.out.println("profile");
291 residueHash.put(PROFILE, new int[][]
293 { jSize, (jSize - values['-']) } });
295 residueHash.put(PAIRPROFILE, pairs);
300 residueHash.put(MAXCOUNT, new Integer(count));
301 residueHash.put(MAXRESIDUE, maxResidue);
303 percentage = ((float) count * 100) / (float) jSize;
304 residueHash.put(PID_GAPS, new Float(percentage));
306 // percentage = ((float) count * 100) / (float) nongap;
307 // residueHash.put(PID_NOGAPS, new Float(percentage));
308 if (result[i] == null)
310 result[i] = residueHash;
314 result[bpEnd] = residueHash;
320 * Method to check if a base-pair is a canonical or a wobble bp
326 * @return True if it is a canonical/wobble bp
328 public static boolean checkBpType(char up, char down)
391 * Compute all or part of the annotation row from the given consensus
395 * - pre-allocated annotation row
399 * @param ignoreGapsInConsensusCalculation
400 * @param includeAllConsSymbols
402 public static void completeConsensus(AlignmentAnnotation consensus,
403 Hashtable[] hconsensus, int iStart, int width,
404 boolean ignoreGapsInConsensusCalculation,
405 boolean includeAllConsSymbols)
407 completeConsensus(consensus, hconsensus, iStart, width,
408 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
410 // { 'A', 'C', 'G', 'T', 'U' });
413 public static void completeConsensus(AlignmentAnnotation consensus,
414 Hashtable[] hconsensus, int iStart, int width,
415 boolean ignoreGapsInConsensusCalculation,
416 boolean includeAllConsSymbols, char[] alphabet)
418 System.out.println("StructureFrequency.completeConsensus "
419 + includeAllConsSymbols);
421 if (consensus == null || consensus.annotations == null
422 || consensus.annotations.length < width)
424 // called with a bad alignment annotation row - wait for it to be
425 // initialised properly
428 for (int i = iStart; i < width; i++)
430 if (i >= hconsensus.length)
432 // happens if sequences calculated over were shorter than alignment
434 consensus.annotations[i] = null;
438 if (ignoreGapsInConsensusCalculation)
440 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
445 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
449 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
451 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE)
453 if (maxRes.length() > 1)
455 mouseOver = "[" + maxRes + "] ";
458 int[][] profile = (int[][]) hconsensus[i]
459 .get(StructureFrequency.PROFILE);
460 if (profile != null && includeAllConsSymbols) // Just responsible for the
463 // System.out.println("StructureFrequency.includeAllConsSymbols");
465 if (alphabet != null)
467 for (int c = 0; c < alphabet.length; c++)
469 tval = ((float) profile[0][alphabet[c]])
471 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
473 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
474 + ((int) tval) + "%";
479 // System.out.println("StructureFrequency.NOTincludeAllConsSymbols");
480 Object[] ca = new Object[profile[0].length];
481 float[] vl = new float[profile[0].length];
482 for (int c = 0; c < ca.length; c++)
486 vl[c] = (float) profile[0][c];
489 jalview.util.QuickSort.sort(vl, ca);
490 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
492 if (((char[]) ca[c])[0] != '-')
494 tval = ((float) profile[0][((char[]) ca[c])[0]])
496 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
498 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
499 + " " + ((int) tval) + "%";
509 mouseOver += ((int) value + "%");
511 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
517 * get the sorted profile for the given position of the consensus
522 public static int[] extractProfile(Hashtable hconsensus,
523 boolean ignoreGapsInConsensusCalculation, int column)
525 // TODO is there a more elegant way to acces the column number?
527 * calculate the frequence of the 16 bp variations for this column 'somehow'
528 * transfer this via getProfile and let it correctly draw
530 int[] rtnval = new int[51]; // 2*(5*5)+1
531 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
532 int[][] pairs = (int[][]) hconsensus
533 .get(StructureFrequency.PAIRPROFILE);
539 for (int j = 65; j <= 90; j++)
541 for (int k = 65; k <= 90; k++)
545 rtnval[rtnval[0]++] = j;
546 rtnval[rtnval[0]++] = k;
547 rtnval[rtnval[0]++] = (int) ((float) pairs[j][k] * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
556 public static void main(String args[])
558 // Short test to see if checkBpType works
559 ArrayList<String> test = new ArrayList<String>();
565 for (String i : test)
567 for (String j : test)
569 System.out.println(i + "-" + j + ": "
570 + StructureFrequency.checkBpType(i.charAt(0), j.charAt(0)));