JAL-2599 complete documentation for HMM and HMMFile classes
[jalview.git] / src / jalview / io / HMMFile.java
index 16a1222..dc5c699 100644 (file)
@@ -15,7 +15,7 @@ import java.util.Scanner;
 
 
 /**
- * reads in and writes out a HMMER standard file
+ * Adds capability to read in and write out HMMER3 files. Currently only supports HMMER3/f.
  * 
  * 
  * @author TZVanaalten
@@ -35,10 +35,10 @@ public class HMMFile extends AlignFile
 
   private final String NEW_LINE = "\n";
 
-
   // file header
   String fileHeader;
 
+  //number of symbols in the alphabet used in the hidden Markov model
   int numberOfSymbols;
 
   private final String SPACE = " ";
@@ -47,35 +47,59 @@ 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";
 
+  /**
+   * Constructor for HMMFile
+   * @param source
+   * @throws IOException
+   */
   public HMMFile(FileParse source) throws IOException
   {
     super(false, source);
   }
 
+  /**
+   * Default constructor, do not use!
+   */
   public HMMFile()
   {
 
   }
 
+  /**
+   * Returns the HMM produced by reading in a HMMER3 file.
+   * 
+   * @return
+   */
   public HiddenMarkovModel getHMM()
   {
     return hmm;
   }
 
+  /**
+   * Sets the HMM used in this file.
+   * 
+   * @param model
+   */
   public void setHMM(HiddenMarkovModel model)
   {
     this.hmm = model;
   }
 
+  /**
+   * Gets the name of the hidden Markov model.
+   * 
+   * @return
+   */
   public String getName()
   {
     return hmm.getName();
   }
 
   /**
-   * reads data from HMM file
+   * Reads the data from HMM file into the HMM field on this object.
    * 
    * @throws IOException
    */
@@ -89,10 +113,10 @@ public class HMMFile extends AlignFile
 
 
   /**
-   * imports file properties from hmm file
+   * Imports the file properties from a HMMER3 file.
    * 
    * @param input
-   *          buffered reader used to read in file
+   *          The buffered reader used to read in the file.
    * @throws IOException
    */
   void parseFileProperties(BufferedReader input) throws IOException
@@ -144,10 +168,10 @@ public class HMMFile extends AlignFile
   }
 
   /**
-   * parses the model data from the hmm file
+   * Parses the model data from the HMMER3 file
    * 
    * @param input
-   *          buffered reader used to read file
+   *          The buffered reader used to read the file.
    * @throws IOException
    */
   void parseModel(BufferedReader input) throws IOException
