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.MessageManager;
18 import jalview.ws.params.ArgumentI;
20 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.util.Hashtable;
26 import java.util.List;
29 * Base class for hmmbuild, hmmalign and hmmsearch
34 public abstract class HmmerCommand implements Runnable
36 public static final String HMMBUILD = "hmmbuild";
38 protected final AlignFrame af;
40 protected final AlignmentI alignment;
42 protected final List<ArgumentI> params;
50 public HmmerCommand(AlignFrame alignFrame, List<ArgumentI> args)
53 alignment = af.getViewport().getAlignment();
58 * Answers true if preference HMMER_PATH is set, and its value is the path to
59 * a directory that contains an executable <code>hmmbuild</code> or
60 * <code>hmmbuild.exe</code>, else false
64 public static boolean isHmmerAvailable()
66 File exec = getExecutable(HMMBUILD, Cache.getProperty(Preferences.HMMER_PATH));
71 * Uniquifies the sequences when exporting and stores their details in a
76 protected Hashtable stashSequences(SequenceI[] seqs)
78 return SeqsetUtils.uniquify(seqs, true);
82 * Restores the sequence data lost by uniquifying
87 protected void recoverSequences(Hashtable hashtable, SequenceI[] seqs)
89 SeqsetUtils.deuniquify(hashtable, seqs);
93 * Runs a command as a separate process and waits for it to complete. Answers
94 * true if the process return status is zero, else false.
97 * the executable command and any arguments to it
100 public boolean runCommand(List<String> command)
105 ProcessBuilder pb = new ProcessBuilder(command);
106 pb.redirectErrorStream(true); // merge syserr to sysout
107 final Process p = pb.start();
108 new Thread(new Runnable()
113 BufferedReader input = new BufferedReader(
114 new InputStreamReader(p.getInputStream()));
117 String line = input.readLine();
120 System.out.println(line);
121 line = input.readLine();
123 } catch (IOException e)
131 return p.exitValue() == 0; // 0 is success, by convention
132 } catch (Exception e)
140 * Exports an alignment, and reference (RF) annotation if present, to the
141 * specified file, in Stockholm format
146 * @throws IOException
148 public void exportStockholm(SequenceI[] seqs, File toFile,
149 AnnotatedCollectionI annotated) throws IOException
155 AlignmentI newAl = new Alignment(seqs);
156 if (!newAl.isAligned())
161 if (toFile != null && annotated != null)
163 AlignmentAnnotation[] annots = annotated.getAlignmentAnnotation();
166 for (AlignmentAnnotation annot : annots)
168 if (annot.label.contains("Reference") || "RF".equals(annot.label))
170 AlignmentAnnotation newRF;
171 if (annot.annotations.length > newAl.getWidth())
173 Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
174 System.arraycopy(annot.annotations, 0, rfAnnots, 0,
176 newRF = new AlignmentAnnotation("RF", "Reference Positions",
181 newRF = new AlignmentAnnotation(annot);
183 newAl.addAnnotation(newRF);
189 StockholmFile file = new StockholmFile(newAl);
190 String output = file.print(seqs, false);
191 PrintWriter writer = new PrintWriter(toFile);
192 writer.println(output);
197 * Answers the full path to the given hmmer executable, or null if file cannot
198 * be found or is not executable
201 * command short name e.g. hmmalign
204 protected String getCommandPath(String cmd)
206 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
207 File file = getExecutable(cmd, binariesFolder);
208 if (file == null && af != null)
210 JvOptionPane.showInternalMessageDialog(af,
211 MessageManager.getString("warn.hmm_command_failed"));
214 return file == null ? null : file.getAbsolutePath();
218 * Answers the executable file for the given hmmer command, or null if not
219 * found or not executable. The path to the executable is the command name
220 * prefixed by the hmmer binaries folder path, optionally with .exe appended.
223 * hmmer command short name, for example hmmbuild
225 * parent folder containing hmmer executables
228 public static File getExecutable(String cmd, String binaryPath)
230 File file = new File(binaryPath, cmd);
231 if (!file.canExecute())
233 file = new File(binaryPath, cmd + ".exe");
235 if (!file.canExecute())
245 * A convenience method to create a temporary file that is deleted on exit of
251 * @throws IOException
253 protected File createTempFile(String prefix, String suffix)
256 File f = File.createTempFile(prefix, suffix);
263 * Exports an HMM to the specified file
267 * @throws IOException
269 public void exportHmm(HiddenMarkovModel hmm, File hmmFile)
274 HMMFile file = new HMMFile(hmm);
275 PrintWriter writer = new PrintWriter(hmmFile);
276 writer.print(file.print());