JAL-3285 fix incorrect file paths on HMMAlign/Search results
[jalview.git] / src / jalview / hmmer / HmmerCommand.java
index dfd5395..20e0083 100644 (file)
@@ -24,6 +24,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
@@ -111,6 +112,12 @@ public abstract class HmmerCommand implements Runnable
     {
       ProcessBuilder pb = new ProcessBuilder(args);
       pb.redirectErrorStream(true); // merge syserr to sysout
+      if (Platform.isWindows())
+      {
+        String path = pb.environment().get("Path");
+        path = jalview.bin.Cache.getProperty("CYGWIN_PATH") + ";" + path;
+        pb.environment().put("Path", path);
+      }
       final Process p = pb.start();
       new Thread(new Runnable()
       {
@@ -167,6 +174,7 @@ public abstract class HmmerCommand implements Runnable
     }
 
     List<String> wrapped = new ArrayList<>();
+    // wrapped.add("C:\Users\tva\run");
     wrapped.add(bash.getAbsolutePath());
     wrapped.add("-c");
 
@@ -247,18 +255,22 @@ public abstract class HmmerCommand implements Runnable
    * @param cmd
    *          command short name e.g. hmmalign
    * @return
+   * @throws IOException
    */
   protected String getCommandPath(String cmd)
+          throws IOException
   {
     String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
+    // ensure any symlink to the directory is resolved:
+    binariesFolder = Paths.get(binariesFolder).toRealPath().toString();
     File file = FileUtils.getExecutable(cmd, binariesFolder);
     if (file == null && af != null)
     {
-        JvOptionPane.showInternalMessageDialog(af,
-                MessageManager.getString("warn.hmm_command_failed"));
+      JvOptionPane.showInternalMessageDialog(af, MessageManager
+              .formatMessage("label.executable_not_found", cmd));
     }
 
-    return file == null ? null : getFilePath(file);
+    return file == null ? null : getFilePath(file, true);
   }
 
   /**
@@ -281,18 +293,46 @@ public abstract class HmmerCommand implements Runnable
   }
 
   /**
+   * Answers the HMM profile for the profile sequence the user selected (default
+   * is just the first HMM sequence in the alignment)
+   * 
+   * @return
+   */
+  protected HiddenMarkovModel getHmmProfile()
+  {
+    String alignToParamName = MessageManager.getString("label.use_hmm");
+    for (ArgumentI arg : params)
+    {
+      String name = arg.getName();
+      if (name.equals(alignToParamName))
+      {
+        String seqName = arg.getValue();
+        SequenceI hmmSeq = alignment.findName(seqName);
+        if (hmmSeq.hasHMMProfile())
+        {
+          return hmmSeq.getHMM();
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
    * Answers an absolute path to the given file, in a format suitable for
    * processing by a hmmer command. On a Windows platform, the native Windows file
    * path is converted to Cygwin format, by replacing '\'with '/' and drive letter
    * X with /cygdrive/x.
    * 
    * @param resultFile
+   * @param isInCygwin
+   *                     True if file is to be read/written from within the Cygwin
+   *                     shell. Should be false for any imports.
    * @return
    */
-  protected String getFilePath(File resultFile)
+  protected String getFilePath(File resultFile, boolean isInCygwin)
   {
     String path = resultFile.getAbsolutePath();
-    if (Platform.isWindows())
+    if (Platform.isWindows() && isInCygwin)
     {
       // the first backslash escapes '\' for the regular expression argument
       path = path.replaceAll("\\" + File.separator, "/");