JAL-2629 manual curation of HMMs via reference annotation functional
[jalview.git] / src / jalview / io / HMMFile.java
index 4ff8e77..49d885c 100644 (file)
@@ -1,21 +1,20 @@
 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.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;
 
 
 /**
- * Adds capability to read in and write out HMMER3 files. Currently only supports HMMER3/f.
+ * Adds capability to read in and write out HMMER3 files. .
  * 
  * 
  * @author TZVanaalten
@@ -25,15 +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;
@@ -44,17 +40,30 @@ 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";
+  //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";
+
+  /**
+   * Parses immediately.
+   * 
+   * @param inFile
+   * @param type
+   * @throws IOException
+   */
+  public HMMFile(String inFile, DataSourceType type) throws IOException
+  {
+    super(inFile, type);
+  }
 
   /**
-   * Constructor for HMMFile
+   * Parses immediately.
+   * 
    * @param source
    * @throws IOException
    */
   public HMMFile(FileParse source) throws IOException
   {
-    super(false, source);
+    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,6 +133,7 @@ public class HMMFile extends AlignFile
   @Override
   public void parse() throws IOException
   {
+    hmm = new HiddenMarkovModel();
     parseFileProperties(dataIn);
     parseModel(dataIn);
   }
@@ -126,6 +146,7 @@ public class HMMFile extends AlignFile
 
   public void parse(BufferedReader br) throws IOException
   {
+    hmm = new HiddenMarkovModel();
     parseFileProperties(br);
     parseModel(br);
   }
@@ -154,7 +175,7 @@ public class HMMFile extends AlignFile
                               // properties)
         {
           readingFile = false;
-          hmm.fillSymbols(parser);
+          fillSymbols(parser);
           numberOfSymbols = hmm.getNumberOfSymbols();
         }
         else if ("STATS".equals(next))
@@ -247,12 +268,12 @@ public class HMMFile extends AlignFile
    */
   void parseAnnotations(Scanner scanner, int index)
   {
-    if (hmm.mapIsActive())
+    if (hmm.mapIsActive() && scanner.hasNext())
     {
       int column;
       column = scanner.nextInt();
-      hmm.getNodes().get(index).setAlignmentColumn(column);
-      hmm.getNodeLookup().put(column, index);
+      hmm.getNodes().get(index).setAlignmentColumn(column - 1);
+      hmm.getNodeLookup().put(column - 1, index);
     }
     else
     {
@@ -298,9 +319,10 @@ public class HMMFile extends AlignFile
    * @param numberOfElements
    *          The number of elements in the list to be filled.
    * @return filled list Returns the list of doubles.
+   * @throws IOException
    */
   static List<Double> fillList(Scanner input,
-          int numberOfElements)
+          int numberOfElements) throws IOException
   {
     List<Double> list = new ArrayList<>();
     for (int i = 0; i < numberOfElements; i++)
@@ -321,31 +343,13 @@ public class HMMFile extends AlignFile
         list.add(prob);
       }
     }
+    if (list.size() < numberOfElements)
+    {
+      throw new IOException("Incomplete data");
+    }
     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();
-
-  }
-
   /**
    * Returns a string to be added to the StringBuilder containing the entire
    * output String.
@@ -447,21 +451,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()
   {
+    StringBuffer output = new StringBuffer();
     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 + TRANSITIONTYPELINE);
 
     int length = hmm.getLength();
 
@@ -487,7 +488,7 @@ public class HMMFile extends AlignFile
 
       if (node != 0)
       {
-        matchLine += SPACE + hmm.getNodeAlignmentColumn(node) + 1;
+        matchLine += SPACE + (hmm.getNodeAlignmentColumn(node) + 1);
         matchLine += SPACE + hmm.getConsensusResidue(node);
         matchLine += SPACE + hmm.getReferenceAnnotation(node);
         if (hmm.getFileHeader().contains("HMMER3/f"))
@@ -498,7 +499,7 @@ public class HMMFile extends AlignFile
 
       }
 
-      writer.println(matchLine);
+      output.append(NL + matchLine);
       
       String insertLine = EMPTY;
       List<String> strInserts;
@@ -508,7 +509,7 @@ public class HMMFile extends AlignFile
       strInserts = doubleListToStringList(doubleInserts);
       insertLine += addData(17, 9, strInserts);
 
-      writer.println(insertLine);
+      output.append(NL + insertLine);
 
       String transitionLine = EMPTY;
       List<String> strTransitions;
@@ -518,47 +519,45 @@ public class HMMFile extends AlignFile
       strTransitions = doubleListToStringList(doubleTransitions);
       transitionLine += addData(17, 9, strTransitions);
 
-      writer.println(transitionLine);
+      output.append(NL + 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;
@@ -567,83 +566,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();
   }
 
 
@@ -664,8 +664,26 @@ public class HMMFile extends AlignFile
   @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();
   }
 
   /**
@@ -688,5 +706,68 @@ public class HMMFile extends AlignFile
 
 
   }
+
+  /**
+   * Returns the HMM sequence produced by reading a .hmm file.
+   */
+  @Override
+  public SequenceI[] getSeqsAsArray()
+  {
+    SequenceI hmmSeq = hmm.initHMMSequence();
+    SequenceI[] seq = new SequenceI[1];
+    seq[0] = hmmSeq;
+    return seq;
+
+  }
+
+  /**
+   * 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";
+  }
+
 }