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.calculate");
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(int x=(rna.length-1); x>=0; x--){
216 System.out.println("BP-"+((rna.length-1)-x)+" "+rna[x].getBegin()+","+rna[x].getEnd()+" "+rna[x].getFeatureGroup());
220 for (i = start; i < end; i++) //foreach column
222 residueHash = new Hashtable();
224 values = new int[255];
228 if (s == '.' || s == ' ')
238 for (j = 0; j < jSize; j++) //foreach row
240 if (sequences[j]==null)
242 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
245 seq = sequences[j].getSequence();
251 //standard representation for gaps in sequence and structure
252 if (c == '.' || c == ' ')
263 //bpEnd=rna[(rna.length-1-nonGap)].getEnd();
264 bpEnd=findPair(rna,i);
266 //System.out.println(i+","+bpEnd);
267 if(checkBpType(c,cEnd)){
268 values['H']++; //H means it's a helix (structured)
276 //UPDATE this for new values
279 // residueHash.put(PROFILE, new int[][]
280 // { values, new int[]
281 // { jSize, nongap } });
287 residueHash.put(MAXCOUNT, new Integer(count));
288 residueHash.put(MAXRESIDUE, maxResidue);
290 percentage = ((float) count * 100) / (float) jSize;
291 residueHash.put(PID_GAPS, new Float(percentage));
293 //percentage = ((float) count * 100) / (float) nongap;
294 //residueHash.put(PID_NOGAPS, new Float(percentage));
296 result[i] = residueHash;
299 result[bpEnd]=residueHash;
306 * Method to check if a base-pair is a canonical or a wobble bp
308 * @param down 3' base
309 * @return True if it is a canonical/wobble bp
311 public static boolean checkBpType(char up, char down){
313 if(down>'Z'){down-=32;}
318 case 'T': return true;
319 case 'U': return true;
324 case 'G': return true;
329 case 'A': return true;
330 case 'G': return true;
335 case 'C': return true;
336 case 'T': return true;
337 case 'U': return true;
342 case 'A': return true;
343 case 'G': return true;
351 * Compute all or part of the annotation row from the given consensus
355 * - pre-allocated annotation row
359 * @param ignoreGapsInConsensusCalculation
360 * @param includeAllConsSymbols
362 public static void completeConsensus(AlignmentAnnotation consensus,
363 Hashtable[] hconsensus, int iStart, int width,
364 boolean ignoreGapsInConsensusCalculation,
365 boolean includeAllConsSymbols)
367 completeConsensus(consensus, hconsensus, iStart, width,
368 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null); // new
370 // { 'A', 'C', 'G', 'T', 'U' });
373 public static void completeConsensus(AlignmentAnnotation consensus,
374 Hashtable[] hconsensus, int iStart, int width,
375 boolean ignoreGapsInConsensusCalculation,
376 boolean includeAllConsSymbols, char[] alphabet)
379 if (consensus == null || consensus.annotations == null
380 || consensus.annotations.length < width)
382 // called with a bad alignment annotation row - wait for it to be
383 // initialised properly
386 for (int i = iStart; i < width; i++)
388 if (i >= hconsensus.length)
390 // happens if sequences calculated over were shorter than alignment
392 consensus.annotations[i] = null;
396 if (ignoreGapsInConsensusCalculation)
398 value = ((Float) hconsensus[i].get(StructureFrequency.PID_NOGAPS))
403 value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
407 String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE).toString();
408 String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE) + " ";
409 if (maxRes.length() > 1)
411 mouseOver = "[" + maxRes + "] ";
414 int[][] profile = (int[][]) hconsensus[i].get(StructureFrequency.PROFILE);
415 if (profile != null && includeAllConsSymbols)
418 if (alphabet != null)
420 for (int c = 0; c < alphabet.length; c++)
422 tval = ((float) profile[0][alphabet[c]])
424 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
426 mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " "
427 + ((int) tval) + "%";
432 Object[] ca = new Object[profile[0].length];
433 float[] vl = new float[profile[0].length];
434 for (int c = 0; c < ca.length; c++)
438 vl[c] = (float) profile[0][c];
441 jalview.util.QuickSort.sort(vl, ca);
442 for (int p = 0, c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
444 if (((char[]) ca[c])[0] != '-')
446 tval = ((float) profile[0][((char[]) ca[c])[0]])
448 / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
450 mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0]
451 + " " + ((int) tval) + "%";
461 mouseOver += ((int) value + "%");
463 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
469 * get the sorted profile for the given position of the consensus
474 public static int[] extractProfile(Hashtable hconsensus,
475 boolean ignoreGapsInConsensusCalculation)
477 int[] rtnval = new int[64];
478 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
481 Object[] ca = new Object[profile[0].length];
482 float[] vl = new float[profile[0].length];
483 for (int c = 0; c < ca.length; c++)
487 vl[c] = (float) profile[0][c];
490 jalview.util.QuickSort.sort(vl, ca);
492 for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
494 if (((char[]) ca[c])[0] != '-')
496 rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
497 rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
507 public static void main(String args[]){
508 //Short test to see if checkBpType works
509 ArrayList<String> test = new ArrayList<String>();
515 for (String i : test) {
516 for (String j : test) {
517 System.out.println(i+"-"+j+": "+StructureFrequency.checkBpType(i.charAt(0),j.charAt(0)));