JAL-2629 tidy unit tests, constants etc
[jalview.git] / src / jalview / io / HMMFile.java
index 28aa62f..95c6f32 100644 (file)
@@ -1,15 +1,13 @@
 package jalview.io;
 
+import jalview.api.AlignExportSettingI;
+import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.HMMNode;
 import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.SequenceI;
 
 import java.io.BufferedReader;
-import java.io.File;
-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;
@@ -26,12 +24,12 @@ public class HMMFile extends AlignFile
         implements AlignmentFileReaderI, AlignmentFileWriterI
 {
   // HMM to store file data
-  private HiddenMarkovModel hmm = new HiddenMarkovModel();
+  private HiddenMarkovModel hmm;
 
   // number of possible transitions
-  private final int NUMBER_OF_TRANSITIONS = 7;
+  private static final int NUMBER_OF_TRANSITIONS = 7;
 
-  private final String NEW_LINE = "\n";
+  private String NL = "\n";
 
   //number of symbols in the alphabet used in the hidden Markov model
   int numberOfSymbols;
@@ -43,18 +41,29 @@ public class HMMFile extends AlignFile
   private final String EMPTY = "";
 
   //This is a line that needs to be added to each HMMER� file. It is purely for readability.
-  private static final String TRANSITIONTYPELINE = "m->m     m->i     m->d     i->m     i->i     d->m     d->d";
+  private static final String TRANSITIONTYPELINE = "            m->m     m->i     m->d     i->m     i->i     d->m     d->d";
 
   /**
-   * Constructor for HMMFile, parses immediately
+   * Parses immediately.
+   * 
+   * @param inFile
+   * @param type
+   * @throws IOException
+   */
+  public HMMFile(String inFile, DataSourceType type) throws IOException
+  {
+    super(inFile, type);
+  }
+
+  /**
+   * Parses immediately.
    * 
    * @param source
    * @throws IOException
    */
   public HMMFile(FileParse source) throws IOException
   {
-    super(false, source);
-    parse();
+    super(source);
   }
 
   /**
@@ -77,6 +86,16 @@ public class HMMFile extends AlignFile
   }
 
   /**
+   * For testing, do not use.
+   * 
+   * @param br
+   */
+  HMMFile(BufferedReader br)
+  {
+    dataIn = br;
+  }
+
+  /**
    * Returns the HMM produced by reading in a HMMER3 file.
    * 
    * @return
@@ -114,8 +133,15 @@ public class HMMFile extends AlignFile
   @Override
   public void parse() throws IOException
   {
-    parseFileProperties(dataIn);
-    parseModel(dataIn);
+    try
+    {
+      hmm = new HiddenMarkovModel();
+      parseFileProperties(dataIn);
+      parseModel(dataIn);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+    }
   }
 
   /**
@@ -126,6 +152,7 @@ public class HMMFile extends AlignFile
 
   public void parse(BufferedReader br) throws IOException
   {
+    hmm = new HiddenMarkovModel();
     parseFileProperties(br);
     parseModel(br);
   }
@@ -154,7 +181,7 @@ public class HMMFile extends AlignFile
                               // properties)
         {
           readingFile = false;
-          hmm.fillSymbols(parser);
+          fillSymbols(parser);
           numberOfSymbols = hmm.getNumberOfSymbols();
         }
         else if ("STATS".equals(next))
@@ -329,49 +356,6 @@ public class HMMFile extends AlignFile
     return list;
   }
 
-  
-  /**
-   * Writes a HMM to a file/
-   * 
-   * @param exportLocation
-   *          Filename, URL or Pasted String to write to.
-   * @throws FileNotFoundException
-   * @throws UnsupportedEncodingException
-   *
-   **/
-  
-  public void exportFile(String exportLocation) throws IOException
-  {
-    PrintWriter writer = new PrintWriter(exportLocation);
-    appendFileProperties(writer);
-    appendModel(writer);
-    writer.println("//");
-
-    writer.close();
-
-  }
-
-  /**
-   * Writes a HMM to a file/
-   * 
-   * @param exportLocation
-   *          Filename, URL or Pasted String to write to.
-   * @throws FileNotFoundException
-   * @throws UnsupportedEncodingException
-   *
-   **/
-
-  public void exportFile(File exportLocation) throws IOException
-  {
-    PrintWriter writer = new PrintWriter(exportLocation);
-    appendFileProperties(writer);
-    appendModel(writer);
-    writer.println("//");
-
-    writer.close();
-
-  }
-
   /**
    * Returns a string to be added to the StringBuilder containing the entire
    * output String.
@@ -473,21 +457,18 @@ public class HMMFile extends AlignFile
   }
 
   /**
-   * Appends the hidden Markov model data to the StringBuilder containing the
-   * output
-   * 
-   * @param file
-   *          The StringBuilder containing the output.
+   * Returns a string containing the model data.
    */
-  void appendModel(PrintWriter writer)
+  String getModelAsString()
   {
+    StringBuilder output = new StringBuilder();
     String symbolLine = "HMM";
     List<Character> charSymbols = hmm.getSymbols();
     List<String> strSymbols;
     strSymbols = charListToStringList(charSymbols);
     symbolLine += addData(11, 9, strSymbols);
-    writer.println(symbolLine);
-    writer.println(TRANSITIONTYPELINE);
+    output.append(symbolLine);
+    output.append(NL).append(TRANSITIONTYPELINE);
 
     int length = hmm.getLength();
 
@@ -524,7 +505,7 @@ public class HMMFile extends AlignFile
 
       }
 
-      writer.println(matchLine);
+      output.append(NL).append(matchLine);
       
       String insertLine = EMPTY;
       List<String> strInserts;
@@ -534,7 +515,7 @@ public class HMMFile extends AlignFile
       strInserts = doubleListToStringList(doubleInserts);
       insertLine += addData(17, 9, strInserts);
 
-      writer.println(insertLine);
+      output.append(NL).append(insertLine);
 
       String transitionLine = EMPTY;
       List<String> strTransitions;
@@ -544,47 +525,45 @@ public class HMMFile extends AlignFile
       strTransitions = doubleListToStringList(doubleTransitions);
       transitionLine += addData(17, 9, strTransitions);
 
-      writer.println(transitionLine);
+      output.append(NL).append(transitionLine);
     }
