Merge branch 'features/mchmmer' of https://source.jalview.org/git/jalview.git into...
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 17 Apr 2018 17:44:41 +0000 (18:44 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 17 Apr 2018 17:44:41 +0000 (18:44 +0100)
Conflicts:
src/jalview/hmmer/HmmerCommand.java

.classpath
src/jalview/gui/Preferences.java
src/jalview/hmmer/HMMAlign.java
src/jalview/hmmer/HMMBuild.java
src/jalview/hmmer/HMMSearch.java
src/jalview/hmmer/HmmerCommand.java
src/jalview/util/FileUtils.java

index c85feaf..dd4fd0d 100644 (file)
@@ -66,7 +66,7 @@
        <classpathentry kind="con" path="org.testng.TESTNG_CONTAINER"/>
        <classpathentry kind="lib" path="lib/biojava-core-4.1.0.jar"/>
        <classpathentry kind="lib" path="lib/biojava-ontology-4.1.0.jar"/>
-       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="lib" path="lib/htsjdk-2.12.0.jar"/>
        <classpathentry kind="lib" path="lib/groovy-all-2.4.12-indy.jar"/>
        <classpathentry kind="output" path="classes"/>
index 296c4bf..5382f8d 100755 (executable)
@@ -258,7 +258,12 @@ public class Preferences extends GPreferences
     });
     if (cygwinPath != null)
     {
-      cygwinPath.setText(Cache.getProperty(CYGWIN_PATH));
+      String path = Cache.getProperty(CYGWIN_PATH);
+      if (path == null)
+      {
+        path = FileUtils.getPathTo("bash");
+      }
+      cygwinPath.setText(path);
       cygwinPath.addActionListener(new ActionListener()
       {
         @Override
index 492e71c..b5f047e 100644 (file)
@@ -152,9 +152,9 @@ public class HMMAlign extends HmmerCommand
       }
     }
     args.add("-o");
-    args.add(resultFile.getAbsolutePath());
-    args.add(modelFile.getAbsolutePath());
-    args.add(alignmentFile.getAbsolutePath());
+    args.add(getFilePath(resultFile));
+    args.add(getFilePath(modelFile));
+    args.add(getFilePath(alignmentFile));
     
     return runCommand(args);
   }
@@ -172,7 +172,7 @@ public class HMMAlign extends HmmerCommand
   private SequenceI[] importData(File resultFile,
           List<AlignmentOrder> allOrders) throws IOException
   {
-    StockholmFile file = new StockholmFile(resultFile.getAbsolutePath(),
+    StockholmFile file = new StockholmFile(getFilePath(resultFile),
             DataSourceType.FILE);
     SequenceI[] result = file.getSeqsAsArray();
     AlignmentOrder msaorder = new AlignmentOrder(result);
index d5c17fc..3c3bd40 100644 (file)
@@ -314,8 +314,8 @@ public class HMMBuild extends HmmerCommand
       args.add(ARG_DNA);
     }
 
-    args.add(hmmFile.getAbsolutePath());
-    args.add(sequencesFile.getAbsolutePath());
+    args.add(getFilePath(hmmFile));
+    args.add(getFilePath(sequencesFile));
 
     return runCommand(args);
   }
index 7738cb5..7413d14 100644 (file)
@@ -130,9 +130,9 @@ public class HMMSearch extends HmmerCommand
     List<String> args = new ArrayList<>();
     args.add(command);
     args.add("-o");
-    args.add(searchOutputFile.getAbsolutePath());
+    args.add(getFilePath(searchOutputFile));
     args.add("-A");
-    args.add(hitsAlignmentFile.getAbsolutePath());
+    args.add(getFilePath(hitsAlignmentFile));
 
     boolean dbFound = false;
     String dbPath = "";
@@ -222,8 +222,8 @@ public class HMMSearch extends HmmerCommand
       // writer.close();
     }
 
-    args.add(hmmFile.getAbsolutePath());
-    args.add(databaseFile.getAbsolutePath());
+    args.add(getFilePath(hmmFile));
+    args.add(getFilePath(databaseFile));
 
     return runCommand(args);
   }
index 8888c20..f8f2cde 100644 (file)
@@ -97,19 +97,19 @@ public abstract class HmmerCommand implements Runnable
    * Runs a command as a separate process and waits for it to complete. Answers
    * true if the process return status is zero, else false.
    * 
-   * @param command
+   * @param commands
    *          the executable command and any arguments to it
    * @throws IOException
    */
