JAL-2629 add hmmbuild validation check which also gets the HMMER version
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Wed, 16 Aug 2017 09:56:57 +0000 (10:56 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Wed, 16 Aug 2017 09:56:57 +0000 (10:56 +0100)
src/jalview/gui/Preferences.java
src/jalview/hmmer/HMMBuildThread.java
src/jalview/io/HMMFile.java
test/jalview/io/HMMFileTest.java

index 0546264..c8fef96 100755 (executable)
@@ -22,8 +22,14 @@ package jalview.gui;
 
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenMarkovModel;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
 import jalview.gui.Help.HelpId;
 import jalview.gui.StructureViewer.ViewerType;
+import jalview.hmmer.HMMBuildThread;
 import jalview.io.FileFormatI;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
@@ -50,7 +56,6 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Scanner;
@@ -692,11 +697,6 @@ public class Preferences extends GPreferences
     {
       frame.updateHMMERStatus(hmmerStatus);
     }
-    if (hmmerStatus)
-    {
-      // String ver = getHMMERVersion();
-      Cache.setProperty("HMMER_VERSION", "3.1b2");
-    }
     
     
     trimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
@@ -1208,28 +1208,43 @@ public class Preferences extends GPreferences
         }
         return false;
       }
-      if (!canExecute(folder + "/hmmbuild"))
+      AlignmentI alignment = new Alignment(
+              new SequenceI[]
+      { new Sequence("test", "WLWL", 0, 3) });
+      if (canExecute(folder + "/hmmbuild"))
         {
-          message += MessageManager.getString("label.hmmbuild_not_found")
-                  + "\n";
-          missing++;
+        validateHMMBuild(alignment);
         }
+      else
+      {
+        message += MessageManager.getString("label.hmmbuild_not_found")
+                + "\n";
+        missing++;
+      }
 
 
-      if (!canExecute(folder + "/hmmalign"))
+      if (canExecute(folder + "/hmmalign"))
         {
-          message += MessageManager.getString("label.hmmalign_not_found")
-                  + "\n";
-          missing++;
+
         }
+      else
+      {
+        message += MessageManager.getString("label.hmmalign_not_found")
+                + "\n";
+        missing++;
+      }
 
 
-      if (!canExecute(folder + "/hmmsearch"))
+      if (canExecute(folder + "/hmmsearch"))
         {
-          message += MessageManager.getString("label.hmmsearch_not_found")
-                  + "\n";
-          missing++;
+
         }
+      else
+      {
+        message += MessageManager.getString("label.hmmsearch_not_found")
+                + "\n";
+        missing++;
+      }
       }
 
       if (missing > 0)
@@ -1285,28 +1300,51 @@ public class Preferences extends GPreferences
   }
 
   /**
-   * Gets the current version of hmmer
+   * Runs hmmbuild to check if it is working. While doing this it parses the
+   * version of HMMER.
    * 
+   * @param frame
    * @return
    */
-  private String getHMMERVersion()
+  public boolean validateHMMBuild(AlignmentI alignment)
   {
-    File file = new File(hmmerPath.getText() + "/release-notes");
-    Scanner scanner = null;
-    try
+    HMMBuildThread hmmbuild = new HMMBuildThread(alignment);
+    hmmbuild.hmmbuildWaitTillComplete();
+    SequenceI hmmSeq = alignment.getSequenceAt(1);
+    HiddenMarkovModel hmm;
+    if (hmmSeq.isHMMConsensusSequence() && hmmSeq.getHMM() != null)
     {
-      scanner = new Scanner(file);
-    } catch (IOException e)
+      hmm = hmmSeq.getHMM();
+
+      if (hmm.getNumberOfSymbols() < 1)
+      {
+        return false;
+      }
+    }
+    else
     {
-      e.printStackTrace();
-      return null;
+      return false;
     }
-    scanner.next();
-    String res = scanner.next();
-    scanner.close();
-    return res;
+
+    String header = hmm.getFileHeader();
+    if (header == null)
+    {
+      return false;
+    }
+    else
+    {
+      Scanner scanner = new Scanner(header);
+      scanner.next();
+      String string = scanner.next();
+      String version = string.substring(1);
+      Cache.setProperty("HMMER_VERSION", version);
+      scanner.close();
+
+    }
+    return true;
   }
 
