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.FileUtils;
15 import jalview.util.MessageManager;
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.util.ArrayList;
24 import java.util.List;
25 import java.util.Scanner;
27 import javax.swing.JOptionPane;
29 public class HMMSearch extends HmmerCommand
31 static final String HMMSEARCH = "hmmsearch";
33 boolean realign = false;
37 int seqsToReturn = Integer.MAX_VALUE;
42 * Constructor for the HMMSearchThread
46 public HMMSearch(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. Call this method directly to execute
55 * synchronously, or via start() in a new Thread for asynchronously.
60 HiddenMarkovModel hmm = af.getSelectedHMM();
63 JOptionPane.showMessageDialog(af,
64 MessageManager.getString("warn.no_selected_hmm"));
68 SequenceI hmmSeq = af.getSelectedHMMSequence();
69 long msgId = System.currentTimeMillis();
70 af.setProgressBar(MessageManager.getString("status.running_hmmsearch"),
75 File hmmFile = FileUtils.createTempFile("hmm", ".hmm");
76 File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment",
78 File searchOutputFile = FileUtils.createTempFile("searchOutput",
81 exportHmm(hmm, hmmFile.getAbsoluteFile());
83 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile, hmmFile);
86 JvOptionPane.showInternalMessageDialog(af,
87 MessageManager.getString("warn.hmmsearch_failed"));
91 importData(hmmSeq, hitsAlignmentFile, hmmFile, searchOutputFile);
92 // TODO make realignment of search results a step at this level
93 // and make it conditional on this.realign
94 } catch (IOException | InterruptedException e)
100 af.setProgressBar("", msgId);
105 * Executes an hmmsearch with the given hmm as input. The database to be
106 * searched is a local file as specified by the 'Database' parameter, or the
107 * current alignment (written to file) if none is specified.
109 * @param searchOutputFile
110 * @param hitsAlignmentFile
114 * @throws IOException
116 private boolean runCommand(File searchOutputFile, File hitsAlignmentFile,
117 File hmmFile) throws IOException
119 String command = getCommandPath(HMMSEARCH);
125 List<String> args = new ArrayList<>();
128 args.add(searchOutputFile.getAbsolutePath());
130 args.add(hitsAlignmentFile.getAbsolutePath());
132 boolean dbFound = false;
134 File databaseFile = null;
138 for (ArgumentI arg : params)
140 String name = arg.getName();
141 if (MessageManager.getString("label.number_of_results")
144 seqsToReturn = Integer.parseInt(arg.getValue());
146 else if (MessageManager.getString("label.auto_align_seqs")
149 realign = true; // TODO: not used
151 else if (MessageManager.getString("label.use_accessions")
156 else if (MessageManager.getString("label.seq_e_value").equals(name))
159 args.add(arg.getValue());
161 else if (MessageManager.getString("label.seq_score").equals(name))
164 args.add(arg.getValue());
166 else if (MessageManager.getString("label.dom_e_value_desc")
169 args.add("--incdomE");
170 args.add(arg.getValue());
172 else if (MessageManager.getString("label.dom_score").equals(name))
174 args.add("--incdomT");
175 args.add(arg.getValue());
177 else if (MessageManager.getString("label.trim_termini")
182 else if (MessageManager.getString("label.database").equals(name))
185 dbPath = arg.getValue();
186 if (!MessageManager.getString("label.this_alignment")
189 databaseFile = new File(dbPath);
195 if (!dbFound || MessageManager.getString("label.this_alignment")
199 * no external database specified for search, so
200 * export current alignment as 'database' to search
202 databaseFile = FileUtils.createTempFile("database", ".sto");
203 AlignmentI al = af.getViewport().getAlignment();
204 AlignmentI copy = new Alignment(al);
205 SequenceI hmms = copy.getHmmConsensus();
208 copy.deleteSequence(hmms);
210 exportStockholm(copy.getSequencesArray(), databaseFile, null);
211 // StockholmFile stoFile = new StockholmFile(copy);
212 // stoFile.setSeqs(copy.getSequencesArray());
213 // String alignmentString = stoFile.print();
214 // PrintWriter writer = new PrintWriter(databaseFile);
215 // writer.print(alignmentString);
219 args.add(hmmFile.getAbsolutePath());
220 args.add(databaseFile.getAbsolutePath());
222 return runCommand(args);
226 * Imports the data from the temporary file to which the output of hmmsearch
231 private void importData(SequenceI hmmSeq, File inputAlignmentTemp,
232 File hmmTemp, File searchOutputFile)
233 throws IOException, InterruptedException
235 BufferedReader br = new BufferedReader(
236 new FileReader(inputAlignmentTemp));
239 if (br.readLine() == null)
241 JOptionPane.showMessageDialog(af,
242 MessageManager.getString("label.no_sequences_found"));
245 StockholmFile file = new StockholmFile(new FileParse(
246 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
247 seqs = file.getSeqsAsArray();
249 readTable(searchOutputFile);
251 int seqCount = Math.min(seqs.length, seqsToReturn);
252 SequenceI[] hmmAndSeqs = new SequenceI[seqCount + 1];
253 hmmAndSeqs[0] = hmmSeq;
254 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqCount);
256 AlignmentI alignment = new Alignment(hmmAndSeqs);
257 AlignFrame frame = new AlignFrame(alignment, 1, 1);
258 frame.setSelectedHMMSequence(hmmSeq);
259 List<ArgumentI> alignArgs = new ArrayList<>();
262 alignArgs.add(new BooleanOption(
263 MessageManager.getString("label.trim_termini"),
264 MessageManager.getString("label.trim_termini_desc"), true,
267 HMMAlign hmmalign = new HMMAlign(frame, alignArgs);
271 inputAlignmentTemp.delete();
272 searchOutputFile.delete();
282 void readTable(File inputTableTemp) throws IOException
284 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
286 while (!line.startsWith("Query:"))
288 line = br.readLine();
290 for (int i = 0; i < 5; i++)
292 line = br.readLine();
296 while (!" ------ inclusion threshold ------".equals(line)
299 Scanner scanner = new Scanner(line);
301 String str = scanner.next(); // full sequence eValue score
302 float eValue = Float.parseFloat(str);
303 int seqLength = seqs[index].getLength();
304 Annotation[] annots = new Annotation[seqLength];
305 for (int j = 0; j < seqLength; j++)
307 annots[j] = new Annotation(eValue);
309 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
311 annot.setScore(Double.parseDouble(str));
312 annot.setSequenceRef(seqs[index]);
313 seqs[index].addAlignmentAnnotation(annot);
316 line = br.readLine();