package jalview.hmmer; import jalview.bin.Cache; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.Desktop; import jalview.gui.JvOptionPane; import jalview.io.DataSourceType; import jalview.io.FileParse; import jalview.io.StockholmFile; import jalview.util.FileUtils; import jalview.util.MessageManager; import jalview.ws.params.ArgumentI; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.JOptionPane; public class JackHMMER extends HmmerCommand { static final String JACKHMMER = "jackhmmer"; boolean realign = false; boolean trim = false; int seqsToReturn = Integer.MAX_VALUE; SequenceI[] seqs; private String databaseName; /** * Constructor for the JackhmmerThread * * @param af */ public JackHMMER(AlignFrame af, List args) { super(af, args); } /** * Runs the JackhmmerThread: the data on the alignment or group is exported, * then the command is executed in the command line and then the data is * imported and displayed in a new frame. Call this method directly to execute * synchronously, or via start() in a new Thread for asynchronously. */ @Override public void run() { SequenceI seq = getSequence(); if (seq == null) { // shouldn't happen if we got this far Cache.log.error("Error: no sequence for jackhmmer"); return; } long msgId = System.currentTimeMillis(); af.setProgressBar(MessageManager.getString("status.running_search"), msgId); try { File seqFile = FileUtils.createTempFile("seq", ".fa"); File hitsAlignmentFile = FileUtils.createTempFile("hitAlignment", ".sto"); File searchOutputFile = FileUtils.createTempFile("searchOutput", ".txt"); exportSequence(seq, seqFile.getAbsoluteFile()); boolean ran = runCommand(searchOutputFile, hitsAlignmentFile, seqFile); if (!ran) { JvOptionPane.showInternalMessageDialog(af, MessageManager .formatMessage("warn.command_failed", "jackhmmer")); return; } importData(hitsAlignmentFile, seqFile, searchOutputFile); // TODO make realignment of search results a step at this level // and make it conditional on this.realign } catch (IOException | InterruptedException e) { e.printStackTrace(); } finally { af.setProgressBar("", msgId); } } /** * Executes an jackhmmer search with the given sequence as input. The database * to be searched is a local file as specified by the 'Database' parameter, or * the current alignment (written to file) if none is specified. * * @param searchOutputFile * @param hitsAlignmentFile * @param seqFile * * @return * @throws IOException */ private boolean runCommand(File searchOutputFile, File hitsAlignmentFile, File seqFile) throws IOException { String command = getCommandPath(JACKHMMER); if (command == null) { return false; } List args = new ArrayList<>(); args.add(command); buildArguments(args, searchOutputFile, hitsAlignmentFile, seqFile); return runCommand(args); } /** * Appends command line arguments to the given list, to specify input and output * files for the search, and any additional options that may have been passed * from the parameters dialog * * @param args * @param searchOutputFile * @param hitsAlignmentFile * @param seqFile * @throws IOException */ protected void buildArguments(List args, File searchOutputFile, File hitsAlignmentFile, File seqFile) throws IOException { args.add("-o"); args.add(getFilePath(searchOutputFile, true)); args.add("-A"); args.add(getFilePath(hitsAlignmentFile, true)); boolean dbFound = false; String dbPath = ""; File databaseFile = null; boolean useEvalueCutoff = false; boolean useScoreCutoff = false; String seqEvalueCutoff = null; String domEvalueCutoff = null; String seqScoreCutoff = null; String domScoreCutoff = null; databaseName = "Alignment"; boolean searchAlignment = false; if (params != null) { for (ArgumentI arg : params) { String name = arg.getName(); if (MessageManager.getString("action.search").equals(name)) { searchAlignment = arg.getValue().equals( MessageManager.getString(JackHMMER.THIS_ALIGNMENT_KEY)); } else if (MessageManager.getString(DATABASE_KEY).equals(name)) { dbPath = arg.getValue(); int pos = dbPath.lastIndexOf(File.separator); databaseName = dbPath.substring(pos + 1); databaseFile = new File(dbPath); } else if (MessageManager.getString(REPORTING_CUTOFF_KEY) .equals(name)) { if (CUTOFF_EVALUE.equals(arg.getValue())) { useEvalueCutoff = true; } else if (CUTOFF_SCORE.equals(arg.getValue())) { useScoreCutoff = true; } } else if (MessageManager.getString(SEQ_EVALUE_KEY).equals(name)) { seqEvalueCutoff = arg.getValue(); } else if (MessageManager.getString(SEQ_SCORE_KEY).equals(name)) { seqScoreCutoff = arg.getValue(); } else if (MessageManager.getString(DOM_EVALUE_KEY).equals(name)) { domEvalueCutoff = arg.getValue(); } else if (MessageManager.getString(DOM_SCORE_KEY).equals(name)) { domScoreCutoff = arg.getValue(); } else if (MessageManager.getString(DATABASE_KEY).equals(name)) { dbFound = true; dbPath = arg.getValue(); if (!MessageManager.getString(THIS_ALIGNMENT_KEY).equals(dbPath)) { int pos = dbPath.lastIndexOf(File.separator); databaseName = dbPath.substring(pos + 1); databaseFile = new File(dbPath); } } } } if (useEvalueCutoff) { args.add("-E"); args.add(seqEvalueCutoff); args.add("--domE"); args.add(domEvalueCutoff); } else if (useScoreCutoff) { args.add("-T"); args.add(seqScoreCutoff); args.add("--domT"); args.add(domScoreCutoff); } // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY) // .equals(dbPath)) if (searchAlignment) { /* * no external database specified for search, so * export current alignment as 'database' to search */ databaseFile = FileUtils.createTempFile("database", ".fa"); AlignmentI al = af.getViewport().getAlignment(); exportFasta(al.getSequencesArray(), databaseFile); } args.add(getFilePath(seqFile, true)); args.add(getFilePath(databaseFile, true)); } /** * Imports the data from the temporary file to which the output of jackhmmer was * directed. */ private void importData(File inputAlignmentTemp, File seqTemp, File searchOutputFile) throws IOException, InterruptedException { BufferedReader br = new BufferedReader( new FileReader(inputAlignmentTemp)); try { if (br.readLine() == null) { JOptionPane.showMessageDialog(af, MessageManager.getString("label.no_sequences_found")); return; } StockholmFile file = new StockholmFile(new FileParse( inputAlignmentTemp.getAbsolutePath(), DataSourceType.FILE)); seqs = file.getSeqsAsArray(); readTable(searchOutputFile); int seqCount = Math.min(seqs.length, seqsToReturn); AlignmentI al = new Alignment(seqs); AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); String ttl = "jackhmmer search of " + databaseName + " using " + seqs[0].getName(); Desktop.addInternalFrame(alignFrame, ttl, AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); seqTemp.delete(); inputAlignmentTemp.delete(); searchOutputFile.delete(); } finally { if (br != null) { br.close(); } } } /** * Reads in the scores table output by jackhmmer and adds annotation to * sequences for E-value and bit score * * @param inputTableTemp * @throws IOException */ void readTable(File inputTableTemp) throws IOException { BufferedReader br = new BufferedReader(new FileReader(inputTableTemp)); String line = ""; while (!line.startsWith("Query:")) { line = br.readLine(); } while (!line.contains("-------")) { line = br.readLine(); } line = br.readLine(); int index = 0; while (!" ------ inclusion threshold ------".equals(line) && !"".equals(line)) { SequenceI seq = seqs[index]; AlignmentAnnotation pp = null; if (seq.getAlignmentAnnotations("", "Posterior Probability") .size() != 0) { pp = seq.getAlignmentAnnotations("", "Posterior Probability") .get(0); } Scanner scanner = new Scanner(line); String str = scanner.next(); str = scanner.next(); addScoreAnnotation(str, seq, "jackhmmer E-value", "Full sequence E-value", pp); str = scanner.next(); addScoreAnnotation(str, seq, "jackhmmer Score", "Full sequence bit score", pp); seq.removeAlignmentAnnotation(pp); scanner.close(); line = br.readLine(); index++; } br.close(); } /** * A helper method that adds one score-only (non-positional) annotation to a * sequence * * @param value * @param seq * @param label * @param description */ protected void addScoreAnnotation(String value, SequenceI seq, String label, String description) { addScoreAnnotation(value, seq, label, description, null); } /** * A helper method that adds one score-only (non-positional) annotation to a * sequence * * @param value * @param seq * @param label * @param description * @param pp * existing posterior probability annotation - values * copied to new annotation row */ protected void addScoreAnnotation(String value, SequenceI seq, String label, String description, AlignmentAnnotation pp) { try { AlignmentAnnotation annot = null; if (pp == null) { annot = new AlignmentAnnotation(label, description, null); } else { annot = new AlignmentAnnotation(pp); annot.label = label; annot.description = description; } annot.setCalcId(JACKHMMER); double eValue = Double.parseDouble(value); annot.setScore(eValue); annot.setSequenceRef(seq); seq.addAlignmentAnnotation(annot); } catch (NumberFormatException e) { System.err.println("Error parsing " + label + " from " + value); } } }