Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / src / jalview / io / HMMFile.java
index 6e49af6..2fce4cc 100644 (file)
@@ -1,21 +1,20 @@
 package jalview.io;
 
+import jalview.api.AlignExportSettingsI;
+import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.HMMNode;
 import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.SequenceI;
 
 import java.io.BufferedReader;
-import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Scanner;
 
 
 /**
- * reads in and writes out a HMMER standard file
+ * Adds capability to read in and write out HMMER3 files. .
  * 
  * 
  * @author TZVanaalten
@@ -24,289 +23,432 @@ import java.util.Scanner;
 public class HMMFile extends AlignFile
         implements AlignmentFileReaderI, AlignmentFileWriterI
 {
-  // HMM to store file data
-  private HiddenMarkovModel hmm = new HiddenMarkovModel();
+  private static final String TERMINATOR = "//";
 
+  /*
+   * keys to data in HMM file, used to store as properties of the HiddenMarkovModel
+   */
+  public static final String HMM = "HMM";
+
+  public static final String NAME = "NAME";
+
+  public static final String ACCESSION_NUMBER = "ACC";
+
+  public static final String DESCRIPTION = "DESC";
+
+  public static final String LENGTH = "LENG";
+
+  public static final String MAX_LENGTH = "MAXL";
+
+  public static final String ALPHABET = "ALPH";
+
+  public static final String DATE = "DATE";
+
+  public static final String COMMAND_LOG = "COM";
+
+  public static final String NUMBER_OF_SEQUENCES = "NSEQ";
+
+  public static final String EFF_NUMBER_OF_SEQUENCES = "EFFN";
+
+  public static final String CHECK_SUM = "CKSUM";
+
+  public static final String STATISTICS = "STATS";
+
+  public static final String COMPO = "COMPO";
 
+  public static final String GATHERING_THRESHOLD = "GA";
 
+  public static final String TRUSTED_CUTOFF = "TC";
 
-  // number of possible transitions
-  private final int NUMBER_OF_TRANSITIONS = 7;
+  public static final String NOISE_CUTOFF = "NC";
 
-  private final String NEW_LINE = "\n";
+  public static final String VITERBI = "VITERBI";
 
+  public static final String MSV = "MSV";
 
-  // file header
-  String fileHeader;
+  public static final String FORWARD = "FORWARD";
 
-  int numberOfSymbols;
+  public static final String MAP = "MAP";
 
-  private final String SPACE = " ";
+  public static final String REFERENCE_ANNOTATION = "RF";
+
+  public static final String CONSENSUS_RESIDUE = "CONS";
+
+  public static final String CONSENSUS_STRUCTURE = "CS";
+
+  public static final String MASKED_VALUE = "MM";
+
+  private static final String ALPH_AMINO = "amino";
+
+  private static final String ALPH_DNA = "DNA";
+
+  private static final String ALPH_RNA = "RNA";
+
+  private static final String ALPHABET_AMINO = "ACDEFGHIKLMNPQRSTVWY";
+
+  private static final String ALPHABET_DNA = "ACGT";
+
+  private static final String ALPHABET_RNA = "ACGU";
+
+  private static final int NUMBER_OF_TRANSITIONS = 7;
+
+  private static final String SPACE = " ";
+
+  /*
+   * optional guide line added to an output HMMER file, purely for readability
+   */
+  private static final String TRANSITIONTYPELINE = "            m->m     m->i     m->d     i->m     i->i     d->m     d->d";
 
-  private final String COMPO = "COMPO";
+  private static String NL = System.lineSeparator();
 
-  private final String EMPTY = "";
+  private HiddenMarkovModel hmm;
 
+  // number of symbols in the alphabet used in the hidden Markov model
+  private int numberOfSymbols;
+
+  /**
+   * Constructor that parses immediately
+   * 
+   * @param inFile
+   * @param type
+   * @throws IOException
+   */
+  public HMMFile(String inFile, DataSourceType type) throws IOException
+  {
+    super(inFile, type);
+  }
+
+  /**
+   * Constructor that parses immediately
+   * 
+   * @param source
+   * @throws IOException
+   */
   public HMMFile(FileParse source) throws IOException
   {
-    super(false, source);
+    super(source);
   }
 