+    return output.toString();
   }
 
   /**
-   * Appends the hidden Markov model file properties to the StringBuilder
-   * containing the output
-   * 
-   * @param file
-   *          The StringBuilder containing the output.
+   * Returns a String containing the HMM file properties
    */
-  void appendFileProperties(PrintWriter writer)
+  String getFilePropertiesAsString()
   {
+    StringBuffer output = new StringBuffer();
     String line;
 
-    writer.println(hmm.getFileHeader());
+    output.append(hmm.getFileHeader());
     
     line = String.format("%-5s %1s", "NAME", hmm.getName());
-    writer.println((line));
+    output.append(NL + line);
 
     if (hmm.getAccessionNumber() != null)
     {
     line = String.format("%-5s %1s", "ACC", hmm.getAccessionNumber());
-      writer.println((line));
+      output.append(NL + line);
     }
 
     if (hmm.getDescription() != null)
     {
     line = String.format("%-5s %1s", "DESC", hmm.getDescription());
-      writer.println((line));
+      output.append(NL + line);
     }
     line = String.format("%-5s %1s", "LENG", hmm.getLength());
-    writer.println((line));
+    output.append(NL + line);
 
     if (hmm.getMaxInstanceLength() != null)
     {
     line = String.format("%-5s %1s", "MAXL", hmm.getMaxInstanceLength());
-      writer.println((line));
+      output.append(NL + line);
     }
     line = String.format("%-5s %1s", "ALPH", hmm.getAlphabetType());
-    writer.println((line));
+    output.append(NL + line);
 
     boolean status;
     String statusStr;
@@ -593,83 +572,84 @@ public class HMMFile extends AlignFile
     statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "RF",
             statusStr);
-    writer.println((line));
+    output.append(NL + line);
 
     status = hmm.maskValueIsActive();
     statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "MM",
             statusStr);
