3 import jalview.analysis.SeqsetUtils;
4 import jalview.bin.Cache;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.AnnotatedCollectionI;
9 import jalview.datamodel.Annotation;
10 import jalview.datamodel.HiddenMarkovModel;
11 import jalview.datamodel.SequenceGroup;
12 import jalview.datamodel.SequenceI;
13 import jalview.gui.AlignFrame;
14 import jalview.gui.JvOptionPane;
15 import jalview.gui.Preferences;
16 import jalview.io.FastaFile;
17 import jalview.io.HMMFile;
18 import jalview.io.StockholmFile;
19 import jalview.util.FileUtils;
20 import jalview.util.MessageManager;
21 import jalview.util.Platform;
22 import jalview.ws.params.ArgumentI;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.io.PrintWriter;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
32 import java.util.List;
35 * Base class for hmmbuild, hmmalign and hmmsearch
40 public abstract class HmmerCommand implements Runnable
42 public static final String HMMBUILD = "hmmbuild";
44 protected final AlignFrame af;
46 protected final AlignmentI alignment;
48 protected final List<ArgumentI> params;
51 * constants for i18n lookup of passed parameter names
53 static final String DATABASE_KEY = "label.database";
55 static final String THIS_ALIGNMENT_KEY = "label.this_alignment";
57 static final String USE_ACCESSIONS_KEY = "label.use_accessions";
59 static final String AUTO_ALIGN_SEQS_KEY = "label.auto_align_seqs";
61 static final String NUMBER_OF_RESULTS_KEY = "label.number_of_results";
63 static final String NUMBER_OF_ITERATIONS = "label.number_of_iterations";
65 static final String TRIM_TERMINI_KEY = "label.trim_termini";
67 static final String RETURN_N_NEW_SEQ = "label.check_for_new_sequences";
69 static final String REPORTING_CUTOFF_KEY = "label.reporting_cutoff";
71 static final String CUTOFF_NONE = "label.default";
73 static final String CUTOFF_SCORE = "label.score";
75 static final String CUTOFF_EVALUE = "label.evalue";
77 static final String REPORTING_SEQ_EVALUE_KEY = "label.reporting_seq_evalue";
79 static final String REPORTING_DOM_EVALUE_KEY = "label.reporting_dom_evalue";
81 static final String REPORTING_SEQ_SCORE_KEY = "label.reporting_seq_score";
83 static final String REPORTING_DOM_SCORE_KEY = "label.reporting_dom_score";
85 static final String INCLUSION_SEQ_EVALUE_KEY = "label.inclusion_seq_evalue";
87 static final String INCLUSION_DOM_EVALUE_KEY = "label.inclusion_dom_evalue";
89 static final String INCLUSION_SEQ_SCORE_KEY = "label.inclusion_seq_score";
91 static final String INCLUSION_DOM_SCORE_KEY = "label.inclusion_dom_score";
93 static final String ARG_TRIM = "--trim";
95 static final String INCLUSION_THRESHOLD_KEY = "label.inclusion_threshold";
103 public HmmerCommand(AlignFrame alignFrame, List<ArgumentI> args)
106 alignment = af.getViewport().getAlignment();
111 * Answers true if preference HMMER_PATH is set, and its value is the path to
112 * a directory that contains an executable <code>hmmbuild</code> or
113 * <code>hmmbuild.exe</code>, else false
117 public static boolean isHmmerAvailable()
119 File exec = FileUtils.getExecutable(HMMBUILD,
120 Cache.getProperty(Preferences.HMMER_PATH));
125 * Uniquifies the sequences when exporting and stores their details in a
130 protected Hashtable stashSequences(SequenceI[] seqs)
132 return SeqsetUtils.uniquify(seqs, true);
136 * Restores the sequence data lost by uniquifying
141 protected void recoverSequences(Hashtable hashtable, SequenceI[] seqs)
143 SeqsetUtils.deuniquify(hashtable, seqs);
147 * Runs a command as a separate process and waits for it to complete. Answers
148 * true if the process return status is zero, else false.
151 * the executable command and any arguments to it
152 * @throws IOException
154 public boolean runCommand(List<String> commands)
157 List<String> args = Platform.isWindowsAndNotJS() ? wrapWithCygwin(commands)
162 ProcessBuilder pb = new ProcessBuilder(args);
163 pb.redirectErrorStream(true); // merge syserr to sysout
164 if (Platform.isWindowsAndNotJS())
166 String path = pb.environment().get("Path");
167 path = jalview.bin.Cache.getProperty("CYGWIN_PATH") + ";" + path;
168 pb.environment().put("Path", path);
170 final Process p = pb.start();
171 new Thread(new Runnable()
176 BufferedReader input = new BufferedReader(
177 new InputStreamReader(p.getInputStream()));
180 String line = input.readLine();
183 System.out.println(line);
184 line = input.readLine();
186 } catch (IOException e)
194 int exitValue = p.exitValue();
197 Cache.log.error("Command failed, return code = " + exitValue);
198 Cache.log.error("Command/args were: " + args.toString());
200 return exitValue == 0; // 0 is success, by convention
201 } catch (Exception e)
209 * Converts the given command to a Cygwin "bash" command wrapper. The hmmer
210 * command and any arguments to it are converted into a single parameter to the
215 protected List<String> wrapWithCygwin(List<String> commands)
217 File bash = FileUtils.getExecutable("bash",
218 Cache.getProperty(Preferences.CYGWIN_PATH));
221 Cache.log.error("Cygwin shell not found");
225 List<String> wrapped = new ArrayList<>();
226 // wrapped.add("C:\Users\tva\run");
227 wrapped.add(bash.getAbsolutePath());
231 * combine hmmbuild/search/align and arguments to a single string
233 StringBuilder sb = new StringBuilder();
234 for (String cmd : commands)
236 sb.append(" ").append(cmd);
238 wrapped.add(sb.toString());
244 * Exports an alignment, and reference (RF) annotation if present, to the
245 * specified file, in Stockholm format, removing all HMM sequences
250 * @throws IOException
252 public void exportStockholm(SequenceI[] seqs, File toFile,
253 AnnotatedCollectionI annotated)
260 AlignmentI newAl = new Alignment(seqs);
262 if (!newAl.isAligned())
267 if (toFile != null && annotated != null)
269 AlignmentAnnotation[] annots = annotated.getAlignmentAnnotation();
272 for (AlignmentAnnotation annot : annots)
274 if (annot.label.contains("Reference") || "RF".equals(annot.label))
276 AlignmentAnnotation newRF;
277 if (annot.annotations.length > newAl.getWidth())
279 Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
280 System.arraycopy(annot.annotations, 0, rfAnnots, 0,
282 newRF = new AlignmentAnnotation("RF", "Reference Positions",
287 newRF = new AlignmentAnnotation(annot);
289 newAl.addAnnotation(newRF);
295 for (SequenceI seq : newAl.getSequencesArray())
297 if (seq.getAnnotation() != null)
299 for (AlignmentAnnotation ann : seq.getAnnotation())
301 seq.removeAlignmentAnnotation(ann);
306 StockholmFile file = new StockholmFile(newAl);
307 String output = file.print(seqs, false);
308 PrintWriter writer = new PrintWriter(toFile);
309 writer.println(output);
314 * Answers the full path to the given hmmer executable, or null if file cannot
315 * be found or is not executable
318 * command short name e.g. hmmalign
320 * @throws IOException
322 protected String getCommandPath(String cmd)
325 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
326 // ensure any symlink to the directory is resolved:
327 binariesFolder = Paths.get(binariesFolder).toRealPath().toString();
328 File file = FileUtils.getExecutable(cmd, binariesFolder);
329 if (file == null && af != null)
331 JvOptionPane.showInternalMessageDialog(af, MessageManager
332 .formatMessage("label.executable_not_found", cmd));
335 return file == null ? null : getFilePath(file, true);
339 * Exports an HMM to the specified file
343 * @throws IOException
345 public void exportHmm(HiddenMarkovModel hmm, File hmmFile)
350 HMMFile file = new HMMFile(hmm);
351 PrintWriter writer = new PrintWriter(hmmFile);
352 writer.print(file.print());
359 * Exports a sequence to the specified file
363 * @throws IOException
365 public void exportSequence(SequenceI seq, File seqFile) throws IOException
369 FastaFile file = new FastaFile();
370 PrintWriter writer = new PrintWriter(seqFile);
371 writer.print(file.print(new SequenceI[] { seq }, false));
377 * Answers the HMM profile for the profile sequence the user selected (default
378 * is just the first HMM sequence in the alignment)
382 protected HiddenMarkovModel getHmmProfile()
384 String alignToParamName = MessageManager.getString("label.use_hmm");
385 for (ArgumentI arg : params)
387 String name = arg.getName();
388 if (name.equals(alignToParamName))
390 String seqName = arg.getValue();
391 SequenceI hmmSeq = alignment.findName(seqName);
392 if (hmmSeq.hasHMMProfile())
394 return hmmSeq.getHMM();
402 * Answers the query sequence the user selected (default is just the first
403 * sequence in the alignment)
407 protected SequenceI getSequence()
409 String alignToParamName = MessageManager
410 .getString("label.use_sequence");
411 for (ArgumentI arg : params)
413 String name = arg.getName();
414 if (name.equals(alignToParamName))
416 String seqName = arg.getValue();
417 SequenceI seq = alignment.findName(seqName);
425 * Answers an absolute path to the given file, in a format suitable for
426 * processing by a hmmer command. On a Windows platform, the native Windows file
427 * path is converted to Cygwin format, by replacing '\'with '/' and drive letter
428 * X with /cygdrive/x.
432 * True if file is to be read/written from within the Cygwin
433 * shell. Should be false for any imports.
436 protected String getFilePath(File resultFile, boolean isInCygwin)
438 String path = resultFile.getAbsolutePath();
439 if (Platform.isWindowsAndNotJS() && isInCygwin)
441 // the first backslash escapes '\' for the regular expression argument
442 path = path.replaceAll("\\" + File.separator, "/");
443 int colon = path.indexOf(':');
446 String drive = path.substring(0, colon);
447 path = path.replaceAll(drive + ":", "/cygdrive/" + drive);
455 * A helper method that deletes any HMM consensus sequence from the given
456 * collection, and from the parent alignment if <code>ac</code> is a subgroup
460 void deleteHmmSequences(AnnotatedCollectionI ac)
462 List<SequenceI> hmmSeqs = ac.getHmmSequences();
463 for (SequenceI hmmSeq : hmmSeqs)
465 if (ac instanceof SequenceGroup)
467 ((SequenceGroup) ac).deleteSequence(hmmSeq, false);
468 AnnotatedCollectionI context = ac.getContext();
469 if (context != null && context instanceof AlignmentI)
471 ((AlignmentI) context).deleteSequence(hmmSeq);
476 ((AlignmentI) ac).deleteSequence(hmmSeq);
482 * Sets the names of any duplicates within the given sequences to include their
483 * respective lengths. Deletes any duplicates that have the same name after this
488 void renameDuplicates(AlignmentI al)
491 SequenceI[] seqs = al.getSequencesArray();
492 List<Boolean> wasRenamed = new ArrayList<>();
494 for (SequenceI seq : seqs)
496 wasRenamed.add(false);
499 for (int i = 0; i < seqs.length; i++)
501 for (int j = 0; j < seqs.length; j++)
503 if (seqs[i].getName().equals(seqs[j].getName()) && i != j
504 && !wasRenamed.get(j))
507 wasRenamed.set(i, true);
508 String range = "/" + seqs[j].getStart() + "-" + seqs[j].getEnd();
509 // setting sequence name to include range - to differentiate between
510 // sequences of the same name. Currently have to include the range twice
511 // because the range is removed (once) when setting the name
512 // TODO come up with a better way of doing this
513 seqs[j].setName(seqs[j].getName() + range + range);
517 if (wasRenamed.get(i))
519 String range = "/" + seqs[i].getStart() + "-" + seqs[i].getEnd();
520 seqs[i].setName(seqs[i].getName() + range + range);
524 for (int i = 0; i < seqs.length; i++)
526 for (int j = 0; j < seqs.length; j++)
528 if (seqs[i].getName().equals(seqs[j].getName()) && i != j)
530 al.deleteSequence(j);