+  /**
+   * Default constructor
+   */
   public HMMFile()
   {
-
   }
 
-  public HiddenMarkovModel getHMM()
+  /**
+   * Constructor for HMMFile used for exporting
+   * 
+   * @param hmm
+   */
+  public HMMFile(HiddenMarkovModel markov)
   {
-    return hmm;
+    hmm = markov;
   }
 
-  public void setHMM(HiddenMarkovModel model)
+  /**
+   * Returns the HMM produced by parsing a HMMER3 file
+   * 
+   * @return
+   */
+  public HiddenMarkovModel getHMM()
   {
-    this.hmm = model;
+    return hmm;
   }
 
+  /**
+   * Gets the name of the hidden Markov model
+   * 
+   * @return
+   */
   public String getName()
   {
     return hmm.getName();
   }
 
   /**
-   * reads data from HMM file
-   * 
-   * @throws IOException
+   * Reads the data from HMM file into the HMM model
    */
   @Override
-  public void parse() throws IOException
+  public void parse()
   {
-    parseFileProperties(dataIn);
-    parseModel(dataIn);
+    try
+    {
+      hmm = new HiddenMarkovModel();
+      parseHeaderLines(dataIn);
+      parseModel(dataIn);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+    }
   }
 
-
-
   /**
-   * imports file properties from hmm file
+   * Reads the header properties from a HMMER3 file and saves them in the
+   * HiddeMarkovModel. This method exits after reading the next line after the
+   * HMM line.
    * 
    * @param input
-   *          buffered reader used to read in file
    * @throws IOException
    */
