fully integrate HMMER3 file format into Jalview
[jalview.git] / src / jalview / io / HMMFile.java
index 7063fe9..6e49af6 100644 (file)
@@ -2,12 +2,12 @@ package jalview.io;
 
 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.FileReader;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
@@ -21,19 +21,19 @@ import java.util.Scanner;
  * @author TZVanaalten
  *
  */
-public class HMMFile extends FileParse
+public class HMMFile extends AlignFile
+        implements AlignmentFileReaderI, AlignmentFileWriterI
 {
   // HMM to store file data
-  HiddenMarkovModel hmm = new HiddenMarkovModel();
+  private HiddenMarkovModel hmm = new HiddenMarkovModel();
+
 
 
-  // Source of file
-  String dataObject;
 
   // number of possible transitions
-  final static int NUMBER_OF_TRANSITIONS = 7;
+  private final int NUMBER_OF_TRANSITIONS = 7;
 
-  final static String NEW_LINE = "\n";
+  private final String NEW_LINE = "\n";
 
 
   // file header
@@ -41,58 +41,50 @@ public class HMMFile extends FileParse
 
   int numberOfSymbols;
 
-  final static String SPACE = " ";
+  private final String SPACE = " ";
 
-  final static String COMPO = "COMPO";
+  private final String COMPO = "COMPO";
 
-  final static String EMPTY = "";
+  private final String EMPTY = "";
 
+  public HMMFile(FileParse source) throws IOException
+  {
+    super(false, source);
+  }
 
-  /**
-   * Constructor which contains model to be filled or exported
-   * 
-   * @param dataSource
-   *          Filename, URL or Pasted String to read from
-   */
-  public HMMFile(String dataSource)
+  public HMMFile()
   {
-    dataObject = dataSource;
+
   }
 
-  public HiddenMarkovModel getHmm()
+  public HiddenMarkovModel getHMM()
   {
     return hmm;
   }
 
-  public void setHmm(HiddenMarkovModel model)
+  public void setHMM(HiddenMarkovModel model)
   {
     this.hmm = model;
   }
 
+  public String getName()
+  {
+    return hmm.getName();
+  }
+
   /**
    * reads data from HMM file
    * 
    * @throws IOException
    */
+  @Override
   public void parse() throws IOException
   {
-    File file = new File(dataObject);
-    FileReader fr = new FileReader(file);
-    BufferedReader br = new BufferedReader(fr);
-    parseFileProperties(br);
-    parseModel(br);
-
+    parseFileProperties(dataIn);
+    parseModel(dataIn);
   }
 
-  public String getDataObject()
-  {
-    return dataObject;
-  }
 
-  public void setDataObject(String value)
-  {
-    this.dataObject = value;
-  }
 
   /**
    * imports file properties from hmm file
@@ -101,7 +93,7 @@ public class HMMFile extends FileParse
    *          buffered reader used to read in file
    * @throws IOException
    */
-  public void parseFileProperties(BufferedReader input) throws IOException
+  void parseFileProperties(BufferedReader input) throws IOException
   {
     boolean readingFile = true;
     fileHeader = input.readLine();
@@ -156,7 +148,7 @@ public class HMMFile extends FileParse
    *          buffered reader used to read file
    * @throws IOException
    */
-  public void parseModel(BufferedReader input) throws IOException
+  void parseModel(BufferedReader input) throws IOException
   {
     for (int i = 0; i < hmm.getLength() + 1; i++)
     {
@@ -205,13 +197,14 @@ public class HMMFile extends FileParse
    * @param index
    *          index of node which is beign scanned
    */
-  public void parseAnnotations(Scanner scanner, int index)
+  void parseAnnotations(Scanner scanner, int index)
   {
     if (hmm.mapIsActive())
     {
       int column;
       column = scanner.nextInt();
       hmm.getNodes().get(index).setAlignmentColumn(column);
+      hmm.getNodeLookup().put(column, index);
     }
     else
     {
@@ -236,43 +229,6 @@ public class HMMFile extends FileParse
     hmm.getNodes().get(index).setConsensusStructure(consensusS);
   }
 
-  /**
-   * 
-   * @param transition
-   *          type of transition occuring
-   * @return index value representing position along stateTransition array.
-   */
-  public Integer getTransitionType(String transition)
-  {
-    Integer index;
-    switch (transition)
-    {
-    case "mm":
-      index = 0;
-      break;
-    case "mi":
-      index = 1;
-      break;
-    case "md":
-      index = 2;
-      break;
-    case "im":
-      index = 3;
-      break;
-    case "ii":
-      index = 4;
-      break;
-    case "dm":
-      index = 5;
-      break;
-    case "dd":
-      index = 6;
-      break;
-    default:
-      index = null;
-    }
-    return index;
-  }
 
   /**
    * 
@@ -282,7 +238,7 @@ public class HMMFile extends FileParse
    *          number of elements in the list to be filled
    * @return filled list
    */
-  public static List<Double> fillList(Scanner input,
+  static List<Double> fillList(Scanner input,
           int numberOfElements)
   {
     List<Double> list = new ArrayList<>();
@@ -325,9 +281,13 @@ public class HMMFile extends FileParse
     
     file.append("//");
 
+    PrintWriter output = new PrintWriter(exportLocation);
+    output.append(file);
+    output.close();
+
   }
 
-  public String addData(int initialColumnSeparation,
+  String addData(int initialColumnSeparation,
           int columnSeparation, List<String> data)
   {
     String line = EMPTY;
@@ -347,7 +307,7 @@ public class HMMFile extends FileParse
     return line;
   }
 
-  public static List<String> charListToStringList(List<Character> list)
+  List<String> charListToStringList(List<Character> list)
   {
     List<String> strList = new ArrayList<>();
     for (char value : list)
@@ -358,7 +318,7 @@ public class HMMFile extends FileParse
     return strList;
   }
 
-  public static List<String> doubleListToStringList(List<Double> list,
+  List<String> doubleListToStringList(List<Double> list,
           int noOfDecimals)
   {
     List<String> strList = new ArrayList<>();
@@ -379,7 +339,7 @@ public class HMMFile extends FileParse
     return strList;
   }
 
-  public static List<String> stringArrayToStringList(String[] array)
+  List<String> stringArrayToStringList(String[] array)
   {
     List<String> list = new ArrayList<>();
     for (String value : array)
@@ -489,26 +449,40 @@ public class HMMFile extends FileParse
     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",
-            hmm.getFileProperties().get("RF"));
+            statusStr);
     file.append((line + NEW_LINE));
 
+    status = hmm.maskValueIsActive();
+    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "MM",
-            hmm.getFileProperties().get("MM"));
+            statusStr);
     file.append((line + NEW_LINE));
     
+    status = hmm.consensusResidueIsActive();
+    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "CONS",
-            hmm.getFileProperties().get("CONS"));
+            statusStr);
     file.append((line + NEW_LINE));
 
+    status = hmm.consensusStructureIsActive();
+    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "CS",
-            hmm.getFileProperties().get("CS"));
+            statusStr);
     file.append((line + NEW_LINE));
 
+    status = hmm.mapIsActive();
+    statusStr = HiddenMarkovModel.findStringFromBoolean(status);
     line = String.format("%-5s %1s", "MAP",
-            hmm.getFileProperties().get("MAP"));
+            statusStr);
     file.append((line + NEW_LINE));
 
+
     if (hmm.getDate() != null)
     {
     line = String.format("%-5s %1s", "DATE", hmm.getDate());
@@ -563,11 +537,18 @@ public class HMMFile extends FileParse
 
 
 
-  public static char charValue(String string)
+  char charValue(String string)
   {
     char character;
     character = string.charAt(0);
     return character;
   }
+
+  @Override
+  public String print(SequenceI[] seqs, boolean jvsuffix)
+  {
+
+    return null;
+  }
 }