3 import jalview.analysis.SeqsetUtils;
4 import jalview.bin.Cache;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.AnnotatedCollectionI;
9 import jalview.datamodel.Annotation;
10 import jalview.datamodel.HiddenMarkovModel;
11 import jalview.datamodel.SequenceI;
12 import jalview.gui.AlignFrame;
13 import jalview.gui.JvOptionPane;
14 import jalview.gui.Preferences;
15 import jalview.io.HMMFile;
16 import jalview.io.StockholmFile;
17 import jalview.util.FileUtils;
18 import jalview.util.MessageManager;
19 import jalview.util.Platform;
20 import jalview.ws.params.ArgumentI;
22 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.PrintWriter;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.Hashtable;
30 import java.util.List;
33 * Base class for hmmbuild, hmmalign and hmmsearch
38 public abstract class HmmerCommand implements Runnable
40 public static final String HMMBUILD = "hmmbuild";
42 protected final AlignFrame af;
44 protected final AlignmentI alignment;
46 protected final List<ArgumentI> params;
54 public HmmerCommand(AlignFrame alignFrame, List<ArgumentI> args)
57 alignment = af.getViewport().getAlignment();
62 * Answers true if preference HMMER_PATH is set, and its value is the path to
63 * a directory that contains an executable <code>hmmbuild</code> or
64 * <code>hmmbuild.exe</code>, else false
68 public static boolean isHmmerAvailable()
70 File exec = FileUtils.getExecutable(HMMBUILD,
71 Cache.getProperty(Preferences.HMMER_PATH));
76 * Uniquifies the sequences when exporting and stores their details in a
81 protected Hashtable stashSequences(SequenceI[] seqs)
83 return SeqsetUtils.uniquify(seqs, true);
87 * Restores the sequence data lost by uniquifying
92 protected void recoverSequences(Hashtable hashtable, SequenceI[] seqs)
94 SeqsetUtils.deuniquify(hashtable, seqs);
98 * Runs a command as a separate process and waits for it to complete. Answers
99 * true if the process return status is zero, else false.
102 * the executable command and any arguments to it
103 * @throws IOException
105 public boolean runCommand(List<String> commands)
108 List<String> args = Platform.isWindows() ? wrapWithCygwin(commands)
113 ProcessBuilder pb = new ProcessBuilder(args);
114 pb.redirectErrorStream(true); // merge syserr to sysout
115 final Process p = pb.start();
116 new Thread(new Runnable()
121 BufferedReader input = new BufferedReader(
122 new InputStreamReader(p.getInputStream()));
125 String line = input.readLine();
128 System.out.println(line);
129 line = input.readLine();
131 } catch (IOException e)
139 int exitValue = p.exitValue();
142 Cache.log.error("Command failed, return code = " + exitValue);
143 Cache.log.error("Command/args were: " + args.toString());
145 return exitValue == 0; // 0 is success, by convention
146 } catch (Exception e)
154 * Converts the given command to a Cygwin "bash" command wrapper. The hmmer
155 * command and any arguments to it are converted into a single parameter to the
160 protected List<String> wrapWithCygwin(List<String> commands)
162 File bash = FileUtils.getExecutable("bash",
163 Cache.getProperty(Preferences.CYGWIN_PATH));
166 Cache.log.error("Cygwin shell not found");
170 List<String> wrapped = new ArrayList<>();
171 wrapped.add(bash.getAbsolutePath());
175 * combine hmmbuild/search/align and arguments to a single string
177 StringBuilder sb = new StringBuilder();
178 for (String cmd : commands)
180 sb.append(" ").append(cmd);
182 wrapped.add(sb.toString());
188 * Exports an alignment, and reference (RF) annotation if present, to the
189 * specified file, in Stockholm format
194 * @throws IOException
196 public void exportStockholm(SequenceI[] seqs, File toFile,
197 AnnotatedCollectionI annotated) throws IOException
203 AlignmentI newAl = new Alignment(seqs);
204 if (!newAl.isAligned())
209 if (toFile != null && annotated != null)
211 AlignmentAnnotation[] annots = annotated.getAlignmentAnnotation();
214 for (AlignmentAnnotation annot : annots)
216 if (annot.label.contains("Reference") || "RF".equals(annot.label))
218 AlignmentAnnotation newRF;
219 if (annot.annotations.length > newAl.getWidth())
221 Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
222 System.arraycopy(annot.annotations, 0, rfAnnots, 0,
224 newRF = new AlignmentAnnotation("RF", "Reference Positions",
229 newRF = new AlignmentAnnotation(annot);
231 newAl.addAnnotation(newRF);
237 StockholmFile file = new StockholmFile(newAl);
238 String output = file.print(seqs, false);
239 PrintWriter writer = new PrintWriter(toFile);
240 writer.println(output);
245 * Answers the full path to the given hmmer executable, or null if file cannot
246 * be found or is not executable
249 * command short name e.g. hmmalign
251 * @throws IOException
253 protected String getCommandPath(String cmd) throws IOException
255 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
256 // ensure any symlink to the directory is resolved:
257 binariesFolder = Paths.get(binariesFolder).toRealPath().toString();
258 File file = FileUtils.getExecutable(cmd, binariesFolder);
259 if (file == null && af != null)
261 JvOptionPane.showInternalMessageDialog(af, MessageManager
262 .formatMessage("label.executable_not_found", cmd));
265 return file == null ? null : getFilePath(file);
269 * Exports an HMM to the specified file
273 * @throws IOException
275 public void exportHmm(HiddenMarkovModel hmm, File hmmFile)
280 HMMFile file = new HMMFile(hmm);
281 PrintWriter writer = new PrintWriter(hmmFile);
282 writer.print(file.print());
288 * Answers the HMM profile for the profile sequence the user selected (default
289 * is just the first HMM sequence in the alignment)
293 protected HiddenMarkovModel getHmmProfile()
295 String alignToParamName = MessageManager.getString("label.use_hmm");
296 for (ArgumentI arg : params)
298 String name = arg.getName();
299 if (name.equals(alignToParamName))
301 String seqName = arg.getValue();
302 SequenceI hmmSeq = alignment.findName(seqName);
303 if (hmmSeq.hasHMMProfile())
305 return hmmSeq.getHMM();
313 * Answers an absolute path to the given file, in a format suitable for
314 * processing by a hmmer command. On a Windows platform, the native Windows file
315 * path is converted to Cygwin format, by replacing '\'with '/' and drive letter
316 * X with /cygdrive/x.
321 protected String getFilePath(File resultFile)
323 String path = resultFile.getAbsolutePath();
324 if (Platform.isWindows())
326 // the first backslash escapes '\' for the regular expression argument
327 path = path.replaceAll("\\" + File.separator, "/");
328 int colon = path.indexOf(':');
331 String drive = path.substring(0, colon);
332 path = path.replaceAll(drive + ":", "/cygdrive/" + drive);