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.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<String, Object>[] result, boolean profile,
97 AlignmentAnnotation rnaStruc)
100 Hashtable<String, Object> 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, canonical = 0;
115 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)
151 jalview.bin.Console.errPrintln(
152 "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))
178 canonicalOrWobblePairCount++;
179 if (Rna.isCanonicalPair(c, cEnd))
193 residueHash = new Hashtable<>();
196 // TODO 1-dim array with jsize in [0], nongapped in [1]; or Pojo
197 residueHash.put(PROFILE,
199 { values, 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
229 residueHash.put(MAXCOUNT, Integer.valueOf(count));
230 residueHash.put(MAXRESIDUE, maxResidue);
232 percentage = ((float) count * 100) / jSize;
233 residueHash.put(PID_GAPS, Float.valueOf(percentage));
235 percentage = ((float) count * 100) / nongap;
236 residueHash.put(PID_NOGAPS, Float.valueOf(percentage));
238 if (result[i] == null)
240 result[i] = residueHash;
244 values[')'] = values['('];
245 values[']'] = values['['];
246 values['}'] = values['{'];
250 maxResidue = maxResidue.equals("(") ? ")"
251 : maxResidue.equals("[") ? "]" : "}";
253 residueHash = new Hashtable<>();
256 residueHash.put(PROFILE,
258 { values, new int[] { jSize, (jSize - values['-']) } });
260 residueHash.put(PAIRPROFILE, pairs);
263 residueHash.put(MAXCOUNT, Integer.valueOf(count));
264 residueHash.put(MAXRESIDUE, maxResidue);
266 percentage = ((float) count * 100) / jSize;
267 residueHash.put(PID_GAPS, Float.valueOf(percentage));
269 percentage = ((float) count * 100) / nongap;
270 residueHash.put(PID_NOGAPS, Float.valueOf(percentage));
272 result[bpEnd] = residueHash;
278 * Compute all or part of the annotation row from the given consensus
282 * - pre-allocated annotation row
286 * @param ignoreGapsInConsensusCalculation
287 * @param includeAllConsSymbols
289 public static void completeConsensus(AlignmentAnnotation consensus,
290 Hashtable<String, Object>[] hconsensus, int iStart, int width,
291 boolean ignoreGapsInConsensusCalculation,
292 boolean includeAllConsSymbols, long nseq)
295 if (consensus == null || consensus.annotations == null
296 || consensus.annotations.length < width)
298 // called with a bad alignment annotation row - wait for it to be
299 // initialised properly
302 String fmtstr = "%3.1f";
311 fmtstr = "%" + (2 + precision) + "." + precision + "f";
313 Format fmt = new Format(fmtstr);
315 for (int i = iStart; i < width; i++)
317 Hashtable<String, Object> hci;
318 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
320 // happens if sequences calculated over were shorter than alignment
322 consensus.annotations[i] = null;
327 if (ignoreGapsInConsensusCalculation)
329 fv = (Float) hci.get(StructureFrequency.PID_NOGAPS);
333 fv = (Float) hci.get(StructureFrequency.PID_GAPS);
337 consensus.annotations[i] = null;
338 // data has changed below us .. give up and
341 value = fv.floatValue();
342 String maxRes = hci.get(StructureFrequency.MAXRESIDUE).toString();
343 String mouseOver = hci.get(StructureFrequency.MAXRESIDUE) + " ";
344 if (maxRes.length() > 1)
346 mouseOver = "[" + maxRes + "] ";
349 int[][] profile = (int[][]) hci.get(StructureFrequency.PROFILE);
350 int[][] pairs = (int[][]) hci.get(StructureFrequency.PAIRPROFILE);
352 if (pairs != null && includeAllConsSymbols) // Just responsible for the
354 // TODO Update tooltips for Structure row
359 * TODO It's not sure what is the purpose of the alphabet and wheter it
360 * is useful for structure?
362 * if (alphabet != null) { for (int c = 0; c < alphabet.length; c++) {
363 * tval = ((float) profile[0][alphabet[c]]) 100f / (float)
364 * profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver +=
365 * ((c == 0) ? "" : "; ") + alphabet[c] + " " + ((int) tval) + "%"; } }
368 int[][] ca = new int[625][];
369 float[] vl = new float[625];
371 for (int c = 65; c < 90; c++)
373 for (int d = 65; d < 90; d++)
375 ca[x] = new int[] { c, d };
380 jalview.util.QuickSort.sort(vl, ca);
384 * profile[1] is {total, ungappedTotal}
386 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
388 for (int c = 624; c > 0; c--)
392 tval = (vl[c] * 100f / divisor);
393 mouseOver += ((p == 0) ? "" : "; ") + (char) ca[c][0]
394 + (char) ca[c][1] + " " + fmt.form(tval) + "%";
404 mouseOver += (fmt.form(value) + "%");
406 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',
412 * get the sorted base-pair profile for the given position of the consensus
415 * @return profile of the given column
417 public static int[] extractProfile(Hashtable<String, Object> hconsensus,
418 boolean ignoreGapsInConsensusCalculation)
420 int[] rtnval = new int[STRUCTURE_PROFILE_LENGTH]; // 2*(5*5)+2
421 int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
422 int[][] pairs = (int[][]) hconsensus
423 .get(StructureFrequency.PAIRPROFILE);
430 // TODO fix the object length, also do it in completeConsensus
431 // Object[] ca = new Object[625];
432 int[][] ca = new int[625][];
433 float[] vl = new float[625];
435 for (int c = 65; c < 90; c++)
437 for (int d = 65; d < 90; d++)
439 ca[x] = new int[] { c, d };
444 jalview.util.QuickSort.sort(vl, ca);
449 final int divisor = profile[1][ignoreGapsInConsensusCalculation ? 1
451 for (int c = 624; c > 0; c--)
455 rtnval[offset++] = ca[c][0];
456 rtnval[offset++] = ca[c][1];
457 rtnval[offset] = (int) (vl[c] * 100f / divisor);
458 rtnval[1] += rtnval[offset++];
462 rtnval[0] = valuesCount;
464 // insert profile type code in position 0
465 int[] result = new int[rtnval.length + 1];
466 result[0] = AlignmentAnnotation.STRUCTURE_PROFILE;
467 System.arraycopy(rtnval, 0, result, 1, rtnval.length);