3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.AnnotatedCollectionI;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.HiddenMarkovModel;
10 import jalview.datamodel.SequenceI;
11 import jalview.gui.AlignFrame;
12 import jalview.gui.JvOptionPane;
13 import jalview.gui.Preferences;
14 import jalview.io.HMMFile;
15 import jalview.io.StockholmFile;
16 import jalview.util.MessageManager;
18 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.io.PrintWriter;
23 import java.util.Hashtable;
24 import java.util.List;
27 * Base class for hmmbuild, hmmalign and hmmsearch
32 public class HmmerCommand
34 public static final String HMMBUILD = "hmmbuild";
36 public String JALVIEWDIRECTORY = System.getProperty("user.dir")
39 public String OUTPUTALIGNMENT;
41 public final String SPACE = " ";
43 public final String ALLCOL = "--allcol ";
45 public final String TRIM = "--trim ";
47 public final String FORCEAMINO = "--amino ";
49 public final String FORCEDNA = "--dna ";
51 public final String FORCERNA = "--rna ";
53 Hashtable hash = new Hashtable();
55 List<SequenceI> hmmSeqs;
57 protected AlignFrame af;
59 public static boolean isHmmerAvailable()
61 File exec = getExecutable(HMMBUILD, Cache.getProperty(Preferences.HMMER_PATH));
66 * Uniquifies the sequences when exporting and stores their details in a
71 public void uniquifySequences(SequenceI[] seqs)
73 hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
77 * Recover the sequence data lost by uniquifying.
81 public void recoverSequenceNames(SequenceI[] seqs)
83 jalview.analysis.SeqsetUtils.deuniquify(hash, seqs);
87 * Runs a command in the command line.
91 * @throws InterruptedException
93 public boolean runCommand(String command)
94 throws IOException, InterruptedException
98 final Process p = Runtime.getRuntime().exec(command);
100 new Thread(new Runnable()
105 BufferedReader input = new BufferedReader(
106 new InputStreamReader(p.getInputStream()));
111 while ((line = input.readLine()) != null)
113 System.out.println(line);
115 } catch (IOException e)
123 } catch (Exception e)
132 * Exports an alignment and/or HMM to the specified file.
135 * @throws IOException
137 public void exportData(SequenceI[] seqs, File stoLocation,
138 HiddenMarkovModel hmm, File hmmLocation, AnnotatedCollectionI al)
143 AlignmentI newAl = new Alignment(seqs);
144 if (stoLocation != null && al != null)
146 for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
148 if (annot.label.contains("Reference") || "RF".equals(annot.label))
150 AlignmentAnnotation newRF;
151 if (annot.annotations.length > newAl.getWidth())
153 Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
154 System.arraycopy(annot.annotations, 0, rfAnnots, 0,
156 newRF = new AlignmentAnnotation("RF", "Reference Positions",
161 newRF = new AlignmentAnnotation(annot);
163 newAl.addAnnotation(newRF);
168 StockholmFile file = new StockholmFile(newAl);
169 String output = file.print(seqs, false);
170 PrintWriter writer = new PrintWriter(stoLocation);
171 writer.println(output);
177 HMMFile file = new HMMFile(hmm);
178 PrintWriter writer = new PrintWriter(hmmLocation);
179 writer.print(file.print());
185 * Returns the list of HMM sequences removed
189 public List<SequenceI> getHmmSeqs()
195 * Sets the list of removed HMM sequences
199 public void setHmmSeqs(List<SequenceI> hmmSeqs)
201 this.hmmSeqs = hmmSeqs;
205 * Answers the full path to the given hmmer executable, or null if file cannot
206 * be found or is not executable
209 * command short name e.g. hmmalign
212 protected String getCommandRoot(String cmd)
214 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
215 File file = getExecutable(cmd, binariesFolder);
216 if (file == null && af != null)
218 JvOptionPane.showInternalMessageDialog(af,
219 MessageManager.getString("warn.hmm_command_failed"));
222 return file == null ? null : file.getAbsolutePath();
226 * Answers the executable file for the given hmmer command, or null if not
227 * found or not executable. The path to the executable is the command name
228 * prefixed by the hmmer binaries folder path, optionally with .exe appended.
231 * hmmer command short name, for example hmmbuild
233 * parent folder containing hmmer executables
236 public static File getExecutable(String cmd, String binaryPath)
238 File file = new File(binaryPath, cmd);
239 if (!file.canExecute())
241 file = new File(binaryPath, cmd + ".exe");
243 if (!file.canExecute())