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 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 = createTempFile("hmm", ".hmm");
76 File hitsAlignmentFile = createTempFile("hitAlignment", ".sto");
77 File searchOutputFile = createTempFile("searchOutput", ".sto");
79 exportHmm(hmm, hmmFile.getAbsoluteFile());
81 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile, hmmFile);
84 JvOptionPane.showInternalMessageDialog(af,
85 MessageManager.getString("warn.hmmsearch_failed"));
89 importData(hmmSeq, hitsAlignmentFile, hmmFile, searchOutputFile);
90 // TODO make realignment of search results a step at this level
91 // and make it conditional on this.realign
92 } catch (IOException | InterruptedException e)
98 af.setProgressBar("", msgId);
103 * Executes an hmmsearch with the given hmm as input. The database to be
104 * searched is a local file as specified by the 'Database' parameter, or the
105 * current alignment (written to file) if none is specified.
107 * @param searchOutputFile
108 * @param hitsAlignmentFile
112 * @throws IOException
114 private boolean runCommand(File searchOutputFile, File hitsAlignmentFile,
115 File hmmFile) throws IOException
117 String command = getCommandPath(HMMSEARCH);
123 List<String> args = new ArrayList<>();
126 args.add(searchOutputFile.getAbsolutePath());
128 args.add(hitsAlignmentFile.getAbsolutePath());
130 boolean dbFound = false;
132 File databaseFile = null;
136 for (ArgumentI arg : params)
138 String name = arg.getName();
139 if (MessageManager.getString("label.number_of_results")
142 seqsToReturn = Integer.parseInt(arg.getValue());
144 else if (MessageManager.getString("label.auto_align_seqs")
147 realign = true; // TODO: not used
149 else if (MessageManager.getString("label.use_accessions")
154 else if (MessageManager.getString("label.seq_e_value").equals(name))
157 args.add(arg.getValue());
159 else if (MessageManager.getString("label.seq_score").equals(name))
162 args.add(arg.getValue());
164 else if (MessageManager.getString("label.dom_e_value_desc")
167 args.add("--incdomE");
168 args.add(arg.getValue());
170 else if (MessageManager.getString("label.dom_score").equals(name))
172 args.add("--incdomT");
173 args.add(arg.getValue());
175 else if (MessageManager.getString("label.trim_termini")
180 else if (MessageManager.getString("label.database").equals(name))
183 dbPath = arg.getValue();
184 if (!MessageManager.getString("label.this_alignment")
187 databaseFile = new File(dbPath);
193 if (!dbFound || MessageManager.getString("label.this_alignment")
196 AlignmentI alignment = af.getViewport().getAlignment();
197 AlignmentI copy = new Alignment(alignment);
198 SequenceI hmms = copy.getHmmConsensus();
201 copy.deleteSequence(hmms);
203 StockholmFile stoFile = new StockholmFile(copy);
204 stoFile.setSeqs(copy.getSequencesArray());
205 String alignmentString = stoFile.print();
206 databaseFile = createTempFile("database", ".sto");
207 PrintWriter writer = new PrintWriter(databaseFile);
208 writer.print(alignmentString);
212 args.add(hmmFile.getAbsolutePath());
213 args.add(databaseFile.getAbsolutePath());
215 return runCommand(args);
219 * Imports the data from the temporary file to which the output of hmmsearch
224 private void importData(SequenceI hmmSeq, File inputAlignmentTemp,
225 File hmmTemp, File searchOutputFile)
226 throws IOException, InterruptedException
228 BufferedReader br = new BufferedReader(
229 new FileReader(inputAlignmentTemp));
232 if (br.readLine() == null)
234 JOptionPane.showMessageDialog(af,
235 MessageManager.getString("label.no_sequences_found"));
238 StockholmFile file = new StockholmFile(new FileParse(
239 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
240 seqs = file.getSeqsAsArray();
242 readTable(searchOutputFile);
244 int seqCount = Math.min(seqs.length, seqsToReturn);
245 SequenceI[] hmmAndSeqs = new SequenceI[seqCount + 1];
246 hmmAndSeqs[0] = hmmSeq;
247 System.arraycopy(seqs, 0, hmmAndSeqs, 1, seqCount);
249 AlignmentI alignment = new Alignment(hmmAndSeqs);
250 AlignFrame frame = new AlignFrame(alignment, 1, 1);
251 frame.setSelectedHMMSequence(hmmSeq);
252 List<ArgumentI> alignArgs = new ArrayList<>();
255 alignArgs.add(new BooleanOption(
256 MessageManager.getString("label.trim_termini"),
257 MessageManager.getString("label.trim_termini_desc"), true,
260 HMMAlign hmmalign = new HMMAlign(frame, alignArgs);
264 inputAlignmentTemp.delete();
265 searchOutputFile.delete();
275 void readTable(File inputTableTemp) throws IOException
277 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
279 while (!line.startsWith("Query:"))
281 line = br.readLine();
283 for (int i = 0; i < 5; i++)
285 line = br.readLine();
289 while (!" ------ inclusion threshold ------".equals(line)
292 Scanner scanner = new Scanner(line);
294 String str = scanner.next(); // full sequence eValue score
295 float eValue = Float.parseFloat(str);
296 int seqLength = seqs[index].getLength();
297 Annotation[] annots = new Annotation[seqLength];
298 for (int j = 0; j < seqLength; j++)
300 annots[j] = new Annotation(eValue);
302 AlignmentAnnotation annot = new AlignmentAnnotation("E-value",
304 annot.setScore(Double.parseDouble(str));
305 annot.setSequenceRef(seqs[index]);
306 seqs[index].addAlignmentAnnotation(annot);
309 line = br.readLine();