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 * Adds any HMM sequences removed before submitting the alignment as a job
186 * back into the alignment.
190 public void addHMMConsensusSequences(AlignFrame af)
192 AlignmentI al = af.getViewport().getAlignment();
193 if (hmmSeqs == null || hmmSeqs.size() < 1)
197 for (SequenceI seq : hmmSeqs)
199 Integer position = seq.getPreviousPosition();
200 al.getSequences().add(position, seq);
202 af.getViewport().setAlignment(al);
203 af.alignPanel.adjustAnnotationHeight();
204 af.getViewport().updateSequenceIdColours();
205 af.buildSortByAnnotationScoresMenu();
209 * Returns the list of HMM sequences removed
213 public List<SequenceI> getHmmSeqs()
219 * Sets the list of removed HMM sequences
223 public void setHmmSeqs(List<SequenceI> hmmSeqs)
225 this.hmmSeqs = hmmSeqs;
229 * Answers the full path to the given hmmer executable, or null if file cannot
230 * be found or is not executable
233 * command short name e.g. hmmalign
236 protected String getCommandRoot(String cmd)
238 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
239 File file = getExecutable(cmd, binariesFolder);
240 if (file == null && af != null)
242 JvOptionPane.showInternalMessageDialog(af,
243 MessageManager.getString("warn.hmm_command_failed"));
246 return file == null ? null : file.getAbsolutePath();
250 * Answers the executable file for the given hmmer command, or null if not
251 * found or not executable. The path to the executable is the command name
252 * prefixed by the hmmer binaries folder path, optionally with .exe appended.
255 * hmmer command short name, for example hmmbuild
257 * parent folder containing hmmer executables
260 public static File getExecutable(String cmd, String binaryPath)
262 File file = new File(binaryPath, cmd);
263 if (!file.canExecute())
265 file = new File(binaryPath, cmd + ".exe");
267 if (!file.canExecute())