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 "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,
200 { values, new int[] { jSize, (jSize - values['-']) } });
202 residueHash.put(PAIRPROFILE, pairs);
204 values['('] = canonicalOrWobblePairCount;
205 values['['] = canonical;
206 values['{'] = otherPairCount;
208 * the count is the number of valid pairs (as a percentage, determines
209 * the relative size of the profile logo)
211 int count = canonicalOrWobblePairCount;
214 * display '(' if most pairs are canonical, or as
215 * '[' if there are more wobble pairs.
217 if (canonicalOrWobblePairCount > 0 || otherPairCount > 0)
219 if (canonicalOrWobblePairCount >= otherPairCount)
221 maxResidue = (canonicalOrWobblePairCount - canonical) < canonical
230 residueHash.put(MAXCOUNT, new Integer(count));
231 residueHash.put(MAXRESIDUE, maxResidue);
233 percentage = ((float) count * 100) / jSize;
234 residueHash.put(PID_GAPS, new Float(percentage));
236 percentage = ((float) count * 100) / nongap;
237 residueHash.put(PID_NOGAPS, new Float(percentage));
239 if (result[i] == null)
241 result[i] = residueHash;
245 values[')'] = values['('];
246 values[']'] = values['['];
247 values['}'] = values['{'];
251 maxResidue = maxResidue.equals("(") ? ")"
252 : maxResidue.equals("[") ? "]" : "}";
254 residueHash = new Hashtable();
257 residueHash.put(PROFILE,
259 { values, new int[] { jSize, (jSize - values['-']) } });
261 residueHash.put(PAIRPROFILE, pairs);
264 residueHash.put(MAXCOUNT, new Integer(count));
265 residueHash.put(MAXRESIDUE, maxResidue);
267 percentage = ((float) count * 100) / jSize;
268 residueHash.put(PID_GAPS, new Float(percentage));
270 percentage = ((float) count * 100) / nongap;
271 residueHash.put(PID_NOGAPS, new Float(percentage));
273 result[bpEnd] = residueHash;
279 * Compute all or part of the annotation row from the given consensus
283 * - pre-allocated annotation row
287 * @param ignoreGapsInConsensusCalculation
288 * @param includeAllConsSymbols
290 public static void completeConsensus(AlignmentAnnotation consensus,
291 Hashtable[] hconsensus, int iStart, int width,
292 boolean ignoreGapsInConsensusCalculation,
293 boolean includeAllConsSymbols, long nseq)
296 if (consensus == null || consensus.annotations == null
297 || consensus.annotations.length < width)
299 // called with a bad alignment annotation row - wait for it to be
300 // initialised properly
303 String fmtstr = "%3.1f";
312 fmtstr = "%" + (2 + precision) + "." + precision + "f";
314 Format fmt = new Format(fmtstr);
316 for (int i = iStart; i < width; i++)
319 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
321 // happens if sequences calculated over were shorter than alignment
323 consensus.annotations[i] = null;
328 if (ignoreGapsInConsensusCalculation)
330 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
334 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
338 consensus.annotations[i] = null;
339 // data has changed below us .. give up and
342 value = fv.floatValue();
343 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
344 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
345 if (maxRes.length() > 1)
347 mouseOver = "[" + maxRes + "] ";
350 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
351 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
353 if (pairs != null && includeAllConsSymbols) // Just responsible for the
355 // TODO Update tooltips for Structure row
360 * TODO It's not sure what is the purpose of the alphabet and wheter it
361 * is useful for structure?
363 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
364 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
365 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
366 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
369 int[][] ca = new int[625][];
370 float[] vl = new float[625];
372 for (int c = 65; c < 90; c++)
374 for (int d = 65; d < 90; d++)
376 ca[x] = new int[] { c, d };
381 jalview.util.QuickSort.sort(vl, ca);
385 * profile[1] is {total, ungappedTotal}
387 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
389 for (int c = 624; c > 0; c--)
393 tval = (vl[c] * 100f / divisor);
394 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
395 + (char) ca[c][1] + " " + fmt.form(tval) + "%";
405 mouseOver += (fmt.form(value) + "%");
407 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
413 * get the sorted base-pair profile for the given position of the consensus
416 * @return profile of the given column
418 public static int[] extractProfile(Hashtable hconsensus,
419 boolean ignoreGapsInConsensusCalculation)
421 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
422 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
423 int[][] pairs = (int[][]) hconsensus
424 .get(StructureFrequency.PAIRPROFILE);
431 // TODO fix the object length, also do it in completeConsensus
432 // Object[] ca = new Object[625];
433 int[][] ca = new int[625][];
434 float[] vl = new float[625];
436 for (int c = 65; c < 90; c++)
438 for (int d = 65; d < 90; d++)
440 ca[x] = new int[] { c, d };
445 jalview.util.QuickSort.sort(vl, ca);
450 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
452 for (int c = 624; c > 0; c--)
456 rtnval[offset++] = ca[c][0];
457 rtnval[offset++] = ca[c][1];
458 rtnval[offset] = (int) (vl[c] * 100f / divisor);
459 rtnval[1] += rtnval[offset++];
463 rtnval[0] = valuesCount;
465 // insert profile type code in position 0
466 int[] result = new int[rtnval.length + 1];
467 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
468 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
472 public static void main(String args[])
474 // Short test to see if checkBpType works
475 ArrayList<String> test = new ArrayList<String>();
481 for (String i : test)
483 for (String j : test)
485 System.out.println(i + "-" + j + ": "
486 + Rna.isCanonicalOrWobblePair(i.charAt(0), j.charAt(0)));