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 class HmmerCommand
36 public static final String HMMBUILD = "hmmbuild";
38 private Hashtable hash = new Hashtable();
40 protected AlignFrame af;
42 protected List<ArgumentI> params;
44 public HmmerCommand(AlignFrame alignFrame, List<ArgumentI> args)
50 public static boolean isHmmerAvailable()
52 File exec = getExecutable(HMMBUILD, Cache.getProperty(Preferences.HMMER_PATH));
57 * Uniquifies the sequences when exporting and stores their details in a
62 protected void stashSequences(SequenceI[] seqs)
64 hash = SeqsetUtils.uniquify(seqs, true);
68 * Restores the sequence data lost by uniquifying
72 protected void recoverSequences(SequenceI[] seqs)
74 SeqsetUtils.deuniquify(hash, seqs);
78 * Runs a command as a separate process
81 * the executable command and any arguments to it
84 public boolean runCommand(List<String> command)
89 ProcessBuilder pb = new ProcessBuilder(command);
90 pb.redirectErrorStream(true); // send syserr to sysout
91 final Process p = pb.start();
92 new Thread(new Runnable()
97 BufferedReader input = new BufferedReader(
98 new InputStreamReader(p.getInputStream()));
101 String line = input.readLine();
104 System.out.println(line);
105 line = input.readLine();
107 } catch (IOException e)
115 return p.exitValue() == 0; // 0 is success, by convention
116 } catch (Exception e)
124 * Exports an alignment (and possibly annotation) to the specified file, in
130 * @throws IOException
132 public void exportStockholm(SequenceI[] seqs, File toFile,
133 AnnotatedCollectionI annotated)
138 AlignmentI newAl = new Alignment(seqs);
139 if (toFile != null && annotated != null)
141 for (AlignmentAnnotation annot : annotated.getAlignmentAnnotation())
143 if (annot.label.contains("Reference") || "RF".equals(annot.label))
145 AlignmentAnnotation newRF;
146 if (annot.annotations.length > newAl.getWidth())
148 Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
149 System.arraycopy(annot.annotations, 0, rfAnnots, 0,
151 newRF = new AlignmentAnnotation("RF", "Reference Positions",
156 newRF = new AlignmentAnnotation(annot);
158 newAl.addAnnotation(newRF);
163 StockholmFile file = new StockholmFile(newAl);
164 String output = file.print(seqs, false);
165 PrintWriter writer = new PrintWriter(toFile);
166 writer.println(output);
172 * Answers the full path to the given hmmer executable, or null if file cannot
173 * be found or is not executable
176 * command short name e.g. hmmalign
179 protected String getCommandPath(String cmd)
181 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
182 File file = getExecutable(cmd, binariesFolder);
183 if (file == null && af != null)
185 JvOptionPane.showInternalMessageDialog(af,
186 MessageManager.getString("warn.hmm_command_failed"));
189 return file == null ? null : file.getAbsolutePath();
193 * Answers the executable file for the given hmmer command, or null if not
194 * found or not executable. The path to the executable is the command name
195 * prefixed by the hmmer binaries folder path, optionally with .exe appended.
198 * hmmer command short name, for example hmmbuild
200 * parent folder containing hmmer executables
203 public static File getExecutable(String cmd, String binaryPath)
205 File file = new File(binaryPath, cmd);
206 if (!file.canExecute())
208 file = new File(binaryPath, cmd + ".exe");
210 if (!file.canExecute())
220 * A convenience method to create a temporary file that is deleted on exit of
226 * @throws IOException
228 protected File createTempFile(String prefix, String suffix)
231 File f = File.createTempFile(prefix, suffix);
238 * Exports an HMM to the specified file
242 * @throws IOException
244 public void exportHmm(HiddenMarkovModel hmm, File hmmFile)
249 HMMFile file = new HMMFile(hmm);
250 PrintWriter writer = new PrintWriter(hmmFile);
251 writer.print(file.print());