-  void parseFileProperties(BufferedReader input) throws IOException
+  void parseHeaderLines(BufferedReader input) throws IOException
   {
-    boolean readingFile = true;
-    fileHeader = input.readLine();
+    boolean readingHeaders = true;
+    hmm.setFileHeader(input.readLine());
     String line = input.readLine();
-    while (readingFile)
+    while (readingHeaders && line != null)
     {
-      if (line != null)
+      Scanner parser = new Scanner(line);
+      String next = parser.next();
+      if (ALPHABET.equals(next))
       {
-        Scanner parser = new Scanner(line);
-        String next = parser.next();
-        if ("HMM".equals(next)) // indicates start of HMM data (end of file
-                              // properties)
-        {
-          readingFile = false;
-          hmm.fillSymbols(parser);
-          numberOfSymbols = hmm.getNumberOfSymbols();
-        }
-        else if ("STATS".equals(next))
-        {
-          parser.next();
-          String key;
-          String value;
-          key = parser.next();
-          value = parser.next() + SPACE + SPACE + parser.next();
-          hmm.addFileProperty(key, value);
-        }
-        else
+        String alphabetType = parser.next();
+        hmm.setProperty(ALPHABET, alphabetType);
+        String alphabet = ALPH_DNA.equalsIgnoreCase(alphabetType)
+                ? ALPHABET_DNA
+                : (ALPH_RNA.equalsIgnoreCase(alphabetType) ? ALPHABET_RNA
+                        : ALPHABET_AMINO);
+        numberOfSymbols = hmm.setAlphabet(alphabet);
+      }
+      else if (HMM.equals(next))
+      {
+        readingHeaders = false;
+        String symbols = line.substring(line.indexOf(HMM) + HMM.length());
+        numberOfSymbols = hmm.setAlphabet(symbols);
+      }
+      else if (STATISTICS.equals(next))
+      {
+        parser.next();
+        String key;
+        String value;
+        key = parser.next();
+        value = parser.next() + SPACE + SPACE + parser.next();
+        hmm.setProperty(key, value);
+      }
+      else
+      {
+        String key = next;
+        String value = parser.next();
+        while (parser.hasNext())
         {
-          String key = next;
-          String value = parser.next();
-          while (parser.hasNext())
-          {
-            value = value + SPACE + parser.next();
-          }
-          hmm.addFileProperty(key, value);
+          value = value + SPACE + parser.next();
         }
-        parser.close();
+        hmm.setProperty(key, value);
       }
+      parser.close();
       line = input.readLine();
-      if (line == null)
-      {
-        readingFile = false;
-      }
     }
-
   }
 
   /**
-   * parses the model data from the hmm file
+   * Parses the model data from the HMMER3 file. The input buffer should be
+   * positioned at the (optional) COMPO line if there is one, else at the insert
+   * emissions line for the BEGIN node of the model.
    * 
    * @param input
-   *          buffered reader used to read file
    * @throws IOException
    */
   void parseModel(BufferedReader input) throws IOException
   {
-    for (int i = 0; i < hmm.getLength() + 1; i++)
+    /*
+     * specification says there must always be an HMM header (already read)
+     * and one more header (guide headings) which is skipped here
+     */
+    int nodeNo = 0;
+    String line = input.readLine();
+    List<HMMNode> nodes = new ArrayList<>();
+
+    while (line != null && !TERMINATOR.equals(line))
     {
-      hmm.getNodes().add(new HMMNode());
-      String next;
-      String line;
-      line = input.readLine();
-      Scanner matchReader = new Scanner(line);
-      next = matchReader.next();
-      if (next.equals(COMPO) || i > 0)
+      HMMNode node = new HMMNode();
+      nodes.add(node);
+      Scanner scanner = new Scanner(line);
+      String next = scanner.next();
+
+      /*
+       * expect COMPO (optional) for average match emissions
+       * or a node number followed by node's match emissions
+       */
+      if (COMPO.equals(next) || nodeNo > 0)
       {
-        // stores match emission line in list
-        List<Double> matches = new ArrayList<>();
-        matches = fillList(matchReader, numberOfSymbols);
-        hmm.getNodes().get(i).setMatchEmissions(matches);
-        if (i > 0)
+        /*
+         * parse match emissions
+         */
+        double[] matches = parseDoubles(scanner, numberOfSymbols);
+        node.setMatchEmissions(matches);
+        if (!COMPO.equals(next))
         {
-          parseAnnotations(matchReader, i);
+          int resNo = parseAnnotations(scanner, node);
+          if (resNo == 0)
+          {
+            /*
+             * no MAP annotation provided, just number off from 0 (begin node)
+             */
+            resNo = nodeNo;
+          }
+          node.setResidueNumber(resNo);
         }
+        line = input.readLine();
       }
-      matchReader.close();
-      // stores insert emission line in list
+      scanner.close();
+
+      /*
+       * parse insert emissions
+       */
+      scanner = new Scanner(line);
+      double[] inserts = parseDoubles(scanner, numberOfSymbols);
+      node.setInsertEmissions(inserts);
+      scanner.close();
+
+      /*
+       * parse state transitions
+       */
       line = input.readLine();
-      Scanner insertReader = new Scanner(line);
-      List<Double> inserts = new ArrayList<>();
-      inserts = fillList(insertReader, numberOfSymbols);
-      hmm.getNodes().get(i).setInsertEmissions(inserts);
-      insertReader.close();
-
-      // stores state transition line in list
+      scanner = new Scanner(line);
+      double[] transitions = parseDoubles(scanner,
+              NUMBER_OF_TRANSITIONS);
+      node.setStateTransitions(transitions);
+      scanner.close();
       line = input.readLine();
-      Scanner transitionReader = new Scanner(line);
-      List<Double> transitions = new ArrayList<>();
-      transitions = fillList(transitionReader, NUMBER_OF_TRANSITIONS);
-      hmm.getNodes().get(i).setStateTransitions(transitions);
-      transitionReader.close();
+
+      nodeNo++;
     }
 
+    hmm.setNodes(nodes);
   }
 
   /**
-   * parses annotations on match emission line
+   * Parses the annotations on the match emission line and add them to the node.
+   * (See p109 of the HMMER User Guide (V3.1b2) for the specification.) Returns
+   * the residue position that the node maps to, if provided, else zero.
    * 
    * @param scanner
-   *          scanner which is processing match emission line
-   * @param index
-   *          index of node which is beign scanned
+   * @param node
    */
