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.util.ArrayList;
23 import java.util.List;
24 import java.util.Scanner;
26 import javax.swing.JOptionPane;
28 public class HMMSearch extends HmmerCommand
30 static final String HMMSEARCH = "hmmsearch";
32 boolean realign = false;
36 int seqsToReturn = Integer.MAX_VALUE;
41 * Constructor for the HMMSearchThread
45 public HMMSearch(AlignFrame af, List<ArgumentI> args)
51 * Runs the HMMSearchThread: the data on the alignment or group is exported,
52 * then the command is executed in the command line and then the data is
53 * imported and displayed in a new frame. Call this method directly to execute
54 * synchronously, or via start() in a new Thread for asynchronously.
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")
196 * no external database specified for search, so
197 * export current alignment as 'database' to search
199 databaseFile = createTempFile("database", ".sto");
200 AlignmentI al = af.getViewport().getAlignment();
201 AlignmentI copy = new Alignment(al);
202 SequenceI hmms = copy.getHmmConsensus();
205 copy.deleteSequence(hmms);
207 exportStockholm(copy.getSequencesArray(), databaseFile, null);
208 // StockholmFile stoFile = new StockholmFile(copy);
209 // stoFile.setSeqs(copy.getSequencesArray());
210 // String alignmentString = stoFile.print();
211 // PrintWriter writer = new PrintWriter(databaseFile);
212 // writer.print(alignmentString);
216 args.add(hmmFile.getAbsolutePath());
217 args.add(databaseFile.getAbsolutePath());
219 return runCommand(args);
223 * Imports the data from the temporary file to which the output of hmmsearch
228 private void importData(SequenceI hmmSeq, File inputAlignmentTemp,
229 File hmmTemp, File searchOutputFile)
230 throws IOException, InterruptedException
232 BufferedReader br = new BufferedReader(
233 new FileReader(inputAlignmentTemp));
236 if (br.readLine() == null)
238 JOptionPane.showMessageDialog(af,
239 MessageManager.getString("label.no_sequences_found"));
242 StockholmFile file = new StockholmFile(new FileParse(
243 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
244 seqs = file.getSeqsAsArray();
246 readTable(searchOutputFile);
248 int seqCount = Math.min(seqs.length, seqsToReturn);
249 SequenceI[] hmmAndSeqs = new SequenceI[seqCount + 1];
250 hmmAndSeqs[0] = hmmSeq;
251 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqCount);
253 AlignmentI alignment = new Alignment(hmmAndSeqs);
254 AlignFrame frame = new AlignFrame(alignment, 1, 1);
255 frame.setSelectedHMMSequence(hmmSeq);
256 List<ArgumentI> alignArgs = new ArrayList<>();
259 alignArgs.add(new BooleanOption(
260 MessageManager.getString("label.trim_termini"),
261 MessageManager.getString("label.trim_termini_desc"), true,
264 HMMAlign hmmalign = new HMMAlign(frame, alignArgs);
268 inputAlignmentTemp.delete();
269 searchOutputFile.delete();
279 void readTable(File inputTableTemp) throws IOException
281 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
283 while (!line.startsWith("Query:"))
285 line = br.readLine();
287 for (int i = 0; i < 5; i++)
289 line = br.readLine();
293 while (!" ------ inclusion threshold ------".equals(line)
296 Scanner scanner = new Scanner(line);
298 String str = scanner.next(); // full sequence eValue score
299 float eValue = Float.parseFloat(str);
300 int seqLength = seqs[index].getLength();
301 Annotation[] annots = new Annotation[seqLength];
302 for (int j = 0; j < seqLength; j++)
304 annots[j] = new Annotation(eValue);
306 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
308 annot.setScore(Double.parseDouble(str));
309 annot.setSequenceRef(seqs[index]);
310 seqs[index].addAlignmentAnnotation(annot);
313 line = br.readLine();