Merge branch 'develop' into features/mchmmer
[jalview.git] / src / jalview / gui / AlignFrame.java
index 5d698c0..054991a 100644 (file)
@@ -54,6 +54,7 @@ import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.HiddenColumns;
+import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.HiddenSequences;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SeqCigar;
@@ -62,6 +63,12 @@ import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.ColourMenuHelper.ColourChangeListener;
 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
+import jalview.hmmer.HMMAlign;
+import jalview.hmmer.HMMBuild;
+import jalview.hmmer.HMMERParamStore;
+import jalview.hmmer.HMMERPreset;
+import jalview.hmmer.HMMSearch;
+import jalview.hmmer.HmmerCommand;
 import jalview.io.AlignmentProperties;
 import jalview.io.AnnotationFile;
 import jalview.io.BioJsHTMLOutput;
@@ -88,6 +95,7 @@ import jalview.schemes.ColourSchemes;
 import jalview.schemes.ResidueColourScheme;
 import jalview.schemes.TCoffeeColourScheme;
 import jalview.util.MessageManager;
+import jalview.util.StringUtils;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.ViewportRanges;
 import jalview.ws.DBRefFetcher;
@@ -95,6 +103,9 @@ import jalview.ws.DBRefFetcher.FetchFinishedListenerI;
 import jalview.ws.jws1.Discoverer;
 import jalview.ws.jws2.Jws2Discoverer;
 import jalview.ws.jws2.jabaws2.Jws2Instance;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws.params.WsParamSetI;
 import jalview.ws.seqfetcher.DbSourceProxy;
 
 import java.awt.BorderLayout;
@@ -124,22 +135,29 @@ import java.awt.print.PrinterJob;
 import java.beans.PropertyChangeEvent;
 import java.io.File;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Deque;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
+import java.util.Scanner;
 import java.util.Vector;
 
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JEditorPane;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
 import javax.swing.JLayeredPane;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.swing.JScrollPane;
 import javax.swing.SwingUtilities;
 
