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.util.ArrayList;
23 import java.util.List;
25 import javax.swing.JOptionPane;
27 public class HMMSearchThread implements Runnable
30 * feature settings from view that job was associated with
32 protected FeatureRendererSettings featureSettings = null;
35 * Object containing frequently used commands.
37 HMMERCommands cmds = new HMMERCommands();
41 HiddenMarkovModel hmm;
43 boolean newFrame, realign = false, trim = false;
45 Integer numberOfSequences = null;
51 List<ArgumentI> params;
55 File inputAlignmentTemp = null;
57 File inputTableTemp = null;
59 File databaseFile = null;
67 * Constructor for the HMMSearchThread. If create new frame is set to true, a
68 * new frame will be created.
71 * @param createNewFrame
73 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
77 newFrame = createNewFrame;
78 featureSettings = af.getFeatureRenderer().getSettings();
83 * Runs the HMMSearchThread: the data on the alignment or group is exported,
84 * then the command is executed in the command line and then the data is
85 * imported and displayed in a new frame (if true).
90 if (af.getSelectedHMM() == null)
92 JOptionPane.showMessageDialog(af,
93 MessageManager.getString("warn.no_selected_hmm"));
98 hmm = af.getSelectedHMM();
101 hmmSeq = af.getSelectedHMMSequence();
102 barID = System.currentTimeMillis();
103 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
105 cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
109 createTemporaryFiles();
110 } catch (IOException e2)
112 e2.printStackTrace();
116 cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile());
117 } catch (IOException e1)
119 e1.printStackTrace();
123 boolean ran = runCommand();
126 JvOptionPane.showInternalMessageDialog(af,
127 MessageManager.getString("warn.hmmsearch_failed"));
130 } catch (IOException | InterruptedException e)
137 } catch (IOException | InterruptedException e)
139 // TODO Auto-generated catch block
143 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
149 * Creates temporary files for exporting and importing the data.
151 * @throws IOException
153 private void createTemporaryFiles() throws IOException
155 hmmTemp = File.createTempFile("hmm", ".hmm");
156 hmmTemp.deleteOnExit();
157 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
158 inputAlignmentTemp.deleteOnExit();
159 inputTableTemp = File.createTempFile("buffer", ".sto");
160 inputTableTemp.deleteOnExit();
161 databaseFile = new File("D:/Thomas/uniref50.fasta");
165 * Executes the hmmsearch command in the command line.
168 * @throws IOException
169 * @throws InterruptedException
171 private boolean runCommand() throws IOException, InterruptedException
173 File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
174 if (!file.canExecute())
176 file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
178 if (!file.canExecute())
185 String command = cmds.HMMERFOLDER + "/hmmsearch -o "
186 + inputTableTemp.getAbsolutePath() + " -A "
187 + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE;
190 for (ArgumentI arg : params)
192 String name = arg.getName();
195 case "Number of Results to Return":
196 numberOfSequences = Integer.parseInt(arg.getValue());
198 case "Automatically Align Fetched Sequences":
199 if ("Automatically Align Fetched Sequences"
200 .equals(arg.getValue()))
205 case "Return Accessions":
206 if ("Return Accessions".equals(arg.getValue()))
211 case "Sequence E-value Cutoff":
212 command += "--incE " + arg.getValue() + cmds.SPACE;
214 case "Sequence Score Threshold":
215 command += "-incT " + arg.getValue() + cmds.SPACE;
217 case "Domain E-value Threshold":
218 command += "--incdomE " + arg.getValue() + cmds.SPACE;
220 case "Domain Score Threshold":
221 command += "--incdomT " + arg.getValue() + cmds.SPACE;
223 case "Trim Non-Matching Termini":
232 command += hmmTemp.getAbsolutePath() + cmds.SPACE
233 + databaseFile.getAbsolutePath();
234 return cmds.runCommand(command);
238 * Imports the data from the temporary file to which the output of hmmsearch
241 private void importData() throws IOException, InterruptedException
243 StockholmFile file = new StockholmFile(new FileParse(
244 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
245 seqs = file.getSeqsAsArray();
249 SequenceI[] hmmAndSeqs;
250 if (numberOfSequences != null && numberOfSequences < seqs.length)
252 hmmAndSeqs = new SequenceI[numberOfSequences + 1];
256 hmmAndSeqs = new SequenceI[seqs.length + 1];
259 AlignmentAnnotation[] list = hmmSeq.getAnnotation();
260 for (AlignmentAnnotation annot : list)
262 if ("HMM".equals(annot.getCalcId()))
264 hmmSeq.removeAlignmentAnnotation(annot);
267 hmmSeq.setHasInfo(false);
268 hmmAndSeqs[0] = hmmSeq;
270 if (numberOfSequences != null && seqs.length > numberOfSequences)
272 System.arraycopy(seqs, 0, hmmAndSeqs, 1, numberOfSequences);
276 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
279 AlignmentI alignment = new Alignment(hmmAndSeqs);
280 AlignFrame frame = new AlignFrame(alignment, 1, 1);
281 frame.setSelectedHMMSequence(hmmSeq);
282 frame.getViewport().initInformation();
283 List<ArgumentI> alignArgs = new ArrayList<>();
286 alignArgs.add(new BooleanOption(
287 MessageManager.getString("label.trim_termini"),
288 MessageManager.getString("label.trim_termini_desc"), true,
291 HMMAlignThread hmmalign = new HMMAlignThread(frame, true, alignArgs);
292 hmmalign.hmmalignWaitTillComplete();
295 inputAlignmentTemp.delete();
296 inputTableTemp.delete();
300 * Runs hmmsearch, and waits for the results to be imported before continuing
302 public void hmmsearchWaitTillComplete()
304 Thread loader = new Thread(this);
307 while (loader.isAlive())
312 } catch (Exception ex)
319 public void readTable() throws IOException
323 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
325 while (!line.startsWith("Query:"))
327 line = br.readLine();
329 for (int i = 0; i < 4; i++)
336 while (!" ------ inclusion threshold ------".equals(line))
338 Scanner scanner = new Scanner(line);
340 String str = scanner.next();
341 AlignmentAnnotation annots = new AlignmentAnnotation("", "", null);
342 annots.setScore(Double.parseDouble(str));
343 seqs[index].addAlignmentAnnotation(annots);