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.Comparison;
28 import jalview.util.Format;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
34 * Takes in a vector or array of sequences and column start and column end and
35 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
36 * This class is used extensively in calculating alignment colourschemes that
37 * depend on the amount of conservation in each alignment column.
42 public class StructureFrequency
44 public static final int STRUCTURE_PROFILE_LENGTH = 74;
46 // No need to store 1000s of strings which are not
47 // visible to the user.
48 public static final String MAXCOUNT = "C";
50 public static final String MAXRESIDUE = "R";
52 public static final String PID_GAPS = "G";
54 public static final String PID_NOGAPS = "N";
56 public static final String PROFILE = "P";
58 public static final String PAIRPROFILE = "B";
61 * Returns the 3' position of a base pair
64 * Secondary structure annotation
66 * 5' position of a base pair
67 * @return 3' position of a base pair
69 public static int findPair(SequenceFeature[] pairs, int indice)
72 for (int i = 0; i < pairs.length; i++)
74 if (pairs[i].getBegin() == indice)
78 return pairs[i].getEnd();
86 * Method to calculate a 'base pair consensus row', very similar to nucleotide
87 * consensus but takes into account a given structure
96 public static final void calculate(SequenceI[] sequences, int start,
97 int end, Hashtable[] result, boolean profile,
98 AlignmentAnnotation rnaStruc)
101 Hashtable residueHash;
103 char[] struc = rnaStruc.getRNAStruc().toCharArray();
105 SequenceFeature[] rna = rnaStruc._rnasecstr;
108 int jSize = sequences.length;
113 for (int i = start; i < end; i++) // foreach column
115 int canonicalOrWobblePairCount = 0, canonical = 0;
116 int otherPairCount = 0;
119 values = new int[255];
120 pairs = new int[255][255];
122 if (i < struc.length)
130 if (s == '.' || s == ' ')
135 if (!Rna.isOpeningParenthesis(s))
144 bpEnd = findPair(rna, i);
148 for (int j = 0; j < jSize; j++) // foreach row
150 if (sequences[j] == null)
153 .println("WARNING: Consensus skipping null sequence - possible race condition.");
157 c = sequences[j].getCharAt(i);
158 cEnd = sequences[j].getCharAt(bpEnd);
160 if (Comparison.isGap(c) || Comparison.isGap(cEnd))
167 * ensure upper-case for counting purposes
169 if ('a' <= c && 'z' >= c)
173 if ('a' <= cEnd && 'z' >= cEnd)
177 if (Rna.isCanonicalOrWobblePair(c, cEnd))
179 canonicalOrWobblePairCount++;
180 if (Rna.isCanonicalPair(c, cEnd))
194 residueHash = new Hashtable();
197 // TODO 1-dim array with jsize in [0], nongapped in [1]; or Pojo
198 residueHash.put(PROFILE, new int[][] { values,
199 new int[] { jSize, (jSize - values['-']) } });
201 residueHash.put(PAIRPROFILE, pairs);
203 values['('] = canonicalOrWobblePairCount;
204 values['['] = canonical;
205 values['{'] = otherPairCount;
207 * the count is the number of valid pairs (as a percentage, determines
208 * the relative size of the profile logo)
210 int count = canonicalOrWobblePairCount;
213 * display '(' if most pairs are canonical, or as
214 * '[' if there are more wobble pairs.
216 if (canonicalOrWobblePairCount > 0 || otherPairCount > 0)
218 if (canonicalOrWobblePairCount >= otherPairCount)
220 maxResidue = (canonicalOrWobblePairCount - canonical) < canonical ? "("
228 residueHash.put(MAXCOUNT, new Integer(count));
229 residueHash.put(MAXRESIDUE, maxResidue);
231 percentage = ((float) count * 100) / jSize;
232 residueHash.put(PID_GAPS, new Float(percentage));
234 percentage = ((float) count * 100) / nongap;
235 residueHash.put(PID_NOGAPS, new Float(percentage));
237 if (result[i] == null)
239 result[i] = residueHash;
243 values[')'] = values['('];
244 values[']'] = values['['];
245 values['}'] = values['{'];
249 maxResidue = maxResidue.equals("(") ? ")"
250 : maxResidue.equals("[") ? "]" : "}";
252 residueHash = new Hashtable();
255 residueHash.put(PROFILE, new int[][] { values,
256 new int[] { jSize, (jSize - values['-']) } });
258 residueHash.put(PAIRPROFILE, pairs);
261 residueHash.put(MAXCOUNT, new Integer(count));
262 residueHash.put(MAXRESIDUE, maxResidue);
264 percentage = ((float) count * 100) / jSize;
265 residueHash.put(PID_GAPS, new Float(percentage));
267 percentage = ((float) count * 100) / nongap;
268 residueHash.put(PID_NOGAPS, new Float(percentage));
270 result[bpEnd] = residueHash;
276 * Compute all or part of the annotation row from the given consensus
280 * - pre-allocated annotation row
284 * @param ignoreGapsInConsensusCalculation
285 * @param includeAllConsSymbols
287 public static void completeConsensus(AlignmentAnnotation consensus,
288 Hashtable[] hconsensus, int iStart, int width,
289 boolean ignoreGapsInConsensusCalculation,
290 boolean includeAllConsSymbols, long nseq)
293 if (consensus == null || consensus.annotations == null
294 || consensus.annotations.length < width)
296 // called with a bad alignment annotation row - wait for it to be
297 // initialised properly
300 String fmtstr = "%3.1f";
309 fmtstr = "%" + (2 + precision) + "." + precision + "f";
311 Format fmt = new Format(fmtstr);
313 for (int i = iStart; i < width; i++)
316 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
318 // happens if sequences calculated over were shorter than alignment
320 consensus.annotations[i] = null;
325 if (ignoreGapsInConsensusCalculation)
327 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
331 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
335 consensus.annotations[i] = null;
336 // data has changed below us .. give up and
339 value = fv.floatValue();
340 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
341 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
342 if (maxRes.length() > 1)
344 mouseOver = "[" + maxRes + "] ";
347 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
348 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
350 if (pairs != null && includeAllConsSymbols) // Just responsible for the
352 // TODO Update tooltips for Structure row
357 * TODO It's not sure what is the purpose of the alphabet and wheter it
358 * is useful for structure?
360 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
361 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
362 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
363 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
366 int[][] ca = new int[625][];
367 float[] vl = new float[625];
369 for (int c = 65; c < 90; c++)
371 for (int d = 65; d < 90; d++)
373 ca[x] = new int[] { c, d };
378 jalview.util.QuickSort.sort(vl, ca);
382 * profile[1] is {total, ungappedTotal}
384 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
386 for (int c = 624; c > 0; c--)
390 tval = (vl[c] * 100f / divisor);
391 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
392 + (char) ca[c][1] + " " + fmt.form(tval) + "%";
402 mouseOver += (fmt.form(value) + "%");
404 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
410 * get the sorted base-pair profile for the given position of the consensus
413 * @return profile of the given column
415 public static int[] extractProfile(Hashtable hconsensus,
416 boolean ignoreGapsInConsensusCalculation)
418 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
419 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
420 int[][] pairs = (int[][]) hconsensus
421 .get(StructureFrequency.PAIRPROFILE);
428 // TODO fix the object length, also do it in completeConsensus
429 // Object[] ca = new Object[625];
430 int[][] ca = new int[625][];
431 float[] vl = new float[625];
433 for (int c = 65; c < 90; c++)
435 for (int d = 65; d < 90; d++)
437 ca[x] = new int[] { c, d };
442 jalview.util.QuickSort.sort(vl, ca);
447 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
448 for (int c = 624; c > 0; c--)
452 rtnval[offset++] = ca[c][0];
453 rtnval[offset++] = ca[c][1];
454 rtnval[offset] = (int) (vl[c] * 100f / divisor);
455 rtnval[1] += rtnval[offset++];
459 rtnval[0] = valuesCount;
461 // insert profile type code in position 0
462 int[] result = new int[rtnval.length + 1];
463 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
464 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
468 public static void main(String args[])
470 // Short test to see if checkBpType works
471 ArrayList<String> test = new ArrayList<String>();
477 for (String i : test)
479 for (String j : test)
481 System.out.println(i + "-" + j + ": "
482 + Rna.isCanonicalOrWobblePair(i.charAt(0), j.charAt(0)));