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.List;
24 import java.util.Scanner;
26 import javax.swing.JOptionPane;
28 public class JackHMMER extends HmmerCommand
30 static final String JACKHMMER = "jackhmmer";
32 boolean realign = false;
36 int seqsToReturn = Integer.MAX_VALUE;
40 private String databaseName;
43 * Constructor for the JackhmmerThread
47 public JackHMMER(AlignFrame af, List<ArgumentI> args)
53 * Runs the JackhmmerThread: the data on the alignment or group is exported,
54 * then the command is executed in the command line and then the data is
55 * imported and displayed in a new frame. Call this method directly to execute
56 * synchronously, or via start() in a new Thread for asynchronously.
61 SequenceI seq = getSequence();
64 // shouldn't happen if we got this far
65 Cache.log.error("Error: no sequence for jackhmmer");
69 long msgId = System.currentTimeMillis();
70 af.setProgressBar(MessageManager.getString("status.running_search"),
75 File seqFile = FileUtils.createTempFile("seq", ".fa");
76 File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment",
78 File searchOutputFile = FileUtils.createTempFile("searchOutput",
81 exportSequence(seq, seqFile.getAbsoluteFile());
83 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile,
87 JvOptionPane.showInternalMessageDialog(af, MessageManager
88 .formatMessage("warn.command_failed", "jackhmmer"));
92 importData(hitsAlignmentFile, seqFile, searchOutputFile);
93 // TODO make realignment of search results a step at this level
94 // and make it conditional on this.realign
95 } catch (IOException | InterruptedException e)
100 af.setProgressBar("", msgId);
105 * Executes an jackhmmer search with the given sequence as input. The database
106 * to be searched is a local file as specified by the 'Database' parameter, or
107 * the 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 seqFile) throws IOException
119 String command = getCommandPath(JACKHMMER);
125 List<String> args = new ArrayList<>();
127 buildArguments(args, searchOutputFile, hitsAlignmentFile, seqFile);
129 return runCommand(args);
133 * Appends command line arguments to the given list, to specify input and output
134 * files for the search, and any additional options that may have been passed
135 * from the parameters dialog
138 * @param searchOutputFile
139 * @param hitsAlignmentFile
141 * @throws IOException
143 protected void buildArguments(List<String> args, File searchOutputFile,
144 File hitsAlignmentFile, File seqFile) throws IOException
147 args.add(getFilePath(searchOutputFile, true));
149 args.add(getFilePath(hitsAlignmentFile, true));
151 boolean dbFound = false;
153 File databaseFile = null;
155 boolean useEvalueCutoff = false;
156 boolean useScoreCutoff = false;
157 String seqEvalueCutoff = null;
158 String domEvalueCutoff = null;
159 String seqScoreCutoff = null;
160 String domScoreCutoff = null;
161 databaseName = "Alignment";
162 boolean searchAlignment = false;
166 for (ArgumentI arg : params)
168 String name = arg.getName();
170 if (MessageManager.getString("action.search").equals(name))
172 searchAlignment = arg.getValue().equals(
173 MessageManager.getString(JackHMMER.THIS_ALIGNMENT_KEY));
175 else if (MessageManager.getString(DATABASE_KEY).equals(name))
177 dbPath = arg.getValue();
178 int pos = dbPath.lastIndexOf(File.separator);
179 databaseName = dbPath.substring(pos + 1);
180 databaseFile = new File(dbPath);
182 else if (MessageManager.getString(REPORTING_CUTOFF_KEY)
185 if (CUTOFF_EVALUE.equals(arg.getValue()))
187 useEvalueCutoff = true;
189 else if (CUTOFF_SCORE.equals(arg.getValue()))
191 useScoreCutoff = true;
194 else if (MessageManager.getString(SEQ_EVALUE_KEY).equals(name))
196 seqEvalueCutoff = arg.getValue();
198 else if (MessageManager.getString(SEQ_SCORE_KEY).equals(name))
200 seqScoreCutoff = arg.getValue();
202 else if (MessageManager.getString(DOM_EVALUE_KEY).equals(name))
204 domEvalueCutoff = arg.getValue();
206 else if (MessageManager.getString(DOM_SCORE_KEY).equals(name))
208 domScoreCutoff = arg.getValue();
210 else if (MessageManager.getString(DATABASE_KEY).equals(name))
213 dbPath = arg.getValue();
214 if (!MessageManager.getString(THIS_ALIGNMENT_KEY).equals(dbPath))
216 int pos = dbPath.lastIndexOf(File.separator);
217 databaseName = dbPath.substring(pos + 1);
218 databaseFile = new File(dbPath);
227 args.add(seqEvalueCutoff);
229 args.add(domEvalueCutoff);
231 else if (useScoreCutoff)
234 args.add(seqScoreCutoff);
236 args.add(domScoreCutoff);
239 // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
244 * no external database specified for search, so
245 * export current alignment as 'database' to search
247 databaseFile = FileUtils.createTempFile("database", ".fa");
248 AlignmentI al = af.getViewport().getAlignment();
249 exportFasta(al.getSequencesArray(), databaseFile);
252 args.add(getFilePath(seqFile, true));
253 args.add(getFilePath(databaseFile, true));
257 * Imports the data from the temporary file to which the output of jackhmmer was
260 private void importData(File inputAlignmentTemp, File seqTemp,
261 File searchOutputFile) throws IOException, InterruptedException
263 BufferedReader br = new BufferedReader(
264 new FileReader(inputAlignmentTemp));
267 if (br.readLine() == null)
269 JOptionPane.showMessageDialog(af,
270 MessageManager.getString("label.no_sequences_found"));
273 StockholmFile file = new StockholmFile(new FileParse(
274 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
275 seqs = file.getSeqsAsArray();
277 readTable(searchOutputFile);
279 int seqCount = Math.min(seqs.length, seqsToReturn);
281 AlignmentI al = new Alignment(seqs);
283 AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
284 AlignFrame.DEFAULT_HEIGHT);
285 String ttl = "jackhmmer search of " + databaseName + " using "
287 Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
288 AlignFrame.DEFAULT_HEIGHT);
291 inputAlignmentTemp.delete();
292 searchOutputFile.delete();
303 * Reads in the scores table output by jackhmmer and adds annotation to
304 * sequences for E-value and bit score
306 * @param inputTableTemp
307 * @throws IOException
309 void readTable(File inputTableTemp) throws IOException
311 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
313 while (!line.startsWith("Query:"))
315 line = br.readLine();
317 while (!line.contains("-------"))
319 line = br.readLine();
321 line = br.readLine();
324 while (!" ------ inclusion threshold ------".equals(line)
327 SequenceI seq = seqs[index];
328 AlignmentAnnotation pp = null;
329 if (seq.getAlignmentAnnotations("", "Posterior Probability")
332 pp = seq.getAlignmentAnnotations("", "Posterior Probability")
335 Scanner scanner = new Scanner(line);
336 String str = scanner.next();
337 str = scanner.next();
338 addScoreAnnotation(str, seq, "jackhmmer E-value",
339 "Full sequence E-value", pp);
340 str = scanner.next();
341 addScoreAnnotation(str, seq, "jackhmmer Score",
342 "Full sequence bit score", pp);
343 seq.removeAlignmentAnnotation(pp);
345 line = br.readLine();
353 * A helper method that adds one score-only (non-positional) annotation to a
361 protected void addScoreAnnotation(String value, SequenceI seq,
362 String label, String description)
364 addScoreAnnotation(value, seq, label, description, null);
368 * A helper method that adds one score-only (non-positional) annotation to a
376 * existing posterior probability annotation - values
377 * copied to new annotation row
379 protected void addScoreAnnotation(String value, SequenceI seq,
380 String label, String description, AlignmentAnnotation pp)
384 AlignmentAnnotation annot = null;
387 annot = new AlignmentAnnotation(label, description, null);
391 annot = new AlignmentAnnotation(pp);
393 annot.description = description;
395 annot.setCalcId(JACKHMMER);
396 double eValue = Double.parseDouble(value);
397 annot.setScore(eValue);
398 annot.setSequenceRef(seq);
399 seq.addAlignmentAnnotation(annot);
400 } catch (NumberFormatException e)
402 System.err.println("Error parsing " + label + " from " + value);