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;
20 import java.io.IOException;
21 import java.util.List;
23 import javax.swing.JOptionPane;
25 public class HMMSearchThread implements Runnable
28 * feature settings from view that job was associated with
30 protected FeatureRendererSettings featureSettings = null;
33 * Object containing frequently used commands.
35 HMMERCommands cmds = new HMMERCommands();
39 HiddenMarkovModel hmm;
47 List<ArgumentI> params;
51 File inputAlignmentTemp = null;
53 File inputTableTemp = null;
55 File databaseFile = null;
61 * Constructor for the HMMSearchThread. If create new frame is set to true, a
62 * new frame will be created.
65 * @param createNewFrame
67 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
71 newFrame = createNewFrame;
72 featureSettings = af.getFeatureRenderer().getSettings();
77 * Runs the HMMSearchThread: the data on the alignment or group is exported,
78 * then the command is executed in the command line and then the data is
79 * imported and displayed in a new frame (if true).
84 if (af.getSelectedHMM() == null)
86 JOptionPane.showMessageDialog(af,
87 MessageManager.getString("warn.no_selected_hmm"));
92 hmm = af.getSelectedHMM();
95 hmmSeq = af.getSelectedHMMSequence();
96 barID = System.currentTimeMillis();
97 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
99 cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
103 createTemporaryFiles();
104 } catch (IOException e2)
106 e2.printStackTrace();
110 cmds.exportData(null, null, hmm, hmmTemp.getAbsoluteFile());
111 } catch (IOException e1)
113 e1.printStackTrace();
117 boolean ran = runCommand();
120 JvOptionPane.showInternalMessageDialog(af,
121 MessageManager.getString("warn.hmmsearch_failed"));
124 } catch (IOException | InterruptedException e)
131 } catch (IOException | InterruptedException e)
133 // TODO Auto-generated catch block
137 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
143 * Creates temporary files for exporting and importing the data.
145 * @throws IOException
147 private void createTemporaryFiles() throws IOException
149 hmmTemp = File.createTempFile("hmm", ".hmm");
150 hmmTemp.deleteOnExit();
151 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
152 inputAlignmentTemp.deleteOnExit();
153 inputTableTemp = File.createTempFile("buffer", ".sto");
154 inputTableTemp.deleteOnExit();
155 databaseFile = new File("D:/Thomas/uniref50.fasta");
159 * Executes the hmmsearch command in the command line.
162 * @throws IOException
163 * @throws InterruptedException
165 private boolean runCommand() throws IOException, InterruptedException
167 File file = new File(cmds.HMMERFOLDER + "/hmmsearch");
168 if (!file.canExecute())
170 file = new File(cmds.HMMERFOLDER + "/hmmsearch.exe");
172 if (!file.canExecute())
178 String command = cmds.HMMERFOLDER + "/hmmsearch -o "
179 + inputTableTemp.getAbsolutePath() + " -A "
180 + inputAlignmentTemp.getAbsolutePath() + cmds.SPACE
181 + hmmTemp.getAbsolutePath() + cmds.SPACE
182 + databaseFile.getAbsolutePath();
183 return cmds.runCommand(command);
187 * Imports the data from the temporary file to which the output of hmmsearch
190 private void importData() throws IOException, InterruptedException
192 StockholmFile file = new StockholmFile(new FileParse(
193 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
194 seqs = file.getSeqsAsArray();
198 SequenceI[] hmmAndSeqs = new SequenceI[seqs.length + 1];
199 AlignmentAnnotation[] list = hmmSeq.getAnnotation();
200 for (AlignmentAnnotation annot : list)
202 if ("HMM annotation".equals(annot.getCalcId()))
204 hmmSeq.removeAlignmentAnnotation(annot);
207 hmmSeq.setHasInfo(false);
208 hmmAndSeqs[0] = hmmSeq;
209 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
210 AlignmentI alignment = new Alignment(hmmAndSeqs);
211 AlignFrame frame = new AlignFrame(alignment, 1, 1);
212 frame.setSelectedHMMSequence(hmmSeq);
213 frame.getViewport().initInformation();
214 HMMAlignThread hmmalign = new HMMAlignThread(frame, true);
215 hmmalign.hmmalignWaitTillComplete();
218 inputAlignmentTemp.delete();
219 inputTableTemp.delete();
223 * Runs hmmsearch, and waits for the results to be imported before continuing
225 public void hmmsearchWaitTillComplete()
227 Thread loader = new Thread(this);
230 while (loader.isAlive())
235 } catch (Exception ex)
242 public void readTable() throws IOException
246 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
248 while (!line.startsWith("Query:"))
250 line = br.readLine();
252 for (int i = 0; i < 4; i++)
259 while (!" ------ inclusion threshold ------".equals(line))
261 Scanner scanner = new Scanner(line);
263 String str = scanner.next();
264 AlignmentAnnotation annots = new AlignmentAnnotation("", "", null);
265 annots.setScore(Double.parseDouble(str));
266 seqs[index].addAlignmentAnnotation(annots);