@@ -192,12 +216,12 @@ public class HMMFile extends AlignFile
   }
 
   /**
-   * parses annotations on match emission line
+   * Parses the annotations on the match emission line.
    * 
    * @param scanner
-   *          scanner which is processing match emission line
+   *          The scanner which is processing match emission line.
    * @param index
-   *          index of node which is beign scanned
+   *          The index of node which is being scanned.
    */
   void parseAnnotations(Scanner scanner, int index)
   {
@@ -233,12 +257,14 @@ public class HMMFile extends AlignFile
 
 
   /**
+   * Fills a list of doubles based on an input line.
    * 
    * @param input
-   *          scanner for line containing data to be transferred to list
+   *          The scanner for the line containing the data to be transferred to
+   *          the list.
    * @param numberOfElements
-   *          number of elements in the list to be filled
-   * @return filled list
+   *          The number of elements in the list to be filled.
+   * @return filled list Returns the list of doubles.
    */
   static List<Double> fillList(Scanner input,
           int numberOfElements)
@@ -251,8 +277,7 @@ public class HMMFile extends AlignFile
       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
+                              // file.
       {
         list.add(Double.NEGATIVE_INFINITY);
       }
@@ -268,10 +293,10 @@ public class HMMFile extends AlignFile
 
   
   /**
-   * writes a HiddenMarkovModel to a file
+   * Writes a HMM to a file/
    * 
    * @param exportLocation
-   *          Filename, URL or Pasted String to write to
+   *          Filename, URL or Pasted String to write to.
    * @throws FileNotFoundException
    * @throws UnsupportedEncodingException
    *
@@ -290,6 +315,19 @@ public class HMMFile extends AlignFile
 
   }
 
+  /**
+   * Returns a string to be added to the StringBuilder containing the entire
+   * output String.
+   * 
+   * @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 fo data to be added to the String.
+   * @return
+   */
   String addData(int initialColumnSeparation,
           int columnSeparation, List<String> data)
   {
@@ -310,6 +348,12 @@ public class HMMFile extends AlignFile
     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<>();
@@ -321,8 +365,15 @@ public class HMMFile extends AlignFile
     return strList;
   }
 
-  List<String> doubleListToStringList(List<Double> list,
-          int noOfDecimals)
+  /**
+   * Converts a list of doubles into a list of Strings, rounded to the nearest
+   * 5th decimal place.
+   * 
+   * @param list
+   * @param noOfDecimals
+   * @return
+   */
+  List<String> doubleListToStringList(List<Double> list)
   {
     List<String> strList = new ArrayList<>();
     for (double value : list)
@@ -347,6 +398,12 @@ public class HMMFile extends AlignFile
     return strList;
   }
 
+  /**
+   * Converts a primitive array of Strings to a list of Strings.
+   * 
+   * @param array
+   * @return
+   */
   List<String> stringArrayToStringList(String[] array)
   {
     List<String> list = new ArrayList<>();
@@ -358,6 +415,13 @@ public class HMMFile extends AlignFile
     return list;
   }
 
+  /**
+   * Appends the hidden Markov model data to the StringBuilder containing the
+   * output
+   * 
+   * @param file
+   *          The StringBuilder containing the output.
+   */
   void appendModel(StringBuilder file)
   {
     String symbolLine = "HMM";
@@ -386,7 +450,7 @@ public class HMMFile extends AlignFile
       List<Double> doubleMatches;
       doubleMatches = hmm.getNode(node).getMatchEmissions();
       convertListToLogSpace(doubleMatches);
-      strMatches = doubleListToStringList(doubleMatches, 5);
+      strMatches = doubleListToStringList(doubleMatches);
       matchLine += addData(10, 9, strMatches);
 
 
@@ -407,7 +471,7 @@ public class HMMFile extends AlignFile
       List<Double> doubleInserts;
       doubleInserts = hmm.getNode(node).getInsertEmissions();
       convertListToLogSpace(doubleInserts);
-      strInserts = doubleListToStringList(doubleInserts, 5);
+      strInserts = doubleListToStringList(doubleInserts);
       insertLine += addData(17, 9, strInserts);
 
       file.append(insertLine + NEW_LINE);
@@ -417,13 +481,20 @@ public class HMMFile extends AlignFile
       List<Double> doubleTransitions;
       doubleTransitions = hmm.getNode(node).getStateTransitions();
       convertListToLogSpace(doubleTransitions);
-      strTransitions = doubleListToStringList(doubleTransitions, 5);
+      strTransitions = doubleListToStringList(doubleTransitions);
       transitionLine += addData(17, 9, strTransitions);
 
       file.append(transitionLine + NEW_LINE);
     }
   }
 
+  /**
+   * Appends the hidden Markov model file properties to the StringBuilder
+   * containing the output
+   * 
+   * @param file
+   *          The StringBuilder containing the output.
+   */
   void appendFileProperties(StringBuilder file)
   {
     String line;
@@ -542,12 +613,18 @@ public class HMMFile extends AlignFile
   }
 
 
-
+  /**
+   * Returns the char value of a single lettered String.
+   * 
+   * @param string
+   * @return
+   */
   char charValue(String string)
   {
     char character;
     character = string.charAt(0);
     return character;
+
   }
 
   @Override
@@ -557,6 +634,11 @@ public class HMMFile extends AlignFile
     return null;
   }
 
+  /**
+   * Converts the probabilities contained in a list into log space.
+   * 
+   * @param list
+   */
   void convertListToLogSpace(List<Double> list)
   {