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.Format;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
33 * Takes in a vector or array of sequences and column start and column end and
34 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
35 * This class is used extensively in calculating alignment colourschemes that
36 * depend on the amount of conservation in each alignment column.
41 public class StructureFrequency
43 public static final int STRUCTURE_PROFILE_LENGTH = 74;
45 // No need to store 1000s of strings which are not
46 // visible to the user.
47 public static final String MAXCOUNT = "C";
49 public static final String MAXRESIDUE = "R";
51 public static final String PID_GAPS = "G";
53 public static final String PID_NOGAPS = "N";
55 public static final String PROFILE = "P";
57 public static final String PAIRPROFILE = "B";
60 * Returns the 3' position of a base pair
63 * Secondary structure annotation
65 * 5' position of a base pair
66 * @return 3' position of a base pair
68 public static int findPair(SequenceFeature[] pairs, int indice)
71 for (int i = 0; i < pairs.length; i++)
73 if (pairs[i].getBegin() == indice)
77 return pairs[i].getEnd();
85 * Method to calculate a 'base pair consensus row', very similar to nucleotide
86 * consensus but takes into account a given structure
95 public static final void calculate(SequenceI[] sequences, int start,
96 int end, Hashtable[] result, boolean profile,
97 AlignmentAnnotation rnaStruc)
100 Hashtable residueHash;
102 char[] struc = rnaStruc.getRNAStruc().toCharArray();
104 SequenceFeature[] rna = rnaStruc._rnasecstr;
107 int jSize = sequences.length;
112 for (int i = start; i < end; i++) // foreach column
114 int canonicalOrWobblePairCount = 0;
115 int otherPairCount = 0;
117 values = new int[255];
118 pairs = new int[255][255];
120 if (i < struc.length)
128 if (s == '.' || s == ' ')
133 if (!Rna.isOpeningParenthesis(s))
142 bpEnd = findPair(rna, i);
146 for (int j = 0; j < jSize; j++) // foreach row
148 if (sequences[j] == null)
151 .println("WARNING: Consensus skipping null sequence - possible race condition.");
154 c = sequences[j].getCharAt(i);
156 // standard representation for gaps in sequence and structure
157 if (c == '.' || c == ' ')
167 cEnd = sequences[j].getCharAt(bpEnd);
170 * ensure upper-case for counting purposes
172 if ('a' <= c && 'z' >= c)
176 if ('a' <= cEnd && 'z' >= cEnd)
180 if (Rna.isCanonicalOrWobblePair(c, cEnd))
184 canonicalOrWobblePairCount++;
198 residueHash = new Hashtable();
201 // TODO 1-dim array with jsize in [0], nongapped in [1]; or Pojo
202 residueHash.put(PROFILE, new int[][] { values,
203 new int[] { jSize, (jSize - values['-']) } });
205 residueHash.put(PAIRPROFILE, pairs);
207 int count = Math.max(canonicalOrWobblePairCount, otherPairCount);
208 if (!maxResidue.equals("-"))
210 maxResidue = canonicalOrWobblePairCount >= otherPairCount ? "("
213 residueHash.put(MAXCOUNT, new Integer(count));
214 residueHash.put(MAXRESIDUE, maxResidue);
216 percentage = ((float) count * 100) / jSize;
217 residueHash.put(PID_GAPS, new Float(percentage));
219 // percentage = ((float) count * 100) / (float) nongap;
220 // residueHash.put(PID_NOGAPS, new Float(percentage));
221 if (result[i] == null)
223 result[i] = residueHash;
227 values[')'] = values['('];
228 values[']'] = values['['];
231 maxResidue = maxResidue.equals("(") ? ")" : "]";
233 residueHash = new Hashtable();
236 residueHash.put(PROFILE, new int[][] { values,
237 new int[] { jSize, (jSize - values['-']) } });
239 residueHash.put(PAIRPROFILE, pairs);
242 residueHash.put(MAXCOUNT, new Integer(count));
243 residueHash.put(MAXRESIDUE, maxResidue);
245 percentage = ((float) count * 100) / jSize;
246 residueHash.put(PID_GAPS, new Float(percentage));
248 result[bpEnd] = residueHash;
254 * Compute all or part of the annotation row from the given consensus
258 * - pre-allocated annotation row
262 * @param ignoreGapsInConsensusCalculation
263 * @param includeAllConsSymbols
265 public static void completeConsensus(AlignmentAnnotation consensus,
266 Hashtable[] hconsensus, int iStart, int width,
267 boolean ignoreGapsInConsensusCalculation,
268 boolean includeAllConsSymbols, long nseq)
271 if (consensus == null || consensus.annotations == null
272 || consensus.annotations.length < width)
274 // called with a bad alignment annotation row - wait for it to be
275 // initialised properly
278 String fmtstr = "%3.1f";
287 fmtstr = "%" + (2 + precision) + "." + precision + "f";
289 Format fmt = new Format(fmtstr);
291 for (int i = iStart; i < width; i++)
294 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
296 // happens if sequences calculated over were shorter than alignment
298 consensus.annotations[i] = null;
303 if (ignoreGapsInConsensusCalculation)
305 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
309 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
313 consensus.annotations[i] = null;
314 // data has changed below us .. give up and
317 value = fv.floatValue();
318 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
319 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
320 if (maxRes.length() > 1)
322 mouseOver = "[" + maxRes + "] ";
325 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
326 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
328 if (pairs != null && includeAllConsSymbols) // Just responsible for the
330 // TODO Update tooltips for Structure row
335 * TODO It's not sure what is the purpose of the alphabet and wheter it
336 * is useful for structure?
338 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
339 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
340 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
341 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
344 int[][] ca = new int[625][];
345 float[] vl = new float[625];
347 for (int c = 65; c < 90; c++)
349 for (int d = 65; d < 90; d++)
351 ca[x] = new int[] { c, d };
356 jalview.util.QuickSort.sort(vl, ca);
360 * profile[1] is {total, ungappedTotal}
362 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
364 for (int c = 624; c > 0; c--)
368 tval = (vl[c] * 100f / divisor);
369 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
370 + (char) ca[c][1] + " " + fmt.form(tval) + "%";
380 mouseOver += (fmt.form(value) + "%");
382 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
388 * get the sorted base-pair profile for the given position of the consensus
391 * @return profile of the given column
393 public static int[] extractProfile(Hashtable hconsensus,
394 boolean ignoreGapsInConsensusCalculation)
396 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
397 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
398 int[][] pairs = (int[][]) hconsensus
399 .get(StructureFrequency.PAIRPROFILE);
406 // TODO fix the object length, also do it in completeConsensus
407 // Object[] ca = new Object[625];
408 int[][] ca = new int[625][];
409 float[] vl = new float[625];
411 for (int c = 65; c < 90; c++)
413 for (int d = 65; d < 90; d++)
415 ca[x] = new int[] { c, d };
420 jalview.util.QuickSort.sort(vl, ca);
425 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
426 for (int c = 624; c > 0; c--)
430 rtnval[offset++] = ca[c][0];
431 rtnval[offset++] = ca[c][1];
432 rtnval[offset] = (int) (vl[c] * 100f / divisor);
433 rtnval[1] += rtnval[offset++];
437 rtnval[0] = valuesCount;
439 // insert profile type code in position 0
440 int[] result = new int[rtnval.length + 1];
441 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
442 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
446 public static void main(String args[])
448 // Short test to see if checkBpType works
449 ArrayList<String> test = new ArrayList<String>();
455 for (String i : test)
457 for (String j : test)
459 System.out.println(i + "-" + j + ": "
460 + Rna.isCanonicalOrWobblePair(i.charAt(0), j.charAt(0)));