JAL-2668 add tests for HMMER commands, annotation and io
[jalview.git] / src / jalview / gui / Preferences.java
index c25fc2f..c8fef96 100755 (executable)
@@ -22,8 +22,14 @@ package jalview.gui;
 
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenMarkovModel;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
 import jalview.gui.Help.HelpId;
 import jalview.gui.StructureViewer.ViewerType;
+import jalview.hmmer.HMMBuildThread;
 import jalview.io.FileFormatI;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
@@ -52,6 +58,7 @@ import java.awt.event.MouseEvent;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Scanner;
 
 import javax.help.HelpSetException;
 import javax.swing.JColorChooser;
@@ -204,6 +211,8 @@ public class Preferences extends GPreferences
      * Set HMMER tab defaults
      */
     trimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
+    isHMMERInstalled
+            .setSelected(Cache.getDefault("HMMER_INSTALLED", false));
     if (Cache.getDefault("USE_UNIPROT", false))
     {
       uniprot.setSelected(true);
@@ -214,7 +223,17 @@ public class Preferences extends GPreferences
     }
     numberOfSequencesToKeepField
             .setText(Cache.getProperty("SEQUENCES_TO_KEEP"));
-    installationLocationField.setText(Cache.getProperty(HMMER_PATH));
+    installationLocation.setEnabled(isHMMERInstalled.isSelected());
+    hmmerPath.setEnabled(isHMMERInstalled.isSelected());
+    hmmerPath.setText(Cache.getProperty(HMMER_PATH));
+    hmmerPath.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        validateHMMERPath();
+      }
+    });
 
     /*
      * Set Visual tab defaults
@@ -665,7 +684,21 @@ public class Preferences extends GPreferences
     Cache.applicationProperties.setProperty("SEQUENCES_TO_KEEP",
             numberOfSequencesToKeepField.getText());
     Cache.applicationProperties.setProperty(HMMER_PATH,
-            installationLocationField.getText());
+            hmmerPath.getText());
+    boolean hmmerInstalled = isHMMERInstalled.isSelected();
+    Cache.applicationProperties.setProperty("HMMER_INSTALLED",
+            Boolean.toString(hmmerInstalled));
+    boolean hmmerFunctioning = validateHMMERPath(false);
+    Cache.applicationProperties.setProperty("HMMER_FUNCTIONING",
+            Boolean.toString(hmmerFunctioning));
+    AlignFrame[] frames = Desktop.getAlignFrames();
+    boolean hmmerStatus = hmmerFunctioning && hmmerInstalled ? true : false;
+    for (AlignFrame frame : frames)
+    {
+      frame.updateHMMERStatus(hmmerStatus);
+    }
+    
+    
     trimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
     if (Cache.getDefault("USE_UNIPROT", false))
     {
@@ -677,7 +710,7 @@ public class Preferences extends GPreferences
     }
     numberOfSequencesToKeepField
             .setText(Cache.getProperty("SEQUENCES_TO_KEEP"));
-    installationLocationField.setText(Cache.getProperty(HMMER_PATH));
+    hmmerPath.setText(Cache.getProperty(HMMER_PATH));
 
     /*
      * Save Structure settings
@@ -836,6 +869,14 @@ public class Preferences extends GPreferences
       structureTab.requestFocusInWindow();
       return false;
     }
+    if (isHMMERInstalled.isSelected())
+    {
+      if (!validateHMMER())
+      {
+        hmmerTab.requestFocusInWindow();
+        return false;
+      }
+    }
     return true;
   }
 
@@ -846,6 +887,13 @@ public class Preferences extends GPreferences
 
   }
 
+  @Override
+  protected boolean validateHMMER()
+  {
+    return validateHMMERPath();
+
+  }
+
   /**
    * DOCUMENT ME!
    */
@@ -1138,27 +1186,170 @@ public class Preferences extends GPreferences
   }
   
   /**
-   * Returns true if hmmer path contains the necessary valid executables, else show an error
-   * dialog.
+   * Returns true if hmmer path contains the necessary valid executables, else
+   * show an error dialog (if showing dialog).
    */
