JAL-2629 simplifying method signatures in Hmmer command classes
[jalview.git] / src / jalview / hmmer / HmmerCommand.java
index a9d7018..557598b 100644 (file)
@@ -1,5 +1,6 @@
 package jalview.hmmer;
 
+import jalview.analysis.SeqsetUtils;
 import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -14,6 +15,7 @@ import jalview.gui.Preferences;
 import jalview.io.HMMFile;
 import jalview.io.StockholmFile;
 import jalview.util.MessageManager;
+import jalview.ws.params.ArgumentI;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -33,28 +35,17 @@ public class HmmerCommand
 {
   public static final String HMMBUILD = "hmmbuild";
 
-  public String JALVIEWDIRECTORY = System.getProperty("user.dir")
-          + File.separator;
+  private Hashtable hash = new Hashtable();
 
-  public String OUTPUTALIGNMENT;
-
-  public final String SPACE = " ";
-
-  public final String ALLCOL = "--allcol ";
-
-  public final String TRIM = "--trim ";
-
-  public final String FORCEAMINO = "--amino ";
-
-  public final String FORCEDNA = "--dna ";
-
-  public final String FORCERNA = "--rna ";
-
-  Hashtable hash = new Hashtable();
+  protected AlignFrame af;
 
-  List<SequenceI> hmmSeqs;
+  protected List<ArgumentI> params;
 
-  protected AlignFrame af;
+  public HmmerCommand(AlignFrame alignFrame, List<ArgumentI> args)
+  {
+    af = alignFrame;
+    params = args;
+  }
 
   public static boolean isHmmerAvailable()
   {
@@ -64,39 +55,40 @@ public class HmmerCommand
 
   /**
    * Uniquifies the sequences when exporting and stores their details in a
-   * hashtable.
+   * hashtable
    * 
    * @param seqs
    */
-  public void uniquifySequences(SequenceI[] seqs)
+  protected void stashSequences(SequenceI[] seqs)
   {
-    hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
+    hash = SeqsetUtils.uniquify(seqs, true);
   }
 
   /**
-   * Recover the sequence data lost by uniquifying.
+   * Restores the sequence data lost by uniquifying
    * 
    * @param seqs
    */
-  public void recoverSequenceNames(SequenceI[] seqs)
+  protected void recoverSequences(SequenceI[] seqs)
   {
-    jalview.analysis.SeqsetUtils.deuniquify(hash, seqs);
+    SeqsetUtils.deuniquify(hash, seqs);
   }
 
   /**
-   * Runs a command in the command line.
+   * Runs a command as a separate process
    * 
    * @param command
+   *          the executable command and any arguments to it
    * @throws IOException
-   * @throws InterruptedException
    */
-  public boolean runCommand(String command)
-          throws IOException, InterruptedException
+  public boolean runCommand(List<String> command)
+          throws IOException
   {
     try
     {
-      final Process p = Runtime.getRuntime().exec(command);
-
+      ProcessBuilder pb = new ProcessBuilder(command);
+      pb.redirectErrorStream(true); // send syserr to sysout
+      final Process p = pb.start();
       new Thread(new Runnable()
       {
         @Override
@@ -104,13 +96,13 @@ public class HmmerCommand
         {
           BufferedReader input = new BufferedReader(
                   new InputStreamReader(p.getInputStream()));
-          String line = null;
-
           try
           {
-            while ((line = input.readLine()) != null)
+            String line = input.readLine();
+            while (line != null)
             {
               System.out.println(line);
+              line = input.readLine();
             }
           } catch (IOException e)
           {
@@ -120,30 +112,33 @@ public class HmmerCommand
       }).start();
 
       p.waitFor();
+      return p.exitValue() == 0; // 0 is success, by convention
     } catch (Exception e)
     {
       e.printStackTrace();
       return false;
     }
-    return true;
   }
 
   /**
-   * Exports an alignment and/or HMM to the specified file.
+   * Exports an alignment (and possibly annotation) to the specified file, in
+   * Stockholm format
    * 
-   * @param alignment
+   * @param seqs
+   * @param toFile
+   * @param annotated
    * @throws IOException
    */
-  public void exportData(SequenceI[] seqs, File stoLocation,
-          HiddenMarkovModel hmm, File hmmLocation, AnnotatedCollectionI al)
+  public void exportStockholm(SequenceI[] seqs, File toFile,
+          AnnotatedCollectionI annotated)
           throws IOException
   {
     if (seqs != null)
     {
       AlignmentI newAl = new Alignment(seqs);
-      if (stoLocation != null && al != null)
+      if (toFile != null && annotated != null)
       {
-        for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
+        for (AlignmentAnnotation annot : annotated.getAlignmentAnnotation())
         {
           if (annot.label.contains("Reference") || "RF".equals(annot.label))
           {
@@ -167,38 +162,10 @@ public class HmmerCommand
 
       StockholmFile file = new StockholmFile(newAl);
       String output = file.print(seqs, false);
-      PrintWriter writer = new PrintWriter(stoLocation);
+      PrintWriter writer = new PrintWriter(toFile);
       writer.println(output);
       writer.close();
     }
-
-    if (hmm != null)
-    {
-      HMMFile file = new HMMFile(hmm);
-      PrintWriter writer = new PrintWriter(hmmLocation);
-      writer.print(file.print());
-      writer.close();
-    }
-  }
-
-  /**
-   * Returns the list of HMM sequences removed
-   * 
-   * @return
-   */
-  public List<SequenceI> getHmmSeqs()
-  {
-    return hmmSeqs;
-  }
-
-  /**
-   * Sets the list of removed HMM sequences
-   * 
-   * @param hmmSeqs
-   */
-  public void setHmmSeqs(List<SequenceI> hmmSeqs)
-  {
-    this.hmmSeqs = hmmSeqs;
   }
 
   /**
@@ -209,7 +176,7 @@ public class HmmerCommand
    *          command short name e.g. hmmalign
    * @return
    */
-  protected String getCommandRoot(String cmd)
+  protected String getCommandPath(String cmd)
   {
     String binariesFolder = Cache.getProperty(Preferences.HMMER_PATH);
     File file = getExecutable(cmd, binariesFolder);
@@ -248,4 +215,41 @@ public class HmmerCommand
     }
     return file;
   }
+
+  /**
+   * A convenience method to create a temporary file that is deleted on exit of
+   * the JVM
+   * 
+   * @param prefix
+   * @param suffix
+   * @return
+   * @throws IOException
+   */
+  protected File createTempFile(String prefix, String suffix)
+          throws IOException
+  {
+    File f = File.createTempFile(prefix, suffix);
+    f.deleteOnExit();
+    return f;
+
+  }
+
+  /**
+   * Exports an HMM to the specified file
+   * 
+   * @param hmm
+   * @param hmmFile
+   * @throws IOException
+   */
+  public void exportHmm(HiddenMarkovModel hmm, File hmmFile)
+          throws IOException
+  {
+    if (hmm != null)
+    {
+      HMMFile file = new HMMFile(hmm);
+      PrintWriter writer = new PrintWriter(hmmFile);
+      writer.print(file.print());
+      writer.close();
+    }
+  }
 }