X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fhmmer%2FHmmerCommand.java;h=f8f2cdeed5d78addce1c876b26c967e27991eb75;hb=d31b51985d01217340aa5f6470d3fd3c2314e3eb;hp=fe6c0f9da02bc0308de310be88f583b6e036f12e;hpb=4d2c8b4049276e1d14ec962d2408bbd50baa596e;p=jalview.git diff --git a/src/jalview/hmmer/HmmerCommand.java b/src/jalview/hmmer/HmmerCommand.java index fe6c0f9..f8f2cde 100644 --- a/src/jalview/hmmer/HmmerCommand.java +++ b/src/jalview/hmmer/HmmerCommand.java @@ -14,7 +14,9 @@ import jalview.gui.JvOptionPane; import jalview.gui.Preferences; import jalview.io.HMMFile; import jalview.io.StockholmFile; +import jalview.util.FileUtils; import jalview.util.MessageManager; +import jalview.util.Platform; import jalview.ws.params.ArgumentI; import java.io.BufferedReader; @@ -22,6 +24,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -63,7 +66,8 @@ public abstract class HmmerCommand implements Runnable */ public static boolean isHmmerAvailable() { - File exec = getExecutable(HMMBUILD, Cache.getProperty(Preferences.HMMER_PATH)); + File exec = FileUtils.getExecutable(HMMBUILD, + Cache.getProperty(Preferences.HMMER_PATH)); return exec != null; } @@ -93,16 +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 command) + public boolean runCommand(List commands) throws IOException { + List args = Platform.isWindows() ? wrapWithCygwin(commands) + : commands; + try { - ProcessBuilder pb = new ProcessBuilder(command); + ProcessBuilder pb = new ProcessBuilder(args); pb.redirectErrorStream(true); // merge syserr to sysout final Process p = pb.start(); new Thread(new Runnable() @@ -128,7 +135,13 @@ public abstract class HmmerCommand implements Runnable }).start(); p.waitFor(); - return p.exitValue() == 0; // 0 is success, by convention + int exitValue = p.exitValue(); + if (exitValue != 0) + { + Cache.log.error("Command failed, return code = " + exitValue); + Cache.log.error("Command/args were: " + args.toString()); + } + return exitValue == 0; // 0 is success, by convention } catch (Exception e) { e.printStackTrace(); @@ -137,6 +150,40 @@ public abstract class HmmerCommand implements Runnable } /** + * 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 commands + */ + protected List wrapWithCygwin(List commands) + { + File bash = FileUtils.getExecutable("bash", + Cache.getProperty(Preferences.CYGWIN_PATH)); + if (bash == null) + { + Cache.log.error("Cygwin shell not found"); + return commands; + } + + List wrapped = new ArrayList<>(); + 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) + { + sb.append(" ").append(cmd); + } + wrapped.add(sb.toString()); + + return wrapped; + } + + /** * Exports an alignment, and reference (RF) annotation if present, to the * specified file, in Stockholm format * @@ -204,77 +251,84 @@ public abstract class HmmerCommand implements Runnable protected String getCommandPath(String cmd) { String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH); - File file = getExecutable(cmd, binariesFolder); + 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 : file.getAbsolutePath(); + return file == null ? null : getFilePath(file); } /** - * 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. + * Exports an HMM to the specified file * - * @param cmd - * hmmer command short name, for example hmmbuild - * @param binaryPath - * parent folder containing hmmer executables - * @return + * @param hmm + * @param hmmFile + * @throws IOException */ - public static File getExecutable(String cmd, String binaryPath) + public void exportHmm(HiddenMarkovModel hmm, File hmmFile) + throws IOException { - File file = new File(binaryPath, cmd); - if (!file.canExecute()) + if (hmm != null) { - file = new File(binaryPath, cmd + ".exe"); - { - if (!file.canExecute()) - { - file = null; - } - } + HMMFile file = new HMMFile(hmm); + PrintWriter writer = new PrintWriter(hmmFile); + writer.print(file.print()); + writer.close(); } - return file; } /** - * A convenience method to create a temporary file that is deleted on exit of - * the JVM + * Answers the HMM profile for the profile sequence the user selected (default + * is just the first HMM sequence in the alignment) * - * @param prefix - * @param suffix * @return - * @throws IOException */ - protected File createTempFile(String prefix, String suffix) - throws IOException + protected HiddenMarkovModel getHmmProfile() { - File f = File.createTempFile(prefix, suffix); - f.deleteOnExit(); - return f; - + 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; } /** - * Exports an HMM to the specified file + * 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 hmm - * @param hmmFile - * @throws IOException + * @param resultFile + * @return */ - public void exportHmm(HiddenMarkovModel hmm, File hmmFile) - throws IOException + protected String getFilePath(File resultFile) { - if (hmm != null) + String path = resultFile.getAbsolutePath(); + if (Platform.isWindows()) { - HMMFile file = new HMMFile(hmm); - PrintWriter writer = new PrintWriter(hmmFile); - writer.print(file.print()); - writer.close(); + // 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; } }