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 boolean dbFound = false;
159 File databaseFile = null;
161 boolean useEvalueCutoff = false;
162 boolean useScoreCutoff = false;
163 String seqEvalueCutoff = null;
164 String domEvalueCutoff = null;
165 String seqScoreCutoff = null;
166 String domScoreCutoff = null;
167 databaseName = "Alignment";
171 for (ArgumentI arg : params)
173 String name = arg.getName();
175 if (MessageManager.getString(REPORTING_CUTOFF_KEY)
178 if (MessageManager.getString(CUTOFF_EVALUE)
179 .equals(arg.getValue()))
181 useEvalueCutoff = true;
183 else if (MessageManager.getString(CUTOFF_SCORE)
184 .equals(arg.getValue()))
186 useScoreCutoff = true;
189 else if (MessageManager.getString(SEQ_EVALUE_KEY).equals(name))
191 seqEvalueCutoff = arg.getValue();
193 else if (MessageManager.getString(SEQ_SCORE_KEY).equals(name))
195 seqScoreCutoff = arg.getValue();
197 else if (MessageManager.getString(DOM_EVALUE_KEY).equals(name))
199 domEvalueCutoff = arg.getValue();
201 else if (MessageManager.getString(DOM_SCORE_KEY).equals(name))
203 domScoreCutoff = arg.getValue();
205 else if (MessageManager.getString(DATABASE_KEY).equals(name))
208 dbPath = arg.getValue();
209 if (!MessageManager.getString(THIS_ALIGNMENT_KEY).equals(dbPath))
211 int pos = dbPath.lastIndexOf(File.separator);
212 databaseName = dbPath.substring(pos + 1);
213 databaseFile = new File(dbPath);
215 searchAlignment = false;
223 args.add(seqEvalueCutoff);
225 args.add(domEvalueCutoff);
227 else if (useScoreCutoff)
230 args.add(seqScoreCutoff);
232 args.add(domScoreCutoff);
235 // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
240 * no external database specified for search, so
241 * export current alignment as 'database' to search
243 databaseFile = FileUtils.createTempFile("database", ".sto");
244 AlignmentI al = af.getViewport().getAlignment();
245 AlignmentI copy = new Alignment(al);
247 deleteHmmSequences(copy);
249 sequencesHash = stashSequences(copy.getSequencesArray());
251 exportStockholm(copy.getSequencesArray(), databaseFile, null);
254 args.add(getFilePath(seqFile, true));
255 args.add(getFilePath(databaseFile, true));
259 * Imports the data from the temporary file to which the output of jackhmmer was
262 private void importData(File inputAlignmentTemp, File seqTemp,
263 File searchOutputFile) throws IOException, InterruptedException
265 BufferedReader br = new BufferedReader(
266 new FileReader(inputAlignmentTemp));
269 if (br.readLine() == null)
271 JOptionPane.showMessageDialog(af,
272 MessageManager.getString("label.no_sequences_found"));
275 StockholmFile file = new StockholmFile(new FileParse(
276 inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE));
277 seqs = file.getSeqsAsArray();
281 recoverSequences(sequencesHash, seqs);
284 readTable(searchOutputFile);
286 int seqCount = Math.min(seqs.length, seqsToReturn);
288 AlignmentI al = new Alignment(seqs);
290 AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
291 AlignFrame.DEFAULT_HEIGHT);
292 String ttl = "jackhmmer search of " + databaseName + " using "
294 Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
295 AlignFrame.DEFAULT_HEIGHT);
298 inputAlignmentTemp.delete();
299 searchOutputFile.delete();
310 * Reads in the scores table output by jackhmmer and adds annotation to
311 * sequences for E-value and bit score
313 * @param inputTableTemp
314 * @throws IOException
316 void readTable(File inputTableTemp) throws IOException
318 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
320 while (!line.startsWith("Query:"))
322 line = br.readLine();
324 while (!line.contains("-------"))
326 line = br.readLine();
328 line = br.readLine();
331 while (!" ------ inclusion threshold ------".equals(line)
334 SequenceI seq = seqs[index];
336 Scanner scanner = new Scanner(line);
337 String evalue = scanner.next();
338 evalue = scanner.next();
339 String score = scanner.next();
340 addScoreAnnotations(evalue, score, seq);
342 line = br.readLine();
349 protected void addScoreAnnotations(String eValue, String bitScore,
352 String label = "Search Scores";
353 String description = "Full sequence bit score and E-Value";
357 AlignmentAnnotation annot = new AlignmentAnnotation(label,
361 annot.description = description;
363 annot.setCalcId(JACKHMMER);
365 double dEValue = Double.parseDouble(eValue);
366 annot.setEValue(dEValue);
368 double dBitScore = Double.parseDouble(bitScore);
369 annot.setBitScore(dBitScore);
371 annot.setSequenceRef(seq);
372 seq.addAlignmentAnnotation(annot);
374 } catch (NumberFormatException e)
376 System.err.println("Error parsing " + label + " from " + eValue