3 import jalview.bin.Cache;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.SequenceI;
8 import jalview.gui.AlignFrame;
9 import jalview.gui.Desktop;
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;
18 import java.io.BufferedReader;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Hashtable;
24 import java.util.List;
25 import java.util.Scanner;
27 import javax.swing.JOptionPane;
29 public class JackHMMER extends HmmerCommand
31 static final String JACKHMMER = "jackhmmer";
33 boolean realign = false;
37 int seqsToReturn = Integer.MAX_VALUE;
41 private String databaseName;
43 boolean searchAlignment = true;
45 Hashtable sequencesHash;
48 * Constructor for the JackhmmerThread
52 public JackHMMER(AlignFrame af, List<ArgumentI> args)
58 * Runs the JackhmmerThread: the data on the alignment or group is exported,
59 * then the command is executed in the command line and then the data is
60 * imported and displayed in a new frame. Call this method directly to execute
61 * synchronously, or via start() in a new Thread for asynchronously.
66 SequenceI seq = getSequence();
69 // shouldn't happen if we got this far
70 Cache.log.error("Error: no sequence for jackhmmer");
74 long msgId = System.currentTimeMillis();
75 af.setProgressBar(MessageManager.getString("status.running_search"),
80 File seqFile = FileUtils.createTempFile("seq", ".sto");
81 File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment",
83 File searchOutputFile = FileUtils.createTempFile("searchOutput",
86 exportStockholm(new SequenceI[] { seq }, seqFile.getAbsoluteFile(),
89 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile,
93 JvOptionPane.showInternalMessageDialog(af, MessageManager
94 .formatMessage("warn.command_failed", "jackhmmer"));
98 importData(hitsAlignmentFile, seqFile, searchOutputFile);
99 // TODO make realignment of search results a step at this level
100 // and make it conditional on this.realign
101 } catch (IOException | InterruptedException e)
106 af.setProgressBar("", msgId);
111 * Executes an jackhmmer search with the given sequence as input. The database
112 * to be searched is a local file as specified by the 'Database' parameter, or
113 * the current alignment (written to file) if none is specified.
115 * @param searchOutputFile
116 * @param hitsAlignmentFile
120 * @throws IOException
122 private boolean runCommand(File searchOutputFile, File hitsAlignmentFile,
123 File seqFile) throws IOException
125 String command = getCommandPath(JACKHMMER);
131 List<String> args = new ArrayList<>();
133 buildArguments(args, searchOutputFile, hitsAlignmentFile, seqFile);
135 return runCommand(args);
139 * Appends command line arguments to the given list, to specify input and output
140 * files for the search, and any additional options that may have been passed
141 * from the parameters dialog
144 * @param searchOutputFile
145 * @param hitsAlignmentFile
147 * @throws IOException
149 protected void buildArguments(List<String> args, File searchOutputFile,
150 File hitsAlignmentFile, File seqFile) throws IOException
153 args.add(getFilePath(searchOutputFile, true));
155 args.add(getFilePath(hitsAlignmentFile, true));
157 File databaseFile = null;
159 boolean useEvalueCutoff = false;
160 boolean useScoreCutoff = false;
161 String seqEvalueCutoff = null;
162 String domEvalueCutoff = null;
163 String seqScoreCutoff = null;
164 String domScoreCutoff = null;
165 databaseName = "Alignment";
169 for (ArgumentI arg : params)
171 String name = arg.getName();
173 if (MessageManager.getString(REPORTING_CUTOFF_KEY)
176 if (MessageManager.getString(CUTOFF_EVALUE)
177 .equals(arg.getValue()))
179 useEvalueCutoff = true;
181 else if (MessageManager.getString(CUTOFF_SCORE)
182 .equals(arg.getValue()))
184 useScoreCutoff = true;
187 else if (MessageManager.getString(SEQ_EVALUE_KEY).equals(name))
189 seqEvalueCutoff = arg.getValue();
191 else if (MessageManager.getString(SEQ_SCORE_KEY).equals(name))
193 seqScoreCutoff = arg.getValue();
195 else if (MessageManager.getString(DOM_EVALUE_KEY).equals(name))
197 domEvalueCutoff = arg.getValue();
199 else if (MessageManager.getString(DOM_SCORE_KEY).equals(name))
201 domScoreCutoff = arg.getValue();
203 else if (MessageManager.getString(DATABASE_KEY).equals(name))
205 databaseFile = new File(arg.getValue());
206 if (!arg.getValue().isEmpty())
208 searchAlignment = false;
217 args.add(seqEvalueCutoff);
219 args.add(domEvalueCutoff);
221 else if (useScoreCutoff)
224 args.add(seqScoreCutoff);
226 args.add(domScoreCutoff);
229 // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
234 * no external database specified for search, so
235 * export current alignment as 'database' to search
237 databaseFile = FileUtils.createTempFile("database", ".sto");
238 AlignmentI al = af.getViewport().getAlignment();
239 AlignmentI copy = new Alignment(al);
241 deleteHmmSequences(copy);
245 sequencesHash = stashSequences(copy.getSequencesArray());
248 exportStockholm(copy.getSequencesArray(), databaseFile, null);
251 args.add(getFilePath(seqFile, true));
252 args.add(getFilePath(databaseFile, true));
256 * Imports the data from the temporary file to which the output of jackhmmer was
259 private void importData(File inputAlignmentTemp, File seqTemp,
260 File searchOutputFile) throws IOException, InterruptedException
262 BufferedReader br = new BufferedReader(
263 new FileReader(inputAlignmentTemp));
266 if (br.readLine() == null)
268 JOptionPane.showMessageDialog(af,
269 MessageManager.getString("label.no_sequences_found"));
272 StockholmFile file = new StockholmFile(new FileParse(
273 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
274 seqs = file.getSeqsAsArray();
278 recoverSequences(sequencesHash, seqs);
281 readTable(searchOutputFile);
283 int seqCount = Math.min(seqs.length, seqsToReturn);
285 AlignmentI al = new Alignment(seqs);
287 AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
288 AlignFrame.DEFAULT_HEIGHT);
289 String ttl = "jackhmmer search of " + databaseName + " using "
291 Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
292 AlignFrame.DEFAULT_HEIGHT);
295 inputAlignmentTemp.delete();
296 searchOutputFile.delete();
307 * Reads in the scores table output by jackhmmer and adds annotation to
308 * sequences for E-value and bit score
310 * @param inputTableTemp
311 * @throws IOException
313 void readTable(File inputTableTemp) throws IOException
315 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
317 while (!line.startsWith("Query:"))
319 line = br.readLine();
321 while (!line.contains("-------"))
323 line = br.readLine();
325 line = br.readLine();
328 while (!" ------ inclusion threshold ------".equals(line)
332 Scanner scanner = new Scanner(line);
333 String evalue = scanner.next();
334 evalue = scanner.next();
335 checkSequenceOrder(index, scanner);
336 SequenceI seq = seqs[index];
337 String score = scanner.next();
338 addScoreAnnotations(evalue, score, seq);
340 line = br.readLine();
347 private void checkSequenceOrder(int index, Scanner scanner)
349 String seqName = null;
351 for (int i = 0; i < 8; i++)
353 seqName = scanner.next();
356 if (!seqs[index + 1].getName().equals(seqName))
358 SequenceI temp = seqs[index + 1];
360 for (int j = 0; j < seqs.length; j++)
362 if (seqs[j].getName().equals(seqName))
364 seqs[index + 1] = seqs[j];
372 protected void addScoreAnnotations(String eValue, String bitScore,
375 String label = "Search Scores";
376 String description = "Full sequence bit score and E-Value";
380 AlignmentAnnotation annot = new AlignmentAnnotation(label,
384 annot.description = description;
386 annot.setCalcId(JACKHMMER);
388 double dEValue = Double.parseDouble(eValue);
389 annot.setEValue(dEValue);
391 double dBitScore = Double.parseDouble(bitScore);
392 annot.setBitScore(dBitScore);
394 annot.setSequenceRef(seq);
395 seq.addAlignmentAnnotation(annot);
397 } catch (NumberFormatException e)
399 System.err.println("Error parsing " + label + " from " + eValue