JAL-2629 use correct gap character, revised deletion of hmm sequences
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 30 Mar 2018 14:46:14 +0000 (15:46 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 30 Mar 2018 14:46:14 +0000 (15:46 +0100)
src/jalview/hmmer/HMMBuild.java

index 97dc59a..d95be48 100644 (file)
@@ -13,14 +13,12 @@ import jalview.io.DataSourceType;
 import jalview.io.FileParse;
 import jalview.io.HMMFile;
 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;
 
 /**
@@ -56,7 +54,7 @@ public class HMMBuild extends HmmerCommand
   @Override
   public void run()
   {
-    if (params == null)
+    if (params == null || params.isEmpty())
     {
       Cache.log.error("No parameters to HMMBuild!|");
       return;
@@ -151,9 +149,6 @@ public class HMMBuild extends HmmerCommand
     {
       hmmFile = createTempFile("hmm", ".hmm");
       alignmentFile = createTempFile("output", ".sto");
-      List<SequenceI> seqs = ac.getSequences();
-      List<SequenceI> copy = new ArrayList<>();
-      copy.addAll(seqs);
 
       if (ac instanceof Alignment)
       {
@@ -165,21 +160,20 @@ public class HMMBuild extends HmmerCommand
         }
       }
 
-      /*
-       * 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);
         }
       }
 
@@ -188,7 +182,7 @@ public class HMMBuild extends HmmerCommand
 
       exportStockholm(copyArray, alignmentFile, ac);
 
-      recoverSequences(sequencesHash, seqs.toArray(new SequenceI[] {}));
+      recoverSequences(sequencesHash, copy.toArray(new SequenceI[] {}));
 
       boolean ran = runCommand(alignmentFile, hmmFile, ac);
       if (!ran)
@@ -213,6 +207,33 @@ public class HMMBuild extends HmmerCommand
   }
 
   /**
+   * 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)
+  {
+    SequenceI hmmSeq = ac.getHmmConsensus();
+    if (hmmSeq != null)
+    {
+      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
@@ -240,7 +261,7 @@ public class HMMBuild extends HmmerCommand
      * 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 = "";
@@ -311,16 +332,15 @@ public class HMMBuild extends HmmerCommand
   {
     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 (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);