-    writer.println((line));
+    output.append(NL + line);
     
     status = hmm.consensusResidueIsActive();
     statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "CONS",
             statusStr);
-    writer.println((line));
+    output.append(NL + line);
 
     status = hmm.consensusStructureIsActive();
     statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "CS",
             statusStr);
-    writer.println((line));
+    output.append(NL + line);
 
     status = hmm.mapIsActive();
     statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "MAP",
             statusStr);
-    writer.println((line));
+    output.append(NL + line);
 
 
     if (hmm.getDate() != null)
     {
     line = String.format("%-5s %1s", "DATE", hmm.getDate());
-      writer.println((line));
+      output.append(NL + line);
     }
     if (hmm.getNumberOfSequences() != null)
     {
     line = String.format("%-5s %1s", "NSEQ", hmm.getNumberOfSequences());
-      writer.println((line));
+      output.append(NL + line);
     }
     if (hmm.getEffectiveNumberOfSequences() != null)
     {
     line = String.format("%-5s %1s", "EFFN",
             hmm.getEffectiveNumberOfSequences());
-      writer.println((line));
+      output.append(NL + line);
     }
     if (hmm.getCheckSum() != null)
     {
     line = String.format("%-5s %1s", "CKSUM", hmm.getCheckSum());
-      writer.println((line));
+      output.append(NL + line);
     }
     if (hmm.getGatheringThreshold() != null)
     {
     line = String.format("%-5s %1s", "GA", hmm.getGatheringThreshold());
-      writer.println((line));
+      output.append(NL + line);
     }
 
     if (hmm.getTrustedCutoff() != null)
     {
     line = String.format("%-5s %1s", "TC", hmm.getTrustedCutoff());
-      writer.println((line));
+      output.append(NL + line);
     }
     if (hmm.getNoiseCutoff() != null)
     {
     line = String.format("%-5s %1s", "NC", hmm.getNoiseCutoff());
-      writer.println((line));
+      output.append(NL + line);
     }
     if (hmm.getMSV() != null)
     {
       line = String.format("%-19s %18s", "STATS LOCAL MSV", hmm.getMSV());
-      writer.println((line));
+      output.append(NL + line);
 
       line = String.format("%-19s %18s", "STATS LOCAL VITERBI",
               hmm.getViterbi());
-      writer.println((line));
+      output.append(NL + line);
     
       line = String.format("%-19s %18s", "STATS LOCAL FORWARD",
               hmm.getForward());
-      writer.println((line));
+      output.append(NL + line);
     }
+    return output.toString();
   }
 
 
@@ -687,12 +667,29 @@ public class HMMFile extends AlignFile
 
   }
 
-  // TODO do this
   @Override
   public String print(SequenceI[] seqs, boolean jvsuffix)
   {
+    if (seqs[0].getHMM() != null)
+    {
+      hmm = seqs[0].getHMM();
+    }
+    return print();
+  }
 
-    return null;
+  /**
+   * Prints the .hmm file to a String.
+   * 
+   * @return
+   */
+  public String print()
+  {
+    StringBuffer output = new StringBuffer();
+    output.append(getFilePropertiesAsString());
+    output.append(NL);
+    output.append(getModelAsString());
+    output.append(NL + "//");
+    return output.toString();
   }
 
   /**
@@ -729,5 +726,54 @@ public class HMMFile extends AlignFile
 
   }
 
+  /**
+   * Fills symbol array and adds each symbol to an index lookup
+   * 
+   * @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();
+      hmm.getSymbols().add(symbol[0]);
+      hmm.setSymbolIndex(symbol[0], i);
+      i++;
+    }
+  }
+
+  @Override
+  public void setNewlineString(String newLine)
+  {
+    NL = newLine;
+  }
+
+  @Override
+  public void setExportSettings(AlignExportSettingI exportSettings)
+  {
+
+  }
+
+  @Override
+  public void configureForView(AlignmentViewPanel viewpanel)
+  {
+
+  }
+
+  @Override
+  public boolean hasWarningMessage()
+  {
+    return false;
+  }
+
+  @Override
+  public String getWarningMessage()
+  {
+    return "warning message";
+  }
+
 }