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 TRIM_TERMINI_KEY = "label.trim_termini";
65 static final String REPORTING_CUTOFF_KEY = "label.reporting_cutoff";
67 static final String CUTOFF_NONE = "None";
69 static final String CUTOFF_SCORE = "Score";
71 static final String CUTOFF_EVALUE = "E-Value";
73 static final String SEQ_EVALUE_KEY = "label.seq_evalue";
75 static final String DOM_EVALUE_KEY = "label.dom_evalue";
77 static final String SEQ_SCORE_KEY = "label.seq_score";
79 static final String DOM_SCORE_KEY = "label.dom_score";
81 static final String ARG_TRIM = "--trim";
89 public HmmerCommand(AlignFrame alignFrame, List<ArgumentI> args)
92 alignment = af.getViewport().getAlignment();
97 * Answers true if preference HMMER_PATH is set, and its value is the path to
98 * a directory that contains an executable <code>hmmbuild</code> or
99 * <code>hmmbuild.exe</code>, else false
103 public static boolean isHmmerAvailable()
105 File exec = FileUtils.getExecutable(HMMBUILD,
106 Cache.getProperty(Preferences.HMMER_PATH));
111 * Uniquifies the sequences when exporting and stores their details in a
116 protected Hashtable stashSequences(SequenceI[] seqs)
118 return SeqsetUtils.uniquify(seqs, true);
122 * Restores the sequence data lost by uniquifying
127 protected void recoverSequences(Hashtable hashtable, SequenceI[] seqs)
129 SeqsetUtils.deuniquify(hashtable, seqs);
133 * Runs a command as a separate process and waits for it to complete. Answers
134 * true if the process return status is zero, else false.
137 * the executable command and any arguments to it
138 * @throws IOException
140 public boolean runCommand(List<String> commands)
143 List<String> args = Platform.isWindows() ? wrapWithCygwin(commands)
148 ProcessBuilder pb = new ProcessBuilder(args);
149 pb.redirectErrorStream(true); // merge syserr to sysout
150 if (Platform.isWindows())
152 String path = pb.environment().get("Path");
153 path = jalview.bin.Cache.getProperty("CYGWIN_PATH") + ";" + path;
154 pb.environment().put("Path", path);
156 final Process p = pb.start();
157 new Thread(new Runnable()
162 BufferedReader input = new BufferedReader(
163 new InputStreamReader(p.getInputStream()));
166 String line = input.readLine();
169 System.out.println(line);
170 line = input.readLine();
172 } catch (IOException e)
180 int exitValue = p.exitValue();
183 Cache.log.error("Command failed, return code = " + exitValue);
184 Cache.log.error("Command/args were: " + args.toString());
186 return exitValue == 0; // 0 is success, by convention
187 } catch (Exception e)
195 * Converts the given command to a Cygwin "bash" command wrapper. The hmmer
196 * command and any arguments to it are converted into a single parameter to the
201 protected List<String> wrapWithCygwin(List<String> commands)
203 File bash = FileUtils.getExecutable("bash",
204 Cache.getProperty(Preferences.CYGWIN_PATH));
207 Cache.log.error("Cygwin shell not found");
211 List<String> wrapped = new ArrayList<>();
212 // wrapped.add("C:\Users\tva\run");
213 wrapped.add(bash.getAbsolutePath());
217 * combine hmmbuild/search/align and arguments to a single string
219 StringBuilder sb = new StringBuilder();
220 for (String cmd : commands)
222 sb.append(" ").append(cmd);
224 wrapped.add(sb.toString());
230 * Exports an alignment, and reference (RF) annotation if present, to the
231 * specified file, in Stockholm format, removing all HMM sequences
236 * @throws IOException
238 public void exportStockholm(SequenceI[] seqs, File toFile,
239 AnnotatedCollectionI annotated)
246 AlignmentI newAl = new Alignment(seqs);
248 if (!newAl.isAligned())
253 if (toFile != null && annotated != null)
255 AlignmentAnnotation[] annots = annotated.getAlignmentAnnotation();
258 for (AlignmentAnnotation annot : annots)
260 if (annot.label.contains("Reference") || "RF".equals(annot.label))
262 AlignmentAnnotation newRF;
263 if (annot.annotations.length > newAl.getWidth())
265 Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
266 System.arraycopy(annot.annotations, 0, rfAnnots, 0,
268 newRF = new AlignmentAnnotation("RF", "Reference Positions",
273 newRF = new AlignmentAnnotation(annot);
275 newAl.addAnnotation(newRF);
281 for (SequenceI seq : newAl.getSequencesArray())
283 if (seq.getAnnotation() != null)
285 for (AlignmentAnnotation ann : seq.getAnnotation())
287 seq.removeAlignmentAnnotation(ann);
292 StockholmFile file = new StockholmFile(newAl);
293 String output = file.print(seqs, false);
294 PrintWriter writer = new PrintWriter(toFile);
295 writer.println(output);
300 * Answers the full path to the given hmmer executable, or null if file cannot
301 * be found or is not executable
304 * command short name e.g. hmmalign
306 * @throws IOException
308 protected String getCommandPath(String cmd)
311 String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
312 // ensure any symlink to the directory is resolved:
313 binariesFolder = Paths.get(binariesFolder).toRealPath().toString();
314 File file = FileUtils.getExecutable(cmd, binariesFolder);
315 if (file == null && af != null)
317 JvOptionPane.showInternalMessageDialog(af, MessageManager
318 .formatMessage("label.executable_not_found", cmd));
321 return file == null ? null : getFilePath(file, true);
325 * Exports an HMM to the specified file
329 * @throws IOException
331 public void exportHmm(HiddenMarkovModel hmm, File hmmFile)
336 HMMFile file = new HMMFile(hmm);
337 PrintWriter writer = new PrintWriter(hmmFile);
338 writer.print(file.print());
345 * Exports a sequence to the specified file
349 * @throws IOException
351 public void exportSequence(SequenceI seq, File seqFile) throws IOException
355 FastaFile file = new FastaFile();
356 PrintWriter writer = new PrintWriter(seqFile);
357 writer.print(file.print(new SequenceI[] { seq }, false));
363 * Answers the HMM profile for the profile sequence the user selected (default
364 * is just the first HMM sequence in the alignment)
368 protected HiddenMarkovModel getHmmProfile()
370 String alignToParamName = MessageManager.getString("label.use_hmm");
371 for (ArgumentI arg : params)
373 String name = arg.getName();
374 if (name.equals(alignToParamName))
376 String seqName = arg.getValue();
377 SequenceI hmmSeq = alignment.findName(seqName);
378 if (hmmSeq.hasHMMProfile())
380 return hmmSeq.getHMM();
388 * Answers the HMM profile for the profile sequence the user selected (default
389 * is just the first HMM sequence in the alignment)
393 protected SequenceI getSequence()
395 String alignToParamName = MessageManager
396 .getString("label.use_sequence");
397 for (ArgumentI arg : params)
399 String name = arg.getName();
400 if (name.equals(alignToParamName))
402 String seqName = arg.getValue();
403 SequenceI seq = alignment.findName(seqName);
411 * Answers an absolute path to the given file, in a format suitable for
412 * processing by a hmmer command. On a Windows platform, the native Windows file
413 * path is converted to Cygwin format, by replacing '\'with '/' and drive letter
414 * X with /cygdrive/x.
418 * True if file is to be read/written from within the Cygwin
419 * shell. Should be false for any imports.
422 protected String getFilePath(File resultFile, boolean isInCygwin)
424 String path = resultFile.getAbsolutePath();
425 if (Platform.isWindows() && isInCygwin)
427 // the first backslash escapes '\' for the regular expression argument
428 path = path.replaceAll("\\" + File.separator, "/");
429 int colon = path.indexOf(':');
432 String drive = path.substring(0, colon);
433 path = path.replaceAll(drive + ":", "/cygdrive/" + drive);
441 * A helper method that deletes any HMM consensus sequence from the given
442 * collection, and from the parent alignment if <code>ac</code> is a subgroup
446 void deleteHmmSequences(AnnotatedCollectionI ac)
448 List<SequenceI> hmmSeqs = ac.getHmmSequences();
449 for (SequenceI hmmSeq : hmmSeqs)
451 if (ac instanceof SequenceGroup)
453 ((SequenceGroup) ac).deleteSequence(hmmSeq, false);
454 AnnotatedCollectionI context = ac.getContext();
455 if (context != null && context instanceof AlignmentI)
457 ((AlignmentI) context).deleteSequence(hmmSeq);
462 ((AlignmentI) ac).deleteSequence(hmmSeq);
468 * Sets the names of any duplicates within the given sequences to include their
469 * respective lengths. Deletes any duplicates that have the same name after this
474 void renameDuplicates(AlignmentI al)
477 SequenceI[] seqs = al.getSequencesArray();
478 List<Boolean> wasRenamed = new ArrayList<>();
480 for (SequenceI seq : seqs)
482 wasRenamed.add(false);
485 for (int i = 0; i < seqs.length; i++)
487 for (int j = 0; j < seqs.length; j++)
489 if (seqs[i].getName().equals(seqs[j].getName()) && i != j
490 && !wasRenamed.get(j))
493 wasRenamed.set(i, true);
494 String range = "/" + seqs[j].getStart() + "-" + seqs[j].getEnd();
495 // setting sequence name to include range - to differentiate between
496 // sequences of the same name. Currently have to include the range twice
497 // because the range is removed (once) when setting the name
498 // TODO come up with a better way of doing this
499 seqs[j].setName(seqs[j].getName() + range + range);
503 if (wasRenamed.get(i))
505 String range = "/" + seqs[i].getStart() + "-" + seqs[i].getEnd();
506 seqs[i].setName(seqs[i].getName() + range + range);
510 for (int i = 0; i < seqs.length; i++)
512 for (int j = 0; j < seqs.length; j++)
514 if (seqs[i].getName().equals(seqs[j].getName()) && i != j)
516 al.deleteSequence(j);