@@ -153,6 +171,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         IProgressIndicator, AlignViewControllerGuiI, ColourChangeListener
 {
 
+  Map<String, Float> distribution = new HashMap<>(); // temporary
+
   public static final int DEFAULT_WIDTH = 700;
 
   public static final int DEFAULT_HEIGHT = 500;
@@ -165,6 +185,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   AlignViewport viewport;
 
   public AlignViewControllerI avc;
+  /*
+   * The selected HMM for this align frame
+   */
+  SequenceI selectedHMMSequence;
 
   List<AlignmentPanel> alignPanels = new ArrayList<>();
 
@@ -178,6 +202,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    */
   String fileName = null;
 
+
   /**
    * Creates a new AlignFrame object with specific width and height.
    * 
@@ -762,6 +787,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       ap.av.updateConservation(ap);
       ap.av.updateConsensus(ap);
       ap.av.updateStrucConsensus(ap);
+      ap.av.updateInformation(ap);
     }
   }
 
@@ -904,6 +930,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     showConsensusHistogram.setSelected(av.isShowConsensusHistogram());
     showSequenceLogo.setSelected(av.isShowSequenceLogo());
     normaliseSequenceLogo.setSelected(av.isNormaliseSequenceLogo());
+    showInformationHistogram.setSelected(av.isShowInformationHistogram());
+    showHMMSequenceLogo.setSelected(av.isShowHMMSequenceLogo());
+    normaliseHMMSequenceLogo.setSelected(av.isNormaliseHMMSequenceLogo());
 
     ColourMenuHelper.setColourSelected(colourMenu,
             av.getGlobalColourScheme());
@@ -995,6 +1024,202 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   @Override
+  public void hmmBuildSettings_actionPerformed()
+  {
+    if (!(alignmentIsSufficient(1)))
+    {
+      return;
+    }
+    WsParamSetI set = new HMMERPreset();
+    List<ArgumentI> args = new ArrayList<>();
+    ParamDatastoreI store = new HMMERParamStore("hmmbuild");
+    WsJobParameters params = new WsJobParameters(new JFrame(), store, set,
+            args);
+    if (params.showRunDialog())
+    {
+      new Thread(new HMMBuild(this, params.getJobParams())).start();
+    }
+    alignPanel.repaint();
+
+  }
+
+  @Override
+  public void hmmAlignSettings_actionPerformed()
+  {
+    if (!(checkForHMM() && alignmentIsSufficient(2)))
+    {
+      return;
+    }
+    WsParamSetI set = new HMMERPreset();
+    List<ArgumentI> args = new ArrayList<>();
+    ParamDatastoreI store = new HMMERParamStore("hmmalign");
+    WsJobParameters params = new WsJobParameters(new JFrame(), store, set,
+            args);
+    if (params.showRunDialog())
+    {
+      new Thread(new HMMAlign(this, params.getJobParams()))
+            .start();
+    }
+    alignPanel.repaint();
+  }
+
+  @Override
+  public void hmmSearchSettings_actionPerformed()
+  {
+    if (!checkForHMM())
+    {
+      return;
+    }
+    WsParamSetI set = new HMMERPreset();
+    List<ArgumentI> args = new ArrayList<>();
+    ParamDatastoreI store = new HMMERParamStore("hmmsearch");
+    WsJobParameters params = new WsJobParameters(new JFrame(), store, set,
+            args);
+    if (params.showRunDialog())
+    {
+      new Thread(new HMMSearch(this, params.getJobParams()))
+            .start();
+    }
+    alignPanel.repaint();
+  }
+
+  @Override
+  public void hmmBuildRun_actionPerformed()
+  {
+    if (!alignmentIsSufficient(1))
+    {
+      return;
+    }
+    new Thread(new HMMBuild(this, null))
+            .start();
+  }
+
+  @Override
+  public void hmmAlignRun_actionPerformed()
+  {
+    if (!(checkForHMM() && alignmentIsSufficient(2)))
+    {
+      return;
+    }
+    new Thread(new HMMAlign(this, null)).start();
+  }
+
+  @Override
+  public void hmmSearchRun_actionPerformed()
+  {
+    if (!checkForHMM())
+    {
+      return;
+    }
+    new Thread(new HMMSearch(this, null)).start();
+  }
+
+  /**
+   * Checks if the frame has a selected hidden Markov model
+   * 
+   * @return
+   */
+  private boolean checkForHMM()
+  {
+    if (getSelectedHMM() == null)
+    {
+      JOptionPane.showMessageDialog(this,
+              MessageManager.getString("warn.no_selected_hmm"));
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * Checks if the alignment contains the required number of sequences.
+   * 
+   * @param required
+   * @return
+   */
+  public boolean alignmentIsSufficient(int required)
+  {
+    if (getViewport().getAlignment().getSequences().size() < required)
+    {
+      JOptionPane.showMessageDialog(this,
+              MessageManager.getString("warn.not_enough_sequences"));
+      return false;
+    }
+    return true;
+  }
+
+  @Override
+  public void addDatabase_actionPerformed() throws IOException
+  {
+    if (Cache.getProperty(Preferences.HMMSEARCH_DB_PATHS) == null)
+    {
+      Cache.setProperty(Preferences.HMMSEARCH_DBS, "");
+      Cache.setProperty(Preferences.HMMSEARCH_DB_PATHS, "");
+    }
+
+    String path = openFileChooser(false);
+    if (new File(path).exists())
+    {
+      IdentifyFile identifier = new IdentifyFile();
+      FileFormatI format = identifier.identify(path, DataSourceType.FILE);
+      if (format == FileFormat.Fasta || format == FileFormat.Stockholm
+              || format == FileFormat.Pfam)
+      {
+        String currentDbs = Cache.getProperty(Preferences.HMMSEARCH_DBS);
+        String currentDbPaths = Cache
+                .getProperty(Preferences.HMMSEARCH_DB_PATHS);
+        currentDbPaths += " " + path;
+
+        String fileName = StringUtils.getLastToken(path, File.separator);
+        Scanner scanner = new Scanner(fileName).useDelimiter(".");
+        String name = scanner.next();
+        scanner.close();
+        currentDbs += " " + path; // TODO remove path from file name
+        scanner.close();
+
+        Cache.setProperty(Preferences.HMMSEARCH_DB_PATHS, currentDbPaths);
+        Cache.setProperty(Preferences.HMMSEARCH_DBS, currentDbPaths);
+      }
+      else
+      {
+        JOptionPane.showMessageDialog(this,
+                MessageManager.getString("warn.invalid_format"));
+      }
+    }
+    else
+    {
+      JOptionPane.showMessageDialog(this,
+              MessageManager.getString("warn.not_enough_sequences"));
+    }
+  }
+
+  /**
+   * Opens a file chooser
+   * 
+   * @param forFolder
+   * @return
+   */
+  protected String openFileChooser(boolean forFolder)
+  {
+    String choice = null;
+    JFileChooser chooser = new JFileChooser();
+    if (forFolder)
+    {
+      chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+    }
+    chooser.setDialogTitle(
+            MessageManager.getString("label.open_local_file"));
+    chooser.setToolTipText(MessageManager.getString("action.open"));
+
+    int value = chooser.showOpenDialog(this);
+
+    if (value == JFileChooser.APPROVE_OPTION)
+    {
+      choice = chooser.getSelectedFile().getPath();
+    }
+    return choice;
+  }
+
+  @Override
   public void reload_actionPerformed(ActionEvent e)
   {
     if (fileName != null)
@@ -1403,6 +1628,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   @Override
   public void associatedData_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     // Pick the tree file
     JalviewFileChooser chooser = new JalviewFileChooser(
@@ -1888,9 +2114,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param e
    *          DOCUMENT ME!
+   * @throws InterruptedException
+   * @throws IOException
    */
   @Override
   protected void pasteNew_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     paste(true);
   }
@@ -1900,9 +2129,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param e
    *          DOCUMENT ME!
+   * @throws InterruptedException
+   * @throws IOException
    */
   @Override
   protected void pasteThis_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     paste(false);
   }
@@ -1912,8 +2144,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param newAlignment
    *          true to paste to a new alignment, otherwise add to this.
+   * @throws InterruptedException
+   * @throws IOException
    */
-  void paste(boolean newAlignment)
+  void paste(boolean newAlignment) throws IOException, InterruptedException
   {
     boolean externalPaste = true;
     try
@@ -2241,7 +2475,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       System.out.println("Exception whilst pasting: " + ex);
       // could be anything being pasted in here
     }
-
   }
 
   @Override
@@ -4541,6 +4774,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param file
    *          either a filename or a URL string.
+   * @throws InterruptedException
+   * @throws IOException
    */
   public void loadJalviewDataFile(String file, DataSourceType sourceType,
           FileFormatI format, SequenceI assocSeq)
@@ -4647,7 +4882,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
       if (isAnnotation)
       {
-
         alignPanel.adjustAnnotationHeight();
         viewport.updateSequenceIdColours();
         buildSortByAnnotationScoresMenu();
@@ -5247,6 +5481,30 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   @Override
+  protected void showInformationHistogram_actionPerformed(ActionEvent e)
+  {
+    viewport.setShowInformationHistogram(
+            showInformationHistogram.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+  }
+
+  @Override
+  protected void showHMMSequenceLogo_actionPerformed(ActionEvent e)
+  {
+    viewport.setShowHMMSequenceLogo(showHMMSequenceLogo.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+  }
+
+  @Override
+  protected void normaliseHMMSequenceLogo_actionPerformed(ActionEvent e)
+  {
+    showHMMSequenceLogo.setState(true);
+    viewport.setShowHMMSequenceLogo(true);
+    viewport.setNormaliseHMMSequenceLogo(normaliseSequenceLogo.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+  }
+
+  @Override
   protected void applyAutoAnnotationSettings_actionPerformed(ActionEvent e)
   {
     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
@@ -5572,6 +5830,68 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
   }
 
+  /**
+   * Sets the status of the HMMER menu
+   */
+  public void updateHMMERStatus()
+  {
+    hmmerMenu.setEnabled(HmmerCommand.isHmmerAvailable());
+  }
+
+  /**
+   * Returns the selected hidden Markov model.
+   * 
+   * @return
+   */
+  public HiddenMarkovModel getSelectedHMM()
+  {
+    if (selectedHMMSequence == null)
+    {
+      return null;
+    }
+    return selectedHMMSequence.getHMM();
+  }
+
+  /**
+   * Returns the selected hidden Markov model.
+   * 
+   * @return
+   */
+  public SequenceI getSelectedHMMSequence()
+  {
+    return selectedHMMSequence;
+  }
+
+  /**
+   * Sets the selected hidden Markov model
+   * 
+   * @param selectedHMM
+   */
+  public void setSelectedHMMSequence(SequenceI selectedHMM)
+  {
+    this.selectedHMMSequence = selectedHMM;
+    hmmAlign.setText(MessageManager.getString("label.hmmalign") + " to "
+            + selectedHMM.getHMM().getName());
+    hmmSearch.setText(MessageManager.getString("label.hmmsearch") + " with "
+            + selectedHMM.getHMM().getName());
+  }
+
+  @Override
+  public void hmmerMenu_actionPerformed(ActionEvent e)
+  {
+    SequenceGroup grp = getViewport().getSelectionGroup();
+    if (grp != null)
+    {
+      hmmBuild.setText(MessageManager.getString("label.hmmbuild") + " from "
+              + grp.getName());
+    }
+    else
+    {
+      hmmBuild.setText(MessageManager.getString("label.hmmbuild")
+              + " from Alignment");
+    }
+  }
+
   @Override
   protected void loadVcf_actionPerformed()
   {