-  void parseAnnotations(Scanner scanner, int index)
+  int parseAnnotations(Scanner scanner, HMMNode node)
   {
-    if (hmm.mapIsActive())
+    int mapTo = 0;
+
+    /*
+     * map from hmm node to sequence position, if provided
+     */
+    if (scanner.hasNext())
     {
-      int column;
-      column = scanner.nextInt();
-      hmm.getNodes().get(index).setAlignmentColumn(column);
-      hmm.getNodeLookup().put(column, index);
+      String value = scanner.next();
+      if (!"-".equals(value))
+      {
+        try
+        {
+          mapTo = Integer.parseInt(value);
+          node.setResidueNumber(mapTo);
+        } catch (NumberFormatException e)
+        {
+          // ignore
+        }
+      }
     }
-    else
+
+    /*
+     * hmm consensus residue if provided, else '-'
+     */
+    if (scanner.hasNext())
     {
-      scanner.next();
+      node.setConsensusResidue(scanner.next().charAt(0));
     }
 
-    char consensusR;
-    consensusR = charValue(scanner.next());
-    hmm.getNodes().get(index).setConsensusResidue(consensusR);
-
-      char reference;
-      reference = charValue(scanner.next());
-      hmm.getNodes().get(index).setReferenceAnnotation(reference);
+    /*
+     * RF reference annotation, if provided, else '-'
+     */
+    if (scanner.hasNext())
+    {
+      node.setReferenceAnnotation(scanner.next().charAt(0));
+    }
 
+    /*
+     * 'm' for masked position, if provided, else '-'
+     */
+    if (scanner.hasNext())
+    {
+      node.setMaskValue(scanner.next().charAt(0));
+    }
 
-      char value;
-      value = charValue(scanner.next());
-      hmm.getNodes().get(index).setMaskValue(value);
+    /*
+     * structure consensus symbol, if provided, else '-'
+     */
+    if (scanner.hasNext())
+    {
+      node.setConsensusStructure(scanner.next().charAt(0));
+    }
 
-    char consensusS;
-    consensusS = charValue(scanner.next());
-    hmm.getNodes().get(index).setConsensusStructure(consensusS);
+    return mapTo;
   }
 
-
   /**
+   * Fills an array of doubles parsed from an input line
    * 
    * @param input
-   *          scanner for line containing data to be transferred to list
    * @param numberOfElements
-   *          number of elements in the list to be filled
-   * @return filled list
+   * @return
+   * @throws IOException
    */
-  static List<Double> fillList(Scanner input,
-          int numberOfElements)
+  static double[] parseDoubles(Scanner input,
+          int numberOfElements) throws IOException
   {
-    List<Double> list = new ArrayList<>();
+    double[] values = new double[numberOfElements];
     for (int i = 0; i < numberOfElements; i++)
     {
-
+      if (!input.hasNext())
+      {
+        throw new IOException("Incomplete data");
+      }
       String next = input.next();
-      if (next.contains("*")) // state transitions to or from delete states
-                              // occasionally have values of -infinity. These
-                              // values are represented by an * in the .hmm
-                              // file, and by a null value in the
-                              // HiddenMarkovModel class
+      if (next.contains("*"))
       {
-        list.add(Double.NEGATIVE_INFINITY);
+        values[i] = Double.NEGATIVE_INFINITY;
       }
       else
       {
-        list.add(Double.valueOf(next));
+        double prob = Double.valueOf(next);
+        prob = Math.pow(Math.E, -prob);
+        values[i] = prob;
       }
     }
-    return list;
+    return values;
   }
 
