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 Hashtable sequencesHash;
46 * Constructor for the JackhmmerThread
50 public JackHMMER(AlignFrame af, List<ArgumentI> args)
56 * Runs the JackhmmerThread: the data on the alignment or group is exported,
57 * then the command is executed in the command line and then the data is
58 * imported and displayed in a new frame. Call this method directly to execute
59 * synchronously, or via start() in a new Thread for asynchronously.
64 SequenceI seq = getSequence();
67 // shouldn't happen if we got this far
68 Cache.log.error("Error: no sequence for jackhmmer");
72 long msgId = System.currentTimeMillis();
73 af.setProgressBar(MessageManager.getString("status.running_search"),
78 File seqFile = FileUtils.createTempFile("seq", ".sto");
79 File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment",
81 File searchOutputFile = FileUtils.createTempFile("searchOutput",
84 exportStockholm(new SequenceI[] { seq }, seqFile.getAbsoluteFile(),
87 boolean ran = runCommand(searchOutputFile, hitsAlignmentFile,
91 JvOptionPane.showInternalMessageDialog(af, MessageManager
92 .formatMessage("warn.command_failed", "jackhmmer"));
96 importData(hitsAlignmentFile, seqFile, searchOutputFile);
97 // TODO make realignment of search results a step at this level
98 // and make it conditional on this.realign
99 } catch (IOException | InterruptedException e)
104 af.setProgressBar("", msgId);
109 * Executes an jackhmmer search with the given sequence as input. The database
110 * to be searched is a local file as specified by the 'Database' parameter, or
111 * the current alignment (written to file) if none is specified.
113 * @param searchOutputFile
114 * @param hitsAlignmentFile
118 * @throws IOException
120 private boolean runCommand(File searchOutputFile, File hitsAlignmentFile,
121 File seqFile) throws IOException
123 String command = getCommandPath(JACKHMMER);
129 List<String> args = new ArrayList<>();
131 buildArguments(args, searchOutputFile, hitsAlignmentFile, seqFile);
133 return runCommand(args);
137 * Appends command line arguments to the given list, to specify input and output
138 * files for the search, and any additional options that may have been passed
139 * from the parameters dialog
142 * @param searchOutputFile
143 * @param hitsAlignmentFile
145 * @throws IOException
147 protected void buildArguments(List<String> args, File searchOutputFile,
148 File hitsAlignmentFile, File seqFile) throws IOException
151 args.add(getFilePath(searchOutputFile, true));
153 args.add(getFilePath(hitsAlignmentFile, true));
155 boolean dbFound = false;
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";
166 boolean searchAlignment = false;
170 for (ArgumentI arg : params)
172 String name = arg.getName();
174 if (MessageManager.getString("action.search").equals(name))
176 searchAlignment = arg.getValue().equals(
177 MessageManager.getString(JackHMMER.THIS_ALIGNMENT_KEY));
179 else if (MessageManager.getString(DATABASE_KEY).equals(name))
181 dbPath = arg.getValue();
182 int pos = dbPath.lastIndexOf(File.separator);
183 databaseName = dbPath.substring(pos + 1);
184 databaseFile = new File(dbPath);
186 else if (MessageManager.getString(REPORTING_CUTOFF_KEY)
189 if (CUTOFF_EVALUE.equals(arg.getValue()))
191 useEvalueCutoff = true;
193 else if (CUTOFF_SCORE.equals(arg.getValue()))
195 useScoreCutoff = true;
198 else if (MessageManager.getString(SEQ_EVALUE_KEY).equals(name))
200 seqEvalueCutoff = arg.getValue();
202 else if (MessageManager.getString(SEQ_SCORE_KEY).equals(name))
204 seqScoreCutoff = arg.getValue();
206 else if (MessageManager.getString(DOM_EVALUE_KEY).equals(name))
208 domEvalueCutoff = arg.getValue();
210 else if (MessageManager.getString(DOM_SCORE_KEY).equals(name))
212 domScoreCutoff = arg.getValue();
214 else if (MessageManager.getString(DATABASE_KEY).equals(name))
217 dbPath = arg.getValue();
218 if (!MessageManager.getString(THIS_ALIGNMENT_KEY).equals(dbPath))
220 int pos = dbPath.lastIndexOf(File.separator);
221 databaseName = dbPath.substring(pos + 1);
222 databaseFile = new File(dbPath);
231 args.add(seqEvalueCutoff);
233 args.add(domEvalueCutoff);
235 else if (useScoreCutoff)
238 args.add(seqScoreCutoff);
240 args.add(domScoreCutoff);
243 // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
248 * no external database specified for search, so
249 * export current alignment as 'database' to search
251 databaseFile = FileUtils.createTempFile("database", ".sto");
252 AlignmentI al = af.getViewport().getAlignment();
253 AlignmentI copy = new Alignment(al);
255 deleteHmmSequences(copy);
257 sequencesHash = stashSequences(copy.getSequencesArray());
259 exportStockholm(copy.getSequencesArray(), databaseFile, null);
262 args.add(getFilePath(seqFile, true));
263 args.add(getFilePath(databaseFile, true));
267 * Imports the data from the temporary file to which the output of jackhmmer was
270 private void importData(File inputAlignmentTemp, File seqTemp,
271 File searchOutputFile) throws IOException, InterruptedException
273 BufferedReader br = new BufferedReader(
274 new FileReader(inputAlignmentTemp));
277 if (br.readLine() == null)
279 JOptionPane.showMessageDialog(af,
280 MessageManager.getString("label.no_sequences_found"));
283 StockholmFile file = new StockholmFile(new FileParse(
284 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
285 seqs = file.getSeqsAsArray();
287 recoverSequences(sequencesHash, seqs);
289 readTable(searchOutputFile);
291 int seqCount = Math.min(seqs.length, seqsToReturn);
293 AlignmentI al = new Alignment(seqs);
295 AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
296 AlignFrame.DEFAULT_HEIGHT);
297 String ttl = "jackhmmer search of " + databaseName + " using "
299 Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
300 AlignFrame.DEFAULT_HEIGHT);
303 inputAlignmentTemp.delete();
304 searchOutputFile.delete();
315 * Reads in the scores table output by jackhmmer and adds annotation to
316 * sequences for E-value and bit score
318 * @param inputTableTemp
319 * @throws IOException
321 void readTable(File inputTableTemp) throws IOException
323 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
325 while (!line.startsWith("Query:"))
327 line = br.readLine();
329 while (!line.contains("-------"))
331 line = br.readLine();
333 line = br.readLine();
336 while (!" ------ inclusion threshold ------".equals(line)
339 SequenceI seq = seqs[index];
341 Scanner scanner = new Scanner(line);
342 String evalue = scanner.next();
343 evalue = scanner.next();
344 String score = scanner.next();
345 addScoreAnnotations(evalue, score, seq);
347 line = br.readLine();
354 protected void addScoreAnnotations(String eValue, String bitScore,
357 String label = "Search Scores";
358 String description = "Full sequence bit score and E-Value";
362 AlignmentAnnotation annot = new AlignmentAnnotation(label,
366 annot.description = description;
368 annot.setCalcId(JACKHMMER);
370 double dEValue = Double.parseDouble(eValue);
371 annot.setEValue(dEValue);
373 double dBitScore = Double.parseDouble(bitScore);
374 annot.setBitScore(dBitScore);
376 annot.setSequenceRef(seq);
377 seq.addAlignmentAnnotation(annot);
378 } catch (NumberFormatException e)
380 System.err.println("Error parsing " + label + " from " + eValue