3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.Annotation;
8 import jalview.datamodel.HiddenMarkovModel;
9 import jalview.datamodel.SequenceI;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.JvOptionPane;
12 import jalview.gui.Preferences;
13 import jalview.io.DataSourceType;
14 import jalview.io.FileParse;
15 import jalview.io.StockholmFile;
16 import jalview.util.MessageManager;
17 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
18 import jalview.ws.params.ArgumentI;
19 import jalview.ws.params.simple.BooleanOption;
21 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Scanner;
30 import javax.swing.JOptionPane;
32 public class HMMSearchThread implements Runnable
35 * feature settings from view that job was associated with
37 protected FeatureRendererSettings featureSettings = null;
40 * Object containing frequently used commands.
42 HMMERCommands cmds = new HMMERCommands();
46 HiddenMarkovModel hmm;
48 boolean newFrame, realign = false, trim = false;
50 Integer numberOfSequences = null;
56 List<ArgumentI> params;
60 File inputAlignmentTemp = null;
62 File inputTableTemp = null;
64 File databaseFile = null;
72 * Constructor for the HMMSearchThread. If create new frame is set to true, a
73 * new frame will be created.
76 * @param createNewFrame
78 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
82 newFrame = createNewFrame;
83 featureSettings = af.getFeatureRenderer().getSettings();
88 * Runs the HMMSearchThread: the data on the alignment or group is exported,
89 * then the command is executed in the command line and then the data is
90 * imported and displayed in a new frame (if true).
95 if (af.getSelectedHMM() == null)
97 JOptionPane.showMessageDialog(af,
98 MessageManager.getString("warn.no_selected_hmm"));
103 hmm = af.getSelectedHMM();
106 hmmSeq = af.getSelectedHMMSequence();
107 barID = System.currentTimeMillis();
108 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
110 cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
114 createTemporaryFiles();
115 } catch (IOException e2)
117 e2.printStackTrace();
121 cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile(), null);
122 } catch (IOException e1)
124 e1.printStackTrace();
128 boolean ran = runCommand();
131 JvOptionPane.showInternalMessageDialog(af,
132 MessageManager.getString("warn.hmmsearch_failed"));
135 } catch (IOException | InterruptedException e)
142 } catch (IOException | InterruptedException e)
144 // TODO Auto-generated catch block
148 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
154 * Creates temporary files for exporting and importing the data.
156 * @throws IOException
158 private void createTemporaryFiles() throws IOException
160 hmmTemp = File.createTempFile("hmm", ".hmm");
161 hmmTemp.deleteOnExit();
162 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
163 inputAlignmentTemp.deleteOnExit();
164 inputTableTemp = File.createTempFile("buffer", ".sto");
165 inputTableTemp.deleteOnExit();
169 * Executes the hmmsearch command in the command line.
172 * @throws IOException
173 * @throws InterruptedException
175 private boolean runCommand() throws IOException, InterruptedException
177 Boolean dbFound = false;
179 File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
180 if (!file.canExecute())
182 file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
184 if (!file.canExecute())
191 String command = cmds.HMMERFOLDER + "/hmmsearch -o "
192 + inputTableTemp.getAbsolutePath() + " -A "
193 + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE;
196 for (ArgumentI arg : params)
198 String name = arg.getName();
201 case "Number of Results to Return":
202 numberOfSequences = Integer.parseInt(arg.getValue());
204 case "Automatically Align Fetched Sequences":
205 if ("Automatically Align Fetched Sequences"
206 .equals(arg.getValue()))
211 case "Return Accessions":
212 if ("Return Accessions".equals(arg.getValue()))
217 case "Sequence E-value Cutoff":
218 command += "--incE " + arg.getValue() + cmds.SPACE;
220 case "Sequence Score Threshold":
221 command += "-incT " + arg.getValue() + cmds.SPACE;
223 case "Domain E-value Threshold":
224 command += "--incdomE " + arg.getValue() + cmds.SPACE;
226 case "Domain Score Threshold":
227 command += "--incdomT " + arg.getValue() + cmds.SPACE;
229 case "Trim Non-Matching Termini":
234 dbPath = arg.getValue();
235 if (!MessageManager.getString("label.this_alignment")
238 databaseFile = new File(dbPath);
245 if (dbFound == false || MessageManager.getString("label.this_alignment")
248 AlignmentI alignment = af.getViewport().getAlignment();
249 AlignmentI copy = new Alignment(alignment);
250 copy.getHMMConsensusSequences(true);
251 StockholmFile stoFile = new StockholmFile(copy);
252 stoFile.setSeqs(copy.getSequencesArray());
253 String alignmentString = stoFile.print();
254 databaseFile = File.createTempFile("database", ".sto");
255 databaseFile.deleteOnExit();
256 PrintWriter writer = new PrintWriter(databaseFile);
257 writer.print(alignmentString);
261 command += hmmTemp.getAbsolutePath() + cmds.SPACE
262 + databaseFile.getAbsolutePath();
263 return cmds.runCommand(command);
267 * Imports the data from the temporary file to which the output of hmmsearch
270 private void importData() throws IOException, InterruptedException
272 BufferedReader br = new BufferedReader(
273 new FileReader(inputAlignmentTemp));
274 if (br.readLine() == null)
276 JOptionPane.showMessageDialog(af,
277 MessageManager.getString("label.no_sequences_found"));
280 StockholmFile file = new StockholmFile(new FileParse(
281 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
282 seqs = file.getSeqsAsArray();
286 SequenceI[] hmmAndSeqs;
287 if (numberOfSequences != null && numberOfSequences < seqs.length)
289 hmmAndSeqs = new SequenceI[numberOfSequences + 1];
293 hmmAndSeqs = new SequenceI[seqs.length + 1];
295 hmmAndSeqs[0] = hmmSeq;
297 if (numberOfSequences != null && seqs.length > numberOfSequences)
299 System.arraycopy(seqs, 0, hmmAndSeqs, 1, numberOfSequences);
303 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
306 AlignmentI alignment = new Alignment(hmmAndSeqs);
307 AlignFrame frame = new AlignFrame(alignment, 1, 1);
308 frame.setSelectedHMMSequence(hmmSeq);
309 List<ArgumentI> alignArgs = new ArrayList<>();
312 alignArgs.add(new BooleanOption(
313 MessageManager.getString("label.trim_termini"),
314 MessageManager.getString("label.trim_termini_desc"), true,
317 HMMAlignThread hmmalign = new HMMAlignThread(frame, true, alignArgs);
318 hmmalign.hmmalignWaitTillComplete();
321 inputAlignmentTemp.delete();
322 inputTableTemp.delete();
326 * Runs hmmsearch, and waits for the results to be imported before continuing
328 public void hmmsearchWaitTillComplete()
330 Thread loader = new Thread(this);
333 while (loader.isAlive())
338 } catch (Exception ex)
345 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 < 5; i++)
355 line = br.readLine();
359 while (!" ------ inclusion threshold ------".equals(line)
362 Scanner scanner = new Scanner(line);
364 String str = scanner.next();
365 Annotation[] annots = new Annotation[seqs[index].getLength()];
366 for (Annotation an : annots)
368 an = new Annotation(Float.parseFloat(str));
370 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
372 annot.setScore(Double.parseDouble(str));
373 annot.setSequenceRef(seqs[index]);
374 seqs[index].addAlignmentAnnotation(annot);
377 line = br.readLine();