JAL-2616 add placeholder sequence for HMM, does not yet align properly
authortzvanaalten <tzvanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Mon, 24 Jul 2017 14:46:18 +0000 (15:46 +0100)
committertzvanaalten <tzvanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Mon, 24 Jul 2017 14:46:18 +0000 (15:46 +0100)
src/jalview/datamodel/HiddenMarkovModel.java
src/jalview/gui/AlignFrame.java

index 0dcc72e..879968f 100644 (file)
@@ -987,5 +987,72 @@ public class HiddenMarkovModel
     return informationContent;
   }
 
+  /**
+   * Returns the consensus sequence based on the most probable symbol at each
+   * position. The sequence is adjusted to match the length of the existing
+   * sequence alignment. Gap characters are used as padding.
+   * 
+   * @param length
+   *          The length of the longest sequence in the existing alignment.
+   * @return
+   */
+  public Sequence getConsensusSequence(int length)
+  {
+    int start;
+    int end;
+    int modelLength;
+    start = getNodeAlignmentColumn(1);
+    modelLength = getLength();
+    end = getNodeAlignmentColumn(modelLength);
+    char[] sequence = new char[length];
+    for (int index = 0; index < length; index++)
+    {
+      Character character;
+      if (consensusResidueIsActive())
+      {
+        character = getConsensusAtAlignColumn(index);
+      }
+      else
+      {
+        character = findConsensusCharacter(index);
+      }
+      if (character == null || character == '-')
+      {
+        sequence[index] = '-';
+      }
+      else
+      {
+        sequence[index] = Character.toUpperCase(character);
+      }
+      }
+
+
+    Sequence seq = new Sequence("HMM CONSENSUS", sequence, start, end);
+    return seq;
+  }
+
+  /**
+   * Finds the most probable character at a column in an alignment based on the
+   * HMM.
+   * 
+   * @param nodeIndex
+   *          The index of the node.
+   * @return
+   */
+  Character findConsensusCharacter(int column)
+  {
+    Character mostLikely = null;
+    double highestProb = 0;
+    for (char character : symbols)
+    {
+      Double prob = getMatchEmissionProbability(column, character);
+      if (prob > highestProb)
+      {
+        highestProb = prob;
+        mostLikely = character;
+      }
+    }
+    return mostLikely;
+  }
 }
 
index 74e7652..9e4f37b 100644 (file)
@@ -4673,6 +4673,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                     getViewport().getAlignment().getWidth());
             getViewport().getAlignment().addAnnotation(annotation);
             annotation.setHMM(hmm);
+            int length = getViewport().getAlignment().getWidth();
+            Sequence consensus = hmm.getConsensusSequence(length);
+            SequenceI[] consensusArr = new Sequence[] { consensus };
+            AlignmentI newAlignment = new Alignment(consensusArr);
+            newAlignment.append(getViewport().getAlignment());
+            getViewport().setAlignment(newAlignment);
             isAnnotation = true;
 
           }