package jalview.hmmer; import jalview.analysis.SeqsetUtils; import jalview.bin.Cache; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.Annotation; import jalview.datamodel.HiddenMarkovModel; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.JvOptionPane; import jalview.gui.Preferences; import jalview.io.HMMFile; import jalview.io.StockholmFile; import jalview.util.MessageManager; import jalview.ws.params.ArgumentI; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Hashtable; import java.util.List; /** * Base class for hmmbuild, hmmalign and hmmsearch * * @author TZVanaalten * */ public class HmmerCommand { public static final String HMMBUILD = "hmmbuild"; private Hashtable hash = new Hashtable(); protected AlignFrame af; protected List params; public HmmerCommand(AlignFrame alignFrame, List args) { af = alignFrame; params = args; } public static boolean isHmmerAvailable() { File exec = getExecutable(HMMBUILD, Cache.getProperty(Preferences.HMMER_PATH)); return exec != null; } /** * Uniquifies the sequences when exporting and stores their details in a * hashtable * * @param seqs */ protected void stashSequences(SequenceI[] seqs) { hash = SeqsetUtils.uniquify(seqs, true); } /** * Restores the sequence data lost by uniquifying * * @param seqs */ protected void recoverSequences(SequenceI[] seqs) { SeqsetUtils.deuniquify(hash, seqs); } /** * Runs a command as a separate process * * @param command * the executable command and any arguments to it * @throws IOException */ public boolean runCommand(List command) throws IOException { try { ProcessBuilder pb = new ProcessBuilder(command); pb.redirectErrorStream(true); // send syserr to sysout final Process p = pb.start(); new Thread(new Runnable() { @Override public void run() { BufferedReader input = new BufferedReader( new InputStreamReader(p.getInputStream())); try { String line = input.readLine(); while (line != null) { System.out.println(line); line = input.readLine(); } } catch (IOException e) { e.printStackTrace(); } } }).start(); p.waitFor(); return p.exitValue() == 0; // 0 is success, by convention } catch (Exception e) { e.printStackTrace(); return false; } } /** * Exports an alignment (and possibly annotation) to the specified file, in * Stockholm format * * @param seqs * @param toFile * @param annotated * @throws IOException */ public void exportStockholm(SequenceI[] seqs, File toFile, AnnotatedCollectionI annotated) throws IOException { if (seqs != null) { AlignmentI newAl = new Alignment(seqs); if (toFile != null && annotated != null) { for (AlignmentAnnotation annot : annotated.getAlignmentAnnotation()) { if (annot.label.contains("Reference") || "RF".equals(annot.label)) { AlignmentAnnotation newRF; if (annot.annotations.length > newAl.getWidth()) { Annotation[] rfAnnots = new Annotation[newAl.getWidth()]; System.arraycopy(annot.annotations, 0, rfAnnots, 0, rfAnnots.length); newRF = new AlignmentAnnotation("RF", "Reference Positions", rfAnnots); } else { newRF = new AlignmentAnnotation(annot); } newAl.addAnnotation(newRF); } } } StockholmFile file = new StockholmFile(newAl); String output = file.print(seqs, false); PrintWriter writer = new PrintWriter(toFile); writer.println(output); writer.close(); } } /** * Answers the full path to the given hmmer executable, or null if file cannot * be found or is not executable * * @param cmd * command short name e.g. hmmalign * @return */ protected String getCommandPath(String cmd) { String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH); File file = getExecutable(cmd, binariesFolder); if (file == null && af != null) { JvOptionPane.showInternalMessageDialog(af, MessageManager.getString("warn.hmm_command_failed")); } return file == null ? null : file.getAbsolutePath(); } /** * 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. * * @param cmd * hmmer command short name, for example hmmbuild * @param binaryPath * parent folder containing hmmer executables * @return */ public static File getExecutable(String cmd, String binaryPath) { File file = new File(binaryPath, cmd); if (!file.canExecute()) { file = new File(binaryPath, cmd + ".exe"); { if (!file.canExecute()) { file = null; } } } return file; } /** * A convenience method to create a temporary file that is deleted on exit of * the JVM * * @param prefix * @param suffix * @return * @throws IOException */ protected File createTempFile(String prefix, String suffix) throws IOException { File f = File.createTempFile(prefix, suffix); f.deleteOnExit(); return f; } /** * Exports an HMM to the specified file * * @param hmm * @param hmmFile * @throws IOException */ public void exportHmm(HiddenMarkovModel hmm, File hmmFile) throws IOException { if (hmm != null) { HMMFile file = new HMMFile(hmm); PrintWriter writer = new PrintWriter(hmmFile); writer.print(file.print()); writer.close(); } } }