Merge branch 'develop' into features/hmmer
[jalview.git] / src / jalview / gui / Preferences.java
index c3c9239..7af3abc 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;
@@ -52,6 +58,7 @@ import java.awt.event.MouseEvent;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Scanner;
 
 import javax.help.HelpSetException;
 import javax.swing.JColorChooser;
@@ -102,6 +109,8 @@ public class Preferences extends GPreferences
   public static final String STRUCTURE_DISPLAY = "STRUCTURE_DISPLAY";
 
   public static final String CHIMERA_PATH = "CHIMERA_PATH";
+  
+  public static final String HMMER_PATH = "HMMER_PATH";
 
   public static final String SORT_ANNOTATIONS = "SORT_ANNOTATIONS";
 
@@ -155,7 +164,7 @@ public class Preferences extends GPreferences
      * .properties file as '|' separated strings
      */
 
-    groupURLLinks = new ArrayList<String>();
+    groupURLLinks = new ArrayList<>();
   }
 
   JInternalFrame frame;
@@ -199,6 +208,34 @@ public class Preferences extends GPreferences
     frame.setMinimumSize(new Dimension(width, height));
 
     /*
+     * Set HMMER tab defaults
+     */
+    trimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
+    isHMMERInstalled
+            .setSelected(Cache.getDefault("HMMER_INSTALLED", false));
+    if (Cache.getDefault("USE_UNIPROT", false))
+    {
+      uniprot.setSelected(true);
+    }
+    else
+    {
+      alignment.setSelected(true);
+    }
+    numberOfSequencesToKeepField
+            .setText(Cache.getProperty("SEQUENCES_TO_KEEP"));
+    installationLocation.setEnabled(isHMMERInstalled.isSelected());
+    hmmerPath.setEnabled(isHMMERInstalled.isSelected());
+    hmmerPath.setText(Cache.getProperty(HMMER_PATH));
+    hmmerPath.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        validateHMMERPath();
+      }
+    });
+
+    /*
      * Set Visual tab defaults
      */
     seqLimit.setSelected(Cache.getDefault("SHOW_JVSUFFIX", true));
@@ -221,6 +258,9 @@ public class Preferences extends GPreferences
             Cache.getDefault("SHOW_CONSENSUS_HISTOGRAM", true));
     showConsensLogo
             .setSelected(Cache.getDefault("SHOW_CONSENSUS_LOGO", false));
+    showInformationHistogram.setSelected(
+            Cache.getDefault("SHOW_INFORMATION_HISTOGRAM", true));
+    showHMMLogo.setSelected(Cache.getDefault("SHOW_HMM_LOGO", false));
     showNpTooltip
             .setSelected(Cache.getDefault("SHOW_NPFEATS_TOOLTIP", true));
     showDbRefTooltip
@@ -585,6 +625,10 @@ public class Preferences extends GPreferences
             Boolean.toString(showConsensHistogram.isSelected()));
     Cache.applicationProperties.setProperty("SHOW_CONSENSUS_LOGO",
             Boolean.toString(showConsensLogo.isSelected()));
+    Cache.applicationProperties.setProperty("SHOW_INFORMATION_HISTOGRAM",
+            Boolean.toString(showConsensHistogram.isSelected()));
+    Cache.applicationProperties.setProperty("SHOW_HMM_LOGO",
+            Boolean.toString(showHMMLogo.isSelected()));
     Cache.applicationProperties.setProperty("ANTI_ALIAS",
             Boolean.toString(smoothFont.isSelected()));
     Cache.applicationProperties.setProperty(SCALE_PROTEIN_TO_CDNA,
@@ -631,6 +675,44 @@ public class Preferences extends GPreferences
             maxColour.getBackground());
 
     /*
+     * Save HMMER settings
+     */
+    Cache.applicationProperties.setProperty("TRIM_TERMINI",
+            Boolean.toString(trimTermini.isSelected()));
+    Cache.applicationProperties.setProperty("USE_UNIPROT",
+            Boolean.toString(uniprot.isSelected()));
+    Cache.applicationProperties.setProperty("SEQUENCES_TO_KEEP",
+            numberOfSequencesToKeepField.getText());
+    Cache.applicationProperties.setProperty(HMMER_PATH,
+            hmmerPath.getText());
+    boolean hmmerInstalled = isHMMERInstalled.isSelected();
+    Cache.applicationProperties.setProperty("HMMER_INSTALLED",
+            Boolean.toString(hmmerInstalled));
+    boolean hmmerFunctioning = validateHMMERPath(false);
+    Cache.applicationProperties.setProperty("HMMER_FUNCTIONING",
+            Boolean.toString(hmmerFunctioning));
+    AlignFrame[] frames = Desktop.getAlignFrames();
+    boolean hmmerStatus = hmmerFunctioning && hmmerInstalled ? true : false;
+    for (AlignFrame frame : frames)
+    {
+      frame.updateHMMERStatus(hmmerStatus);
+    }
+    
+    
+    trimTermini.setSelected(Cache.getDefault("TRIM_TERMINI", false));
+    if (Cache.getDefault("USE_UNIPROT", false))
+    {
+      uniprot.setSelected(true);
+    }
+    else
+    {
+      alignment.setSelected(true);
+    }
+    numberOfSequencesToKeepField
+            .setText(Cache.getProperty("SEQUENCES_TO_KEEP"));
+    hmmerPath.setText(Cache.getProperty(HMMER_PATH));
+
+    /*
      * Save Structure settings
      */
     Cache.applicationProperties.setProperty(ADD_TEMPFACT_ANN,
@@ -787,6 +869,14 @@ public class Preferences extends GPreferences
       structureTab.requestFocusInWindow();
       return false;
     }
