3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.HiddenMarkovModel;
7 import jalview.datamodel.SequenceI;
8 import jalview.gui.AlignFrame;
9 import jalview.gui.JvOptionPane;
10 import jalview.gui.Preferences;
11 import jalview.io.DataSourceType;
12 import jalview.io.FileParse;
13 import jalview.io.StockholmFile;
14 import jalview.util.MessageManager;
15 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
16 import jalview.ws.params.ArgumentI;
17 import jalview.ws.params.simple.BooleanOption;
19 import java.io.BufferedReader;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.swing.JOptionPane;
29 public class HMMSearchThread implements Runnable
32 * feature settings from view that job was associated with
34 protected FeatureRendererSettings featureSettings = null;
37 * Object containing frequently used commands.
39 HMMERCommands cmds = new HMMERCommands();
43 HiddenMarkovModel hmm;
45 boolean newFrame, realign = false, trim = false;
47 Integer numberOfSequences = null;
53 List<ArgumentI> params;
57 File inputAlignmentTemp = null;
59 File inputTableTemp = null;
61 File databaseFile = null;
69 * Constructor for the HMMSearchThread. If create new frame is set to true, a
70 * new frame will be created.
73 * @param createNewFrame
75 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
79 newFrame = createNewFrame;
80 featureSettings = af.getFeatureRenderer().getSettings();
85 * Runs the HMMSearchThread: the data on the alignment or group is exported,
86 * then the command is executed in the command line and then the data is
87 * imported and displayed in a new frame (if true).
92 if (af.getSelectedHMM() == null)
94 JOptionPane.showMessageDialog(af,
95 MessageManager.getString("warn.no_selected_hmm"));
100 hmm = af.getSelectedHMM();
103 hmmSeq = af.getSelectedHMMSequence();
104 barID = System.currentTimeMillis();
105 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
107 cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
111 createTemporaryFiles();
112 } catch (IOException e2)
114 e2.printStackTrace();
118 cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile());
119 } catch (IOException e1)
121 e1.printStackTrace();
125 boolean ran = runCommand();
128 JvOptionPane.showInternalMessageDialog(af,
129 MessageManager.getString("warn.hmmsearch_failed"));
132 } catch (IOException | InterruptedException e)
139 } catch (IOException | InterruptedException e)
141 // TODO Auto-generated catch block
145 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
151 * Creates temporary files for exporting and importing the data.
153 * @throws IOException
155 private void createTemporaryFiles() throws IOException
157 hmmTemp = File.createTempFile("hmm", ".hmm");
158 hmmTemp.deleteOnExit();
159 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
160 inputAlignmentTemp.deleteOnExit();
161 inputTableTemp = File.createTempFile("buffer", ".sto");
162 inputTableTemp.deleteOnExit();
166 * Executes the hmmsearch command in the command line.
169 * @throws IOException
170 * @throws InterruptedException
172 private boolean runCommand() throws IOException, InterruptedException
174 Boolean dbFound = false;
176 File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
177 if (!file.canExecute())
179 file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
181 if (!file.canExecute())
188 String command = cmds.HMMERFOLDER + "/hmmsearch -o "
189 + inputTableTemp.getAbsolutePath() + " -A "
190 + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE;
193 for (ArgumentI arg : params)
195 String name = arg.getName();
198 case "Number of Results to Return":
199 numberOfSequences = Integer.parseInt(arg.getValue());
201 case "Automatically Align Fetched Sequences":
202 if ("Automatically Align Fetched Sequences"
203 .equals(arg.getValue()))
208 case "Return Accessions":
209 if ("Return Accessions".equals(arg.getValue()))
214 case "Sequence E-value Cutoff":
215 command += "--incE " + arg.getValue() + cmds.SPACE;
217 case "Sequence Score Threshold":
218 command += "-incT " + arg.getValue() + cmds.SPACE;
220 case "Domain E-value Threshold":
221 command += "--incdomE " + arg.getValue() + cmds.SPACE;
223 case "Domain Score Threshold":
224 command += "--incdomT " + arg.getValue() + cmds.SPACE;
226 case "Trim Non-Matching Termini":
231 dbPath = arg.getValue();
232 if (!MessageManager.getString("label.this_alignment")
235 databaseFile = new File(dbPath);
242 if (dbFound == false || MessageManager.getString("label.this_alignment")
245 AlignmentI alignment = af.getViewport().getAlignment();
246 AlignmentI copy = new Alignment(alignment);
247 copy.getHMMConsensusSequences(true);
248 StockholmFile stoFile = new StockholmFile(copy);
249 stoFile.setSeqs(copy.getSequencesArray());
250 String alignmentString = stoFile.print();
251 databaseFile = File.createTempFile("database", ".sto");
252 databaseFile.deleteOnExit();
253 PrintWriter writer = new PrintWriter(databaseFile);
254 writer.print(alignmentString);
258 command += hmmTemp.getAbsolutePath() + cmds.SPACE
259 + databaseFile.getAbsolutePath();
260 return cmds.runCommand(command);
264 * Imports the data from the temporary file to which the output of hmmsearch
267 private void importData() throws IOException, InterruptedException
269 BufferedReader br = new BufferedReader(
270 new FileReader(inputAlignmentTemp));
271 if (br.readLine() == null)
273 JOptionPane.showMessageDialog(af,
274 MessageManager.getString("label.no_sequences_found"));
277 StockholmFile file = new StockholmFile(new FileParse(
278 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
279 seqs = file.getSeqsAsArray();
283 SequenceI[] hmmAndSeqs;
284 if (numberOfSequences != null && numberOfSequences < seqs.length)
286 hmmAndSeqs = new SequenceI[numberOfSequences + 1];
290 hmmAndSeqs = new SequenceI[seqs.length + 1];
292 hmmAndSeqs[0] = hmmSeq;
294 if (numberOfSequences != null && seqs.length > numberOfSequences)
296 System.arraycopy(seqs, 0, hmmAndSeqs, 1, numberOfSequences);
300 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
303 AlignmentI alignment = new Alignment(hmmAndSeqs);
304 AlignFrame frame = new AlignFrame(alignment, 1, 1);
305 frame.setSelectedHMMSequence(hmmSeq);
306 List<ArgumentI> alignArgs = new ArrayList<>();
309 alignArgs.add(new BooleanOption(
310 MessageManager.getString("label.trim_termini"),
311 MessageManager.getString("label.trim_termini_desc"), true,
314 HMMAlignThread hmmalign = new HMMAlignThread(frame, true, alignArgs);
315 hmmalign.hmmalignWaitTillComplete();
318 inputAlignmentTemp.delete();
319 inputTableTemp.delete();
323 * Runs hmmsearch, and waits for the results to be imported before continuing
325 public void hmmsearchWaitTillComplete()
327 Thread loader = new Thread(this);
330 while (loader.isAlive())
335 } catch (Exception ex)
342 public void readTable() throws IOException
346 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
348 while (!line.startsWith("Query:"))
350 line = br.readLine();
352 for (int i = 0; i < 4; i++)
359 while (!" ------ inclusion threshold ------".equals(line))
361 Scanner scanner = new Scanner(line);
363 String str = scanner.next();
364 AlignmentAnnotation annots = new AlignmentAnnotation("", "", null);
365 annots.setScore(Double.parseDouble(str));
366 seqs[index].addAlignmentAnnotation(annots);