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.ws.params.ArgumentI;
16 import jalview.ws.params.simple.BooleanOption;
18 import java.io.BufferedReader;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Scanner;
27 import javax.swing.JOptionPane;
29 public class HMMSearchThread extends HmmerCommand implements Runnable
31 static final String HMMSEARCH = "hmmsearch";
33 boolean realign = false;
37 int seqsToReturn = Integer.MAX_VALUE;
42 * Constructor for the HMMSearchThread
46 public HMMSearchThread(AlignFrame af, List<ArgumentI> args)
52 * Runs the HMMSearchThread: the data on the alignment or group is exported,
53 * then the command is executed in the command line and then the data is
54 * imported and displayed in a new frame
59 HiddenMarkovModel hmm = af.getSelectedHMM();
62 JOptionPane.showMessageDialog(af,
63 MessageManager.getString("warn.no_selected_hmm"));
67 SequenceI hmmSeq = af.getSelectedHMMSequence();
68 long msgId = System.currentTimeMillis();
69 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
74 File hmmFile = createTempFile("hmm", ".hmm");
75 File hitsAlignmentFile = createTempFile("hitAlignment", ".sto");
76 File searchOutputFile = createTempFile("searchOutput", ".sto");
78 exportHmm(hmm, hmmFile.getAbsoluteFile());
80 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile, hmmFile);
83 JvOptionPane.showInternalMessageDialog(af,
84 MessageManager.getString("warn.hmmsearch_failed"));
88 importData(hmmSeq, hitsAlignmentFile, hmmFile, searchOutputFile);
89 // TODO make realignment of search results a step at this level
90 // and make it conditional on this.realign
91 } catch (IOException | InterruptedException e)
97 af.setProgressBar("", msgId);
102 * Executes an hmmsearch with the given hmm as input. The database to be
103 * searched is a local file as specified by the 'Database' parameter, or the
104 * current alignment (written to file) if none is specified.
106 * @param searchOutputFile
107 * @param hitsAlignmentFile
111 * @throws IOException
113 private boolean runCommand(File searchOutputFile, File hitsAlignmentFile,
114 File hmmFile) throws IOException
116 String command = getCommandPath(HMMSEARCH);
122 List<String> args = new ArrayList<>();
125 args.add(searchOutputFile.getAbsolutePath());
127 args.add(hitsAlignmentFile.getAbsolutePath());
129 boolean dbFound = false;
131 File databaseFile = null;
135 for (ArgumentI arg : params)
137 String name = arg.getName();
138 if (MessageManager.getString("label.number_of_results")
141 seqsToReturn = Integer.parseInt(arg.getValue());
143 else if (MessageManager.getString("label.auto_align_seqs")
146 realign = true; // TODO: not used
148 else if (MessageManager.getString("label.use_accessions")
153 else if (MessageManager.getString("label.seq_e_value").equals(name))
156 args.add(arg.getValue());
158 else if (MessageManager.getString("label.seq_score").equals(name))
161 args.add(arg.getValue());
163 else if (MessageManager.getString("label.dom_e_value_desc")
166 args.add("--incdomE");
167 args.add(arg.getValue());
169 else if (MessageManager.getString("label.dom_score").equals(name))
171 args.add("--incdomT");
172 args.add(arg.getValue());
174 else if (MessageManager.getString("label.trim_termini")
179 else if (MessageManager.getString("label.database").equals(name))
182 dbPath = arg.getValue();
183 if (!MessageManager.getString("label.this_alignment")
186 databaseFile = new File(dbPath);
192 if (!dbFound || MessageManager.getString("label.this_alignment")
195 AlignmentI alignment = af.getViewport().getAlignment();
196 AlignmentI copy = new Alignment(alignment);
197 List<SequenceI> hmms = copy.getHMMConsensusSequences();
198 for (SequenceI seq : hmms)
200 copy.deleteSequence(seq);
202 StockholmFile stoFile = new StockholmFile(copy);
203 stoFile.setSeqs(copy.getSequencesArray());
204 String alignmentString = stoFile.print();
205 databaseFile = createTempFile("database", ".sto");
206 PrintWriter writer = new PrintWriter(databaseFile);
207 writer.print(alignmentString);
211 args.add(hmmFile.getAbsolutePath());
212 args.add(databaseFile.getAbsolutePath());
214 return runCommand(args);
218 * Imports the data from the temporary file to which the output of hmmsearch
223 private void importData(SequenceI hmmSeq, File inputAlignmentTemp,
224 File hmmTemp, File searchOutputFile)
225 throws IOException, InterruptedException
227 BufferedReader br = new BufferedReader(
228 new FileReader(inputAlignmentTemp));
231 if (br.readLine() == null)
233 JOptionPane.showMessageDialog(af,
234 MessageManager.getString("label.no_sequences_found"));
237 StockholmFile file = new StockholmFile(new FileParse(
238 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
239 seqs = file.getSeqsAsArray();
241 readTable(searchOutputFile);
243 int seqCount = Math.min(seqs.length, seqsToReturn);
244 SequenceI[] hmmAndSeqs = new SequenceI[seqCount + 1];
245 hmmAndSeqs[0] = hmmSeq;
246 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqCount);
248 AlignmentI alignment = new Alignment(hmmAndSeqs);
249 AlignFrame frame = new AlignFrame(alignment, 1, 1);
250 frame.setSelectedHMMSequence(hmmSeq);
251 List<ArgumentI> alignArgs = new ArrayList<>();
254 alignArgs.add(new BooleanOption(
255 MessageManager.getString("label.trim_termini"),
256 MessageManager.getString("label.trim_termini_desc"), true,
259 HMMAlignThread hmmalign = new HMMAlignThread(frame, alignArgs);
260 hmmalign.hmmalignWaitTillComplete();
263 inputAlignmentTemp.delete();
264 searchOutputFile.delete();
275 * Runs hmmsearch, and waits for the results to be imported before continuing
277 public void hmmsearchWaitTillComplete()
279 Thread loader = new Thread(this);
282 while (loader.isAlive())
287 } catch (Exception ex)
294 void readTable(File inputTableTemp) throws IOException
296 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
298 while (!line.startsWith("Query:"))
300 line = br.readLine();
302 for (int i = 0; i < 5; i++)
304 line = br.readLine();
308 while (!" ------ inclusion threshold ------".equals(line)
311 Scanner scanner = new Scanner(line);
313 String str = scanner.next(); // full sequence eValue score
314 float eValue = Float.parseFloat(str);
315 int seqLength = seqs[index].getLength();
316 Annotation[] annots = new Annotation[seqLength];
317 for (int j = 0; j < seqLength; j++)
319 annots[j] = new Annotation(eValue);
321 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
323 annot.setScore(Double.parseDouble(str));
324 annot.setSequenceRef(seqs[index]);
325 seqs[index].addAlignmentAnnotation(annot);
328 line = br.readLine();