3 import jalview.analysis.SeqsetUtils.SequenceInfo;
4 import jalview.api.AlignViewportI;
5 import jalview.bin.Cache;
6 import jalview.datamodel.Alignment;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.AnnotatedCollectionI;
9 import jalview.datamodel.ResidueCount;
10 import jalview.datamodel.SequenceGroup;
11 import jalview.datamodel.SequenceI;
12 import jalview.gui.AlignFrame;
13 import jalview.gui.JvOptionPane;
14 import jalview.io.DataSourceType;
15 import jalview.io.FileParse;
16 import jalview.io.HMMFile;
17 import jalview.util.FileUtils;
18 import jalview.util.MessageManager;
19 import jalview.ws.params.ArgumentI;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Hashtable;
25 import java.util.List;
29 * A class that runs the hmmbuild command as a separate process.
34 public class HMMBuild extends HmmerCommand
36 static final String ARG_AMINO = "--amino";
38 static final String ARG_DNA = "--dna";
40 static final String ARG_RNA = "--rna";
48 public HMMBuild(AlignFrame alignFrame, List<ArgumentI> args)
50 super(alignFrame, args);
54 * Builds a HMM from an alignment (and/or groups), then imports and adds it to
55 * the alignment (and/or groups). Call this method directly to execute
56 * synchronously, or via start() in a new Thread for asynchronously.
61 if (params == null || params.isEmpty())
63 Cache.log.error("No parameters to HMMBuild!|");
67 long msgID = System.currentTimeMillis();
68 af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
71 AlignViewportI viewport = af.getViewport();
75 * run hmmbuild for alignment and/or groups as selected
77 List<AnnotatedCollectionI> runBuildFor = parseParameters(viewport);
79 for (AnnotatedCollectionI grp : runBuildFor)
85 af.setProgressBar("", msgID);
86 viewport.alignmentChanged(af.alignPanel);
87 af.buildColourMenu(); // to enable HMMER colour schemes
92 * Scans the parameters to determine whether to run hmmmbuild for the whole
93 * alignment or specified subgroup(s) or both
98 protected List<AnnotatedCollectionI> parseParameters(
99 AlignViewportI viewport)
101 List<AnnotatedCollectionI> runBuildFor = new ArrayList<>();
102 boolean foundArg = false;
104 for (ArgumentI arg : params)
106 String name = arg.getName();
107 if (MessageManager.getString("label.hmmbuild_for").equals(name))
110 String value = arg.getValue();
112 if (MessageManager.getString("label.alignment").equals(value))
114 runBuildFor.add(viewport.getAlignmentView(false)
115 .getVisibleAlignment('-'));
117 else if (MessageManager.getString("label.groups_and_alignment")
120 runBuildFor.add(viewport.getAlignmentView(false)
121 .getVisibleAlignment('-'));
122 runBuildFor.addAll(viewport.getAlignment().getGroups());
124 else if (MessageManager.getString("label.groups").equals(value))
126 runBuildFor.addAll(viewport.getAlignment().getGroups());
128 else if (MessageManager.getString("label.selected_group")
131 runBuildFor.add(viewport.getSelectionGroup());
134 else if (MessageManager.getString("label.use_reference")
137 // todo disable this option if no RF annotation on alignment
138 if (!af.getViewport().hasReferenceAnnotation())
140 JvOptionPane.showInternalMessageDialog(af, MessageManager
141 .getString("warn.no_reference_annotation"));
148 * default is to build for the whole alignment
152 runBuildFor.add(alignment);
159 * Runs hmmbuild on the given sequences (alignment or group)
163 private void runHMMBuild(AnnotatedCollectionI ac)
166 File alignmentFile = null;
169 hmmFile = FileUtils.createTempFile("hmm", ".hmm");
170 alignmentFile = FileUtils.createTempFile("output", ".sto");
172 if (ac instanceof Alignment)
174 AlignmentI al = (Alignment) ac;
175 // todo pad gaps in an unaligned SequenceGroup as well?
182 deleteHmmSequences(ac);
184 List<SequenceI> copy = new ArrayList<>();
185 if (ac instanceof Alignment)
187 copy.addAll(ac.getSequences());
191 SequenceI[] sel = ((SequenceGroup) ac)
192 .getSelectionAsNewSequences((AlignmentI) ac.getContext());
193 for (SequenceI seq : sel)
201 // TODO rather than copy alignment data we should anonymize in situ -
202 // export/File import could use anonymization hash to reinstate references
205 SequenceI[] copyArray = copy.toArray(new SequenceI[copy.size()]);
206 Map<String, SequenceInfo> sequencesHash = stashSequences(copyArray);
208 exportStockholm(copyArray, alignmentFile, ac);
210 recoverSequences(sequencesHash, copy.toArray(new SequenceI[] {}));
212 boolean ran = runCommand(alignmentFile, hmmFile, ac);
215 JvOptionPane.showInternalMessageDialog(af, MessageManager
216 .formatMessage("warn.command_failed", "hmmbuild"));
219 importData(hmmFile, ac);
220 } catch (Exception e)
229 if (alignmentFile != null)
231 alignmentFile.delete();
237 * Constructs and executes the hmmbuild command as a separate process
239 * @param sequencesFile
240 * the alignment from which the HMM is built
242 * the output file to which the HMM is written
244 * alignment or group for which the hmm is generated
247 * @throws IOException
249 private boolean runCommand(File sequencesFile, File hmmFile,
250 AnnotatedCollectionI group) throws IOException
252 String cmd = getCommandPath(HMMBUILD);
255 return false; // executable not found
257 List<String> args = new ArrayList<>();
261 * HMM name (will be given to consensus sequence) is
262 * - as specified by an input parameter if set
263 * - else group name with _HMM appended (if for a group)
264 * - else align frame title with _HMM appended (if title is not too long)
265 * - else "Alignment_HMM"
271 for (ArgumentI arg : params)
273 String argName = arg.getName();
277 name = arg.getValue().trim();
279 case "Use Reference Annotation":
286 if (group instanceof SequenceGroup)
288 name = ((SequenceGroup) group).getName() + "_HMM";
293 if (af != null && af.getTitle().length() < 15)
295 name = af.getTitle();
299 name = "Alignment_HMM";
304 args.add(name.replace(' ', '_'));
305 if (!alignment.isNucleotide())
307 args.add(ARG_AMINO); // TODO check for rna
314 args.add(getFilePath(hmmFile, true));
315 args.add(getFilePath(sequencesFile, true));
317 return runCommand(args);
321 * Imports the .hmm file produced by hmmbuild, and inserts the HMM consensus
322 * sequence (with attached HMM profile) as the first sequence in the alignment
323 * or group for which it was generated
327 * (optional) the group for which the hmm was generated
328 * @throws IOException
330 private void importData(File hmmFile, AnnotatedCollectionI ac)
333 if (hmmFile.length() == 0L)
335 Cache.log.error("Error: hmmbuild produced empty hmm file");
339 HMMFile file = new HMMFile(
340 new FileParse(hmmFile.getAbsolutePath(), DataSourceType.FILE));
341 SequenceI hmmSeq = file.getHMM().getConsensusSequence();
345 ResidueCount counts = new ResidueCount(alignment.getSequences());
346 hmmSeq.getHMM().setBackgroundFrequencies(counts);
350 // hmmbuild failure not detected earlier
354 if (ac instanceof SequenceGroup)
356 SequenceGroup grp = (SequenceGroup) ac;
357 char gapChar = alignment.getGapCharacter();
358 hmmSeq.insertCharAt(0, ac.getStartRes(), gapChar);
359 hmmSeq.insertCharAt(ac.getEndRes() + 1,
360 alignment.getWidth() - ac.getEndRes() - 1, gapChar);
361 SequenceI topSeq = grp.getSequencesInOrder(alignment)[0];
362 int topIndex = alignment.findIndex(topSeq);
363 alignment.insertSequenceAt(topIndex, hmmSeq);
364 ac.setSeqrep(hmmSeq);
365 grp.addSequence(hmmSeq, false);
369 alignment.insertSequenceAt(0, hmmSeq);