JAL-2629 partial refactoring of Hmmer command classes
[jalview.git] / src / jalview / hmmer / HmmerCommand.java
similarity index 72%
rename from src/jalview/hmmer/HMMERCommands.java
rename to src/jalview/hmmer/HmmerCommand.java
index 854f0c5..0f45184 100644 (file)
@@ -1,5 +1,6 @@
 package jalview.hmmer;
 
+import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
@@ -8,8 +9,11 @@ import jalview.datamodel.Annotation;
 import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.JvOptionPane;
+import jalview.gui.Preferences;
 import jalview.io.HMMFile;
 import jalview.io.StockholmFile;
+import jalview.util.MessageManager;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -20,24 +24,17 @@ import java.util.Hashtable;
 import java.util.List;
 
 /**
- * Contains multiple commands and methods frequently used to run hmmbuild,
- * hmmalign and hmmsearch
+ * Base class for hmmbuild, hmmalign and hmmsearch
  * 
  * @author TZVanaalten
  *
  */
-public class HMMERCommands
+public class HmmerCommand
 {
-  // Path of hmmer binaries directory
-  String HMMERFOLDER = "/Documents/";
+  public static final String HMMBUILD = "hmmbuild";
 
-  public String JALVIEWDIRECTORY = System.getProperty("user.dir") + "/";
-
-  public final String HMMALIGN = "/hmmalign ";
-
-  public final String HMMBUILD = "/hmmbuild ";
-
-  public final String HMMSEARCH = "/hmmsearch ";
+  public String JALVIEWDIRECTORY = System.getProperty("user.dir")
+          + File.separator;
 
   public String OUTPUTALIGNMENT;
 
@@ -57,6 +54,14 @@ public class HMMERCommands
 
   List<SequenceI> hmmSeqs;
 
+  protected AlignFrame af;
+
+  public static boolean isHmmerAvailable()
+  {
+    File exec = getExecutable(HMMBUILD, Cache.getProperty(Preferences.HMMER_PATH));
+    return exec != null;
+  }
+
   /**
    * Uniquifies the sequences when exporting and stores their details in a
    * hashtable.
@@ -136,7 +141,7 @@ public class HMMERCommands
     if (seqs != null)
     {
       AlignmentI newAl = new Alignment(seqs);
-      if (stoLocation != null)
+      if (stoLocation != null && al != null)
       {
         for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
         {
@@ -219,4 +224,52 @@ public class HMMERCommands
   {
     this.hmmSeqs = hmmSeqs;
   }
+
+  /**
+   * Answers the full path to the given hmmer executable, or null if file cannot
+   * be found or is not executable
+   * 
+   * @param cmd
+   *          command short name e.g. hmmalign
+   * @return
+   */
+  protected String getCommandRoot(String cmd)
+  {
+    String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
+    File file = getExecutable(cmd, binariesFolder);
+    if (file == null && af != null)
+    {
+        JvOptionPane.showInternalMessageDialog(af,
+                MessageManager.getString("warn.hmm_command_failed"));
+    }
+
+    return file == null ? null : file.getAbsolutePath();
+  }
+
+  /**
+   * Answers the executable file for the given hmmer command, or null if not
+   * found or not executable. The path to the executable is the command name
+   * prefixed by the hmmer binaries folder path, optionally with .exe appended.
+   * 
+   * @param cmd
+   *          hmmer command short name, for example hmmbuild
+   * @param binaryPath
+   *          parent folder containing hmmer executables
+   * @return
+   */
+  public static File getExecutable(String cmd, String binaryPath)
+  {
+    File file = new File(binaryPath, cmd);
+    if (!file.canExecute())
+    {
+      file = new File(binaryPath, cmd + ".exe");
+      {
+        if (!file.canExecute())
+        {
+          file = null;
+        }
+      }
+    }
+    return file;
+  }
 }