-  
   /**
-   * writes a HiddenMarkovModel to a file
+   * Returns a string to be added to the StringBuilder containing the entire
+   * output String.
    * 
-   * @param exportLocation
-   *          Filename, URL or Pasted String to write to
-   * @throws FileNotFoundException
-   * @throws UnsupportedEncodingException
-   *
-   **/
-  
-  public void exportFile(String exportLocation) throws IOException
-  {
-    StringBuilder file = new StringBuilder();
-    appendFileProperties(file);
-    appendModel(file);
-    
-    file.append("//");
-
-    PrintWriter output = new PrintWriter(exportLocation);
-    output.append(file);
-    output.close();
-
-  }
-
+   * @param initialColumnSeparation
+   *          The initial whitespace separation between the left side of the
+   *          file and first character.
+   * @param columnSeparation
+   *          The separation between subsequent data entries.
+   * @param data
+   *          The list of data to be added to the String.
+   * @return
+   */
   String addData(int initialColumnSeparation,
           int columnSeparation, List<String> data)
   {
-    String line = EMPTY;
-    int index = 0;
+    String line = "";
+    boolean first = true;
     for (String value : data)
     {
-      if (index == 0)
-      {
-        line += String.format("%" + initialColumnSeparation + "s", value);
-      }
-      else
-      {
-        line += String.format("%" + columnSeparation + "s", value);
-      }
-      index++;
+      int sep = first ? initialColumnSeparation : columnSeparation;
+      line += String.format("%" + sep + "s", value);
+      first = false;
     }
     return line;
   }
 
+  /**
+   * Converts list of characters into a list of Strings.
+   * 
+   * @param list
+   * @return Returns the list of Strings.
+   */
   List<String> charListToStringList(List<Character> list)
   {
     List<String> strList = new ArrayList<>();
@@ -318,237 +460,257 @@ public class HMMFile extends AlignFile
     return strList;
   }
 
-  List<String> doubleListToStringList(List<Double> list,
-          int noOfDecimals)
+  /**
+   * Converts an array of doubles into a list of Strings, rounded to the nearest
+   * 5th decimal place
+   * 
+   * @param doubles
+   * @param noOfDecimals
+   * @return
+   */
+  List<String> doublesToStringList(double[] doubles)
   {
     List<String> strList = new ArrayList<>();
-    for (double value : list)
+    for (double value : doubles)
     {
       String strValue;
-      if (value == Double.NEGATIVE_INFINITY)
+      if (value > 0)
       {
-        strValue = "*";
+        strValue = String.format("%.5f", value);
+      }
+      else if (value == -0.00000d)
+      {
+        strValue = "0.00000";
       }
       else
       {
-        strValue = String.format("%.5f", value);
+        strValue = "*";
       }
-
       strList.add(strValue);
     }
     return strList;
   }
 
