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.HiddenMarkovModel;
28 import jalview.datamodel.Profile;
29 import jalview.datamodel.ProfileI;
30 import jalview.datamodel.Profiles;
31 import jalview.datamodel.ProfilesI;
32 import jalview.datamodel.ResidueCount;
33 import jalview.datamodel.ResidueCount.SymbolCounts;
34 import jalview.datamodel.SequenceI;
35 import jalview.ext.android.SparseIntArray;
36 import jalview.schemes.ResidueProperties;
37 import jalview.util.Comparison;
38 import jalview.util.Format;
39 import jalview.util.MappingUtils;
40 import jalview.util.QuickSort;
42 import java.awt.Color;
43 import java.util.Arrays;
44 import java.util.Hashtable;
45 import java.util.List;
48 * Takes in a vector or array of sequences and column start and column end and
49 * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
50 * This class is used extensively in calculating alignment colourschemes that
51 * depend on the amount of conservation in each alignment column.
56 public class AAFrequency
58 public static final String PROFILE = "P";
60 private static final String AMINO = "amino";
62 private static final String DNA = "DNA";
65 * Quick look-up of String value of char 'A' to 'Z'
67 private static final String[] CHARS = new String['Z' - 'A' + 1];
71 for (char c = 'A'; c <= 'Z'; c++)
73 CHARS[c - 'A'] = String.valueOf(c);
77 public static final ProfilesI calculate(List<SequenceI> list, int start,
80 return calculate(list, start, end, false);
83 public static final ProfilesI calculate(List<SequenceI> sequences,
84 int start, int end, boolean profile)
86 SequenceI[] seqs = new SequenceI[sequences.size()];
88 synchronized (sequences)
90 for (int i = 0; i < sequences.size(); i++)
92 seqs[i] = sequences.get(i);
93 int length = seqs[i].getLength();
105 ProfilesI reply = calculate(seqs, width, start, end, profile);
111 * Calculate the consensus symbol(s) for each column in the given range.
115 * the full width of the alignment
117 * start column (inclusive, base zero)
119 * end column (exclusive)
120 * @param saveFullProfile
121 * if true, store all symbol counts
123 public static final ProfilesI calculate(final SequenceI[] sequences,
124 int width, int start, int end, boolean saveFullProfile)
126 // long now = System.currentTimeMillis();
127 int seqCount = sequences.length;
128 boolean nucleotide = false;
129 int nucleotideCount = 0;
130 int peptideCount = 0;
132 ProfileI[] result = new ProfileI[width];
134 for (int column = start; column < end; column++)
137 * Apply a heuristic to detect nucleotide data (which can
138 * be counted in more compact arrays); here we test for
139 * more than 90% nucleotide; recheck every 10 columns in case
140 * of misleading data e.g. highly conserved Alanine in peptide!
141 * Mistakenly guessing nucleotide has a small performance cost,
142 * as it will result in counting in sparse arrays.
143 * Mistakenly guessing peptide has a small space cost,
144 * as it will use a larger than necessary array to hold counts.
146 if (nucleotideCount > 100 && column % 10 == 0)
148 nucleotide = (9 * peptideCount < nucleotideCount);
150 ResidueCount residueCounts = new ResidueCount(nucleotide);
152 for (int row = 0; row < seqCount; row++)
154 if (sequences[row] == null)
157 .println("WARNING: Consensus skipping null sequence - possible race condition.");
160 char[] seq = sequences[row].getSequence();
161 if (seq.length > column)
163 char c = seq[column];
164 residueCounts.add(c);
165 if (Comparison.isNucleotide(c))
169 else if (!Comparison.isGap(c))
177 * count a gap if the sequence doesn't reach this column
179 residueCounts.addGap();
183 int maxCount = residueCounts.getModalCount();
184 String maxResidue = residueCounts.getResiduesForCount(maxCount);
185 int gapCount = residueCounts.getGapCount();
186 ProfileI profile = new Profile(seqCount, gapCount, maxCount,
191 profile.setCounts(residueCounts);
194 result[column] = profile;
196 return new Profiles(result);
197 // long elapsed = System.currentTimeMillis() - now;
198 // System.out.println(elapsed);
202 * Make an estimate of the profile size we are going to compute i.e. how many
203 * different characters may be present in it. Overestimating has a cost of
204 * using more memory than necessary. Underestimating has a cost of needing to
205 * extend the SparseIntArray holding the profile counts.
207 * @param profileSizes
208 * counts of sizes of profiles so far encountered
211 static int estimateProfileSize(SparseIntArray profileSizes)
213 if (profileSizes.size() == 0)
219 * could do a statistical heuristic here e.g. 75%ile
220 * for now just return the largest value
222 return profileSizes.keyAt(profileSizes.size() - 1);
226 * Derive the consensus annotations to be added to the alignment for display.
227 * This does not recompute the raw data, but may be called on a change in
228 * display options, such as 'ignore gaps', which may in turn result in a
229 * change in the derived values.
232 * the annotation row to add annotations to
234 * the source consensus data
236 * start column (inclusive)
238 * end column (exclusive)
240 * if true, normalise residue percentages ignoring gaps
241 * @param showSequenceLogo
242 * if true include all consensus symbols, else just show modal
245 * number of sequences
247 public static void completeConsensus(AlignmentAnnotation consensus,
248 ProfilesI profiles, int startCol, int endCol, boolean ignoreGaps,
249 boolean showSequenceLogo, long nseq)
251 // long now = System.currentTimeMillis();
252 if (consensus == null || consensus.annotations == null
253 || consensus.annotations.length < endCol)
256 * called with a bad alignment annotation row
257 * wait for it to be initialised properly
262 for (int i = startCol; i < endCol; i++)
264 ProfileI profile = profiles.get(i);
268 * happens if sequences calculated over were
269 * shorter than alignment width
271 consensus.annotations[i] = null;
275 final int dp = getPercentageDp(nseq);
277 float value = profile.getPercentageIdentity(ignoreGaps);
279 String description = getTooltip(profile, value, showSequenceLogo,
282 String modalResidue = profile.getModalResidue();
283 if ("".equals(modalResidue))
287 else if (modalResidue.length() > 1)
291 consensus.annotations[i] = new Annotation(modalResidue, description,
294 // long elapsed = System.currentTimeMillis() - now;
295 // System.out.println(-elapsed);
299 * Derive the gap count annotation row.
302 * the annotation row to add annotations to
304 * the source consensus data
306 * start column (inclusive)
308 * end column (exclusive)
310 public static void completeGapAnnot(AlignmentAnnotation gaprow,
311 ProfilesI profiles, int startCol, int endCol, long nseq)
313 if (gaprow == null || gaprow.annotations == null
314 || gaprow.annotations.length < endCol)
317 * called with a bad alignment annotation row
318 * wait for it to be initialised properly
322 // always set ranges again
323 gaprow.graphMax = nseq;
325 double scale = 0.8/nseq;
326 for (int i = startCol; i < endCol; i++)
328 ProfileI profile = profiles.get(i);
332 * happens if sequences calculated over were
333 * shorter than alignment width
335 gaprow.annotations[i] = null;
339 final int gapped = profile.getNonGapped();
341 String description = "" + gapped;
343 gaprow.annotations[i] = new Annotation("", description,
344 '\0', gapped, jalview.util.ColorUtils.bleachColour(
345 Color.DARK_GRAY, (float) scale * gapped));
350 * Returns a tooltip showing either
352 * <li>the full profile (percentages of all residues present), if
353 * showSequenceLogo is true, or</li>
354 * <li>just the modal (most common) residue(s), if showSequenceLogo is false</li>
356 * Percentages are as a fraction of all sequence, or only ungapped sequences
357 * if ignoreGaps is true.
361 * @param showSequenceLogo
364 * the number of decimal places to format percentages to
367 static String getTooltip(ProfileI profile, float pid,
368 boolean showSequenceLogo, boolean ignoreGaps, int dp)
370 ResidueCount counts = profile.getCounts();
372 String description = null;
373 if (counts != null && showSequenceLogo)
375 int normaliseBy = ignoreGaps ? profile.getNonGapped() : profile
377 description = counts.getTooltip(normaliseBy, dp);
381 StringBuilder sb = new StringBuilder(64);
382 String maxRes = profile.getModalResidue();
383 if (maxRes.length() > 1)
385 sb.append("[").append(maxRes).append("]");
391 if (maxRes.length() > 0)
394 Format.appendPercentage(sb, pid, dp);
397 description = sb.toString();
403 * Returns the sorted profile for the given consensus data. The returned array
407 * [profileType, numberOfValues, nonGapCount, charValue1, percentage1, charValue2, percentage2, ...]
408 * in descending order of percentage value
412 * the data object from which to extract and sort values
414 * if true, only non-gapped values are included in percentage
418 public static int[] extractProfile(ProfileI profile, boolean ignoreGaps)
420 int[] rtnval = new int[64];
421 ResidueCount counts = profile.getCounts();
427 SymbolCounts symbolCounts = counts.getSymbolCounts();
428 char[] symbols = symbolCounts.symbols;
429 int[] values = symbolCounts.values;
430 QuickSort.sort(values, symbols);
431 int nextArrayPos = 2;
432 int totalPercentage = 0;
433 final int divisor = ignoreGaps ? profile.getNonGapped() : profile
437 * traverse the arrays in reverse order (highest counts first)
439 for (int i = symbols.length - 1; i >= 0; i--)
441 int theChar = symbols[i];
442 int charCount = values[i];
444 rtnval[nextArrayPos++] = theChar;
445 final int percentage = (charCount * 100) / divisor;
446 rtnval[nextArrayPos++] = percentage;
447 totalPercentage += percentage;
449 rtnval[0] = symbols.length;
450 rtnval[1] = totalPercentage;
451 int[] result = new int[rtnval.length + 1];
452 result[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
453 System.arraycopy(rtnval, 0, result, 1, rtnval.length);
460 * Extract a sorted extract of cDNA codon profile data. The returned array
464 * [profileType, numberOfValues, totalCount, charValue1, percentage1, charValue2, percentage2, ...]
465 * in descending order of percentage value, where the character values encode codon triplets
471 public static int[] extractCdnaProfile(Hashtable hashtable,
474 // this holds #seqs, #ungapped, and then codon count, indexed by encoded
476 int[] codonCounts = (int[]) hashtable.get(PROFILE);
477 int[] sortedCounts = new int[codonCounts.length - 2];
478 System.arraycopy(codonCounts, 2, sortedCounts, 0,
479 codonCounts.length - 2);
481 int[] result = new int[3 + 2 * sortedCounts.length];
482 // first value is just the type of profile data
483 result[0] = AlignmentAnnotation.CDNA_PROFILE;
485 char[] codons = new char[sortedCounts.length];
486 for (int i = 0; i < codons.length; i++)
488 codons[i] = (char) i;
490 QuickSort.sort(sortedCounts, codons);
491 int totalPercentage = 0;
492 int distinctValuesCount = 0;
494 int divisor = ignoreGaps ? codonCounts[1] : codonCounts[0];
495 for (int i = codons.length - 1; i >= 0; i--)
497 final int codonCount = sortedCounts[i];
500 break; // nothing else of interest here
502 distinctValuesCount++;
503 result[j++] = codons[i];
504 final int percentage = codonCount * 100 / divisor;
505 result[j++] = percentage;
506 totalPercentage += percentage;
508 result[2] = totalPercentage;
511 * Just return the non-zero values
513 // todo next value is redundant if we limit the array to non-zero counts
514 result[1] = distinctValuesCount;
515 return Arrays.copyOfRange(result, 0, j);
519 * Compute a consensus for the cDNA coding for a protein alignment.
522 * the protein alignment (which should hold mappings to cDNA
525 * the consensus data stores to be populated (one per column)
527 public static void calculateCdna(AlignmentI alignment,
528 Hashtable[] hconsensus)
530 final char gapCharacter = alignment.getGapCharacter();
531 List<AlignedCodonFrame> mappings = alignment.getCodonFrames();
532 if (mappings == null || mappings.isEmpty())
537 int cols = alignment.getWidth();
538 for (int col = 0; col < cols; col++)
540 // todo would prefer a Java bean for consensus data
541 Hashtable<String, int[]> columnHash = new Hashtable<>();
542 // #seqs, #ungapped seqs, counts indexed by (codon encoded + 1)
543 int[] codonCounts = new int[66];
544 codonCounts[0] = alignment.getSequences().size();
545 int ungappedCount = 0;
546 for (SequenceI seq : alignment.getSequences())
548 if (seq.getCharAt(col) == gapCharacter)
552 List<char[]> codons = MappingUtils
553 .findCodonsFor(seq, col, mappings);
554 for (char[] codon : codons)
556 int codonEncoded = CodingUtils.encodeCodon(codon);
557 if (codonEncoded >= 0)
559 codonCounts[codonEncoded + 2]++;
564 codonCounts[1] = ungappedCount;
565 // todo: sort values here, save counts and codons?
566 columnHash.put(PROFILE, codonCounts);
567 hconsensus[col] = columnHash;
572 * Derive displayable cDNA consensus annotation from computed consensus data.
574 * @param consensusAnnotation
575 * the annotation row to be populated for display
576 * @param consensusData
577 * the computed consensus data
578 * @param showProfileLogo
579 * if true show all symbols present at each position, else only the
582 * the number of sequences in the alignment
584 public static void completeCdnaConsensus(
585 AlignmentAnnotation consensusAnnotation,
586 Hashtable[] consensusData, boolean showProfileLogo, int nseqs)
588 if (consensusAnnotation == null
589 || consensusAnnotation.annotations == null
590 || consensusAnnotation.annotations.length < consensusData.length)
592 // called with a bad alignment annotation row - wait for it to be
593 // initialised properly
597 // ensure codon triplet scales with font size
598 consensusAnnotation.scaleColLabel = true;
599 for (int col = 0; col < consensusData.length; col++)
601 Hashtable hci = consensusData[col];
604 // gapped protein column?
607 // array holds #seqs, #ungapped, then codon counts indexed by codon
608 final int[] codonCounts = (int[]) hci.get(PROFILE);
612 * First pass - get total count and find the highest
614 final char[] codons = new char[codonCounts.length - 2];
615 for (int j = 2; j < codonCounts.length; j++)
617 final int codonCount = codonCounts[j];
618 codons[j - 2] = (char) (j - 2);
619 totalCount += codonCount;
623 * Sort array of encoded codons by count ascending - so the modal value
624 * goes to the end; start by copying the count (dropping the first value)
626 int[] sortedCodonCounts = new int[codonCounts.length - 2];
627 System.arraycopy(codonCounts, 2, sortedCodonCounts, 0,
628 codonCounts.length - 2);
629 QuickSort.sort(sortedCodonCounts, codons);
631 int modalCodonEncoded = codons[codons.length - 1];
632 int modalCodonCount = sortedCodonCounts[codons.length - 1];
633 String modalCodon = String.valueOf(CodingUtils
634 .decodeCodon(modalCodonEncoded));
635 if (sortedCodonCounts.length > 1
636 && sortedCodonCounts[codons.length - 2] == sortedCodonCounts[codons.length - 1])
639 * two or more codons share the modal count
643 float pid = sortedCodonCounts[sortedCodonCounts.length - 1] * 100
644 / (float) totalCount;
647 * todo ? Replace consensus hashtable with sorted arrays of codons and
648 * counts (non-zero only). Include total count in count array [0].
652 * Scan sorted array backwards for most frequent values first. Show
653 * repeated values compactly.
655 StringBuilder mouseOver = new StringBuilder(32);
656 StringBuilder samePercent = new StringBuilder();
657 String percent = null;
658 String lastPercent = null;
659 int percentDecPl = getPercentageDp(nseqs);
661 for (int j = codons.length - 1; j >= 0; j--)
663 int codonCount = sortedCodonCounts[j];
667 * remaining codons are 0% - ignore, but finish off the last one if
670 if (samePercent.length() > 0)
672 mouseOver.append(samePercent).append(": ").append(percent)
677 int codonEncoded = codons[j];
678 final int pct = codonCount * 100 / totalCount;
679 String codon = String
680 .valueOf(CodingUtils.decodeCodon(codonEncoded));
681 StringBuilder sb = new StringBuilder();
682 Format.appendPercentage(sb, pct, percentDecPl);
683 percent = sb.toString();
684 if (showProfileLogo || codonCount == modalCodonCount)
686 if (percent.equals(lastPercent) && j > 0)
688 samePercent.append(samePercent.length() == 0 ? "" : ", ");
689 samePercent.append(codon);
693 if (samePercent.length() > 0)
695 mouseOver.append(samePercent).append(": ")
696 .append(lastPercent).append("% ");
698 samePercent.setLength(0);
699 samePercent.append(codon);
701 lastPercent = percent;
705 consensusAnnotation.annotations[col] = new Annotation(modalCodon,
706 mouseOver.toString(), ' ', pid);
711 * Returns the number of decimal places to show for profile percentages. For
712 * less than 100 sequences, returns zero (the integer percentage value will be
713 * displayed). For 100-999 sequences, returns 1, for 1000-9999 returns 2, etc.
718 protected static int getPercentageDp(long nseq)
730 * produces a HMM profile for a column in an alignment
733 * Alignment annotation for which the profile is being calculated
735 * column in the alignment the profile is being made for
736 * @param removeBelowBackground
737 * boolean, indicating whether to ignore residues with probabilities
738 * less than their background frequencies
741 public static int[] getHMMProfileFor(AlignmentAnnotation aa, int column,
742 boolean removeBelowBackground)
745 HiddenMarkovModel hmm;
749 String alph = hmm.getAlphabetType();
750 int size = hmm.getNumberOfSymbols();
751 char symbols[] = new char[size];
752 int values[] = new int[size];
753 List<Character> charList = hmm.getSymbols();
754 Integer totalCount = 0;
756 for (int i = 0; i < size; i++)
758 char symbol = charList.get(i);
762 value = hmm.getMatchEmissionProbability(column, symbol);
765 if (alph == AMINO && removeBelowBackground)
767 freq = ResidueProperties.aminoBackgroundFrequencies.get(symbol);
773 else if (alph == DNA && removeBelowBackground)
775 freq = ResidueProperties.nucleotideBackgroundFrequencies
782 value = value * 10000;
783 values[i] = value.intValue();
784 totalCount += value.intValue();
787 QuickSort.sort(values, symbols);
789 int[] profile = new int[3 + size * 2];
791 profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
793 profile[2] = totalCount / 100;
798 for (int k = size - 1; k >= 0; k--)
801 Integer value = values[k];
802 percentage = (value.doubleValue() / totalCount.doubleValue())
804 profile[arrayPos] = symbols[k];
805 profile[arrayPos + 1] = percentage.intValue();