3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.Annotation;
7 import jalview.datamodel.HiddenMarkovModel;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.gui.JvOptionPane;
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;
26 import java.util.Scanner;
28 import javax.swing.JOptionPane;
30 public class HMMSearchThread extends HmmerCommand implements Runnable
32 static final String HMMSEARCH = "hmmsearch";
35 * feature settings from view that job was associated with
37 protected FeatureRendererSettings featureSettings = null;
39 HiddenMarkovModel hmm;
43 boolean realign = false;
47 Integer numberOfSequences = null;
51 List<ArgumentI> params;
55 File inputAlignmentTemp = null;
57 File inputTableTemp = null;
59 File databaseFile = null;
64 * Constructor for the HMMSearchThread. If create new frame is set to true, a
65 * new frame will be created.
68 * @param createNewFrame
70 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
74 newFrame = createNewFrame;
75 featureSettings = af.getFeatureRenderer().getSettings();
80 * Runs the HMMSearchThread: the data on the alignment or group is exported,
81 * then the command is executed in the command line and then the data is
82 * imported and displayed in a new frame (if true).
87 if (af.getSelectedHMM() == null)
89 JOptionPane.showMessageDialog(af,
90 MessageManager.getString("warn.no_selected_hmm"));
95 hmm = af.getSelectedHMM();
98 SequenceI hmmSeq = af.getSelectedHMMSequence();
99 barID = System.currentTimeMillis();
100 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
105 createTemporaryFiles();
106 } catch (IOException e2)
108 e2.printStackTrace();
112 exportData(null, null, hmm, hmmTemp.getAbsoluteFile(), null);
113 } catch (IOException e1)
115 e1.printStackTrace();
119 boolean ran = runCommand();
122 JvOptionPane.showInternalMessageDialog(af,
123 MessageManager.getString("warn.hmmsearch_failed"));
126 } catch (IOException | InterruptedException e)
133 } catch (IOException | InterruptedException e)
135 // TODO Auto-generated catch block
139 af.setProgressBar("", barID);
144 * Creates temporary files for exporting and importing the data.
146 * @throws IOException
148 private void createTemporaryFiles() throws IOException
150 hmmTemp = File.createTempFile("hmm", ".hmm");
151 hmmTemp.deleteOnExit();
152 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
153 inputAlignmentTemp.deleteOnExit();
154 inputTableTemp = File.createTempFile("buffer", ".sto");
155 inputTableTemp.deleteOnExit();
159 * Executes the hmmsearch command in the command line.
162 * @throws IOException
163 * @throws InterruptedException
165 private boolean runCommand() throws IOException, InterruptedException
167 String binaryPath = getCommandRoot(HMMSEARCH);
168 if (binaryPath == null)
173 String command = binaryPath + " -o "
174 + inputTableTemp.getAbsolutePath() + " -A "
175 + inputAlignmentTemp.getAbsolutePath() + SPACE;
177 boolean dbFound = false;
181 for (ArgumentI arg : params)
183 String name = arg.getName();
186 case "Number of Results to Return":
187 numberOfSequences = Integer.parseInt(arg.getValue());
189 case "Automatically Align Fetched Sequences":
190 if ("Automatically Align Fetched Sequences"
191 .equals(arg.getValue()))
196 case "Return Accessions":
197 if ("Return Accessions".equals(arg.getValue()))
202 case "Sequence E-value Cutoff":
203 command += "--incE " + arg.getValue() + SPACE;
205 case "Sequence Score Threshold":
206 command += "-incT " + arg.getValue() + SPACE;
208 case "Domain E-value Threshold":
209 command += "--incdomE " + arg.getValue() + SPACE;
211 case "Domain Score Threshold":
212 command += "--incdomT " + arg.getValue() + SPACE;
214 case "Trim Non-Matching Termini":
219 dbPath = arg.getValue();
220 if (!MessageManager.getString("label.this_alignment")
223 databaseFile = new File(dbPath);
230 if (!dbFound || MessageManager.getString("label.this_alignment")
233 AlignmentI alignment = af.getViewport().getAlignment();
234 AlignmentI copy = new Alignment(alignment);
235 List<SequenceI> hmms = copy.getHMMConsensusSequences();
236 for (SequenceI seq : hmms)
238 copy.deleteSequence(seq);
240 StockholmFile stoFile = new StockholmFile(copy);
241 stoFile.setSeqs(copy.getSequencesArray());
242 String alignmentString = stoFile.print();
243 databaseFile = File.createTempFile("database", ".sto");
244 databaseFile.deleteOnExit();
245 PrintWriter writer = new PrintWriter(databaseFile);
246 writer.print(alignmentString);
250 command += hmmTemp.getAbsolutePath() + SPACE
251 + databaseFile.getAbsolutePath();
252 return runCommand(command);
256 * Imports the data from the temporary file to which the output of hmmsearch
261 private void importData(SequenceI hmmSeq)
262 throws IOException, InterruptedException
264 BufferedReader br = new BufferedReader(
265 new FileReader(inputAlignmentTemp));
268 if (br.readLine() == null)
270 JOptionPane.showMessageDialog(af,
271 MessageManager.getString("label.no_sequences_found"));
274 StockholmFile file = new StockholmFile(new FileParse(
275 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
276 seqs = file.getSeqsAsArray();
280 SequenceI[] hmmAndSeqs;
281 if (numberOfSequences != null && numberOfSequences < seqs.length)
283 hmmAndSeqs = new SequenceI[numberOfSequences + 1];
287 hmmAndSeqs = new SequenceI[seqs.length + 1];
289 hmmAndSeqs[0] = hmmSeq;
291 if (numberOfSequences != null && seqs.length > numberOfSequences)
293 System.arraycopy(seqs, 0, hmmAndSeqs, 1, numberOfSequences);
297 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
300 AlignmentI alignment = new Alignment(hmmAndSeqs);
301 AlignFrame frame = new AlignFrame(alignment, 1, 1);
302 frame.setSelectedHMMSequence(hmmSeq);
303 List<ArgumentI> alignArgs = new ArrayList<>();
306 alignArgs.add(new BooleanOption(
307 MessageManager.getString("label.trim_termini"),
308 MessageManager.getString("label.trim_termini_desc"), true,
311 HMMAlignThread hmmalign = new HMMAlignThread(frame, true, alignArgs);
312 hmmalign.hmmalignWaitTillComplete();
315 inputAlignmentTemp.delete();
316 inputTableTemp.delete();
327 * Runs hmmsearch, and waits for the results to be imported before continuing
329 public void hmmsearchWaitTillComplete()
331 Thread loader = new Thread(this);
334 while (loader.isAlive())
339 } catch (Exception ex)
346 void readTable() throws IOException
348 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
350 while (!line.startsWith("Query:"))
352 line = br.readLine();
354 for (int i = 0; i < 5; i++)
356 line = br.readLine();
360 while (!" ------ inclusion threshold ------".equals(line)
363 Scanner scanner = new Scanner(line);
365 String str = scanner.next(); // full sequence eValue score
366 float eValue = Float.parseFloat(str);
367 int seqLength = seqs[index].getLength();
368 Annotation[] annots = new Annotation[seqLength];
369 for (int j = 0; j < seqLength; j++)
371 annots[j] = new Annotation(eValue);
373 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
375 annot.setScore(Double.parseDouble(str));
376 annot.setSequenceRef(seqs[index]);
377 seqs[index].addAlignmentAnnotation(annot);
380 line = br.readLine();