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 Hashtable[] calculate(Vector sequences, int start,
50 return calculate(sequences, start, end, false);
53 public static final Hashtable[] calculate(Vector sequences, int start,
54 int end, boolean profile)
56 SequenceI[] seqs = new SequenceI[sequences.size()];
58 for (int i = 0; i < sequences.size(); i++)
60 seqs[i] = (SequenceI) sequences.elementAt(i);
61 if (seqs[i].getLength() > width)
63 width = seqs[i].getLength();
67 Hashtable[] reply = new Hashtable[width];
74 calculate(seqs, start, end, reply, profile);
79 public static final void calculate(SequenceI[] sequences, int start,
80 int end, Hashtable[] result)
82 calculate(sequences, start, end, result, false);
85 public static final void calculate(SequenceI[] sequences, int start,
86 int end, Hashtable[] result, boolean profile)
88 Hashtable residueHash;
89 int maxCount, nongap, i, j, v, jSize = sequences.length;
94 int[] values = new int[255];
98 for (i = start; i < end; i++)
100 residueHash = new Hashtable();
104 values = new int[255];
107 for (j = 0; j < jSize; j++)
109 if (sequences[j]==null)
111 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
114 seq = sequences[j].getSequence();
119 if (c == '.' || c == ' ')
129 else if ('a' <= c && c <= 'z')
131 c -= 32; // ('a' - 'A');
144 for (v = 'A'; v < 'Z'; v++)
146 if (values[v] < 2 || values[v] < maxCount)
151 if (values[v] > maxCount)
153 maxResidue = String.valueOf((char) v);
155 else if (values[v] == maxCount)
157 maxResidue += String.valueOf((char) v);
159 maxCount = values[v];
162 if (maxResidue.length() == 0)
168 residueHash.put(PROFILE, new int[][]
170 { jSize, nongap } });
172 residueHash.put(MAXCOUNT, new Integer(maxCount));
173 residueHash.put(MAXRESIDUE, maxResidue);
175 percentage = ((float) maxCount * 100) / (float) jSize;
176 residueHash.put(PID_GAPS, new Float(percentage));
178 percentage = ((float) maxCount * 100) / (float) nongap;
179 residueHash.put(PID_NOGAPS, new Float(percentage));
180 result[i] = residueHash;
184 public static int findPair(SequenceFeature[] pairs,int indice){
185 for(int i=0; i<pairs.length; i++){
186 if(pairs[i].getBegin()==indice){
187 return pairs[i].getEnd();
194 * Method to calculate a 'base pair consensus row', very similar
195 * to nucleotide consensus but takes into account a given structure
203 public static final void calculate(SequenceI[] sequences, int start,
204 int end, Hashtable[] result, boolean profile, AlignmentAnnotation rnaStruc){
205 //System.out.println("StructureFrequency.calculateNEW4");
206 Hashtable residueHash;
208 char[] seq, struc=rnaStruc.getRNAStruc().toCharArray();
209 SequenceFeature[] rna =rnaStruc._rnasecstr;
211 int count,nonGap=0,i,bpEnd=-1,j,jSize = sequences.length;
212 int[] values = new int[255];
215 for (i = start; i < end; i++) //foreach column
217 residueHash = new Hashtable();
219 values = new int[255];
223 if (s == '.' || s == ' ')
235 for (j = 0; j < jSize; j++) //foreach row
237 if (sequences[j]==null)
239 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
242 seq = sequences[j].getSequence();
248 //standard representation for gaps in sequence and structure
249 if (c == '.' || c == ' ')
259 bpEnd=findPair(rna,i);
261 if(checkBpType(c,cEnd)){
262 values['H']++; //H means it's a helix (structured)
269 //UPDATE this for new values
272 //System.out.println("profile");
273 residueHash.put(PROFILE, new int[][]
275 { jSize, values['H'] } });
281 residueHash.put(MAXCOUNT, new Integer(count));
282 residueHash.put(MAXRESIDUE, maxResidue);
284 percentage = ((float) count * 100) / (float) jSize;
285 residueHash.put(PID_GAPS, new Float(percentage));
287 //percentage = ((float) count * 100) / (float) nongap;
288 //residueHash.put(PID_NOGAPS, new Float(percentage));
290 result[i] = residueHash;
293 result[bpEnd]=residueHash;
300 * Method to check if a base-pair is a canonical or a wobble bp
302 * @param down 3' base
303 * @return True if it is a canonical/wobble bp
305 public static boolean checkBpType(char up, char down){
307 if(down>'Z'){down-=32;}
312 case 'T': return true;
313 case 'U': return true;
318 case 'G': return true;
323 case 'A': return true;
324 case 'G': return true;
329 case 'C': return true;
330 case 'T': return true;
331 case 'U': return true;
336 case 'A': return true;
337 case 'G': return true;
345 * Compute all or part of the annotation row from the given consensus
349 * - pre-allocated annotation row
353 * @param ignoreGapsInConsensusCalculation
354 * @param includeAllConsSymbols
356 public static void completeConsensus(AlignmentAnnotation consensus,
357 Hashtable[] hconsensus, int iStart, int width,
358 boolean ignoreGapsInConsensusCalculation,
359 boolean includeAllConsSymbols)
361 completeConsensus(consensus, hconsensus, iStart, width,
362 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
364 // { 'A', 'C', 'G', 'T', 'U' });
367 public static void completeConsensus(AlignmentAnnotation consensus,
368 Hashtable[] hconsensus, int iStart, int width,
369 boolean ignoreGapsInConsensusCalculation,
370 boolean includeAllConsSymbols, char[] alphabet)
372 System.out.println("StructureFrequency.completeConsensus "+includeAllConsSymbols);
374 if (consensus == null || consensus.annotations == null
375 || consensus.annotations.length < width)
377 // called with a bad alignment annotation row - wait for it to be
378 // initialised properly
381 for (int i = iStart; i < width; i++)
383 if (i >= hconsensus.length)
385 // happens if sequences calculated over were shorter than alignment
387 consensus.annotations[i] = null;
391 if (ignoreGapsInConsensusCalculation)
393 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
398 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
402 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE).toString();
403 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE) + " ";
404 if (maxRes.length() > 1)
406 mouseOver = "[" + maxRes + "] ";
409 int[][] profile = (int[][]) hconsensus[i].get(StructureFrequency.PROFILE);
410 if (profile != null && includeAllConsSymbols) //Just responsible for the tooltip
412 //System.out.println("StructureFrequency.includeAllConsSymbols");
414 if (alphabet != null)
416 for (int c = 0; c < alphabet.length; c++)
418 tval = ((float) profile[0][alphabet[c]])
420 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
422 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
423 + ((int) tval) + "%";
428 //System.out.println("StructureFrequency.NOTincludeAllConsSymbols");
429 Object[] ca = new Object[profile[0].length];
430 float[] vl = new float[profile[0].length];
431 for (int c = 0; c < ca.length; c++)
435 vl[c] = (float) profile[0][c];
438 jalview.util.QuickSort.sort(vl, ca);
439 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
441 if (((char[]) ca[c])[0] != '-')
443 tval = ((float) profile[0][((char[]) ca[c])[0]])
445 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
447 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
448 + " " + ((int) tval) + "%";
458 mouseOver += ((int) value + "%");
460 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
466 * get the sorted profile for the given position of the consensus
471 public static int[] extractProfile(Hashtable hconsensus,
472 boolean ignoreGapsInConsensusCalculation,
475 //TODO is there a more elegant way to acces the column number?
477 * calculate the frequence of the 16 bp variations for this column
478 * 'somehow' transfer this via getProfile and let it correctly draw
480 int[] rtnval = new int[22];
481 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
485 Object[] ca = new Object[profile[0].length];
486 float[] vl = new float[profile[0].length];
487 for (int c = 0; c < ca.length; c++)
491 vl[c] = (float) profile[0][c];
494 jalview.util.QuickSort.sort(vl, ca);
496 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
498 if (((char[]) ca[c])[0] != '-')
500 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
501 //System.out.println("rtnval-"+c+": "+((char[]) ca[c])[0]);
502 rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
506 for(int i=0; i<rtnval.length;i++){
507 // System.out.print(rtnval[i]+",");
509 System.out.print("\n");
513 public static void main(String args[]){
514 //Short test to see if checkBpType works
515 ArrayList<String> test = new ArrayList<String>();
521 for (String i : test) {
522 for (String j : test) {
523 System.out.println(i+"-"+j+": "+StructureFrequency.checkBpType(i.charAt(0),j.charAt(0)));