JAL-2629 implement hmmbuild
[jalview.git] / src / jalview / datamodel / HiddenMarkovModel.java
index 96a5a6f..727ffc7 100644 (file)
@@ -469,14 +469,33 @@ public class HiddenMarkovModel
    */
   public char getConsensusAtAlignColumn(int columnIndex)
   {
-    char value;
+    char mostLikely = '-';
+    if (consensusResidueIsActive())
+    {
+
     Integer index = findNodeIndex(columnIndex);
     if (index == null)
     {
       return '-';
     }
-    value = getNodes().get(index).getConsensusResidue();
-    return value;
+      mostLikely = getNodes().get(index).getConsensusResidue();
+      return mostLikely;
+    }
+    else
+    {
+      double highestProb = 0;
+      for (char character : symbols)
+      {
+        Double prob = getMatchEmissionProbability(columnIndex, character);
+        if (prob > highestProb)
+        {
+          highestProb = prob;
+          mostLikely = character;
+        }
+      }
+      return mostLikely;
+    }
+
   }
 
   /**
@@ -933,7 +952,9 @@ public class HiddenMarkovModel
       }
 
       Character cons;
+
       cons = getConsensusAtAlignColumn(alignPos);
+
       cons = Character.toUpperCase(cons);
 
       String description = String.format("%.3f", content);
@@ -948,6 +969,7 @@ public class HiddenMarkovModel
             "The information content of each column, measured in bits",
             annotations,
             0f, max, AlignmentAnnotation.BAR_GRAPH);
+    annotation.setHMM(this);
     return annotation;
   }
 
@@ -1008,14 +1030,9 @@ public class HiddenMarkovModel
     for (int index = 0; index < length; index++)
     {
       Character character;
-      if (consensusResidueIsActive())
-      {
+
         character = getConsensusAtAlignColumn(index);
-      }
-      else
-      {
-        character = findConsensusCharacter(index);
-      }
+
       if (character == null || character == '-')
       {
         sequence[index] = '-';
@@ -1031,29 +1048,6 @@ public class HiddenMarkovModel
     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;
-  }
 
   /**
    * Maps the nodes of the hidden Markov model to the reference annotation.
@@ -1089,5 +1083,17 @@ public class HiddenMarkovModel
 
     }
   }
+
+  public AlignmentI initPlaceholder(AlignmentI alignment)
+  {
+    int length = alignment.getWidth();
+    Sequence consensus = getConsensusSequence(length);
+    consensus.setHMM(this);
+    SequenceI[] consensusArr = new Sequence[] { consensus };
+    AlignmentI newAlignment = new Alignment(consensusArr);
+    newAlignment.append(alignment);
+    return newAlignment;
+  }
+
 }