-  List<String> stringArrayToStringList(String[] array)
+  /**
+   * Appends model data in string format to the string builder
+   * 
+   * @param output
+   */
+  void appendModelAsString(StringBuilder output)
   {
-    List<String> list = new ArrayList<>();
-    for (String value : array)
+    output.append(HMM).append("  ");
+    String charSymbols = hmm.getSymbols();
+    for (char c : charSymbols.toCharArray())
     {
-      list.add(value);
+      output.append(String.format("%9s", c));
     }
-
-    return list;
-  }
-
-  void appendModel(StringBuilder file)
-  {
-    String symbolLine = "HMM";
-    List<Character> charSymbols = hmm.getSymbols();
-    List<String> strSymbols;
-    strSymbols = charListToStringList(charSymbols);
-    symbolLine += addData(11, 9, strSymbols);
-    file.append(symbolLine + NEW_LINE);
-
-    String transitionTypeLine = "";
-    List<String> transitionTypes;
-    transitionTypes = stringArrayToStringList(hmm.getTransitionTypes());
-    transitionTypeLine += addData(16, 9, transitionTypes);
-    file.append(transitionTypeLine + NEW_LINE);
+    output.append(NL).append(TRANSITIONTYPELINE);
 
     int length = hmm.getLength();
 
-    for (int node = 0; node <= length; node++)
+    for (int nodeNo = 0; nodeNo <= length; nodeNo++)
     {
-      String matchLine;
-      if (node == 0)
-      {
-        matchLine = String.format("%7s", "COMPO");
-      }
-      else
-      {
-        matchLine = String.format("%7s", node);
-      }
+      String matchLine = String.format("%7s",
+              nodeNo == 0 ? COMPO : Integer.toString(nodeNo));
 
-      List<String> strMatches;
-      List<Double> doubleMatches;
-      doubleMatches = hmm.getNode(node).getMatchEmissions();
-      strMatches = doubleListToStringList(doubleMatches, 5);
+      double[] doubleMatches = convertToLogSpace(
+              hmm.getNode(nodeNo).getMatchEmissions());
+      List<String> strMatches = doublesToStringList(doubleMatches);
       matchLine += addData(10, 9, strMatches);
 
-
-      if (node != 0)
+      if (nodeNo != 0)
       {
-        matchLine += SPACE + hmm.getNodeAlignmentColumn(node);
-        matchLine += SPACE + hmm.getConsensusResidue(node);
-        matchLine += SPACE + hmm.getReferenceAnnotation(node);
-        matchLine += SPACE + hmm.getMaskedValue(node);
-        matchLine += SPACE + hmm.getConsensusStructure(node);
-
+        matchLine += SPACE + (hmm.getNodeMapPosition(nodeNo));
+        matchLine += SPACE + hmm.getConsensusResidue(nodeNo);
+        matchLine += SPACE + hmm.getReferenceAnnotation(nodeNo);
+        if (hmm.getFileHeader().contains("HMMER3/f"))
+        {
+          matchLine += SPACE + hmm.getMaskedValue(nodeNo);
+          matchLine += SPACE + hmm.getConsensusStructure(nodeNo);
+        }
       }
 
-      file.append(matchLine + NEW_LINE);
+      output.append(NL).append(matchLine);
       
-      String insertLine = EMPTY;
-      List<String> strInserts;
-      List<Double> doubleInserts;
-      doubleInserts = hmm.getNode(node).getInsertEmissions();
-      strInserts = doubleListToStringList(doubleInserts, 5);
+      String insertLine = "";
+
+      double[] doubleInserts = convertToLogSpace(
+              hmm.getNode(nodeNo).getInsertEmissions());
+      List<String> strInserts = doublesToStringList(doubleInserts);
       insertLine += addData(17, 9, strInserts);
 
-      file.append(insertLine + NEW_LINE);
+      output.append(NL).append(insertLine);
 
-      String transitionLine = EMPTY;
-      List<String> strTransitions;
-      List<Double> doubleTransitions;
-      doubleTransitions = hmm.getNode(node).getStateTransitions();
-      strTransitions = doubleListToStringList(doubleTransitions, 5);
+      String transitionLine = "";
+      double[] doubleTransitions = convertToLogSpace(
+              hmm.getNode(nodeNo).getStateTransitions());
+      List<String> strTransitions = doublesToStringList(
+              doubleTransitions);
       transitionLine += addData(17, 9, strTransitions);
 
-      file.append(transitionLine + NEW_LINE);
+      output.append(NL).append(transitionLine);
     }
   }
 
