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;
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))
181 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) / nongap;
226 residueHash.put(PID_NOGAPS, new Float(percentage));
228 if (result[i] == null)
230 result[i] = residueHash;
234 values[')'] = values['('];
235 values[']'] = values['['];
238 maxResidue = maxResidue.equals("(") ? ")" : "]";
240 residueHash = new Hashtable();
243 residueHash.put(PROFILE, new int[][] { values,
244 new int[] { jSize, (jSize - values['-']) } });
246 residueHash.put(PAIRPROFILE, pairs);
249 residueHash.put(MAXCOUNT, new Integer(count));
250 residueHash.put(MAXRESIDUE, maxResidue);
252 percentage = ((float) count * 100) / jSize;
253 residueHash.put(PID_GAPS, new Float(percentage));
255 percentage = ((float) count * 100) / nongap;
256 residueHash.put(PID_NOGAPS, new Float(percentage));
258 result[bpEnd] = residueHash;
264 * Compute all or part of the annotation row from the given consensus
268 * - pre-allocated annotation row
272 * @param ignoreGapsInConsensusCalculation
273 * @param includeAllConsSymbols
275 public static void completeConsensus(AlignmentAnnotation consensus,
276 Hashtable[] hconsensus, int iStart, int width,
277 boolean ignoreGapsInConsensusCalculation,
278 boolean includeAllConsSymbols, long nseq)
281 if (consensus == null || consensus.annotations == null
282 || consensus.annotations.length < width)
284 // called with a bad alignment annotation row - wait for it to be
285 // initialised properly
288 String fmtstr = "%3.1f";
297 fmtstr = "%" + (2 + precision) + "." + precision + "f";
299 Format fmt = new Format(fmtstr);
301 for (int i = iStart; i < width; i++)
304 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
306 // happens if sequences calculated over were shorter than alignment
308 consensus.annotations[i] = null;
313 if (ignoreGapsInConsensusCalculation)
315 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
319 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
323 consensus.annotations[i] = null;
324 // data has changed below us .. give up and
327 value = fv.floatValue();
328 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
329 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
330 if (maxRes.length() > 1)
332 mouseOver = "[" + maxRes + "] ";
335 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
336 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
338 if (pairs != null && includeAllConsSymbols) // Just responsible for the
340 // TODO Update tooltips for Structure row
345 * TODO It's not sure what is the purpose of the alphabet and wheter it
346 * is useful for structure?
348 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
349 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
350 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
351 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
354 int[][] ca = new int[625][];
355 float[] vl = new float[625];
357 for (int c = 65; c < 90; c++)
359 for (int d = 65; d < 90; d++)
361 ca[x] = new int[] { c, d };
366 jalview.util.QuickSort.sort(vl, ca);
370 * profile[1] is {total, ungappedTotal}
372 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
374 for (int c = 624; c > 0; c--)
378 tval = (vl[c] * 100f / divisor);
379 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
380 + (char) ca[c][1] + " " + fmt.form(tval) + "%";
390 mouseOver += (fmt.form(value) + "%");
392 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
398 * get the sorted base-pair profile for the given position of the consensus
401 * @return profile of the given column
403 public static int[] extractProfile(Hashtable hconsensus,
404 boolean ignoreGapsInConsensusCalculation)
406 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
407 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
408 int[][] pairs = (int[][]) hconsensus
409 .get(StructureFrequency.PAIRPROFILE);
416 // TODO fix the object length, also do it in completeConsensus
417 // Object[] ca = new Object[625];
418 int[][] ca = new int[625][];
419 float[] vl = new float[625];
421 for (int c = 65; c < 90; c++)
423 for (int d = 65; d < 90; d++)
425 ca[x] = new int[] { c, d };
430 jalview.util.QuickSort.sort(vl, ca);
435 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
436 for (int c = 624; c > 0; c--)
440 rtnval[offset++] = ca[c][0];
441 rtnval[offset++] = ca[c][1];
442 rtnval[offset] = (int) (vl[c] * 100f / divisor);
443 rtnval[1] += rtnval[offset++];
447 rtnval[0] = valuesCount;
449 // insert profile type code in position 0
450 int[] result = new int[rtnval.length + 1];
451 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
452 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
456 public static void main(String args[])
458 // Short test to see if checkBpType works
459 ArrayList<String> test = new ArrayList<String>();
465 for (String i : test)
467 for (String j : test)
469 System.out.println(i + "-" + j + ": "
470 + Rna.isCanonicalOrWobblePair(i.charAt(0), j.charAt(0)));