3 import jalview.analysis.SeqsetUtils.SequenceInfo;
4 import jalview.api.AlignViewportI;
5 import jalview.bin.Cache;
6 import jalview.bin.Console;
7 import jalview.datamodel.Alignment;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.AnnotatedCollectionI;
10 import jalview.datamodel.ResidueCount;
11 import jalview.datamodel.SequenceGroup;
12 import jalview.datamodel.SequenceI;
13 import jalview.gui.AlignFrame;
14 import jalview.gui.JvOptionPane;
15 import jalview.io.DataSourceType;
16 import jalview.io.FileParse;
17 import jalview.io.HMMFile;
18 import jalview.util.FileUtils;
19 import jalview.util.MessageManager;
20 import jalview.ws.params.ArgumentI;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Hashtable;
26 import java.util.List;
30 * A class that runs the hmmbuild command as a separate process.
35 public class HMMBuild extends HmmerCommand
37 static final String ARG_AMINO = "--amino";
39 static final String ARG_DNA = "--dna";
41 static final String ARG_RNA = "--rna";
49 public HMMBuild(AlignFrame alignFrame, List<ArgumentI> args)
51 super(alignFrame, args);
55 * Builds a HMM from an alignment (and/or groups), then imports and adds it to
56 * the alignment (and/or groups). Call this method directly to execute
57 * synchronously, or via start() in a new Thread for asynchronously.
62 if (params == null || params.isEmpty())
64 Console.error("No parameters to HMMBuild!|");
68 long msgID = System.currentTimeMillis();
69 af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
72 AlignViewportI viewport = af.getViewport();
76 * run hmmbuild for alignment and/or groups as selected
78 List<AnnotatedCollectionI> runBuildFor = parseParameters(viewport);
80 for (AnnotatedCollectionI grp : runBuildFor)
86 af.setProgressBar("", msgID);
87 viewport.alignmentChanged(af.alignPanel);
88 af.buildColourMenu(); // to enable HMMER colour schemes
93 * Scans the parameters to determine whether to run hmmmbuild for the whole
94 * alignment or specified subgroup(s) or both
99 protected List<AnnotatedCollectionI> parseParameters(
100 AlignViewportI viewport)
102 List<AnnotatedCollectionI> runBuildFor = new ArrayList<>();
103 boolean foundArg = false;
105 for (ArgumentI arg : params)
107 String name = arg.getName();
108 if (MessageManager.getString("label.hmmbuild_for").equals(name))
111 String value = arg.getValue();
113 if (MessageManager.getString("label.alignment").equals(value))
115 runBuildFor.add(viewport.getAlignmentView(false)
116 .getVisibleAlignment('-'));
118 else if (MessageManager.getString("label.groups_and_alignment")
121 runBuildFor.add(viewport.getAlignmentView(false)
122 .getVisibleAlignment('-'));
123 runBuildFor.addAll(viewport.getAlignment().getGroups());
125 else if (MessageManager.getString("label.groups").equals(value))
127 runBuildFor.addAll(viewport.getAlignment().getGroups());
129 else if (MessageManager.getString("label.selected_group")
132 runBuildFor.add(viewport.getSelectionGroup());
135 else if (MessageManager.getString("label.use_reference")
138 // todo disable this option if no RF annotation on alignment
139 if (!af.getViewport().hasReferenceAnnotation())
141 JvOptionPane.showInternalMessageDialog(af, MessageManager
142 .getString("warn.no_reference_annotation"));
149 * default is to build for the whole alignment
153 runBuildFor.add(alignment);
160 * Runs hmmbuild on the given sequences (alignment or group)
164 private void runHMMBuild(AnnotatedCollectionI ac)
167 File alignmentFile = null;
170 hmmFile = FileUtils.createTempFile("hmm", ".hmm");
171 alignmentFile = FileUtils.createTempFile("output", ".sto");
173 if (ac instanceof Alignment)
175 AlignmentI al = (Alignment) ac;
176 // todo pad gaps in an unaligned SequenceGroup as well?
183 deleteHmmSequences(ac);
185 List<SequenceI> copy = new ArrayList<>();
186 if (ac instanceof Alignment)
188 copy.addAll(ac.getSequences());
192 SequenceI[] sel = ((SequenceGroup) ac)
193 .getSelectionAsNewSequences((AlignmentI) ac.getContext());
194 for (SequenceI seq : sel)
202 // TODO rather than copy alignment data we should anonymize in situ -
203 // export/File import could use anonymization hash to reinstate references
206 SequenceI[] copyArray = copy.toArray(new SequenceI[copy.size()]);
207 Map<String, SequenceInfo> sequencesHash = stashSequences(copyArray);
209 exportStockholm(copyArray, alignmentFile, ac);
211 recoverSequences(sequencesHash, copy.toArray(new SequenceI[] {}));
213 boolean ran = runCommand(alignmentFile, hmmFile, ac);
216 JvOptionPane.showInternalMessageDialog(af, MessageManager
217 .formatMessage("warn.command_failed", "hmmbuild"));
220 importData(hmmFile, ac);
221 } catch (Exception e)
230 if (alignmentFile != null)
232 alignmentFile.delete();
238 * Constructs and executes the hmmbuild command as a separate process
240 * @param sequencesFile
241 * the alignment from which the HMM is built
243 * the output file to which the HMM is written
245 * alignment or group for which the hmm is generated
248 * @throws IOException
250 private boolean runCommand(File sequencesFile, File hmmFile,
251 AnnotatedCollectionI group) throws IOException
253 String cmd = getCommandPath(HMMBUILD);
256 return false; // executable not found
258 List<String> args = new ArrayList<>();
262 * HMM name (will be given to consensus sequence) is
263 * - as specified by an input parameter if set
264 * - else group name with _HMM appended (if for a group)
265 * - else align frame title with _HMM appended (if title is not too long)
266 * - else "Alignment_HMM"
272 for (ArgumentI arg : params)
274 String argName = arg.getName();
278 name = arg.getValue().trim();
280 case "Use Reference Annotation":
287 if (group instanceof SequenceGroup)
289 name = ((SequenceGroup) group).getName() + "_HMM";
294 if (af != null && af.getTitle().length() < 15)
296 name = af.getTitle();
300 name = "Alignment_HMM";
305 args.add(name.replace(' ', '_'));
306 if (!alignment.isNucleotide())
308 args.add(ARG_AMINO); // TODO check for rna
315 args.add(getFilePath(hmmFile, true));
316 args.add(getFilePath(sequencesFile, true));
318 return runCommand(args);
322 * Imports the .hmm file produced by hmmbuild, and inserts the HMM consensus
323 * sequence (with attached HMM profile) as the first sequence in the alignment
324 * or group for which it was generated
328 * (optional) the group for which the hmm was generated
329 * @throws IOException
331 private void importData(File hmmFile, AnnotatedCollectionI ac)
334 if (hmmFile.length() == 0L)
336 Console.error("Error: hmmbuild produced empty hmm file");
340 HMMFile file = new HMMFile(
341 new FileParse(hmmFile.getAbsolutePath(), DataSourceType.FILE));
342 SequenceI hmmSeq = file.getHMM().getConsensusSequence();
346 ResidueCount counts = new ResidueCount(alignment.getSequences());
347 hmmSeq.getHMM().setBackgroundFrequencies(counts);
351 // hmmbuild failure not detected earlier
355 if (ac instanceof SequenceGroup)
357 SequenceGroup grp = (SequenceGroup) ac;
358 char gapChar = alignment.getGapCharacter();
359 hmmSeq.insertCharAt(0, ac.getStartRes(), gapChar);
360 hmmSeq.insertCharAt(ac.getEndRes() + 1,
361 alignment.getWidth() - ac.getEndRes() - 1, gapChar);
362 SequenceI topSeq = grp.getSequencesInOrder(alignment)[0];
363 int topIndex = alignment.findIndex(topSeq);
364 alignment.insertSequenceAt(topIndex, hmmSeq);
365 ac.setSeqrep(hmmSeq);
366 grp.addSequence(hmmSeq, false);
370 alignment.insertSequenceAt(0, hmmSeq);