import jalview.io.DataSourceType;
import jalview.io.FileParse;
import jalview.io.HMMFile;
+import jalview.util.FileUtils;
import jalview.util.MessageManager;
-import jalview.viewmodel.AlignmentViewport;
import jalview.ws.params.ArgumentI;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
-import java.util.Iterator;
import java.util.List;
/**
@Override
public void run()
{
- if (params == null)
+ if (params == null || params.isEmpty())
{
Cache.log.error("No parameters to HMMBuild!|");
return;
File alignmentFile = null;
try
{
- hmmFile = createTempFile("hmm", ".hmm");
- alignmentFile = createTempFile("output", ".sto");
- List<SequenceI> seqs = ac.getSequences();
- List<SequenceI> copy = new ArrayList<>();
- copy.addAll(seqs);
+ hmmFile = FileUtils.createTempFile("hmm", ".hmm");
+ alignmentFile = FileUtils.createTempFile("output", ".sto");
if (ac instanceof Alignment)
{
}
}
- /*
- * copy over sequences, excluding hmm consensus sequences
- * hmm sequences are also deleted in preparation for
- * re-adding them when recalculated; Information annotation is not
- * deleted, it will be updated to reference the new hmm sequence
- * by InformationThread.findOrCreateAnnotation
- */
- Iterator<SequenceI> it = copy.iterator();
- while (it.hasNext())
+ deleteHmmSequences(ac);
+
+ List<SequenceI> copy = new ArrayList<>();
+ if (ac instanceof Alignment)
{
- SequenceI seq = it.next();
- if (seq.isHMMConsensusSequence())
+ copy.addAll(ac.getSequences());
+ }
+ else
+ {
+ SequenceI[] sel = ((SequenceGroup) ac)
+ .getSelectionAsNewSequences((AlignmentI) ac.getContext());
+ for (SequenceI seq : sel)
{
- alignment.deleteSequence(seq);
- it.remove();
+ copy.add(seq);
}
}
exportStockholm(copyArray, alignmentFile, ac);
- recoverSequences(sequencesHash, seqs.toArray(new SequenceI[] {}));
+ recoverSequences(sequencesHash, copy.toArray(new SequenceI[] {}));
boolean ran = runCommand(alignmentFile, hmmFile, ac);
if (!ran)
{
+ JvOptionPane.showInternalMessageDialog(af, MessageManager
+ .formatMessage("warn.command_failed", "hmmbuild"));
return;
}
importData(hmmFile, ac);
}
/**
+ * A helper method that deletes any HMM consensus sequence from the given
+ * collection, and from the parent alignment if <code>ac</code> is a subgroup
+ *
+ * @param ac
+ */
+ void deleteHmmSequences(AnnotatedCollectionI ac)
+ {
+ List<SequenceI> hmmSeqs = ac.getHmmSequences();
+ for (SequenceI hmmSeq : hmmSeqs)
+ {
+ if (ac instanceof SequenceGroup)
+ {
+ ((SequenceGroup) ac).deleteSequence(hmmSeq, false);
+ AnnotatedCollectionI context = ac.getContext();
+ if (context != null && context instanceof AlignmentI)
+ {
+ ((AlignmentI) context).deleteSequence(hmmSeq);
+ }
+ }
+ else
+ {
+ ((AlignmentI) ac).deleteSequence(hmmSeq);
+ }
+ }
+ }
+
+ /**
* Constructs and executes the hmmbuild command as a separate process
*
* @param sequencesFile
* HMM name (will be given to consensus sequence) is
* - as specified by an input parameter if set
* - else group name with _HMM appended (if for a group)
- * - else align fame title with _HMM appended (if title is not too long)
+ * - else align frame title with _HMM appended (if title is not too long)
* - else "Alignment_HMM"
*/
String name = "";
args.add(ARG_DNA);
}
- args.add(hmmFile.getAbsolutePath());
- args.add(sequencesFile.getAbsolutePath());
+ args.add(getFilePath(hmmFile));
+ args.add(getFilePath(sequencesFile));
return runCommand(args);
}
private void importData(File hmmFile, AnnotatedCollectionI ac)
throws IOException
{
+ if (hmmFile.length() == 0L)
+ {
+ Cache.log.error("Error: hmmbuild produced empty hmm file");
+ return;
+ }
+
HMMFile file = new HMMFile(
new FileParse(hmmFile.getAbsolutePath(), DataSourceType.FILE));
- SequenceI[] seqs = file.getSeqsAsArray();
- SequenceI hmmSeq = seqs[0];
- hmmSeq.createDatasetSequence();
+ SequenceI hmmSeq = file.getHMM().getConsensusSequence();
+
+ if (hmmSeq == null)
+ {
+ // hmmbuild failure not detected earlier
+ return;
+ }
+
if (ac instanceof SequenceGroup)
{
SequenceGroup grp = (SequenceGroup) ac;
- hmmSeq.insertCharAt(0, ac.getStartRes(), '-');
+ char gapChar = alignment.getGapCharacter();
+ hmmSeq.insertCharAt(0, ac.getStartRes(), gapChar);
hmmSeq.insertCharAt(ac.getEndRes() + 1,
- alignment.getWidth() - ac.getEndRes() - 1, '-');
- hmmSeq.updateHMMMapping();
+ alignment.getWidth() - ac.getEndRes() - 1, gapChar);
SequenceI topSeq = grp.getSequencesInOrder(alignment)[0];
int topIndex = alignment.findIndex(topSeq);
alignment.insertSequenceAt(topIndex, hmmSeq);
ac.setSeqrep(hmmSeq);
grp.addSequence(hmmSeq, false);
- grp.setHmmConsensus(hmmSeq);
}
else
{
alignment.insertSequenceAt(0, hmmSeq);
- alignment.setHmmConsensus(hmmSeq);
- }
-
- if (af.getSelectedHMM() == null)
- {
- af.setSelectedHMMSequence(hmmSeq);
}
}
}