-  void appendFileProperties(StringBuilder file)
+  /**
+   * Appends formatted HMM file properties to the string builder
+   * 
+   * @param output
+   */
+  void appendProperties(StringBuilder output)
   {
-    String line;
+    output.append(hmm.getFileHeader());
+
+    String format = "%n%-5s %1s";
+    appendProperty(output, format, NAME);
+    appendProperty(output, format, ACCESSION_NUMBER);
+    appendProperty(output, format, DESCRIPTION);
+    appendProperty(output, format, LENGTH);
+    appendProperty(output, format, MAX_LENGTH);
+    appendProperty(output, format, ALPHABET);
+    appendBooleanProperty(output, format, REFERENCE_ANNOTATION);
+    appendBooleanProperty(output, format, MASKED_VALUE);
+    appendBooleanProperty(output, format, CONSENSUS_RESIDUE);
+    appendBooleanProperty(output, format, CONSENSUS_STRUCTURE);
+    appendBooleanProperty(output, format, MAP);
+    appendProperty(output, format, DATE);
+    appendProperty(output, format, NUMBER_OF_SEQUENCES);
+    appendProperty(output, format, EFF_NUMBER_OF_SEQUENCES);
+    appendProperty(output, format, CHECK_SUM);
+    appendProperty(output, format, GATHERING_THRESHOLD);
+    appendProperty(output, format, TRUSTED_CUTOFF);
+    appendProperty(output, format, NOISE_CUTOFF);
 
-    file.append(fileHeader + NEW_LINE);
-    
-    line = String.format("%-5s %1s", "NAME", hmm.getName());
-    file.append((line + NEW_LINE));
-
-    if (hmm.getAccessionNumber() != null)
+    if (hmm.getMSV() != null)
     {
-    line = String.format("%-5s %1s", "ACC", hmm.getAccessionNumber());
-    file.append((line + NEW_LINE));
-    }
+      format = "%n%-19s %18s";
+      output.append(String.format(format, "STATS LOCAL MSV", hmm.getMSV()));
 
-    if (hmm.getDescription() != null)
-    {
-    line = String.format("%-5s %1s", "DESC", hmm.getDescription());
-    file.append((line + NEW_LINE));
-    }
-    line = String.format("%-5s %1s", "LENG", hmm.getLength());
-    file.append((line + NEW_LINE));
+      output.append(String.format(format, "STATS LOCAL VITERBI",
+              hmm.getViterbi()));
 
-    if (hmm.getMaxInstanceLength() != null)
-    {
-    line = String.format("%-5s %1s", "MAXL", hmm.getMaxInstanceLength());
-    file.append((line + NEW_LINE));
-    }
-    line = String.format("%-5s %1s", "ALPH", hmm.getAlphabetType());
-    file.append((line + NEW_LINE));
-
-    boolean status;
-    String statusStr;
-
-    status = hmm.referenceAnnotationIsActive();
-    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
-    line = String.format("%-5s %1s", "RF",
-            statusStr);
-    file.append((line + NEW_LINE));
-
-    status = hmm.maskValueIsActive();
-    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
-    line = String.format("%-5s %1s", "MM",
-            statusStr);
-    file.append((line + NEW_LINE));
-    
-    status = hmm.consensusResidueIsActive();
-    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
-    line = String.format("%-5s %1s", "CONS",
-            statusStr);
-    file.append((line + NEW_LINE));
-
-    status = hmm.consensusStructureIsActive();
-    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
-    line = String.format("%-5s %1s", "CS",
-            statusStr);
-    file.append((line + NEW_LINE));
-
-    status = hmm.mapIsActive();
-    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
-    line = String.format("%-5s %1s", "MAP",
-            statusStr);
-    file.append((line + NEW_LINE));
-
-
-    if (hmm.getDate() != null)
-    {
-    line = String.format("%-5s %1s", "DATE", hmm.getDate());
-    file.append((line + NEW_LINE));
-    }
-    if (hmm.getNumberOfSequences() != null)
-    {
-    line = String.format("%-5s %1s", "NSEQ", hmm.getNumberOfSequences());
-    file.append((line + NEW_LINE));
-    }
-    if (hmm.getEffectiveNumberOfSequences() != null)
-    {
-    line = String.format("%-5s %1s", "EFFN",
-            hmm.getEffectiveNumberOfSequences());
-    file.append((line + NEW_LINE));
-    }
-    if (hmm.getCheckSum() != null)
-    {
-    line = String.format("%-5s %1s", "CKSUM", hmm.getCheckSum());
-    file.append((line + NEW_LINE));
-    }
-    if (hmm.getGatheringThreshold() != null)
-    {
-    line = String.format("%-5s %1s", "GA", hmm.getGatheringThreshold());
-    file.append((line + NEW_LINE));
+      output.append(String.format(format, "STATS LOCAL FORWARD",
+              hmm.getForward()));
     }
+  }
+
+  /**
+   * Appends 'yes' or 'no' for the given property, according to whether or not
+   * it is set in the HMM
+   * 
+   * @param output
+   * @param format
+   * @param propertyName
+   */
+  private void appendBooleanProperty(StringBuilder output, String format,
+          String propertyName)
+  {
+    boolean set = hmm.getBooleanProperty(propertyName);
+    output.append(String.format(format, propertyName,
+            set ? HiddenMarkovModel.YES : HiddenMarkovModel.NO));
+  }
 
