3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.HiddenMarkovModel;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.gui.JvOptionPane;
11 import jalview.gui.Preferences;
12 import jalview.io.DataSourceType;
13 import jalview.io.FileParse;
14 import jalview.io.StockholmFile;
15 import jalview.util.MessageManager;
16 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
17 import jalview.ws.params.ArgumentI;
18 import jalview.ws.params.simple.BooleanOption;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.swing.JOptionPane;
28 public class HMMSearchThread implements Runnable
31 * feature settings from view that job was associated with
33 protected FeatureRendererSettings featureSettings = null;
36 * Object containing frequently used commands.
38 HMMERCommands cmds = new HMMERCommands();
42 HiddenMarkovModel hmm;
44 boolean newFrame, realign = false, trim = false;
46 Integer numberOfSequences = null;
52 List<ArgumentI> params;
56 File inputAlignmentTemp = null;
58 File inputTableTemp = null;
60 File databaseFile = null;
68 * Constructor for the HMMSearchThread. If create new frame is set to true, a
69 * new frame will be created.
72 * @param createNewFrame
74 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
78 newFrame = createNewFrame;
79 featureSettings = af.getFeatureRenderer().getSettings();
84 * Runs the HMMSearchThread: the data on the alignment or group is exported,
85 * then the command is executed in the command line and then the data is
86 * imported and displayed in a new frame (if true).
91 if (af.getSelectedHMM() == null)
93 JOptionPane.showMessageDialog(af,
94 MessageManager.getString("warn.no_selected_hmm"));
99 hmm = af.getSelectedHMM();
102 hmmSeq = af.getSelectedHMMSequence();
103 barID = System.currentTimeMillis();
104 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
106 cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
110 createTemporaryFiles();
111 } catch (IOException e2)
113 e2.printStackTrace();
117 cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile());
118 } catch (IOException e1)
120 e1.printStackTrace();
124 boolean ran = runCommand();
127 JvOptionPane.showInternalMessageDialog(af,
128 MessageManager.getString("warn.hmmsearch_failed"));
131 } catch (IOException | InterruptedException e)
138 } catch (IOException | InterruptedException e)
140 // TODO Auto-generated catch block
144 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
150 * Creates temporary files for exporting and importing the data.
152 * @throws IOException
154 private void createTemporaryFiles() throws IOException
156 hmmTemp = File.createTempFile("hmm", ".hmm");
157 hmmTemp.deleteOnExit();
158 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
159 inputAlignmentTemp.deleteOnExit();
160 inputTableTemp = File.createTempFile("buffer", ".sto");
161 inputTableTemp.deleteOnExit();
165 * Executes the hmmsearch command in the command line.
168 * @throws IOException
169 * @throws InterruptedException
171 private boolean runCommand() throws IOException, InterruptedException
173 Boolean dbFound = false;
175 File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
176 if (!file.canExecute())
178 file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
180 if (!file.canExecute())
187 String command = cmds.HMMERFOLDER + "/hmmsearch -o "
188 + inputTableTemp.getAbsolutePath() + " -A "
189 + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE;
192 for (ArgumentI arg : params)
194 String name = arg.getName();
197 case "Number of Results to Return":
198 numberOfSequences = Integer.parseInt(arg.getValue());
200 case "Automatically Align Fetched Sequences":
201 if ("Automatically Align Fetched Sequences"
202 .equals(arg.getValue()))
207 case "Return Accessions":
208 if ("Return Accessions".equals(arg.getValue()))
213 case "Sequence E-value Cutoff":
214 command += "--incE " + arg.getValue() + cmds.SPACE;
216 case "Sequence Score Threshold":
217 command += "-incT " + arg.getValue() + cmds.SPACE;
219 case "Domain E-value Threshold":
220 command += "--incdomE " + arg.getValue() + cmds.SPACE;
222 case "Domain Score Threshold":
223 command += "--incdomT " + arg.getValue() + cmds.SPACE;
225 case "Trim Non-Matching Termini":
230 dbPath = arg.getValue();
231 if (!MessageManager.getString("label.this_alignment")
234 databaseFile = new File(dbPath);
241 if (dbFound == false || MessageManager.getString("label.this_alignment")
244 AlignmentI alignment = af.getViewport().getAlignment();
245 AlignmentI copy = new Alignment(alignment);
246 copy.getHMMConsensusSequences(true);
247 StockholmFile stoFile = new StockholmFile(copy);
248 stoFile.setSeqs(copy.getSequencesArray());
249 String alignmentString = stoFile.print();
250 databaseFile = File.createTempFile("database", ".sto");
251 databaseFile.deleteOnExit();
252 PrintWriter writer = new PrintWriter(databaseFile);
253 writer.print(alignmentString);
257 command += hmmTemp.getAbsolutePath() + cmds.SPACE
258 + databaseFile.getAbsolutePath();
259 return cmds.runCommand(command);
263 * Imports the data from the temporary file to which the output of hmmsearch
266 private void importData() throws IOException, InterruptedException
268 StockholmFile file = new StockholmFile(new FileParse(
269 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
270 seqs = file.getSeqsAsArray();
274 SequenceI[] hmmAndSeqs;
275 if (numberOfSequences != null && numberOfSequences < seqs.length)
277 hmmAndSeqs = new SequenceI[numberOfSequences + 1];
281 hmmAndSeqs = new SequenceI[seqs.length + 1];
284 AlignmentAnnotation[] list = hmmSeq.getAnnotation();
285 for (AlignmentAnnotation annot : list)
287 if ("HMM".equals(annot.getCalcId()))
289 hmmSeq.removeAlignmentAnnotation(annot);
292 hmmSeq.setHasInfo(false);
293 hmmAndSeqs[0] = hmmSeq;
295 if (numberOfSequences != null && seqs.length > numberOfSequences)
297 System.arraycopy(seqs, 0, hmmAndSeqs, 1, numberOfSequences);
301 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
304 AlignmentI alignment = new Alignment(hmmAndSeqs);
305 AlignFrame frame = new AlignFrame(alignment, 1, 1);
306 frame.setSelectedHMMSequence(hmmSeq);
307 List<ArgumentI> alignArgs = new ArrayList<>();
310 alignArgs.add(new BooleanOption(
311 MessageManager.getString("label.trim_termini"),
312 MessageManager.getString("label.trim_termini_desc"), true,
315 HMMAlignThread hmmalign = new HMMAlignThread(frame, true, alignArgs);
316 hmmalign.hmmalignWaitTillComplete();
319 inputAlignmentTemp.delete();
320 inputTableTemp.delete();
324 * Runs hmmsearch, and waits for the results to be imported before continuing
326 public void hmmsearchWaitTillComplete()
328 Thread loader = new Thread(this);
331 while (loader.isAlive())
336 } catch (Exception ex)
343 public void readTable() throws IOException
347 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
349 while (!line.startsWith("Query:"))
351 line = br.readLine();
353 for (int i = 0; i < 4; i++)
360 while (!" ------ inclusion threshold ------".equals(line))
362 Scanner scanner = new Scanner(line);
364 String str = scanner.next();
365 AlignmentAnnotation annots = new AlignmentAnnotation("", "", null);
366 annots.setScore(Double.parseDouble(str));
367 seqs[index].addAlignmentAnnotation(annots);