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.AlignedCodonFrame;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceI;
28 import jalview.ext.android.SparseIntArray;
29 import jalview.util.Format;
30 import jalview.util.MappingUtils;
31 import jalview.util.QuickSort;
33 import java.util.Arrays;
34 import java.util.Hashtable;
35 import java.util.List;
38 * Takes in a vector or array of sequences and column start and column end and
39 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
40 * This class is used extensively in calculating alignment colourschemes that
41 * depend on the amount of conservation in each alignment column.
46 public class AAFrequency
48 private static final int TO_UPPER_CASE = 'A' - 'a'; // -32
50 public static final String MAXCOUNT = "C";
52 public static final String MAXRESIDUE = "R";
54 public static final String PID_GAPS = "G";
56 public static final String PID_NOGAPS = "N";
58 public static final String PROFILE = "P";
60 public static final String ENCODED_CHARS = "E";
63 * Quick look-up of String value of char 'A' to 'Z'
65 private static final String[] CHARS = new String['Z' - 'A' + 1];
69 for (char c = 'A'; c <= 'Z'; c++)
71 CHARS[c - 'A'] = String.valueOf(c);
75 public static final Hashtable[] calculate(List<SequenceI> list,
78 return calculate(list, start, end, false);
81 public static final Hashtable[] calculate(List<SequenceI> sequences,
82 int start, int end, boolean profile)
84 SequenceI[] seqs = new SequenceI[sequences.size()];
86 synchronized (sequences)
88 for (int i = 0; i < sequences.size(); i++)
90 seqs[i] = sequences.get(i);
91 if (seqs[i].getLength() > width)
93 width = seqs[i].getLength();
97 Hashtable[] reply = new Hashtable[width];
104 calculate(seqs, start, end, reply, profile);
109 public static final void calculate(SequenceI[] sequences, int start,
110 int end, Hashtable[] result, boolean profile)
112 Hashtable residueHash;
113 int maxCount, nongap, i, j, v;
114 int jSize = sequences.length;
119 // int[] values = new int[255];
123 for (i = start; i < end; i++)
125 residueHash = new Hashtable();
129 // values = new int[255];
130 SparseIntArray values = new SparseIntArray();
132 for (j = 0; j < jSize; j++)
134 if (sequences[j] == null)
137 .println("WARNING: Consensus skipping null sequence - possible race condition.");
140 seq = sequences[j].getSequence();
145 if (c == '.' || c == ' ')
153 values.put('-', values.get('-') + 1);
156 else if ('a' <= c && c <= 'z')
163 values.put(c, values.get(c) + 1);
169 values.put('-', values.get('-') + 1);
174 maxResidue = String.valueOf(c);
179 for (v = 'A'; v <= 'Z'; v++)
181 // TODO why ignore values[v] == 1?
182 int count = values.get(v); // values[v];
183 if (count < 1 /* 2 */|| count < maxCount)
188 if (count > maxCount)
190 maxResidue = CHARS[v - 'A'];
192 else if (count == maxCount)
194 maxResidue += CHARS[v - 'A'];
199 if (maxResidue.length() == 0)
205 // TODO use a 1-dimensional array with jSize, nongap in [0] and [1]
206 // residueHash.put(PROFILE, new int[][] { values,
207 // new int[] { jSize, nongap } });
208 residueHash.put(PROFILE, new Profile(values, jSize, nongap));
210 residueHash.put(MAXCOUNT, new Integer(maxCount));
211 residueHash.put(MAXRESIDUE, maxResidue);
213 percentage = ((float) maxCount * 100) / jSize;
214 residueHash.put(PID_GAPS, new Float(percentage));
218 // calculate for non-gapped too
219 percentage = ((float) maxCount * 100) / nongap;
221 residueHash.put(PID_NOGAPS, new Float(percentage));
223 result[i] = residueHash;
228 * Compute all or part of the annotation row from the given consensus
232 * - pre-allocated annotation row
236 * @param ignoreGapsInConsensusCalculation
237 * @param includeAllConsSymbols
240 public static void completeConsensus(AlignmentAnnotation consensus,
241 Hashtable[] hconsensus, int iStart, int width,
242 boolean ignoreGapsInConsensusCalculation,
243 boolean includeAllConsSymbols, long nseq)
245 completeConsensus(consensus, hconsensus, iStart, width,
246 ignoreGapsInConsensusCalculation, includeAllConsSymbols, null,
251 * Derive the consensus annotations to be added to the alignment for display.
252 * This does not recompute the raw data, but may be called on a change in
253 * display options, such as 'show logo', which may in turn result in a change
254 * in the derived values.
257 * the annotation row to add annotations to
259 * the source consensus data
264 * @param ignoreGapsInConsensusCalculation
265 * if true, use the consensus calculated ignoring gaps
266 * @param includeAllConsSymbols
267 * if true include all consensus symbols, else just show modal
271 * number of sequences
273 public static void completeConsensus(AlignmentAnnotation consensus,
274 Hashtable[] hconsensus, int iStart, int width,
275 boolean ignoreGapsInConsensusCalculation,
276 boolean includeAllConsSymbols, char[] alphabet, long nseq)
278 if (consensus == null || consensus.annotations == null
279 || consensus.annotations.length < width)
281 // called with a bad alignment annotation row - wait for it to be
282 // initialised properly
286 final Format fmt = getPercentageFormat(nseq);
288 for (int i = iStart; i < width; i++)
291 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null))
293 // happens if sequences calculated over were shorter than alignment
295 consensus.annotations[i] = null;
298 Float fv = (Float) hci
299 .get(ignoreGapsInConsensusCalculation ? PID_NOGAPS : PID_GAPS);
302 consensus.annotations[i] = null;
303 // data has changed below us .. give up and
306 float value = fv.floatValue();
307 String maxRes = hci.get(AAFrequency.MAXRESIDUE).toString();
308 StringBuilder mouseOver = new StringBuilder(64);
309 if (maxRes.length() > 1)
311 mouseOver.append("[").append(maxRes).append("] ");
316 mouseOver.append(hci.get(AAFrequency.MAXRESIDUE) + " ");
318 int[][] profile = (int[][]) hci.get(AAFrequency.PROFILE);
319 if (profile != null && includeAllConsSymbols)
321 int sequenceCount = profile[1][0];
322 int nonGappedCount = profile[1][1];
323 int normalisedBy = ignoreGapsInConsensusCalculation ? nonGappedCount
325 mouseOver.setLength(0);
326 if (alphabet != null)
328 for (int c = 0; c < alphabet.length; c++)
330 float tval = profile[0][alphabet[c]] * 100f / normalisedBy;
332 .append(((c == 0) ? "" : "; "))
335 .append(((fmt != null) ? fmt.form(tval) : ((int) tval)))
341 // TODO do this sort once only in calculate()?
342 // char[][] ca = new char[profile[0].length][];
343 char[] ca = new char[profile[0].length];
344 float[] vl = new float[profile[0].length];
345 for (int c = 0; c < ca.length; c++)
348 // ca[c] = new char[]
350 vl[c] = profile[0][c];
352 QuickSort.sort(vl, ca);
353 for (int p = 0, c = ca.length - 1; profile[0][ca[c]] > 0; c--)
355 final char residue = ca[c];
358 float tval = profile[0][residue] * 100f / normalisedBy;
360 .append((((p == 0) ? "" : "; ")))
363 .append(((fmt != null) ? fmt.form(tval)
364 : ((int) tval))).append("%");
373 (((fmt != null) ? fmt.form(value) : ((int) value))))
376 consensus.annotations[i] = new Annotation(maxRes,
377 mouseOver.toString(), ' ', value);
382 * Returns a Format designed to show all significant figures for profile
383 * percentages. For less than 100 sequences, returns null (the integer
384 * percentage value will be displayed). For 100-999 sequences, returns "%3.1f"
389 protected static Format getPercentageFormat(long nseq)
397 return scale <= 1 ? null : new Format("%3." + (scale - 1) + "f");
401 * Returns the sorted profile for the given consensus data. The returned array
405 * [profileType, numberOfValues, nonGapCount, charValue1, percentage1, charValue2, percentage2, ...]
406 * in descending order of percentage value
410 * the data table from which to extract and sort values
412 * if true, only non-gapped values are included in percentage
416 public static int[] extractProfile(Hashtable hconsensus,
419 int[] rtnval = new int[64];
420 // int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE);
421 Profile profile = (Profile) hconsensus.get(AAFrequency.PROFILE);
426 // int profileLength = profile[0].length;
427 int profileLength = profile.profile.size();
428 char[] ca = new char[profileLength];
429 float[] vl = new float[profileLength];
430 // for (int c = 0; c < ca.length; c++)
433 // vl[c] = profile[0][c];
435 for (int i = 0; i < profileLength; i++)
437 int c = profile.profile.keyAt(i);
439 vl[i] = profile.profile.get(c);
441 QuickSort.sort(vl, ca);
442 int nextArrayPos = 2;
443 int totalPercentage = 0;
444 int distinctValuesCount = 0;
445 final int divisor = profile[1][ignoreGaps ? 1 : 0];
446 for (int c = ca.length - 1; profile[0][ca[c]] > 0; c--)
450 rtnval[nextArrayPos++] = ca[c];
451 final int percentage = (int) (profile[0][ca[c]] * 100f / divisor);
452 rtnval[nextArrayPos++] = percentage;
453 totalPercentage += percentage;
454 distinctValuesCount++;
457 rtnval[0] = distinctValuesCount;
458 rtnval[1] = totalPercentage;
459 int[] result = new int[rtnval.length + 1];
460 result[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
461 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
467 * Extract a sorted extract of cDNA codon profile data. The returned array
471 * [profileType, numberOfValues, totalCount, charValue1, percentage1, charValue2, percentage2, ...]
472 * in descending order of percentage value, where the character values encode codon triplets
478 public static int[] extractCdnaProfile(Hashtable hashtable,
481 // this holds #seqs, #ungapped, and then codon count, indexed by encoded
483 int[] codonCounts = (int[]) hashtable.get(PROFILE);
484 int[] sortedCounts = new int[codonCounts.length - 2];
485 System.arraycopy(codonCounts, 2, sortedCounts, 0,
486 codonCounts.length - 2);
488 int[] result = new int[3 + 2 * sortedCounts.length];
489 // first value is just the type of profile data
490 result[0] = AlignmentAnnotation.CDNA_PROFILE;
492 char[] codons = new char[sortedCounts.length];
493 for (int i = 0; i < codons.length; i++)
495 codons[i] = (char) i;
497 QuickSort.sort(sortedCounts, codons);
498 int totalPercentage = 0;
499 int distinctValuesCount = 0;
501 int divisor = ignoreGaps ? codonCounts[1] : codonCounts[0];
502 for (int i = codons.length - 1; i >= 0; i--)
504 final int codonCount = sortedCounts[i];
507 break; // nothing else of interest here
509 distinctValuesCount++;
510 result[j++] = codons[i];
511 final int percentage = codonCount * 100 / divisor;
512 result[j++] = percentage;
513 totalPercentage += percentage;
515 result[2] = totalPercentage;
518 * Just return the non-zero values
520 // todo next value is redundant if we limit the array to non-zero counts
521 result[1] = distinctValuesCount;
522 return Arrays.copyOfRange(result, 0, j);
526 * Compute a consensus for the cDNA coding for a protein alignment.
529 * the protein alignment (which should hold mappings to cDNA
532 * the consensus data stores to be populated (one per column)
534 public static void calculateCdna(AlignmentI alignment,
535 Hashtable[] hconsensus)
537 final char gapCharacter = alignment.getGapCharacter();
538 List<AlignedCodonFrame> mappings = alignment.getCodonFrames();
539 if (mappings == null || mappings.isEmpty())
544 int cols = alignment.getWidth();
545 for (int col = 0; col < cols; col++)
547 // todo would prefer a Java bean for consensus data
548 Hashtable<String, int[]> columnHash = new Hashtable<String, int[]>();
549 // #seqs, #ungapped seqs, counts indexed by (codon encoded + 1)
550 int[] codonCounts = new int[66];
551 codonCounts[0] = alignment.getSequences().size();
552 int ungappedCount = 0;
553 for (SequenceI seq : alignment.getSequences())
555 if (seq.getCharAt(col) == gapCharacter)
559 List<char[]> codons = MappingUtils
560 .findCodonsFor(seq, col, mappings);
561 for (char[] codon : codons)
563 int codonEncoded = CodingUtils.encodeCodon(codon);
564 if (codonEncoded >= 0)
566 codonCounts[codonEncoded + 2]++;
571 codonCounts[1] = ungappedCount;
572 // todo: sort values here, save counts and codons?
573 columnHash.put(PROFILE, codonCounts);
574 hconsensus[col] = columnHash;
579 * Derive displayable cDNA consensus annotation from computed consensus data.
581 * @param consensusAnnotation
582 * the annotation row to be populated for display
583 * @param consensusData
584 * the computed consensus data
585 * @param showProfileLogo
586 * if true show all symbols present at each position, else only the
589 * the number of sequences in the alignment
591 public static void completeCdnaConsensus(
592 AlignmentAnnotation consensusAnnotation,
593 Hashtable[] consensusData, boolean showProfileLogo, int nseqs)
595 if (consensusAnnotation == null
596 || consensusAnnotation.annotations == null
597 || consensusAnnotation.annotations.length < consensusData.length)
599 // called with a bad alignment annotation row - wait for it to be
600 // initialised properly
604 // ensure codon triplet scales with font size
605 consensusAnnotation.scaleColLabel = true;
606 for (int col = 0; col < consensusData.length; col++)
608 Hashtable hci = consensusData[col];
611 // gapped protein column?
614 // array holds #seqs, #ungapped, then codon counts indexed by codon
615 final int[] codonCounts = (int[]) hci.get(PROFILE);
619 * First pass - get total count and find the highest
621 final char[] codons = new char[codonCounts.length - 2];
622 for (int j = 2; j < codonCounts.length; j++)
624 final int codonCount = codonCounts[j];
625 codons[j - 2] = (char) (j - 2);
626 totalCount += codonCount;
630 * Sort array of encoded codons by count ascending - so the modal value
631 * goes to the end; start by copying the count (dropping the first value)
633 int[] sortedCodonCounts = new int[codonCounts.length - 2];
634 System.arraycopy(codonCounts, 2, sortedCodonCounts, 0,
635 codonCounts.length - 2);
636 QuickSort.sort(sortedCodonCounts, codons);
638 int modalCodonEncoded = codons[codons.length - 1];
639 int modalCodonCount = sortedCodonCounts[codons.length - 1];
640 String modalCodon = String.valueOf(CodingUtils
641 .decodeCodon(modalCodonEncoded));
642 if (sortedCodonCounts.length > 1
643 && sortedCodonCounts[codons.length - 2] == sortedCodonCounts[codons.length - 1])
646 * two or more codons share the modal count
650 float pid = sortedCodonCounts[sortedCodonCounts.length - 1] * 100
651 / (float) totalCount;
654 * todo ? Replace consensus hashtable with sorted arrays of codons and
655 * counts (non-zero only). Include total count in count array [0].
659 * Scan sorted array backwards for most frequent values first. Show
660 * repeated values compactly.
662 StringBuilder mouseOver = new StringBuilder(32);
663 StringBuilder samePercent = new StringBuilder();
664 String percent = null;
665 String lastPercent = null;
666 Format fmt = getPercentageFormat(nseqs);
668 for (int j = codons.length - 1; j >= 0; j--)
670 int codonCount = sortedCodonCounts[j];
674 * remaining codons are 0% - ignore, but finish off the last one if
677 if (samePercent.length() > 0)
679 mouseOver.append(samePercent).append(": ").append(percent)
684 int codonEncoded = codons[j];
685 final int pct = codonCount * 100 / totalCount;
686 String codon = String
687 .valueOf(CodingUtils.decodeCodon(codonEncoded));
688 percent = fmt == null ? Integer.toString(pct) : fmt.form(pct);
689 if (showProfileLogo || codonCount == modalCodonCount)
691 if (percent.equals(lastPercent) && j > 0)
693 samePercent.append(samePercent.length() == 0 ? "" : ", ");
694 samePercent.append(codon);
698 if (samePercent.length() > 0)
700 mouseOver.append(samePercent).append(": ")
701 .append(lastPercent).append("% ");
703 samePercent.setLength(0);
704 samePercent.append(codon);
706 lastPercent = percent;
710 consensusAnnotation.annotations[col] = new Annotation(modalCodon,
711 mouseOver.toString(), ' ', pid);