+
   private boolean validateHMMERPath()
   {
     return validateHMMERPath(true);
index 880ed0d..656b6e5 100644 (file)
@@ -1,16 +1,20 @@
 package jalview.hmmer;
 
+
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.AlignViewport;
 import jalview.gui.JvOptionPane;
 import jalview.gui.Preferences;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
 import jalview.io.FileLoader;
+import jalview.io.FileParse;
+import jalview.io.HMMFile;
 import jalview.util.MessageManager;
 
 import java.io.File;
@@ -24,11 +28,13 @@ public class HMMBuildThread implements Runnable
 {
   HMMERCommands cmds = new HMMERCommands();
   AlignFrame af;
+
+  AlignViewport viewport;
   AlignmentI alignment;
   SequenceGroup group;
 
+
   boolean forGroup = false;
-  boolean canRun = true;
 
   File hmmTemp = null;
 
@@ -36,6 +42,17 @@ public class HMMBuildThread implements Runnable
 
   long barID;
   
+  /**
+   * This is used for validation purposes. Do not use!
+   * 
+   * @param viewport
+   */
+  public HMMBuildThread(AlignmentI alignment)
+  {
+    this.alignment = alignment;
+    forGroup = false;
+  }
+
   public HMMBuildThread(AlignFrame af)
   {
     this.af = af;
@@ -52,8 +69,11 @@ public class HMMBuildThread implements Runnable
   public void run()
   {
     barID = System.currentTimeMillis();
+    if (af != null)
+    {
     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
             barID);
+    }
     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
     if (alignment == null && group == null)
     {
@@ -94,8 +114,11 @@ public class HMMBuildThread implements Runnable
         }
         if (array.length < 1)
         {
-          JOptionPane.showMessageDialog(af,
+          if (af != null)
+          {
+            JOptionPane.showMessageDialog(af,
                   MessageManager.getString("warn.no_sequence_data"));
+          }
           return;
         }
         SequenceI[] newArr = new SequenceI[array.length];
@@ -121,8 +144,11 @@ public class HMMBuildThread implements Runnable
         boolean ran = runCommand();
         if (!ran)
         {
-          JvOptionPane.showInternalMessageDialog(af,
+          if (af != null)
+          {
+            JvOptionPane.showInternalMessageDialog(af,
                   MessageManager.getString("warn.hmmbuild_failed"));
+          }
           return;
         }
     } catch (IOException | InterruptedException e)
@@ -144,8 +170,12 @@ public class HMMBuildThread implements Runnable
       e.printStackTrace();
     } finally
     {
-      af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
+      if (af != null)
+      {
+        af.setProgressBar(
+                MessageManager.getString("status.running_hmmbuild"),
               barID);
+      }
     }
   }
 
@@ -172,7 +202,11 @@ public class HMMBuildThread implements Runnable
     }
     else
     {
-      String name = af.getName();
+      String name = null;
+      if (af != null)
+      {
+        name = af.getName();
+      }
       if (name == null)
       {
         name = "Alignment";
@@ -196,20 +230,38 @@ public class HMMBuildThread implements Runnable
   
   private void importData() throws IOException, InterruptedException
   {
-    cmds.addHMMConsensusSequences(af);
-    FileLoader loader = new FileLoader();
-    loader.LoadFileOntoAlignmentWaitTillLoaded(af.getViewport(),
-            hmmTemp.getAbsolutePath(), DataSourceType.FILE,
-            FileFormat.HMMER3);
+    if (af != null)
+    {
+      cmds.addHMMConsensusSequences(af);
+
+      FileLoader loader = new FileLoader();
+      loader.LoadFileOntoAlignmentWaitTillLoaded(viewport,
+              hmmTemp.getAbsolutePath(), DataSourceType.FILE,
+              FileFormat.HMMER3);
+    }
+    else
+    {
+      HMMFile file = new HMMFile(new FileParse(hmmTemp.getAbsolutePath(),
+              DataSourceType.FILE));
+      alignment.addSequence(file.getSeqsAsArray()[0]);
+    }
     hmmTemp.delete();
     stoTemp.delete();
   }
   
-  public boolean canRun()
+  public void hmmbuildWaitTillComplete()
   {
-    return canRun;
+    Thread loader = new Thread(this);
+    loader.start();
+
+    while (loader.isAlive())
+    {
+      try
+      {
+        Thread.sleep(500);
+      } catch (Exception ex)
+      {
+      }
+    }
   }
-  
-  
 }
index bf86c68..0f79c64 100644 (file)
@@ -16,7 +16,7 @@ import java.util.Scanner;
 
 
 /**
- * Adds capability to read in and write out HMMER3 files. Currently only supports HMMER3/f.
+ * Adds capability to read in and write out HMMER3 files. .
  * 
  * 
  * @author TZVanaalten
@@ -247,7 +247,7 @@ public class HMMFile extends AlignFile
    */
   void parseAnnotations(Scanner scanner, int index)
   {
-    if (hmm.mapIsActive())
+    if (hmm.mapIsActive() && scanner.hasNext())
     {
       int column;
       column = scanner.nextInt();
@@ -298,9 +298,10 @@ public class HMMFile extends AlignFile
    * @param numberOfElements
    *          The number of elements in the list to be filled.
    * @return filled list Returns the list of doubles.
+   * @throws IOException
    */
   static List<Double> fillList(Scanner input,
-          int numberOfElements)
+          int numberOfElements) throws IOException
   {
     List<Double> list = new ArrayList<>();
     for (int i = 0; i < numberOfElements; i++)
@@ -321,6 +322,10 @@ public class HMMFile extends AlignFile
         list.add(prob);
       }
     }
+    if (list.size() < numberOfElements)
+    {
+      throw new IOException("Incomplete data");
+    }
     return list;
   }
 
index 15d51c2..f16d6f5 100644 (file)
@@ -204,7 +204,7 @@ public class HMMFileTest {
   }
   
   @Test
-  public void testFillList()
+  public void testFillList() throws IOException
   {
     Scanner scanner1 = new Scanner("1.3 2.4 5.3 3.9 9.8 4.7 4.3 2.3 6.9");
     ArrayList<Double> filledArray = new ArrayList<>();