JAL-2629 add StringParameter
[jalview.git] / src / jalview / datamodel / HiddenMarkovModel.java
index d5e93e3..a837b4f 100644 (file)
@@ -1,12 +1,9 @@
 package jalview.datamodel;
 
-import jalview.gui.AlignFrame;
-
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Scanner;
 
 /**
  * Data structure which stores a hidden Markov model. Currently contains file
@@ -43,8 +40,6 @@ public class HiddenMarkovModel
   final static String YES = "yes";
 
   final static String NO = "no";
-
-  int numberOfSymbols;
   
   // keys for file properties hashmap
   private final String NAME = "NAME";
@@ -131,7 +126,6 @@ public class HiddenMarkovModel
     this.nodeLookup = new HashMap<>(hmm.nodeLookup);
     this.symbolIndexLookup = new HashMap<>(
             hmm.symbolIndexLookup);
-    this.numberOfSymbols = hmm.numberOfSymbols;
     this.fileHeader = new String(hmm.fileHeader);
   }
 
@@ -456,7 +450,6 @@ public class HiddenMarkovModel
   public Double getStateTransitionProbability(int alignColumn,
           int transition)
   {
-    int transitionIndex;
     int nodeIndex;
     Double probability;
     if (nodeLookup.containsKey(alignColumn))
@@ -501,7 +494,8 @@ public class HiddenMarkovModel
   }
   
   /**
-   * Returns the consensus at a given alignment column.
+   * Returns the consensus at a given alignment column. If the character is
+   * lower case, its emission probability is less than 0.5.
    * 
    * @param columnIndex
    *          The index of the column in the alignment for which the consensus
@@ -534,6 +528,10 @@ public class HiddenMarkovModel
           mostLikely = character;
         }
       }
+      if (highestProb < 0.5)
+      {
+        mostLikely = Character.toLowerCase(mostLikely);
+      }
       return mostLikely;
     }
 
@@ -599,28 +597,7 @@ public class HiddenMarkovModel
    */
   public int getNumberOfSymbols()
   {
-    return numberOfSymbols;
-  }
-
-  /**
-   * Fills symbol array and whilst doing so, updates the value of the number of
-   * symbols.
-   * 
-   * @param parser
-   *          The scanner scanning the symbol line in the file.
-   */
-  public void fillSymbols(Scanner parser)
-  {
-    int i = 0;
-    while (parser.hasNext())
-    {
-      String strSymbol = parser.next();
-      char[] symbol = strSymbol.toCharArray();
-      symbols.add(symbol[0]);
-      symbolIndexLookup.put(symbol[0], i);
-      i++;
-    }
-    numberOfSymbols = symbols.size();
+    return symbols.size();
   }
 
   /**
@@ -771,10 +748,22 @@ public class HiddenMarkovModel
    */
   public void setAlignmentColumn(int nodeIndex, int column)
   {
+    int currentCol = getNodeAlignmentColumn(nodeIndex);
+    nodeLookup.remove(currentCol);
     nodes.get(nodeIndex).setAlignmentColumn(column);
+    nodeLookup.put(column, nodeIndex);
   }
 
   /**
+   * Clears all data in the node lookup map
+   */
+  public void emptyNodeLookup()
+  {
+    nodeLookup = new HashMap<>();
+  }
+
+
+  /**
    * Sets the reference annotation at a given node.
    * 
    * @param nodeIndex
@@ -1011,119 +1000,17 @@ public class HiddenMarkovModel
       }
 
 
-    Sequence seq = new Sequence(getName() + "_HMM", sequence, start,
+    Sequence seq = new Sequence(getName(), sequence, start,
             end);
     return seq;
   }
 
 
   /**
-   * Maps the nodes of the hidden Markov model to the reference annotation and
-   * then deletes this annotation.
+   * Initiates a HMM consensus sequence
+   * 
+   * @return A new HMM consensus sequence
    */
-  public void mapToReferenceAnnotation(AlignFrame af, SequenceI seq)
-  {
-    AlignmentAnnotation annotArray[] = af.getViewport().getAlignment()
-            .getAlignmentAnnotation();
-
-    AlignmentAnnotation reference = null;
-    for (AlignmentAnnotation annot : annotArray)
-    {
-      if (annot.label.contains("Reference"))
-      {
-        reference = annot;
-      }
-    }
-
-    if (reference == null)
-    {
-      return;
-    }
-
-    mapToReferenceAnnotation(reference, seq);
-    af.getViewport().getAlignment().deleteAnnotation(reference);
-  }
-
-  public void mapToReferenceAnnotation(AlignmentAnnotation reference,
-          SequenceI seq)
-  {
-    HiddenMarkovModel hmm = seq.getHMM();
-    Annotation[] annots = reference.annotations;
-    {
-      int nodeIndex = 0;
-      for (int col = 0; col < annots.length; col++)
-      {
-        String character = annots[col].displayCharacter;
-        if ("x".equals(character) || "X".equals(character))
-        {
-          nodeIndex++;
-          if (nodeIndex < hmm.getNodes().size())
-          {
-            HMMNode node = hmm.getNode(nodeIndex);
-            int alignPos = getNodeAlignmentColumn(nodeIndex);
-            char seqCharacter = seq.getCharAt(alignPos);
-            if (alignPos >= seq.getLength() || col >= seq.getLength())
-            {
-              seq.insertCharAt(seq.getLength(),
-                      (alignPos + 1) - seq.getLength(),
-                      '-');
-            }
-            seq.getSequence()[alignPos] = '-';
-            seq.getSequence()[col] = seqCharacter;
-            node.setAlignmentColumn(col);
-            hmm.nodeLookup.put(col, nodeIndex);
-          }
-          else
-          {
-            System.out.println(
-                    "The reference annotation contains more consensus columns than the hidden Markov model");
-            break;
-          }
-        }
-        else
-        {
-          hmm.nodeLookup.remove(col);
-        }
-      }
-
-    }
-
-  }
-
-  public void mapToReferenceAnnotation(AlignmentAnnotation reference)
-  {
-    Annotation[] annots = reference.annotations;
-    {
-      int nodeIndex = 0;
-      for (int col = 0; col < annots.length; col++)
-      {
-        String character = annots[col].displayCharacter;
-        if ("x".equals(character) || "X".equals(character))
-        {
-          nodeIndex++;
-          if (nodeIndex < nodes.size())
-          {
-            HMMNode node = nodes.get(nodeIndex);
-            node.setAlignmentColumn(col + 1);
-            nodeLookup.put(col, nodeIndex);
-          }
-          else
-          {
-            System.out.println(
-                    "The reference annotation contains more consensus columns than the hidden Markov model");
-            break;
-          }
-        }
-        else
-        {
-          nodeLookup.remove(col);
-        }
-      }
-
-    }
-
-  }
-
   public SequenceI initHMMSequence()
   {
     Sequence consensus = getConsensusSequence();
@@ -1132,6 +1019,15 @@ public class HiddenMarkovModel
     return consensus;
   }
 
+  public int getSymbolIndex(char c)
+  {
+    return symbolIndexLookup.get(c);
+  }
+
+  public void setSymbolIndex(Character c, Integer i)
+  {
+    symbolIndexLookup.put(c, i);
+  }
 
 }