+
+ /**
+ * Returns the number of decimal places to show for profile percentages. For
+ * less than 100 sequences, returns zero (the integer percentage value will be
+ * displayed). For 100-999 sequences, returns 1, for 1000-9999 returns 2, etc.
+ *
+ * @param nseq
+ * @return
+ */
+ protected static int getPercentageDp(long nseq)
+ {
+ int scale = 0;
+ while (nseq >= 100)
+ {
+ scale++;
+ nseq /= 10;
+ }
+ return scale;
+ }
+
+ /**
+ * Returns the information content at a specified column.
+ *
+ * @param column
+ * Index of the column, starting from 0.
+ * @return
+ */
+ public static float getInformationContent(int column,
+ HiddenMarkovModel hmm)
+ {
+ float informationContent = 0f;
+
+ for (char symbol : hmm.getSymbols())
+ {
+ float freq = 0f;
+ freq = ResidueProperties.backgroundFrequencies
+ .get(hmm.getAlphabetType()).get(symbol);
+ Double hmmProb = hmm.getMatchEmissionProbability(column, symbol);
+ float prob = hmmProb.floatValue();
+ informationContent += prob * (Math.log(prob / freq) / Math.log(2));
+
+ }
+
+ return informationContent;
+ }
+
+ /**
+ * Produces a HMM profile for a column in an alignment
+ *
+ * @param aa
+ * Alignment annotation for which the profile is being calculated.
+ * @param column
+ * Column in the alignment the profile is being made for.
+ * @param removeBelowBackground
+ * Boolean indicating whether to ignore residues with probabilities
+ * less than their background frequencies.
+ * @return
+ */
+ public static int[] extractHMMProfile(HiddenMarkovModel hmm, int column,
+ boolean removeBelowBackground, boolean infoHeight)
+ {
+
+ if (hmm != null)
+ {
+ int size = hmm.getNumberOfSymbols();
+ char symbols[] = new char[size];
+ int values[] = new int[size];
+ List<Character> charList = hmm.getSymbols();
+ Integer totalCount = 0;
+
+ for (int i = 0; i < size; i++)
+ {
+ char symbol = charList.get(i);
+ symbols[i] = symbol;
+ int value = getAnalogueCount(hmm, column, symbol,
+ removeBelowBackground, infoHeight);
+ values[i] = value;
+ totalCount += value;
+ }
+
+ QuickSort.sort(values, symbols);
+
+ int[] profile = new int[3 + size * 2];
+
+ profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
+ profile[1] = size;
+ profile[2] = 100;
+
+ if (totalCount != 0)
+ {
+ int arrayPos = 3;
+ for (int k = size - 1; k >= 0; k--)
+ {
+ Float percentage;
+ Integer value = values[k];
+ if (removeBelowBackground)
+ {
+ percentage = (value.floatValue() / totalCount.floatValue())
+ * 100;
+ }
+ else
+ {
+ percentage = value.floatValue() / 100f;
+ }
+ int intPercent = Math.round(percentage);
+ profile[arrayPos] = symbols[k];
+ profile[arrayPos + 1] = intPercent;
+ arrayPos += 2;
+ }
+ }
+ return profile;
+ }
+ return null;
+ }
+
+ /**
+ * Converts the emission probability of a residue at a column in the alignment
+ * to a 'count' to allow for processing by the annotation renderer.
+ *
+ * @param hmm
+ * @param column
+ * @param removeBelowBackground
+ * When true, this method returns 0 for any symbols with a match
+ * emission probability less than the background frequency.
+ * @param symbol
+ * @return
+ */
+ static int getAnalogueCount(HiddenMarkovModel hmm, int column,
+ char symbol, boolean removeBelowBackground, boolean infoHeight)
+ {
+ Double value;
+
+ value = hmm.getMatchEmissionProbability(column, symbol);
+ double freq;
+
+ freq = ResidueProperties.backgroundFrequencies
+ .get(hmm.getAlphabetType()).get(symbol);
+ if (value < freq && removeBelowBackground)
+ {
+ return 0;
+ }
+
+ if (infoHeight)
+ {
+ value = value * (Math.log(value / freq) / Math.log(2));
+ }
+
+ value = value * 10000;
+ return Math.round(value.floatValue());
+ }