JAL-2629 fix incorrect hmmbuild on groups behaviour
[jalview.git] / src / jalview / hmmer / HMMBuild.java
index d95be48..0c47c1d 100644 (file)
@@ -5,6 +5,7 @@ import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
+import jalview.datamodel.ResidueCount;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
@@ -12,6 +13,7 @@ import jalview.gui.JvOptionPane;
 import jalview.io.DataSourceType;
 import jalview.io.FileParse;
 import jalview.io.HMMFile;
+import jalview.util.FileUtils;
 import jalview.util.MessageManager;
 import jalview.ws.params.ArgumentI;
 
@@ -95,20 +97,26 @@ public class HMMBuild extends HmmerCommand
           AlignViewportI viewport)
   {
     List<AnnotatedCollectionI> runBuildFor = new ArrayList<>();
+    boolean foundArg = false;
+
     for (ArgumentI arg : params)
     {
       String name = arg.getName();
       if (MessageManager.getString("label.hmmbuild_for").equals(name))
       {
+        foundArg = true;
         String value = arg.getValue();
+
         if (MessageManager.getString("label.alignment").equals(value))
         {
-          runBuildFor.add(alignment);
+          runBuildFor.add(viewport.getAlignmentView(false)
+                  .getVisibleAlignment('-'));
         }
         else if (MessageManager.getString("label.groups_and_alignment")
                 .equals(value))
         {
-          runBuildFor.add(alignment);
+          runBuildFor.add(viewport.getAlignmentView(false)
+                  .getVisibleAlignment('-'));
           runBuildFor.addAll(viewport.getAlignment().getGroups());
         }
         else if (MessageManager.getString("label.groups").equals(value))
@@ -133,6 +141,15 @@ public class HMMBuild extends HmmerCommand
         }
       }
     }
+
+    /*
+     * default is to build for the whole alignment
+     */
+    if (!foundArg)
+    {
+      runBuildFor.add(alignment);
+    }
+
     return runBuildFor;
   }
 
@@ -147,8 +164,8 @@ public class HMMBuild extends HmmerCommand
     File alignmentFile = null;
     try
     {
-      hmmFile = createTempFile("hmm", ".hmm");
-      alignmentFile = createTempFile("output", ".sto");
+      hmmFile = FileUtils.createTempFile("hmm", ".hmm");
+      alignmentFile = FileUtils.createTempFile("output", ".sto");
 
       if (ac instanceof Alignment)
       {
@@ -170,12 +187,18 @@ public class HMMBuild extends HmmerCommand
       else
       {
         SequenceI[] sel = ((SequenceGroup) ac)
-                .getSelectionAsNewSequences((AlignmentI) ac.getContext());
+                                               .getSelectionAsNewSequences((AlignmentI) ac.getContext());
         for (SequenceI seq : sel)
         {
-          copy.add(seq);
+          if (seq != null)
+          {
+            copy.add(seq);
+          }
         }
       }
+      // TODO rather than copy alignment data we should anonymize in situ -
+      // export/File import could use anonymization hash to reinstate references
+      // at import level ?
 
       SequenceI[] copyArray = copy.toArray(new SequenceI[copy.size()]);
       Hashtable sequencesHash = stashSequences(copyArray);
@@ -187,6 +210,8 @@ public class HMMBuild extends HmmerCommand
       boolean ran = runCommand(alignmentFile, hmmFile, ac);
       if (!ran)
       {
+        JvOptionPane.showInternalMessageDialog(af, MessageManager
+                .formatMessage("warn.command_failed", "hmmbuild"));
         return;
       }
       importData(hmmFile, ac);
@@ -207,33 +232,6 @@ 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
@@ -311,8 +309,8 @@ public class HMMBuild extends HmmerCommand
       args.add(ARG_DNA);
     }
 
-    args.add(hmmFile.getAbsolutePath());
-    args.add(sequencesFile.getAbsolutePath());
+    args.add(getFilePath(hmmFile, true));
+    args.add(getFilePath(sequencesFile, true));
 
     return runCommand(args);
   }
@@ -330,10 +328,27 @@ public class HMMBuild extends HmmerCommand
   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 hmmSeq = file.getHMM().getConsensusSequence();
 
+
+
+    ResidueCount counts = new ResidueCount(alignment.getSequences());
+    hmmSeq.getHMM().setBackgroundFrequencies(counts);
+
+    if (hmmSeq == null)
+    {
+      // hmmbuild failure not detected earlier
+      return;
+    }
+
     if (ac instanceof SequenceGroup)
     {
       SequenceGroup grp = (SequenceGroup) ac;
@@ -346,17 +361,10 @@ public class HMMBuild extends HmmerCommand
       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);
     }
   }
 }