-  public boolean runCommand(List<String> command)
+  public boolean runCommand(List<String> commands)
           throws IOException
   {
-    List<String> commands = Platform.isWindows() ? wrapWithCygwin(command)
-            : command;
+    List<String> args = Platform.isWindows() ? wrapWithCygwin(commands)
+            : commands;
 
     try
     {
-      ProcessBuilder pb = new ProcessBuilder(commands);
+      ProcessBuilder pb = new ProcessBuilder(args);
       pb.redirectErrorStream(true); // merge syserr to sysout
       final Process p = pb.start();
       new Thread(new Runnable()
@@ -139,7 +139,7 @@ public abstract class HmmerCommand implements Runnable
       if (exitValue != 0)
       {
         Cache.log.error("Command failed, return code = " + exitValue);
-        Cache.log.error("Command/args were: " + commands.toString());
+        Cache.log.error("Command/args were: " + args.toString());
       }
       return exitValue == 0; // 0 is success, by convention
     } catch (Exception e)
@@ -150,35 +150,35 @@ public abstract class HmmerCommand implements Runnable
   }
 
   /**
-   * Converts the given command to a Cygwin "run" command wrapper
+   * Converts the given command to a Cygwin "bash" command wrapper. The hmmer
+   * command and any arguments to it are converted into a single parameter to the
+   * bash command.
    * 
-   * @param command
-   * @return
+   * @param commands
    */
-  protected List<String> wrapWithCygwin(List<String> command)
+  protected List<String> wrapWithCygwin(List<String> commands)
   {
-    File runCygwin = FileUtils.getExecutable("run",
+    File bash = FileUtils.getExecutable("bash",
             Cache.getProperty(Preferences.CYGWIN_PATH));
-    if (runCygwin == null)
+    if (bash == null)
     {
       Cache.log.error("Cygwin shell not found");
-      return command;
+      return commands;
     }
 
     List<String> wrapped = new ArrayList<>();
-    wrapped.add(runCygwin.getAbsolutePath());
-    if (!command.isEmpty())
+    wrapped.add(bash.getAbsolutePath());
+    wrapped.add("-c");
+
+    /*
+     * combine hmmbuild/search/align and arguments to a single string
+     */
+    StringBuilder sb = new StringBuilder();
+    for (String cmd : commands)
     {
-      wrapped.add(command.get(0));
-      // wrapped.add("--quote");
-      StringBuilder args = new StringBuilder();
-      for (String arg : command.subList(1, command.size()))
-      {
-        args.append(" ").append(arg);
-      }
-      wrapped.add(args.toString());
+      sb.append(" ").append(cmd);
     }
-    // TODO this doesn't yet pass parameters successfully
+    wrapped.add(sb.toString());
 
     return wrapped;
   }
@@ -258,7 +258,7 @@ public abstract class HmmerCommand implements Runnable
               .formatMessage("label.executable_not_found", cmd));
     }
 
-    return file == null ? null : file.getAbsolutePath();
+    return file == null ? null : getFilePath(file);
   }
 
   /**
@@ -304,4 +304,31 @@ public abstract class HmmerCommand implements Runnable
     }
     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
+   * @return
+   */
+  protected String getFilePath(File resultFile)
+  {
+    String path = resultFile.getAbsolutePath();
+    if (Platform.isWindows())
+    {
+      // the first backslash escapes '\' for the regular expression argument
+      path = path.replaceAll("\\" + File.separator, "/");
+      int colon = path.indexOf(':');
+      if (colon > 0)
+      {
+        String drive = path.substring(0, colon);
+        path = path.replaceAll(drive + ":", "/cygdrive/" + drive);
+      }
+    }
+
+    return path;
+  }
 }
index b81ec71..1684763 100644 (file)
@@ -37,6 +37,28 @@ public final class FileUtils
   }
 
   /**
+   * Answers the path to the folder containing the given executable file, by
+   * searching the PATH environment variable. Answers null if no such executable
+   * can be found.
+   * 
+   * @param cmd
+   * @return
+   */
+  public static String getPathTo(String cmd)
+  {
+    String paths = System.getenv("PATH");
+    // backslash is to escape regular expression argument
+    for (String path : paths.split("\\" + File.pathSeparator))
+    {
+      if (getExecutable(cmd, path) != null)
+      {
+        return path;
+      }
+    }
+    return null;
+  }
+
+  /**
    * A convenience method to create a temporary file that is deleted on exit of
    * the JVM
    *