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;
53 List<ArgumentI> params;
57 File inputAlignmentTemp = null;
59 File inputTableTemp = null;
61 File databaseFile = null;
66 * Constructor for the HMMSearchThread. If create new frame is set to true, a
67 * new frame will be created.
70 * @param createNewFrame
72 public HMMSearchThread(AlignFrame af, boolean createNewFrame,
76 newFrame = createNewFrame;
77 featureSettings = af.getFeatureRenderer().getSettings();
82 * Runs the HMMSearchThread: the data on the alignment or group is exported,
83 * then the command is executed in the command line and then the data is
84 * imported and displayed in a new frame (if true).
89 if (af.getSelectedHMM() == null)
91 JOptionPane.showMessageDialog(af,
92 MessageManager.getString("warn.no_selected_hmm"));
97 hmm = af.getSelectedHMM();
100 hmmSeq = af.getSelectedHMMSequence();
101 barID = System.currentTimeMillis();
102 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
107 createTemporaryFiles();
108 } catch (IOException e2)
110 e2.printStackTrace();
114 exportData(null, null, hmm, hmmTemp.getAbsoluteFile(), null);
115 } catch (IOException e1)
117 e1.printStackTrace();
121 boolean ran = runCommand();
124 JvOptionPane.showInternalMessageDialog(af,
125 MessageManager.getString("warn.hmmsearch_failed"));
128 } catch (IOException | InterruptedException e)
135 } catch (IOException | InterruptedException e)
137 // TODO Auto-generated catch block
141 af.setProgressBar("", barID);
146 * Creates temporary files for exporting and importing the data.
148 * @throws IOException
150 private void createTemporaryFiles() throws IOException
152 hmmTemp = File.createTempFile("hmm", ".hmm");
153 hmmTemp.deleteOnExit();
154 inputAlignmentTemp = File.createTempFile("inputAl", ".sto");
155 inputAlignmentTemp.deleteOnExit();
156 inputTableTemp = File.createTempFile("buffer", ".sto");
157 inputTableTemp.deleteOnExit();
161 * Executes the hmmsearch command in the command line.
164 * @throws IOException
165 * @throws InterruptedException
167 private boolean runCommand() throws IOException, InterruptedException
169 String binaryPath = getCommandRoot(HMMSEARCH);
170 if (binaryPath == null)
175 String command = binaryPath + " -o "
176 + inputTableTemp.getAbsolutePath() + " -A "
177 + inputAlignmentTemp.getAbsolutePath() + SPACE;
179 boolean dbFound = false;
183 for (ArgumentI arg : params)
185 String name = arg.getName();
188 case "Number of Results to Return":
189 numberOfSequences = Integer.parseInt(arg.getValue());
191 case "Automatically Align Fetched Sequences":
192 if ("Automatically Align Fetched Sequences"
193 .equals(arg.getValue()))
198 case "Return Accessions":
199 if ("Return Accessions".equals(arg.getValue()))
204 case "Sequence E-value Cutoff":
205 command += "--incE " + arg.getValue() + SPACE;
207 case "Sequence Score Threshold":
208 command += "-incT " + arg.getValue() + SPACE;
210 case "Domain E-value Threshold":
211 command += "--incdomE " + arg.getValue() + SPACE;
213 case "Domain Score Threshold":
214 command += "--incdomT " + arg.getValue() + SPACE;
216 case "Trim Non-Matching Termini":
221 dbPath = arg.getValue();
222 if (!MessageManager.getString("label.this_alignment")
225 databaseFile = new File(dbPath);
232 if (!dbFound || MessageManager.getString("label.this_alignment")
235 AlignmentI alignment = af.getViewport().getAlignment();
236 AlignmentI copy = new Alignment(alignment);
237 copy.getHMMConsensusSequences(true);
238 StockholmFile stoFile = new StockholmFile(copy);
239 stoFile.setSeqs(copy.getSequencesArray());
240 String alignmentString = stoFile.print();
241 databaseFile = File.createTempFile("database", ".sto");
242 databaseFile.deleteOnExit();
243 PrintWriter writer = new PrintWriter(databaseFile);
244 writer.print(alignmentString);
248 command += hmmTemp.getAbsolutePath() + SPACE
249 + databaseFile.getAbsolutePath();
250 return runCommand(command);
254 * Imports the data from the temporary file to which the output of hmmsearch
257 private void importData() throws IOException, InterruptedException
259 BufferedReader br = new BufferedReader(
260 new FileReader(inputAlignmentTemp));
263 if (br.readLine() == null)
265 JOptionPane.showMessageDialog(af,
266 MessageManager.getString("label.no_sequences_found"));
269 StockholmFile file = new StockholmFile(new FileParse(
270 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
271 seqs = file.getSeqsAsArray();
275 SequenceI[] hmmAndSeqs;
276 if (numberOfSequences != null && numberOfSequences < seqs.length)
278 hmmAndSeqs = new SequenceI[numberOfSequences + 1];
282 hmmAndSeqs = new SequenceI[seqs.length + 1];
284 hmmAndSeqs[0] = hmmSeq;
286 if (numberOfSequences != null && seqs.length > numberOfSequences)
288 System.arraycopy(seqs, 0, hmmAndSeqs, 1, numberOfSequences);
292 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqs.length);
295 AlignmentI alignment = new Alignment(hmmAndSeqs);
296 AlignFrame frame = new AlignFrame(alignment, 1, 1);
297 frame.setSelectedHMMSequence(hmmSeq);
298 List<ArgumentI> alignArgs = new ArrayList<>();
301 alignArgs.add(new BooleanOption(
302 MessageManager.getString("label.trim_termini"),
303 MessageManager.getString("label.trim_termini_desc"), true,
306 HMMAlignThread hmmalign = new HMMAlignThread(frame, true, alignArgs);
307 hmmalign.hmmalignWaitTillComplete();
310 inputAlignmentTemp.delete();
311 inputTableTemp.delete();
322 * Runs hmmsearch, and waits for the results to be imported before continuing
324 public void hmmsearchWaitTillComplete()
326 Thread loader = new Thread(this);
329 while (loader.isAlive())
334 } catch (Exception ex)
341 void readTable() throws IOException
343 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
345 while (!line.startsWith("Query:"))
347 line = br.readLine();
349 for (int i = 0; i < 5; i++)
351 line = br.readLine();
355 while (!" ------ inclusion threshold ------".equals(line)
358 Scanner scanner = new Scanner(line);
360 String str = scanner.next(); // full sequence eValue score
361 float eValue = Float.parseFloat(str);
362 int seqLength = seqs[index].getLength();
363 Annotation[] annots = new Annotation[seqLength];
364 for (int j = 0; j < seqLength; j++)
366 annots[j] = new Annotation(eValue);
368 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
370 annot.setScore(Double.parseDouble(str));
371 annot.setSequenceRef(seqs[index]);
372 seqs[index].addAlignmentAnnotation(annot);
375 line = br.readLine();