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;
116 int otherPairCount = 0;
118 values = new int[255];
119 pairs = new int[255][255];
121 if (i < struc.length)
129 if (s == '.' || s == ' ')
134 if (!Rna.isOpeningParenthesis(s))
143 bpEnd = findPair(rna, i);
147 for (int j = 0; j < jSize; j++) // foreach row
149 if (sequences[j] == null)
152 .println("WARNING: Consensus skipping null sequence - possible race condition.");
156 c = sequences[j].getCharAt(i);
157 cEnd = sequences[j].getCharAt(bpEnd);
159 if (Comparison.isGap(c) || Comparison.isGap(cEnd))
166 * ensure upper-case for counting purposes
168 if ('a' <= c && 'z' >= c)
172 if ('a' <= cEnd && 'z' >= cEnd)
176 if (Rna.isCanonicalOrWobblePair(c, cEnd))
180 canonicalOrWobblePairCount++;
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);
205 * the count is the number of valid pairs (as a percentage, determines
206 * the relative size of the profile logo)
208 int count = canonicalOrWobblePairCount;
211 * currently displaying as '(' if most pairs are valid, or as
212 * '[' if there are more invalid than valid pairs
214 if (!maxResidue.equals("-"))
216 maxResidue = canonicalOrWobblePairCount >= otherPairCount ? "("
219 residueHash.put(MAXCOUNT, new Integer(count));
220 residueHash.put(MAXRESIDUE, maxResidue);
222 percentage = ((float) count * 100) / jSize;
223 residueHash.put(PID_GAPS, new Float(percentage));
225 // percentage = ((float) count * 100) / (float) nongap;
226 // residueHash.put(PID_NOGAPS, new Float(percentage));
227 if (result[i] == null)
229 result[i] = residueHash;
233 values[')'] = values['('];
234 values[']'] = values['['];
237 maxResidue = maxResidue.equals("(") ? ")" : "]";
239 residueHash = new Hashtable();
242 residueHash.put(PROFILE, new int[][] { values,
243 new int[] { jSize, (jSize - values['-']) } });
245 residueHash.put(PAIRPROFILE, pairs);
248 residueHash.put(MAXCOUNT, new Integer(count));
249 residueHash.put(MAXRESIDUE, maxResidue);
251 percentage = ((float) count * 100) / jSize;
252 residueHash.put(PID_GAPS, new Float(percentage));
254 result[bpEnd] = residueHash;
260 * Compute all or part of the annotation row from the given consensus
264 * - pre-allocated annotation row
268 * @param ignoreGapsInConsensusCalculation
269 * @param includeAllConsSymbols
271 public static void completeConsensus(AlignmentAnnotation consensus,
272 Hashtable[] hconsensus, int iStart, int width,
273 boolean ignoreGapsInConsensusCalculation,
274 boolean includeAllConsSymbols, long nseq)
277 if (consensus == null || consensus.annotations == null
278 || consensus.annotations.length < width)
280 // called with a bad alignment annotation row - wait for it to be
281 // initialised properly
284 String fmtstr = "%3.1f";
293 fmtstr = "%" + (2 + precision) + "." + precision + "f";
295 Format fmt = new Format(fmtstr);
297 for (int i = iStart; i < width; i++)
300 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
302 // happens if sequences calculated over were shorter than alignment
304 consensus.annotations[i] = null;
309 if (ignoreGapsInConsensusCalculation)
311 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
315 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
319 consensus.annotations[i] = null;
320 // data has changed below us .. give up and
323 value = fv.floatValue();
324 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
325 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
326 if (maxRes.length() > 1)
328 mouseOver = "[" + maxRes + "] ";
331 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
332 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
334 if (pairs != null && includeAllConsSymbols) // Just responsible for the
336 // TODO Update tooltips for Structure row
341 * TODO It's not sure what is the purpose of the alphabet and wheter it
342 * is useful for structure?
344 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
345 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
346 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
347 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
350 int[][] ca = new int[625][];
351 float[] vl = new float[625];
353 for (int c = 65; c < 90; c++)
355 for (int d = 65; d < 90; d++)
357 ca[x] = new int[] { c, d };
362 jalview.util.QuickSort.sort(vl, ca);
366 * profile[1] is {total, ungappedTotal}
368 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
370 for (int c = 624; c > 0; c--)
374 tval = (vl[c] * 100f / divisor);
375 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
376 + (char) ca[c][1] + " " + fmt.form(tval) + "%";
386 mouseOver += (fmt.form(value) + "%");
388 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
394 * get the sorted base-pair profile for the given position of the consensus
397 * @return profile of the given column
399 public static int[] extractProfile(Hashtable hconsensus,
400 boolean ignoreGapsInConsensusCalculation)
402 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
403 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
404 int[][] pairs = (int[][]) hconsensus
405 .get(StructureFrequency.PAIRPROFILE);
412 // TODO fix the object length, also do it in completeConsensus
413 // Object[] ca = new Object[625];
414 int[][] ca = new int[625][];
415 float[] vl = new float[625];
417 for (int c = 65; c < 90; c++)
419 for (int d = 65; d < 90; d++)
421 ca[x] = new int[] { c, d };
426 jalview.util.QuickSort.sort(vl, ca);
431 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
432 for (int c = 624; c > 0; c--)
436 rtnval[offset++] = ca[c][0];
437 rtnval[offset++] = ca[c][1];
438 rtnval[offset] = (int) (vl[c] * 100f / divisor);
439 rtnval[1] += rtnval[offset++];
443 rtnval[0] = valuesCount;
445 // insert profile type code in position 0
446 int[] result = new int[rtnval.length + 1];
447 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
448 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
452 public static void main(String args[])
454 // Short test to see if checkBpType works
455 ArrayList<String> test = new ArrayList<String>();
461 for (String i : test)
463 for (String j : test)
465 System.out.println(i + "-" + j + ": "
466 + Rna.isCanonicalOrWobblePair(i.charAt(0), j.charAt(0)));