+    if (isHMMERInstalled.isSelected())
+    {
+      if (!validateHMMER())
+      {
+        hmmerTab.requestFocusInWindow();
+        return false;
+      }
+    }
     return true;
   }
 
@@ -797,6 +887,13 @@ public class Preferences extends GPreferences
 
   }
 
+  @Override
+  protected boolean validateHMMER()
+  {
+    return validateHMMERPath();
+
+  }
+
   /**
    * DOCUMENT ME!
    */
@@ -863,6 +960,8 @@ public class Preferences extends GPreferences
             && (identity.isSelected() || showGroupConsensus.isSelected()));
     showConsensLogo.setEnabled(annotations.isSelected()
             && (identity.isSelected() || showGroupConsensus.isSelected()));
+    showInformationHistogram.setEnabled(annotations.isSelected());
+    showHMMLogo.setEnabled(annotations.isSelected());
   }
 
   @Override
@@ -1087,6 +1186,171 @@ public class Preferences extends GPreferences
     }
     return true;
   }
+  
+  /**
+   * Returns true if hmmer path contains the necessary valid executables, else
+   * show an error dialog (if showing dialog).
+   */
+  private boolean validateHMMERPath(boolean showDialog)
+  {
+    int missing = 0;
+    String message = "";
+    String folder = hmmerPath.getText().trim();
+    if (folder.length() > 0)
+    {
+      File f = new File(folder);
+      if (!f.exists())
+      {
+        if (showDialog)
+        {
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                MessageManager.getString("label.folder_not_exists"),
+                MessageManager.getString("label.invalid_folder"),
+                JvOptionPane.ERROR_MESSAGE);
+        }
+        return false;
+      }
+      AlignmentI alignment = new Alignment(
+              new SequenceI[]
+      { new Sequence("test", "WLWL", 0, 3) });
+      if (canExecute(folder + "/hmmbuild"))
+        {
+        validateHMMBuild(alignment);
+        }
+      else
+      {
+        message += MessageManager.getString("label.hmmbuild_not_found")
+                + "\n";
+        missing++;
+      }
+
+
+      if (canExecute(folder + "/hmmalign"))
+        {
+
+        }
+      else
+      {
+        message += MessageManager.getString("label.hmmalign_not_found")
+                + "\n";
+        missing++;
+      }
+
+
+      if (canExecute(folder + "/hmmsearch"))
+        {
+
+        }
+      else
+      {
+        message += MessageManager.getString("label.hmmsearch_not_found")
+                + "\n";
+        missing++;
+      }
+      }
+
+      if (missing > 0)
+      {
+        if (missing < 3)
+        {
+          if (showDialog)
+          {
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop, message,
+                  MessageManager.getString("label.invalid_folder"),
+                  JvOptionPane.ERROR_MESSAGE);
+          }
+          return false;
+        }
+        else
+        {
+          if (showDialog)
+          {
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                  MessageManager.getString("label.no_binaries"),
+                  MessageManager.getString("label.invalid_folder"),
+                  JvOptionPane.ERROR_MESSAGE);
+          }
+
+          return false;
+        }
+      }
+
+    return true;
+  }
+
+  /**
+   * Checks if a file can be executed
+   * 
+   * @param path
+   *          the path to the file
+   * @return
+   */
+  public boolean canExecute(String path)
+  {
+    File file = new File(path);
+    if (!file.canExecute())
+    {
+      file = new File(path + ".exe");
+      {
+        if (!file.canExecute())
+        {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
+
+  /**
+   * Runs hmmbuild to check if it is working. While doing this it parses the
+   * version of HMMER.
+   * 
+   * @param frame
+   * @return
+   */
+  public boolean validateHMMBuild(AlignmentI alignment)
+  {
+    HMMBuildThread hmmbuild = new HMMBuildThread(alignment);
+    hmmbuild.hmmbuildWaitTillComplete();
+    SequenceI hmmSeq = alignment.getSequenceAt(1);
+    HiddenMarkovModel hmm;
+    if (hmmSeq.isHMMConsensusSequence() && hmmSeq.getHMM() != null)
+    {
+      hmm = hmmSeq.getHMM();
+
+      if (hmm.getNumberOfSymbols() < 1)
+      {
+        return false;
+      }
+    }
+    else
+    {
+      return false;
+    }
+
+    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);
+  }
 
   /**
    * If Chimera is selected, check it can be found on default or user-specified