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 AlignmentI copy = new Alignment(al);
250 deleteHmmSequences(copy);
251 exportFasta(copy, databaseFile);
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();
279 readTable(searchOutputFile);
281 int seqCount = Math.min(seqs.length, seqsToReturn);
283 AlignmentI al = new Alignment(seqs);
285 AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
286 AlignFrame.DEFAULT_HEIGHT);
287 String ttl = "jackhmmer search of " + databaseName + " using "
289 Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH,
290 AlignFrame.DEFAULT_HEIGHT);
293 inputAlignmentTemp.delete();
294 searchOutputFile.delete();
305 * Reads in the scores table output by jackhmmer and adds annotation to
306 * sequences for E-value and bit score
308 * @param inputTableTemp
309 * @throws IOException
311 void readTable(File inputTableTemp) throws IOException
313 BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
315 while (!line.startsWith("Query:"))
317 line = br.readLine();
319 while (!line.contains("-------"))
321 line = br.readLine();
323 line = br.readLine();
326 while (!" ------ inclusion threshold ------".equals(line)
329 SequenceI seq = seqs[index];
331 Scanner scanner = new Scanner(line);
332 String evalue = scanner.next();
333 evalue = scanner.next();
334 String score = scanner.next();
335 addScoreAnnotations(evalue, score, seq);
337 line = br.readLine();
344 protected void addScoreAnnotations(String eValue, String bitScore,
347 String label = "Search Scores";
348 String description = "Full sequence bit score and E-Value";
352 AlignmentAnnotation annot = new AlignmentAnnotation(label,
356 annot.description = description;
358 annot.setCalcId(JACKHMMER);
360 double dEValue = Double.parseDouble(eValue);
361 annot.setEValue(dEValue);
363 double dBitScore = Double.parseDouble(bitScore);
364 annot.setEValue(dBitScore);
366 annot.setSequenceRef(seq);
367 seq.addAlignmentAnnotation(annot);
368 } catch (NumberFormatException e)
370 System.err.println("Error parsing " + label + " from " + eValue