3 import jalview.api.AlignViewportI;
4 import jalview.bin.Cache;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.AnnotatedCollectionI;
8 import jalview.datamodel.SequenceGroup;
9 import jalview.datamodel.SequenceI;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.JvOptionPane;
12 import jalview.io.DataSourceType;
13 import jalview.io.FileParse;
14 import jalview.io.HMMFile;
15 import jalview.util.MessageManager;
16 import jalview.ws.params.ArgumentI;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Hashtable;
22 import java.util.List;
25 * A class that runs the hmmbuild command as a separate process.
30 public class HMMBuild extends HmmerCommand
32 static final String ARG_AMINO = "--amino";
34 static final String ARG_DNA = "--dna";
36 static final String ARG_RNA = "--rna";
44 public HMMBuild(AlignFrame alignFrame, List<ArgumentI> args)
46 super(alignFrame, args);
50 * Builds a HMM from an alignment (and/or groups), then imports and adds it to
51 * the alignment (and/or groups). Call this method directly to execute
52 * synchronously, or via start() in a new Thread for asynchronously.
57 if (params == null || params.isEmpty())
59 Cache.log.error("No parameters to HMMBuild!|");
63 long msgID = System.currentTimeMillis();
64 af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
67 AlignViewportI viewport = af.getViewport();
71 * run hmmbuild for alignment and/or groups as selected
73 List<AnnotatedCollectionI> runBuildFor = parseParameters(viewport);
75 for (AnnotatedCollectionI grp : runBuildFor)
81 af.setProgressBar("", msgID);
82 viewport.alignmentChanged(af.alignPanel);
83 af.buildColourMenu(); // to enable HMMER colour schemes
88 * Scans the parameters to determine whether to run hmmmbuild for the whole
89 * alignment or specified subgroup(s) or both
94 protected List<AnnotatedCollectionI> parseParameters(
95 AlignViewportI viewport)
97 List<AnnotatedCollectionI> runBuildFor = new ArrayList<>();
98 for (ArgumentI arg : params)
100 String name = arg.getName();
101 if (MessageManager.getString("label.hmmbuild_for").equals(name))
103 String value = arg.getValue();
104 if (MessageManager.getString("label.alignment").equals(value))
106 runBuildFor.add(alignment);
108 else if (MessageManager.getString("label.groups_and_alignment")
111 runBuildFor.add(alignment);
112 runBuildFor.addAll(viewport.getAlignment().getGroups());
114 else if (MessageManager.getString("label.groups").equals(value))
116 runBuildFor.addAll(viewport.getAlignment().getGroups());
118 else if (MessageManager.getString("label.selected_group")
121 runBuildFor.add(viewport.getSelectionGroup());
124 else if (MessageManager.getString("label.use_reference")
127 // todo disable this option if no RF annotation on alignment
128 if (!af.getViewport().hasReferenceAnnotation())
130 JvOptionPane.showInternalMessageDialog(af, MessageManager
131 .getString("warn.no_reference_annotation"));
140 * Runs hmmbuild on the given sequences (alignment or group)
144 private void runHMMBuild(AnnotatedCollectionI ac)
147 File alignmentFile = null;
150 hmmFile = createTempFile("hmm", ".hmm");
151 alignmentFile = createTempFile("output", ".sto");
153 if (ac instanceof Alignment)
155 AlignmentI al = (Alignment) ac;
156 // todo pad gaps in an unaligned SequenceGroup as well?
163 deleteHmmSequences(ac);
165 List<SequenceI> copy = new ArrayList<>();
166 if (ac instanceof Alignment)
168 copy.addAll(ac.getSequences());
172 SequenceI[] sel = ((SequenceGroup) ac)
173 .getSelectionAsNewSequences((AlignmentI) ac.getContext());
174 for (SequenceI seq : sel)
180 SequenceI[] copyArray = copy.toArray(new SequenceI[copy.size()]);
181 Hashtable sequencesHash = stashSequences(copyArray);
183 exportStockholm(copyArray, alignmentFile, ac);
185 recoverSequences(sequencesHash, copy.toArray(new SequenceI[] {}));
187 boolean ran = runCommand(alignmentFile, hmmFile, ac);
192 importData(hmmFile, ac);
193 } catch (Exception e)
202 if (alignmentFile != null)
204 alignmentFile.delete();
210 * A helper method that deletes any HMM consensus sequence from the given
211 * collection, and from the parent alignment if <code>ac</code> is a subgroup
215 void deleteHmmSequences(AnnotatedCollectionI ac)
217 SequenceI hmmSeq = ac.getHmmConsensus();
220 if (ac instanceof SequenceGroup)
222 ((SequenceGroup) ac).deleteSequence(hmmSeq, false);
223 AnnotatedCollectionI context = ac.getContext();
224 if (context != null && context instanceof AlignmentI)
226 ((AlignmentI) context).deleteSequence(hmmSeq);
231 ((AlignmentI) ac).deleteSequence(hmmSeq);
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(hmmFile.getAbsolutePath());
315 args.add(sequencesFile.getAbsolutePath());
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 HMMFile file = new HMMFile(
334 new FileParse(hmmFile.getAbsolutePath(), DataSourceType.FILE));
335 SequenceI hmmSeq = file.getHMM().getConsensusSequence();
337 if (ac instanceof SequenceGroup)
339 SequenceGroup grp = (SequenceGroup) ac;
340 char gapChar = alignment.getGapCharacter();
341 hmmSeq.insertCharAt(0, ac.getStartRes(), gapChar);
342 hmmSeq.insertCharAt(ac.getEndRes() + 1,
343 alignment.getWidth() - ac.getEndRes() - 1, gapChar);
344 SequenceI topSeq = grp.getSequencesInOrder(alignment)[0];
345 int topIndex = alignment.findIndex(topSeq);
346 alignment.insertSequenceAt(topIndex, hmmSeq);
347 ac.setSeqrep(hmmSeq);
348 grp.addSequence(hmmSeq, false);
349 grp.setHmmConsensus(hmmSeq);
353 alignment.insertSequenceAt(0, hmmSeq);
354 alignment.setHmmConsensus(hmmSeq);
357 if (af.getSelectedHMM() == null)
359 af.setSelectedHMMSequence(hmmSeq);