X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fhmmer%2FHmmerCommand.java;h=a30c524e8b21910baaaa557f7907fa2a5985ca95;hb=dd2b17330673faf79c5e239afd163668fdb3fe0a;hp=9bec181b9af1a517c7543b519b895fc6299e1439;hpb=1ed4ac38ab7e097a051afaa25853434f1cfb6d4c;p=jalview.git diff --git a/src/jalview/hmmer/HmmerCommand.java b/src/jalview/hmmer/HmmerCommand.java index 9bec181..a30c524 100644 --- a/src/jalview/hmmer/HmmerCommand.java +++ b/src/jalview/hmmer/HmmerCommand.java @@ -8,10 +8,12 @@ import jalview.datamodel.AlignmentI; import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.Annotation; import jalview.datamodel.HiddenMarkovModel; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.JvOptionPane; import jalview.gui.Preferences; +import jalview.io.FastaFile; import jalview.io.HMMFile; import jalview.io.StockholmFile; import jalview.util.FileUtils; @@ -24,6 +26,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; @@ -44,6 +47,41 @@ public abstract class HmmerCommand implements Runnable protected final List params; + /* + * constants for i18n lookup of passed parameter names + */ + static final String DATABASE_KEY = "label.database"; + + static final String THIS_ALIGNMENT_KEY = "label.this_alignment"; + + static final String USE_ACCESSIONS_KEY = "label.use_accessions"; + + static final String AUTO_ALIGN_SEQS_KEY = "label.auto_align_seqs"; + + static final String NUMBER_OF_RESULTS_KEY = "label.number_of_results"; + + static final String TRIM_TERMINI_KEY = "label.trim_termini"; + + static final String RETURN_N_NEW_SEQ = "label.check_for_new_sequences"; + + static final String REPORTING_CUTOFF_KEY = "label.reporting_cutoff"; + + static final String CUTOFF_NONE = "label.default"; + + static final String CUTOFF_SCORE = "label.score"; + + static final String CUTOFF_EVALUE = "label.evalue"; + + static final String SEQ_EVALUE_KEY = "label.seq_evalue"; + + static final String DOM_EVALUE_KEY = "label.dom_evalue"; + + static final String SEQ_SCORE_KEY = "label.seq_score"; + + static final String DOM_SCORE_KEY = "label.dom_score"; + + static final String ARG_TRIM = "--trim"; + /** * Constructor * @@ -97,20 +135,26 @@ 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 commands = Platform.isWindows() ? wrapWithCygwin(command) - : command; + List args = Platform.isWindows() ? wrapWithCygwin(commands) + : commands; try { - ProcessBuilder pb = new ProcessBuilder(commands); + 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() { @@ -139,7 +183,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,42 +194,43 @@ 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 wrapWithCygwin(List command) + protected List wrapWithCygwin(List 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 wrapped = new ArrayList<>(); - wrapped.add(runCygwin.getAbsolutePath()); - if (!command.isEmpty()) + // wrapped.add("C:\Users\tva\run"); + 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; } /** * Exports an alignment, and reference (RF) annotation if present, to the - * specified file, in Stockholm format + * specified file, in Stockholm format, removing all HMM sequences * * @param seqs * @param toFile @@ -193,13 +238,15 @@ public abstract class HmmerCommand implements Runnable * @throws IOException */ public void exportStockholm(SequenceI[] seqs, File toFile, - AnnotatedCollectionI annotated) throws IOException + AnnotatedCollectionI annotated) + throws IOException { if (seqs == null) { return; } AlignmentI newAl = new Alignment(seqs); + if (!newAl.isAligned()) { newAl.padGaps(); @@ -233,6 +280,17 @@ public abstract class HmmerCommand implements Runnable } } + for (SequenceI seq : newAl.getSequencesArray()) + { + if (seq.getAnnotation() != null) + { + for (AlignmentAnnotation ann : seq.getAnnotation()) + { + seq.removeAlignmentAnnotation(ann); + } + } + } + StockholmFile file = new StockholmFile(newAl); String output = file.print(seqs, false); PrintWriter writer = new PrintWriter(toFile); @@ -247,18 +305,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 : file.getAbsolutePath(); + return file == null ? null : getFilePath(file, true); } /** @@ -279,4 +341,184 @@ public abstract class HmmerCommand implements Runnable writer.close(); } } + + // TODO is needed? + /** + * Exports a sequence to the specified file + * + * @param hmm + * @param hmmFile + * @throws IOException + */ + public void exportSequence(SequenceI seq, File seqFile) throws IOException + { + if (seq != null) + { + FastaFile file = new FastaFile(); + PrintWriter writer = new PrintWriter(seqFile); + writer.print(file.print(new SequenceI[] { seq }, false)); + writer.close(); + } + } + + /** + * 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 the HMM profile for the profile sequence the user selected (default + * is just the first HMM sequence in the alignment) + * + * @return + */ + protected SequenceI getSequence() + { + String alignToParamName = MessageManager + .getString("label.use_sequence"); + for (ArgumentI arg : params) + { + String name = arg.getName(); + if (name.equals(alignToParamName)) + { + String seqName = arg.getValue(); + SequenceI seq = alignment.findName(seqName); + return seq; + } + } + 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, boolean isInCygwin) + { + String path = resultFile.getAbsolutePath(); + if (Platform.isWindows() && isInCygwin) + { + // 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; + } + + /** + * A helper method that deletes any HMM consensus sequence from the given + * collection, and from the parent alignment if ac is a subgroup + * + * @param ac + */ + void deleteHmmSequences(AnnotatedCollectionI ac) + { + List hmmSeqs = ac.getHmmSequences(); + for (SequenceI hmmSeq : hmmSeqs) + { + if (ac instanceof SequenceGroup) + { + ((SequenceGroup) ac).deleteSequence(hmmSeq, false); + AnnotatedCollectionI context = ac.getContext(); + if (context != null && context instanceof AlignmentI) + { + ((AlignmentI) context).deleteSequence(hmmSeq); + } + } + else + { + ((AlignmentI) ac).deleteSequence(hmmSeq); + } + } + } + + /** + * Sets the names of any duplicates within the given sequences to include their + * respective lengths. Deletes any duplicates that have the same name after this + * step + * + * @param seqs + */ + void renameDuplicates(AlignmentI al) + { + + SequenceI[] seqs = al.getSequencesArray(); + List wasRenamed = new ArrayList<>(); + + for (SequenceI seq : seqs) + { + wasRenamed.add(false); + } + + for (int i = 0; i < seqs.length; i++) + { + for (int j = 0; j < seqs.length; j++) + { + if (seqs[i].getName().equals(seqs[j].getName()) && i != j + && !wasRenamed.get(j)) + { + + wasRenamed.set(i, true); + String range = "/" + seqs[j].getStart() + "-" + seqs[j].getEnd(); + // setting sequence name to include range - to differentiate between + // sequences of the same name. Currently have to include the range twice + // because the range is removed (once) when setting the name + // TODO come up with a better way of doing this + seqs[j].setName(seqs[j].getName() + range + range); + } + + } + if (wasRenamed.get(i)) + { + String range = "/" + seqs[i].getStart() + "-" + seqs[i].getEnd(); + seqs[i].setName(seqs[i].getName() + range + range); + } + } + + for (int i = 0; i < seqs.length; i++) + { + for (int j = 0; j < seqs.length; j++) + { + if (seqs[i].getName().equals(seqs[j].getName()) && i != j) + { + al.deleteSequence(j); + } + } + } + } + }