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", ".sto");
76 File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment",
78 File searchOutputFile = FileUtils.createTempFile("searchOutput",
81 exportStockholm(new SequenceI[] { seq }, seqFile.getAbsoluteFile(),
84 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile,
88 JvOptionPane.showInternalMessageDialog(af, MessageManager
89 .formatMessage("warn.command_failed", "jackhmmer"));
93 importData(hitsAlignmentFile, seqFile, searchOutputFile);
94 // TODO make realignment of search results a step at this level
95 // and make it conditional on this.realign
96 } catch (IOException | InterruptedException e)
101 af.setProgressBar("", msgId);
106 * Executes an jackhmmer search with the given sequence as input. The database
107 * to be searched is a local file as specified by the 'Database' parameter, or
108 * the current alignment (written to file) if none is specified.
110 * @param searchOutputFile
111 * @param hitsAlignmentFile
115 * @throws IOException
117 private boolean runCommand(File searchOutputFile, File hitsAlignmentFile,
118 File seqFile) throws IOException
120 String command = getCommandPath(JACKHMMER);
126 List<String> args = new ArrayList<>();
128 buildArguments(args, searchOutputFile, hitsAlignmentFile, seqFile);
130 return runCommand(args);
134 * Appends command line arguments to the given list, to specify input and output
135 * files for the search, and any additional options that may have been passed
136 * from the parameters dialog
139 * @param searchOutputFile
140 * @param hitsAlignmentFile
142 * @throws IOException
144 protected void buildArguments(List<String> args, File searchOutputFile,
145 File hitsAlignmentFile, File seqFile) throws IOException
148 args.add(getFilePath(searchOutputFile, true));
150 args.add(getFilePath(hitsAlignmentFile, true));
152 boolean dbFound = false;
154 File databaseFile = null;
156 boolean useEvalueCutoff = false;
157 boolean useScoreCutoff = false;
158 String seqEvalueCutoff = null;
159 String domEvalueCutoff = null;
160 String seqScoreCutoff = null;
161 String domScoreCutoff = null;
162 databaseName = "Alignment";
163 boolean searchAlignment = false;
167 for (ArgumentI arg : params)
169 String name = arg.getName();
171 if (MessageManager.getString("action.search").equals(name))
173 searchAlignment = arg.getValue().equals(
174 MessageManager.getString(JackHMMER.THIS_ALIGNMENT_KEY));
176 else if (MessageManager.getString(DATABASE_KEY).equals(name))
178 dbPath = arg.getValue();
179 int pos = dbPath.lastIndexOf(File.separator);
180 databaseName = dbPath.substring(pos + 1);
181 databaseFile = new File(dbPath);
183 else if (MessageManager.getString(REPORTING_CUTOFF_KEY)
186 if (CUTOFF_EVALUE.equals(arg.getValue()))
188 useEvalueCutoff = true;
190 else if (CUTOFF_SCORE.equals(arg.getValue()))
192 useScoreCutoff = true;
195 else if (MessageManager.getString(SEQ_EVALUE_KEY).equals(name))
197 seqEvalueCutoff = arg.getValue();
199 else if (MessageManager.getString(SEQ_SCORE_KEY).equals(name))
201 seqScoreCutoff = arg.getValue();
203 else if (MessageManager.getString(DOM_EVALUE_KEY).equals(name))
205 domEvalueCutoff = arg.getValue();
207 else if (MessageManager.getString(DOM_SCORE_KEY).equals(name))
209 domScoreCutoff = arg.getValue();
211 else if (MessageManager.getString(DATABASE_KEY).equals(name))
214 dbPath = arg.getValue();
215 if (!MessageManager.getString(THIS_ALIGNMENT_KEY).equals(dbPath))
217 int pos = dbPath.lastIndexOf(File.separator);
218 databaseName = dbPath.substring(pos + 1);
219 databaseFile = new File(dbPath);
228 args.add(seqEvalueCutoff);
230 args.add(domEvalueCutoff);
232 else if (useScoreCutoff)
235 args.add(seqScoreCutoff);
237 args.add(domScoreCutoff);
240 // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
245 * no external database specified for search, so
246 * export current alignment as 'database' to search
248 databaseFile = FileUtils.createTempFile("database", ".sto");
249 AlignmentI al = af.getViewport().getAlignment();
250 AlignmentI copy = new Alignment(al);
252 deleteHmmSequences(copy);
254 // jackhmmer fails if file contains duplicate sequence names
255 renameDuplicates(copy.getSequencesArray());
257 exportStockholm(copy.getSequencesArray(), databaseFile, null, true);
260 args.add(getFilePath(seqFile, true));
261 args.add(getFilePath(databaseFile, true));
265 * Imports the data from the temporary file to which the output of jackhmmer was
268 private void importData(File inputAlignmentTemp, File seqTemp,
269 File searchOutputFile) throws IOException, InterruptedException
271 BufferedReader br = new BufferedReader(
272 new FileReader(inputAlignmentTemp));
275 if (br.readLine() == null)
277 JOptionPane.showMessageDialog(af,
278 MessageManager.getString("label.no_sequences_found"));
281 StockholmFile file = new StockholmFile(new FileParse(
282 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
283 seqs = file.getSeqsAsArray();
285 readTable(searchOutputFile);
287 int seqCount = Math.min(seqs.length, seqsToReturn);
289 AlignmentI al = new Alignment(seqs);
291 AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
292 AlignFrame.DEFAULT_HEIGHT);
293 String ttl = "jackhmmer search of " + databaseName + " using "
295 Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
296 AlignFrame.DEFAULT_HEIGHT);
299 inputAlignmentTemp.delete();
300 searchOutputFile.delete();
311 * Reads in the scores table output by jackhmmer and adds annotation to
312 * sequences for E-value and bit score
314 * @param inputTableTemp
315 * @throws IOException
317 void readTable(File inputTableTemp) throws IOException
319 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
321 while (!line.startsWith("Query:"))
323 line = br.readLine();
325 while (!line.contains("-------"))
327 line = br.readLine();
329 line = br.readLine();
332 while (!" ------ inclusion threshold ------".equals(line)
335 SequenceI seq = seqs[index];
337 Scanner scanner = new Scanner(line);
338 String evalue = scanner.next();
339 evalue = scanner.next();
340 String score = scanner.next();
341 addScoreAnnotations(evalue, score, seq);
343 line = br.readLine();
350 protected void addScoreAnnotations(String eValue, String bitScore,
353 String label = "Search Scores";
354 String description = "Full sequence bit score and E-Value";
358 AlignmentAnnotation annot = new AlignmentAnnotation(label,
362 annot.description = description;
364 annot.setCalcId(JACKHMMER);
366 double dEValue = Double.parseDouble(eValue);
367 annot.setEValue(dEValue);
369 double dBitScore = Double.parseDouble(bitScore);
370 annot.setBitScore(dBitScore);
372 annot.setSequenceRef(seq);
373 seq.addAlignmentAnnotation(annot);
374 } catch (NumberFormatException e)
376 System.err.println("Error parsing " + label + " from " + eValue