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.FileUtils;
16 import jalview.util.MessageManager;
17 import jalview.ws.params.ArgumentI;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Hashtable;
23 import java.util.List;
26 * A class that runs the hmmbuild command as a separate process.
31 public class HMMBuild extends HmmerCommand
33 static final String ARG_AMINO = "--amino";
35 static final String ARG_DNA = "--dna";
37 static final String ARG_RNA = "--rna";
45 public HMMBuild(AlignFrame alignFrame, List<ArgumentI> args)
47 super(alignFrame, args);
51 * Builds a HMM from an alignment (and/or groups), then imports and adds it to
52 * the alignment (and/or groups). Call this method directly to execute
53 * synchronously, or via start() in a new Thread for asynchronously.
58 if (params == null || params.isEmpty())
60 Cache.log.error("No parameters to HMMBuild!|");
64 long msgID = System.currentTimeMillis();
65 af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
68 AlignViewportI viewport = af.getViewport();
72 * run hmmbuild for alignment and/or groups as selected
74 List<AnnotatedCollectionI> runBuildFor = parseParameters(viewport);
76 for (AnnotatedCollectionI grp : runBuildFor)
82 af.setProgressBar("", msgID);
83 viewport.alignmentChanged(af.alignPanel);
84 af.buildColourMenu(); // to enable HMMER colour schemes
89 * Scans the parameters to determine whether to run hmmmbuild for the whole
90 * alignment or specified subgroup(s) or both
95 protected List<AnnotatedCollectionI> parseParameters(
96 AlignViewportI viewport)
98 List<AnnotatedCollectionI> runBuildFor = new ArrayList<>();
99 for (ArgumentI arg : params)
101 String name = arg.getName();
102 if (MessageManager.getString("label.hmmbuild_for").equals(name))
104 String value = arg.getValue();
105 if (MessageManager.getString("label.alignment").equals(value))
107 runBuildFor.add(alignment);
109 else if (MessageManager.getString("label.groups_and_alignment")
112 runBuildFor.add(alignment);
113 runBuildFor.addAll(viewport.getAlignment().getGroups());
115 else if (MessageManager.getString("label.groups").equals(value))
117 runBuildFor.addAll(viewport.getAlignment().getGroups());
119 else if (MessageManager.getString("label.selected_group")
122 runBuildFor.add(viewport.getSelectionGroup());
125 else if (MessageManager.getString("label.use_reference")
128 // todo disable this option if no RF annotation on alignment
129 if (!af.getViewport().hasReferenceAnnotation())
131 JvOptionPane.showInternalMessageDialog(af, MessageManager
132 .getString("warn.no_reference_annotation"));
141 * Runs hmmbuild on the given sequences (alignment or group)
145 private void runHMMBuild(AnnotatedCollectionI ac)
148 File alignmentFile = null;
151 hmmFile = FileUtils.createTempFile("hmm", ".hmm");
152 alignmentFile = FileUtils.createTempFile("output", ".sto");
154 if (ac instanceof Alignment)
156 AlignmentI al = (Alignment) ac;
157 // todo pad gaps in an unaligned SequenceGroup as well?
164 deleteHmmSequences(ac);
166 List<SequenceI> copy = new ArrayList<>();
167 if (ac instanceof Alignment)
169 copy.addAll(ac.getSequences());
173 SequenceI[] sel = ((SequenceGroup) ac)
174 .getSelectionAsNewSequences((AlignmentI) ac.getContext());
175 for (SequenceI seq : sel)
181 SequenceI[] copyArray = copy.toArray(new SequenceI[copy.size()]);
182 Hashtable sequencesHash = stashSequences(copyArray);
184 exportStockholm(copyArray, alignmentFile, ac);
186 recoverSequences(sequencesHash, copy.toArray(new SequenceI[] {}));
188 boolean ran = runCommand(alignmentFile, hmmFile, ac);
191 JvOptionPane.showInternalMessageDialog(af, MessageManager
192 .formatMessage("warn.command_failed", "hmmbuild"));
195 importData(hmmFile, ac);
196 } catch (Exception e)
205 if (alignmentFile != null)
207 alignmentFile.delete();
213 * A helper method that deletes any HMM consensus sequence from the given
214 * collection, and from the parent alignment if <code>ac</code> is a subgroup
218 void deleteHmmSequences(AnnotatedCollectionI ac)
220 List<SequenceI> hmmSeqs = ac.getHmmSequences();
221 for (SequenceI hmmSeq : hmmSeqs)
223 if (ac instanceof SequenceGroup)
225 ((SequenceGroup) ac).deleteSequence(hmmSeq, false);
226 AnnotatedCollectionI context = ac.getContext();
227 if (context != null && context instanceof AlignmentI)
229 ((AlignmentI) context).deleteSequence(hmmSeq);
234 ((AlignmentI) ac).deleteSequence(hmmSeq);
240 * Constructs and executes the hmmbuild command as a separate process
242 * @param sequencesFile
243 * the alignment from which the HMM is built
245 * the output file to which the HMM is written
247 * alignment or group for which the hmm is generated
250 * @throws IOException
252 private boolean runCommand(File sequencesFile, File hmmFile,
253 AnnotatedCollectionI group) throws IOException
255 String cmd = getCommandPath(HMMBUILD);
258 return false; // executable not found
260 List<String> args = new ArrayList<>();
264 * HMM name (will be given to consensus sequence) is
265 * - as specified by an input parameter if set
266 * - else group name with _HMM appended (if for a group)
267 * - else align frame title with _HMM appended (if title is not too long)
268 * - else "Alignment_HMM"
274 for (ArgumentI arg : params)
276 String argName = arg.getName();
280 name = arg.getValue().trim();
282 case "Use Reference Annotation":
289 if (group instanceof SequenceGroup)
291 name = ((SequenceGroup) group).getName() + "_HMM";
296 if (af != null && af.getTitle().length() < 15)
298 name = af.getTitle();
302 name = "Alignment_HMM";
307 args.add(name.replace(' ', '_'));
308 if (!alignment.isNucleotide())
310 args.add(ARG_AMINO); // TODO check for rna
317 args.add(getFilePath(hmmFile));
318 args.add(getFilePath(sequencesFile));
320 return runCommand(args);
324 * Imports the .hmm file produced by hmmbuild, and inserts the HMM consensus
325 * sequence (with attached HMM profile) as the first sequence in the alignment
326 * or group for which it was generated
330 * (optional) the group for which the hmm was generated
331 * @throws IOException
333 private void importData(File hmmFile, AnnotatedCollectionI ac)
336 if (hmmFile.length() == 0L)
338 Cache.log.error("Error: hmmbuild produced empty hmm file");
342 HMMFile file = new HMMFile(
343 new FileParse(hmmFile.getAbsolutePath(), DataSourceType.FILE));
344 SequenceI hmmSeq = file.getHMM().getConsensusSequence();
348 // hmmbuild failure not detected earlier
352 if (ac instanceof SequenceGroup)
354 SequenceGroup grp = (SequenceGroup) ac;
355 char gapChar = alignment.getGapCharacter();
356 hmmSeq.insertCharAt(0, ac.getStartRes(), gapChar);
357 hmmSeq.insertCharAt(ac.getEndRes() + 1,
358 alignment.getWidth() - ac.getEndRes() - 1, gapChar);
359 SequenceI topSeq = grp.getSequencesInOrder(alignment)[0];
360 int topIndex = alignment.findIndex(topSeq);
361 alignment.insertSequenceAt(topIndex, hmmSeq);
362 ac.setSeqrep(hmmSeq);
363 grp.addSequence(hmmSeq, false);
367 alignment.insertSequenceAt(0, hmmSeq);