-  private boolean validateHMMERPath()
+  private boolean validateHMMERPath(boolean showDialog)
   {
-    String path = Cache.getProperty("HMMERPATH");
-    if (path.length() > 0)
+    int missing = 0;
+    String message = "";
+    String folder = hmmerPath.getText().trim();
+    if (folder.length() > 0)
     {
-      File f = new File(path);
-      if (!f.canExecute())
+      File f = new File(folder);
+      if (!f.exists())
       {
-        JvOptionPane.showInternalMessageDialog(Desktop.desktop,
-                MessageManager.getString("label.invalid_hmmer_folder"),
-                MessageManager.getString("Invalid folder"),
+        if (showDialog)
+        {
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                MessageManager.getString("label.folder_not_exists"),
+                MessageManager.getString("label.invalid_folder"),
                 JvOptionPane.ERROR_MESSAGE);
+        }
         return false;
       }
+      AlignmentI alignment = new Alignment(
+              new SequenceI[]
+      { new Sequence("test", "WLWL", 0, 3) });
+      if (canExecute(folder + "/hmmbuild"))
+        {
+        validateHMMBuild(alignment);
+        }
+      else
+      {
+        message += MessageManager.getString("label.hmmbuild_not_found")
+                + "\n";
+        missing++;
+      }
+
+
+      if (canExecute(folder + "/hmmalign"))
+        {
+
+        }
+      else
+      {
+        message += MessageManager.getString("label.hmmalign_not_found")
+                + "\n";
+        missing++;
+      }
+
+
+      if (canExecute(folder + "/hmmsearch"))
+        {
+
+        }
+      else
+      {
+        message += MessageManager.getString("label.hmmsearch_not_found")
+                + "\n";
+        missing++;
+      }
+      }
+
+      if (missing > 0)
+      {
+        if (missing < 3)
+        {
+          if (showDialog)
+          {
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop, message,
+                  MessageManager.getString("label.invalid_folder"),
+                  JvOptionPane.ERROR_MESSAGE);
+          }
+          return false;
+        }
+        else
+        {
+          if (showDialog)
+          {
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                  MessageManager.getString("label.no_binaries"),
+                  MessageManager.getString("label.invalid_folder"),
+                  JvOptionPane.ERROR_MESSAGE);
+          }
+
+          return false;
+        }
+      }
+
+    return true;
+  }
+
+  /**
+   * Checks if a file can be executed
+   * 
+   * @param path
+   *          the path to the file
+   * @return
+   */
+  public boolean canExecute(String path)
+  {
+    File file = new File(path);
+    if (!file.canExecute())
+    {
+      file = new File(path + ".exe");
+      {
+        if (!file.canExecute())
+        {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
+
+  /**
+   * Runs hmmbuild to check if it is working. While doing this it parses the
+   * version of HMMER.
+   * 
+   * @param frame
+   * @return
+   */
+  public boolean validateHMMBuild(AlignmentI alignment)
+  {
+    HMMBuildThread hmmbuild = new HMMBuildThread(alignment);
+    hmmbuild.hmmbuildWaitTillComplete();
+    SequenceI hmmSeq = alignment.getSequenceAt(1);
+    HiddenMarkovModel hmm;
+    if (hmmSeq.isHMMConsensusSequence() && hmmSeq.getHMM() != null)
+    {
+      hmm = hmmSeq.getHMM();
+
+      if (hmm.getNumberOfSymbols() < 1)
+      {
+        return false;
+      }
+    }
+    else
+    {
+      return false;
+    }
+
+    String header = hmm.getFileHeader();
+    if (header == null)
+    {
+      return false;
+    }
+    else
+    {
+      Scanner scanner = new Scanner(header);
+      scanner.next();
+      String string = scanner.next();
+      String version = string.substring(1);
+      Cache.setProperty("HMMER_VERSION", version);
+      scanner.close();
+
     }
     return true;
   }
 
+
+  private boolean validateHMMERPath()
+  {
+    return validateHMMERPath(true);
+  }
+
   /**
    * If Chimera is selected, check it can be found on default or user-specified
    * path, if not show a warning/help dialog.