-    if (hmm.getTrustedCutoff() != null)
+  /**
+   * Appends the value of the given property to the output, if not null
+   * 
+   * @param output
+   * @param format
+   * @param propertyName
+   */
+  private void appendProperty(StringBuilder output, String format,
+          String propertyName)
+  {
+    String value = hmm.getProperty(propertyName);
+    if (value != null)
     {
-    line = String.format("%-5s %1s", "TC", hmm.getTrustedCutoff());
-    file.append((line + NEW_LINE));
+      output.append(String.format(format, propertyName, value));
     }
-    if (hmm.getNoiseCutoff() != null)
+  }
+
+  @Override
+  public String print(SequenceI[] sequences, boolean jvsuffix)
+  {
+    if (sequences[0].getHMM() != null)
     {
-    line = String.format("%-5s %1s", "NC", hmm.getNoiseCutoff());
-    file.append((line + NEW_LINE));
+      hmm = sequences[0].getHMM();
     }
-    if (hmm.getMSV() != null)
+    return print();
+  }
+
+  /**
+   * Prints the .hmm file to a String.
+   * 
+   * @return
+   */
+  public String print()
+  {
+    StringBuilder output = new StringBuilder();
+    appendProperties(output);
+    output.append(NL);
+    appendModelAsString(output);
+    output.append(NL).append(TERMINATOR).append(NL);
+    return output.toString();
+  }
+
+  /**
+   * Converts the probabilities contained in an array into log space
+   * 
+   * @param ds
+   */
+  double[] convertToLogSpace(double[] ds)
+  {
+    double[] converted = new double[ds.length];
+    for (int i = 0; i < ds.length; i++)
     {
-      line = String.format("%-19s %18s", "STATS LOCAL MSV", hmm.getMSV());
-      file.append((line + NEW_LINE));
-
-      line = String.format("%-19s %18s", "STATS LOCAL VITERBI",
-              hmm.getViterbi());
-      file.append((line + NEW_LINE));
-    
-      line = String.format("%-19s %18s", "STATS LOCAL FORWARD",
-              hmm.getForward());
-      file.append((line + NEW_LINE));
+      double prob = ds[i];
+      double logProb = -1 * Math.log(prob);
+
+      converted[i] = logProb;
     }
+    return converted;
+  }
+
+  /**
+   * Returns the HMM sequence produced by reading a .hmm file.
+   */
+  @Override
+  public SequenceI[] getSeqsAsArray()
+  {
+    SequenceI hmmSeq = hmm.getConsensusSequence();
+    SequenceI[] seq = new SequenceI[1];
+    seq[0] = hmmSeq;
+    return seq;
   }
 
+  @Override
+  public void setNewlineString(String newLine)
+  {
+    NL = newLine;
+  }
 
+  @Override
+  public void setExportSettings(AlignExportSettingsI exportSettings)
+  {
+
+  }
 
-  char charValue(String string)
+  @Override
+  public void configureForView(AlignmentViewPanel viewpanel)
   {
-    char character;
-    character = string.charAt(0);
-    return character;
+
   }
 
   @Override
-  public String print(SequenceI[] seqs, boolean jvsuffix)
+  public boolean hasWarningMessage()
   {
+    return false;
+  }
 
-    return null;
+  @Override
+  public String getWarningMessage()
+  {
+    return "warning message";
   }
+
 }