JAL-3878 Create WebServiceMenuBuilder which will replace PreferredServiceRegistry
[jalview.git] / src / jalview / gui / AlignFrame.java
index 5f7d0d4..3d00450 100644 (file)
@@ -1,19 +1,19 @@
 /*
  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
  * Copyright (C) $$Year-Rel$$ The Jalview Authors
- * 
+ *
  * This file is part of Jalview.
- * 
+ *
  * Jalview is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License 
+ * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation, either version 3
  * of the License, or (at your option) any later version.
- *  
- * Jalview is distributed in the hope that it will be useful, but 
- * WITHOUT ANY WARRANTY; without even the implied warranty 
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  * PURPOSE.  See the GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
@@ -64,6 +64,13 @@ 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.hmmer.JackHMMER;
 import jalview.io.AlignmentProperties;
 import jalview.io.AnnotationFile;
 import jalview.io.BackupFiles;
@@ -98,10 +105,27 @@ import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.ViewportRanges;
 import jalview.ws.DBRefFetcher;
 import jalview.ws.DBRefFetcher.FetchFinishedListenerI;
+import jalview.ws.ServiceChangeListener;
+import jalview.ws.WSDiscovererI;
+import jalview.ws.api.ServiceWithParameters;
 import jalview.ws.jws1.Discoverer;
 import jalview.ws.jws2.Jws2Discoverer;
-import jalview.ws.jws2.jabaws2.Jws2Instance;
+import jalview.ws.jws2.PreferredServiceRegistry;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws.params.WsParamSetI;
 import jalview.ws.seqfetcher.DbSourceProxy;
+import jalview.ws2.WebServiceDiscoverer;
+import jalview.ws2.WebServiceI;
+import jalview.ws2.operations.Operation;
+import jalview.ws2.slivka.SlivkaWSDiscoverer;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.swing.JFileChooser;
+import javax.swing.JOptionPane;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
@@ -130,16 +154,21 @@ import java.awt.event.MouseEvent;
 import java.awt.print.PageFormat;
 import java.awt.print.PrinterJob;
 import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.PrintWriter;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
 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.Vector;
 
 import javax.swing.ButtonGroup;
@@ -154,18 +183,22 @@ import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.SwingUtilities;
+import javax.swing.event.InternalFrameAdapter;
+import javax.swing.event.InternalFrameEvent;
 
 import ext.vamsas.ServiceHandle;
 
 /**
  * DOCUMENT ME!
- * 
+ *
  * @author $author$
  * @version $Revision$
  */
 @SuppressWarnings("serial")
-public class AlignFrame extends GAlignFrame implements DropTargetListener,
-        IProgressIndicator, AlignViewControllerGuiI, ColourChangeListener
+public class AlignFrame extends GAlignFrame
+        implements DropTargetListener, IProgressIndicator,
+        AlignViewControllerGuiI, ColourChangeListener, ServiceChangeListener,
+        WebServiceDiscoverer.ServiceChangeListener
 {
 
   public static int frameCount;
@@ -195,6 +228,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    */
   String fileName = null;
 
+  /**
+   * TODO: remove reference to 'FileObject' in AlignFrame - not correct mapping
+   */
   File fileObject;
 
   private int id;
@@ -203,7 +239,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Creates a new AlignFrame object with specific width and height.
-   * 
+   *
    * @param al
    * @param width
    * @param height
@@ -216,7 +252,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Creates a new AlignFrame object with specific width, height and
    * sequenceSetId
-   * 
+   *
    * @param al
    * @param width
    * @param height
@@ -231,7 +267,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Creates a new AlignFrame object with specific width, height and
    * sequenceSetId
-   * 
+   *
    * @param al
    * @param width
    * @param height
@@ -246,7 +282,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * new alignment window with hidden columns
-   * 
+   *
    * @param al
    *          AlignmentI
    * @param hiddenColumns
@@ -265,7 +301,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Create alignment frame for al with hiddenColumns, a specific width and
    * height, and specific sequenceId
-   * 
+   *
    * @param al
    * @param hiddenColumns
    * @param width
@@ -282,7 +318,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Create alignment frame for al with hiddenColumns, a specific width and
    * height, and specific sequenceId
-   * 
+   *
    * @param al
    * @param hiddenColumns
    * @param width
@@ -336,7 +372,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Make a new AlignFrame from existing alignmentPanels
-   * 
+   *
    * @param ap
    *          AlignmentPanel
    * @param av
@@ -357,7 +393,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   void init()
   {
-
     boolean newPanel = (alignPanel == null);
     viewport.setShowAutocalculatedAbove(isShowAutoCalculatedAbove());
     if (newPanel)
@@ -434,9 +469,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     if (Desktop.getDesktopPane() != null)
     {
       this.setDropTarget(new java.awt.dnd.DropTarget(this, this));
+      addServiceListeners();
       if (!Platform.isJS())
       {
-        addServiceListeners();
       }
       setGUINucleotide();
     }
@@ -543,7 +578,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Change the filename and format for the alignment, and enable the 'reload'
    * button functionality.
-   * 
+   *
    * @param file
    *          valid filename
    * @param format
@@ -559,7 +594,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * 
+   *
    * @param fileName
    * @param file  from SwingJS; may contain bytes -- for reload
    * @param protocol from SwingJS; may be RELATIVE_URL
@@ -577,7 +612,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * JavaScript will have this, maybe others. More dependable than a file name
    * and maintains a reference to the actual bytes loaded.
-   * 
+   *
    * @param file
    */
 
@@ -855,6 +890,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         ap.av.updateConservation(ap);
         ap.av.updateConsensus(ap);
         ap.av.updateStrucConsensus(ap);
+        ap.av.initInformationWorker(ap);
       }
     }
   }
@@ -874,58 +910,50 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     return viewport;
   }
 
-  /* Set up intrinsic listeners for dynamically generated GUI bits. */
-  private void addServiceListeners()
+  @Override
+  public void servicesChanged(WSDiscovererI discoverer,
+          Collection<? extends ServiceWithParameters> services)
   {
-    final java.beans.PropertyChangeListener thisListener;
-    Desktop.getInstance().addJalviewPropertyChangeListener("services",
-            thisListener = new java.beans.PropertyChangeListener()
-            {
-
-              @Override
-              public void propertyChange(PropertyChangeEvent evt)
-              {
-                // // System.out.println("Discoverer property change.");
-                // if (evt.getPropertyName().equals("services"))
-                {
-                  SwingUtilities.invokeLater(new Runnable()
-                  {
+    buildWebServicesMenu();
+  }
 
-                    @Override
-                    public void run()
-                    {
-                      System.err.println(
-                              "Rebuild WS Menu for service change");
-                      BuildWebServiceMenu();
-                    }
+  @Override
+  public void servicesChanged(WebServiceDiscoverer discoverer,
+          Collection<? extends WebServiceI> services)
+  {
+    buildWebServicesMenu();
+  }
 
-                  });
-                }
-              }
-            });
-    addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
+  /* Set up intrinsic listeners for dynamically generated GUI bits. */
+  private void addServiceListeners()
+  {
+    if (Cache.getDefault("SHOW_SLIVKA_SERVICES", true))
+    {
+      WebServiceDiscoverer discoverer = SlivkaWSDiscoverer.getInstance();
+      discoverer.addServiceChangeListener((disc, srvcs) -> buildWebServicesMenu());
+    }
+    if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
     {
+      WSDiscovererI discoverer = Jws2Discoverer.getInstance();
+      discoverer.addServiceChangeListener(this);
+    }
+    // legacy event listener for compatibility with jws1
+    PropertyChangeListener legacyListener = (changeEvent) -> {
+      buildWebServicesMenu();
+    };
+    Desktop.getInstance().addJalviewPropertyChangeListener("services",legacyListener);
 
+    addInternalFrameListener(new InternalFrameAdapter() {
       @Override
-      public void internalFrameClosed(
-              javax.swing.event.InternalFrameEvent evt)
-      {
-        // System.out.println("deregistering discoverer listener");
-        Desktop.getInstance().removeJalviewPropertyChangeListener(
-                "services", thisListener);
+      public void internalFrameClosed(InternalFrameEvent e) {
+        System.out.println("deregistering discoverer listener");
+//        SlivkaWSDiscoverer.getInstance().removeServiceChangeListener(AlignFrame.this);
+        Jws2Discoverer.getInstance().removeServiceChangeListener(AlignFrame.this);
+        Desktop.getInstance().removeJalviewPropertyChangeListener("services", legacyListener);
         closeMenuItem_actionPerformed(true);
       }
     });
-    // Finally, build the menu once to get current service state
-    new Thread(new Runnable()
-    {
-
-      @Override
-      public void run()
-      {
-        BuildWebServiceMenu();
-      }
-    }).start();
+    buildWebServicesMenu();
   }
 
   /**
@@ -967,7 +995,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Need to call this method when tabs are selected for multiple views, or when
    * loading from Jalview2XML.java
-   * 
+   *
    * @param av
    *          AlignViewport
    */
@@ -999,6 +1027,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());
@@ -1021,7 +1052,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Set the enabled state of the 'Run Groovy' option in the Calculate menu
-   * 
+   *
    * @param b
    */
 
@@ -1034,7 +1065,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
    */
 
@@ -1045,6 +1076,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   @Override
+  public void removeProgressBar(long id)
+  {
+    progressBar.removeProgressBar(id);
+  }
+
+  @Override
   public void registerHandler(final long id,
           final IProgressIndicatorHandler handler)
   {
@@ -1052,7 +1089,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * 
+   *
    * @return true if any progress bars are still active
    */
 
@@ -1101,6 +1138,266 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   @Override
+  public void hmmBuild_actionPerformed(boolean withDefaults)
+  {
+    if (!alignmentIsSufficient(1))
+    {
+      return;
+    }
+
+    /*
+     * get default parameters, and optionally show a dialog
+     * to allow them to be modified
+     */
+    ParamDatastoreI store = HMMERParamStore.forBuild(viewport);
+    List<ArgumentI> args = store.getServiceParameters();
+
+    if (!withDefaults)
+    {
+      WsParamSetI set = new HMMERPreset();
+      WsJobParameters params = new WsJobParameters(store, set, args);
+      params.showRunDialog().thenAccept((startJob) -> {
+        if (startJob)
+        {
+          var args2 = params.getJobParams();
+          new Thread(new HMMBuild(this, args2)).start();
+        }
+      });
+    }
+    else
+    {
+      new Thread(new HMMBuild(this, args)).start();
+    }
+  }
+
+  @Override
+  public void hmmAlign_actionPerformed(boolean withDefaults)
+  {
+    if (!(checkForHMM() && alignmentIsSufficient(2)))
+    {
+      return;
+    }
+
+    /*
+     * get default parameters, and optionally show a dialog
+     * to allow them to be modified
+     */
+    ParamDatastoreI store = HMMERParamStore.forAlign(viewport);
+    List<ArgumentI> args = store.getServiceParameters();
+
+    if (!withDefaults)
+    {
+      WsParamSetI set = new HMMERPreset();
+      WsJobParameters params = new WsJobParameters(store, set, args);
+      params.showRunDialog().thenAccept((startJob) -> {
+        if (startJob)
+        {
+          var args2 = params.getJobParams();
+          new Thread(new HMMAlign(this, args2)).start();
+        }
+      });
+    }
+    else
+    {
+      new Thread(new HMMAlign(this, args)).start();
+    }
+  }
+
+  @Override
+  public void hmmSearch_actionPerformed(boolean withDefaults)
+  {
+    if (!checkForHMM())
+    {
+      return;
+    }
+
+    /*
+     * get default parameters, and (if requested) show
+     * dialog to allow modification
+     */
+    ParamDatastoreI store = HMMERParamStore.forSearch(viewport);
+    List<ArgumentI> args = store.getServiceParameters();
+
+    if (!withDefaults)
+    {
+      WsParamSetI set = new HMMERPreset();
+      WsJobParameters params = new WsJobParameters(store, set, args);
+      params.showRunDialog().thenAccept((startJob) -> {
+        if (startJob)
+        {
+          var args2 = params.getJobParams();
+          new Thread(new HMMSearch(this, args2)).start();
+          alignPanel.repaint();
+        }
+      });
+    }
+    else
+    {
+      new Thread(new HMMSearch(this, args)).start();
+      alignPanel.repaint();
+    }
+  }
+
+  @Override
+  public void jackhmmer_actionPerformed(boolean withDefaults)
+  {
+
+    /*
+     * get default parameters, and (if requested) show
+     * dialog to allow modification
+     */
+
+    ParamDatastoreI store = HMMERParamStore.forJackhmmer(viewport);
+    List<ArgumentI> args = store.getServiceParameters();
+
+    if (!withDefaults)
+    {
+      WsParamSetI set = new HMMERPreset();
+      WsJobParameters params = new WsJobParameters(store, set, args);
+      params.showRunDialog().thenAccept((startJob) -> {
+        if (startJob)
+        {
+          var args2 = params.getJobParams();
+          new Thread(new JackHMMER(this, args2)).start();
+          alignPanel.repaint();
+        }
+      });
+    }
+    else
+    {
+      new Thread(new JackHMMER(this, args)).start();
+      alignPanel.repaint();
+    }
+  }
+
+  /**
+   * Checks if the alignment has at least one hidden Markov model, if not shows
+   * a dialog advising to run hmmbuild or load an HMM profile
+   *
+   * @return
+   */
+  private boolean checkForHMM()
+  {
+    if (viewport.getAlignment().getHmmSequences().isEmpty())
+    {
+      JOptionPane.showMessageDialog(this,
+              MessageManager.getString("warn.no_hmm"));
+      return false;
+    }
+    return true;
+  }
+
+  @Override
+  protected void filterByEValue_actionPerformed()
+  {
+    viewport.filterByEvalue(inputDouble("Enter E-Value Cutoff"));
+  }
+
+  @Override
+  protected void filterByScore_actionPerformed()
+  {
+    viewport.filterByScore(inputDouble("Enter Bit Score Threshold"));
+  }
+
+  private double inputDouble(String message)
+  {
+    String str = null;
+    Double d = null;
+    while (d == null || d <= 0)
+    {
+      str = JOptionPane.showInputDialog(this.alignPanel, message);
+      try
+      {
+        d = Double.valueOf(str);
+      } catch (NumberFormatException e)
+      {
+      }
+    }
+    return d;
+  }
+
+  /**
+   * Checks if the alignment contains the required number of sequences.
+   *
+   * @param required
+   * @return
+   */
+  public boolean alignmentIsSufficient(int required)
+  {
+    if (getViewport().getSequenceSelection().length < required)
+    {
+      JOptionPane.showMessageDialog(this,
+              MessageManager.getString("label.not_enough_sequences"));
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * Opens a file browser and adds the selected file, if in Fasta, Stockholm or
+   * Pfam format, to the list held under preference key "HMMSEARCH_DBS" (as a
+   * comma-separated list)
+   */
+  @Override
+  public void addDatabase_actionPerformed() throws IOException
+  {
+    if (Cache.getProperty(Preferences.HMMSEARCH_DBS) == null)
+    {
+      Cache.setProperty(Preferences.HMMSEARCH_DBS, "");
+    }
+
+    String path = openFileChooser(false);
+    if (path != null && 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 currentDbPaths = Cache
+                .getProperty(Preferences.HMMSEARCH_DBS);
+        currentDbPaths += Preferences.COMMA + path;
+        Cache.setProperty(Preferences.HMMSEARCH_DBS, currentDbPaths);
+      }
+      else
+      {
+        JOptionPane.showMessageDialog(this,
+                MessageManager.getString("warn.invalid_format"));
+      }
+    }
+  }
+
+  /**
+   * Opens a file chooser, optionally restricted to selecting folders
+   * (directories) only. Answers the path to the selected file or folder, or
+   * null if none is chosen.
+   *
+   * @param
+   * @return
+   */
+  protected String openFileChooser(boolean forFolder)
+  {
+    // TODO duplicates GPreferences method - relocate to JalviewFileChooser?
+    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 && fileObject == null)
@@ -1297,7 +1594,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * alignment has hidden regions, or the format is one capable of including
    * non-sequence data (features, annotations, groups), then the user may be
    * prompted to specify what to include in the output.
-   * 
+   *
    * @param file
    * @param format
    */
@@ -1410,7 +1707,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * Outputs the alignment to textbox in the requested format, if necessary
    * first prompting the user for whether to include hidden regions or
    * non-sequence data
-   * 
+   *
    * @param fileFormatName
    */
 
@@ -1469,7 +1766,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -1498,7 +1795,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Creates a PNG image of the alignment and writes it to the given file. If
    * the file is null, the user is prompted to choose a file.
-   * 
+   *
    * @param f
    */
 
@@ -1511,7 +1808,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Creates an EPS image of the alignment and writes it to the given file. If
    * the file is null, the user is prompted to choose a file.
-   * 
+   *
    * @param f
    */
 
@@ -1524,7 +1821,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Creates an SVG image of the alignment and writes it to the given file. If
    * the file is null, the user is prompted to choose a file.
-   * 
+   *
    * @param f
    */
 
@@ -1543,7 +1840,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -1570,6 +1867,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   @Override
   public void associatedData_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     final JalviewFileChooser chooser = new JalviewFileChooser(
             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
@@ -1596,7 +1894,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Close the current view or all views in the alignment frame. If the frame
    * only contains one view then the alignment will be removed from memory.
-   * 
+   *
    * @param closeAllTabs
    */
 
@@ -1651,7 +1949,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Close the specified panel and close up tabs appropriately.
-   * 
+   *
    * @param panelToClose
    */
 
@@ -1729,7 +2027,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * 
+   *
    * @return alignment objects for all views
    */
 
@@ -1754,7 +2052,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -1793,7 +2091,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -1876,7 +2174,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param up
    *          DOCUMENT ME!
    */
@@ -2001,7 +2299,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2067,38 +2365,45 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
+   * @throws InterruptedException
+   * @throws IOException
    */
 
   @Override
   protected void pasteNew_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     paste(true);
   }
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
+   * @throws InterruptedException
+   * @throws IOException
    */
 
   @Override
   protected void pasteThis_actionPerformed(ActionEvent e)
+          throws IOException, InterruptedException
   {
     paste(false);
   }
 
   /**
    * Paste contents of Jalview clipboard
-   * 
+   *
    * @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
@@ -2427,7 +2732,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       System.out.println("Exception whilst pasting: " + ex);
       // could be anything being pasted in here
     }
-
   }
 
   @Override
@@ -2567,7 +2871,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2601,7 +2905,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2614,7 +2918,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2627,7 +2931,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2667,7 +2971,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2680,7 +2984,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2753,7 +3057,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2804,7 +3108,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2842,7 +3146,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2857,7 +3161,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -2880,7 +3184,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Creates and shows a new view of the current alignment.
-   * 
+   *
    * @param viewTitle
    *          title of newly created view; if null, one will be generated
    * @param copyAnnotation
@@ -2956,7 +3260,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * Make a new name for the view, ensuring it is unique within the current
    * sequenceSetId. (This used to be essential for Jalview Project archives, but
    * these now use viewId. Unique view names are still desirable for usability.)
-   * 
+   *
    * @param viewTitle
    * @return
    */
@@ -2991,7 +3295,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Returns a list of distinct view names found in the given list of
    * components. View names are held on the viewport of an AlignmentPanel.
-   * 
+   *
    * @param comps
    * @return
    */
@@ -3035,7 +3339,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3048,7 +3352,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3079,7 +3383,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see jalview.jbgui.GAlignFrame#followHighlight_actionPerformed()
    */
 
@@ -3100,7 +3404,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3114,7 +3418,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3151,7 +3455,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * called by key handler and the hide all/show all menu items
-   * 
+   *
    * @param toggleSeqs
    * @param toggleCols
    */
@@ -3218,7 +3522,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#hideAllButSelection_actionPerformed(java.awt.
    * event.ActionEvent)
@@ -3233,7 +3537,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#hideAllSelection_actionPerformed(java.awt.event
    * .ActionEvent)
@@ -3253,7 +3557,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showAllhidden_actionPerformed(java.awt.event.
    * ActionEvent)
@@ -3286,7 +3590,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3301,7 +3605,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3316,7 +3620,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3331,7 +3635,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3345,7 +3649,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3359,7 +3663,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3405,7 +3709,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Set or clear 'Show Sequence Features'
-   * 
+   *
    * @param evt
    *          DOCUMENT ME!
    */
@@ -3420,10 +3724,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Action on toggle of the 'Show annotations' menu item. This shows or hides
    * the annotations panel as a whole.
-   * 
+   *
    * The options to show/hide all annotations should be enabled when the panel
    * is shown, and disabled when the panel is hidden.
-   * 
+   *
    * @param e
    */
 
@@ -3442,7 +3746,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       {
         alignPanel.updateScrollBarsFromRanges();
       }
-      
+
     });
   }
 
@@ -3480,7 +3784,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     else
     /**
      * Java only
-     * 
+     *
      * @j2sIgnore
      */
     {
@@ -3501,7 +3805,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3596,7 +3900,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * Action on the user checking or unchecking the option to apply the selected
    * colour scheme to all groups. If unchecked, groups may have their own
    * independent colour schemes.
-   * 
+   *
    * @param selected
    */
 
@@ -3608,7 +3912,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Action on user selecting a colour from the colour menu
-   * 
+   *
    * @param name
    *          the name (not the menu item label!) of the colour scheme
    */
@@ -3637,7 +3941,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Actions on setting or changing the alignment colour scheme
-   * 
+   *
    * @param cs
    */
 
@@ -3726,7 +4030,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3744,7 +4048,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3761,7 +4065,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3778,7 +4082,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3794,9 +4098,31 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     alignPanel.paintAlignment(true, false);
   }
 
+  @Override
+  public void sortEValueMenuItem_actionPerformed(ActionEvent e)
+  {
+    SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+    AlignmentSorter.sortByEValue(viewport.getAlignment());
+    addHistoryItem(new OrderCommand("Group Sort", oldOrder,
+            viewport.getAlignment()));
+    alignPanel.paintAlignment(true, false);
+
+  }
+
+  @Override
+  public void sortBitScoreMenuItem_actionPerformed(ActionEvent e)
+  {
+    SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+    AlignmentSorter.sortByBitScore(viewport.getAlignment());
+    addHistoryItem(new OrderCommand("Group Sort", oldOrder,
+            viewport.getAlignment()));
+    alignPanel.paintAlignment(true, false);
+
+  }
+
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3809,7 +4135,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -3864,7 +4190,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Constructs a tree panel and adds it to the desktop
-   * 
+   *
    * @param type
    *          tree type (NJ or AV)
    * @param modelName
@@ -3928,7 +4254,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param title
    *          DOCUMENT ME!
    * @param order
@@ -3964,7 +4290,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Add a new sort by annotation score menu item
-   * 
+   *
    * @param sort
    *          the menu to add the option to
    * @param scoreLabel
@@ -4003,7 +4329,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * search the alignment and rebuild the sort by annotation score submenu the
    * last alignment annotation vector hash is stored to minimize cost of
    * rebuilding in subsequence calls.
-   * 
+   *
    */
 
   @Override
@@ -4015,40 +4341,39 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
 
     if (viewport.getAlignment().getAlignmentAnnotation()
-            .hashCode() != _annotationScoreVectorHash)
+            .hashCode() == _annotationScoreVectorHash)
+    {
+      return;
+    }
+
+    sortByAnnotScore.removeAll();
+    Set<String> scoreSorts = new HashSet<>();
+    for (SequenceI sqa : viewport.getAlignment().getSequences())
     {
-      sortByAnnotScore.removeAll();
-      // almost certainly a quicker way to do this - but we keep it simple
-      Hashtable<String, String> scoreSorts = new Hashtable<>();
-      AlignmentAnnotation aann[];
-      for (SequenceI sqa : viewport.getAlignment().getSequences())
+      AlignmentAnnotation[] anns = sqa.getAnnotation();
+      for (int i = 0; anns != null && i < anns.length; i++)
       {
-        aann = sqa.getAnnotation();
-        for (int i = 0; aann != null && i < aann.length; i++)
+        AlignmentAnnotation aa = anns[i];
+        if (aa != null && aa.hasScore() && aa.sequenceRef != null)
         {
-          if (aann[i].hasScore() && aann[i].sequenceRef != null)
-          {
-            scoreSorts.put(aann[i].label, aann[i].label);
-          }
+          scoreSorts.add(aa.label);
         }
       }
-      Enumeration<String> labels = scoreSorts.keys();
-      while (labels.hasMoreElements())
-      {
-        addSortByAnnotScoreMenuItem(sortByAnnotScore, labels.nextElement());
-      }
-      sortByAnnotScore.setVisible(scoreSorts.size() > 0);
-      scoreSorts.clear();
-
-      _annotationScoreVectorHash = viewport.getAlignment()
-              .getAlignmentAnnotation().hashCode();
     }
+    for (String label : scoreSorts)
+    {
+      addSortByAnnotScoreMenuItem(sortByAnnotScore, label);
+    }
+    sortByAnnotScore.setVisible(!scoreSorts.isEmpty());
+
+    _annotationScoreVectorHash = viewport.getAlignment()
+            .getAlignmentAnnotation().hashCode();
   }
 
   /**
-   * Enable (or, if desired, make visible) the By Tree 
+   * Enable (or, if desired, make visible) the By Tree
    * submenu only if it has at least one element (or will have).
-   * 
+   *
    */
   @Override
   protected void enableSortMenuOptions()
@@ -4056,7 +4381,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     List<TreePanel> treePanels = getTreePanels();
     sortByTreeMenu.setEnabled(!treePanels.isEmpty());
   }
-  
+
   /**
    * Maintain the Order by->Displayed Tree menu. Creates a new menu item for a
    * TreePanel with an appropriate <code>jalview.analysis.AlignmentSorter</code>
@@ -4122,7 +4447,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Work out whether the whole set of sequences or just the selected set will
    * be submitted for multiple alignment.
-   * 
+   *
    */
 
   public jalview.datamodel.AlignmentView gatherSequencesForAlignment()
@@ -4138,7 +4463,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       /*
        * SequenceGroup seqs = viewport.getSelectionGroup(); int sz; msa = new
        * SequenceI[sz = seqs.getSize(false)];
-       * 
+       *
        * for (int i = 0; i < sz; i++) { msa[i] = (SequenceI)
        * seqs.getSequenceAt(i); }
        */
@@ -4200,7 +4525,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * DOCUMENT ME!
-   * 
+   *
    * @param e
    *          DOCUMENT ME!
    */
@@ -4267,7 +4592,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Add a treeviewer for the tree extracted from a Newick file object to the
    * current alignment view
-   * 
+   *
    * @param nf
    *          the tree
    * @param title
@@ -4324,190 +4649,113 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     return tp;
   }
 
-  private boolean buildingMenu = false;
-
   /**
-   * Generates menu items and listener event actions for web service clients
-   * 
+   * Schedule the web services menu rebuild to the event dispatch thread.
    */
-
-  public void BuildWebServiceMenu()
+  public void buildWebServicesMenu()
   {
-    while (buildingMenu)
-    {
-      try
+    SwingUtilities.invokeLater(() -> {
+      Cache.log.info("Rebuiling WS menu");
+      webService.removeAll();
+      if (Cache.getDefault("SHOW_SLIVKA_SERVICES", true))
       {
-        System.err.println("Waiting for building menu to finish.");
-        Thread.sleep(10);
-      } catch (Exception e)
+        Cache.log.info("Building web service menu for slivka");
+        SlivkaWSDiscoverer discoverer = SlivkaWSDiscoverer.getInstance();
+        JMenu submenu = new JMenu("Slivka");
+        buildWebServicesMenu(discoverer, submenu);
+        webService.add(submenu);
+      }
+      if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
       {
+        WSDiscovererI jws2servs = Jws2Discoverer.getInstance();
+        JMenu submenu = new JMenu("JABAWS");
+        buildLegacyWebServicesMenu(submenu);
+        buildWebServicesMenu(jws2servs, submenu);
+        webService.add(submenu);
       }
-    }
-    final AlignFrame me = this;
-    buildingMenu = true;
-    new Thread(new Runnable()
-    {
+      build_fetchdbmenu(webService);
+    });
+  }
 
-      @Override
-      public void run()
+  private void buildLegacyWebServicesMenu(JMenu menu)
+  {
+    JMenu secstrmenu = new JMenu("Secondary Structure Prediction");
+    if (Discoverer.getServices() != null && Discoverer.getServices().size() > 0)
+    {
+      var secstrpred = Discoverer.getServices().get("SecStrPred");
+      if (secstrpred != null)
       {
-        final List<JMenuItem> legacyItems = new ArrayList<>();
-        try
-        {
-          // System.err.println("Building ws menu again "
-          // + Thread.currentThread());
-          // TODO: add support for context dependent disabling of services based
-          // on
-          // alignment and current selection
-          // TODO: add additional serviceHandle parameter to specify abstract
-          // handler
-          // class independently of AbstractName
-          // TODO: add in rediscovery GUI function to restart discoverer
-          // TODO: group services by location as well as function and/or
-          // introduce
-          // object broker mechanism.
-          final Vector<JMenu> wsmenu = new Vector<>();
-          final IProgressIndicator af = me;
-
-          /*
-           * do not i18n these strings - they are hard-coded in class
-           * compbio.data.msa.Category, Jws2Discoverer.isRecalculable() and
-           * SequenceAnnotationWSClient.initSequenceAnnotationWSClient()
-           */
-          final JMenu msawsmenu = new JMenu("Alignment");
-          final JMenu secstrmenu = new JMenu(
-                  "Secondary Structure Prediction");
-          final JMenu seqsrchmenu = new JMenu("Sequence Database Search");
-          final JMenu analymenu = new JMenu("Analysis");
-          final JMenu dismenu = new JMenu("Protein Disorder");
-          // JAL-940 - only show secondary structure prediction services from
-          // the legacy server
-          Hashtable<String, Vector<ServiceHandle>> ds = Discoverer
-                  .getInstance().getServices();
-          if (// Cache.getDefault("SHOW_JWS1_SERVICES", true)
-              // &&
-          ds != null && (ds.size() > 0))
-          {
-            // TODO: refactor to allow list of AbstractName/Handler bindings to
-            // be
-            // stored or retrieved from elsewhere
-            // No MSAWS used any more:
-            // Vector msaws = null; // (Vector)
-            // Discoverer.services.get("MsaWS");
-            Vector<ServiceHandle> secstrpr = ds.get("SecStrPred");
-            if (secstrpr != null)
-            {
-              // Add any secondary structure prediction services
-              for (int i = 0, j = secstrpr.size(); i < j; i++)
-              {
-                final ext.vamsas.ServiceHandle sh = secstrpr.get(i);
-                jalview.ws.WSMenuEntryProviderI impl = jalview.ws.jws1.Discoverer
-                        .getServiceClient(sh);
-                int p = secstrmenu.getItemCount();
-                impl.attachWSMenuEntry(secstrmenu, me);
-                int q = secstrmenu.getItemCount();
-                for (int litm = p; litm < q; litm++)
-                {
-                  legacyItems.add(secstrmenu.getItem(litm));
-                }
-              }
-            }
-          }
-
-          // Add all submenus in the order they should appear on the web
-          // services menu
-          wsmenu.add(msawsmenu);
-          wsmenu.add(secstrmenu);
-          wsmenu.add(dismenu);
-          wsmenu.add(analymenu);
-          // No search services yet
-          // wsmenu.add(seqsrchmenu);
-
-          javax.swing.SwingUtilities.invokeLater(new Runnable()
-          {
-
-            @Override
-            public void run()
-            {
-              try
-              {
-                webService.removeAll();
-                // first, add discovered services onto the webservices menu
-                if (wsmenu.size() > 0)
-                {
-                  for (int i = 0, j = wsmenu.size(); i < j; i++)
-                  {
-                    webService.add(wsmenu.get(i));
-                  }
-                }
-                else
-                {
-                  webService.add(me.webServiceNoServices);
-                }
-                // TODO: move into separate menu builder class.
-                // boolean new_sspred = false;
-                if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
-                {
-                  Jws2Discoverer jws2servs = Jws2Discoverer.getInstance();
-                  if (jws2servs != null)
-                  {
-                    if (jws2servs.hasServices())
-                    {
-                      jws2servs.attachWSMenuEntry(webService, me);
-                      for (Jws2Instance sv : jws2servs.getServices())
-                      {
-                        if (sv.description.toLowerCase().contains("jpred"))
-                        {
-                          for (JMenuItem jmi : legacyItems)
-                          {
-                            jmi.setVisible(false);
-                          }
-                        }
-                      }
-
-                    }
-                    if (jws2servs.isRunning())
-                    {
-                      JMenuItem tm = new JMenuItem(
-                              "Still discovering JABA Services");
-                      tm.setEnabled(false);
-                      webService.add(tm);
-                    }
-                  }
-                }
-                build_urlServiceMenu(me.webService);
-                build_fetchdbmenu(webService);
-                for (JMenu item : wsmenu)
-                {
-                  if (item.getItemCount() == 0)
-                  {
-                    item.setEnabled(false);
-                  }
-                  else
-                  {
-                    item.setEnabled(true);
-                  }
-                }
-              } catch (Exception e)
-              {
-                Cache.log.debug(
-                        "Exception during web service menu building process.",
-                        e);
-              }
-            }
-          });
-        } catch (Exception e)
+        for (ext.vamsas.ServiceHandle sh : secstrpred)
         {
+          var menuProvider = Discoverer.getServiceClient(sh);
+          menuProvider.attachWSMenuEntry(secstrmenu, this);
         }
-        buildingMenu = false;
       }
-    }).start();
+    }
+    menu.add(secstrmenu);
+  }
+
+  /**
+   * Constructs the web services menu for the given discoverer under the
+   * specified menu. This method must be called on the EDT
+   *
+   * @param discoverer
+   *          the discoverer used to build the menu
+   * @param menu
+   *          parent component which the elements will be attached to
+   */
+  private void buildWebServicesMenu(WSDiscovererI discoverer, JMenu menu)
+  {
+    if (discoverer.hasServices())
+    {
+      PreferredServiceRegistry.getRegistry().populateWSMenuEntry(
+              discoverer.getServices(), sv -> buildWebServicesMenu(), menu,
+              this, null);
+    }
+    if (discoverer.isRunning())
+    {
+      JMenuItem item = new JMenuItem("Service discovery in progress.");
+      item.setEnabled(false);
+      menu.add(item);
+    }
+    else if (!discoverer.hasServices())
+    {
+      JMenuItem item = new JMenuItem("No services available.");
+      item.setEnabled(false);
+      menu.add(item);
+    }
+  }
 
+  private void buildWebServicesMenu(WebServiceDiscoverer discoverer, final JMenu menu)
+  {
+    if (discoverer.hasServices())
+    {
+      var builder = new WebServicesMenuBuilder();
+      for (var service : discoverer.getServices())
+        builder.addAllOperations(service.getOperations());
+      builder.addSelectedHostChangeListener((name, op) -> {
+        menu.removeAll();
+        builder.buildMenu(menu, this);
+      });
+      builder.buildMenu(menu, this);
+    }
+    if (discoverer.isRunning())
+    {
+      JMenuItem item = new JMenuItem("Service discovery in progress.");
+      item.setEnabled(false);
+      menu.add(item);
+    }
+    else if (!discoverer.hasServices())
+    {
+      JMenuItem item = new JMenuItem("No services available.");
+      item.setEnabled(false);
+      menu.add(item);
+    }
   }
 
   /**
    * construct any groupURL type service menu entries.
-   * 
+   *
    * @param webService
    */
 
@@ -4518,12 +4766,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     /*
      * JMenuItem testAlView = new JMenuItem("Test AlignmentView"); final
      * AlignFrame af = this; testAlView.addActionListener(new ActionListener() {
-     * 
+     *
      *  public void actionPerformed(ActionEvent e) {
      * jalview.datamodel.AlignmentView
      * .testSelectionViews(af.viewport.getAlignment(),
      * af.viewport.getColumnSelection(), af.viewport.selectionGroup); }
-     * 
+     *
      * }); webService.add(testAlView);
      */
     // TODO: refactor to RestClient discoverer and merge menu entries for
@@ -4545,7 +4793,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * Cross-References menu (formerly called Show Products), with database
    * sources for which cross-references are found (protein sources for a
    * nucleotide alignment and vice versa)
-   * 
+   *
    * @return true if Show Cross-references menu should be enabled
    */
 
@@ -4601,7 +4849,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Finds and displays cross-references for the selected sequences (protein
    * products for nucleotide sequences, dna coding sequences for peptides).
-   * 
+   *
    * @param sel
    *          the sequences to show cross-references for
    * @param dna
@@ -4676,7 +4924,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Set the file format
-   * 
+   *
    * @param format
    */
 
@@ -4687,7 +4935,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Try to load a features file onto the alignment.
-   * 
+   *
    * @param file
    *          contents or path to retrieve file or a File object
    * @param sourceType
@@ -4940,9 +5188,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * <li>a features file</li>
    * <li>else try to interpret as an alignment file</li>
    * </ul>
-   * 
+   *
    * @param file
    *          either a filename or a URL string.
+   * @throws InterruptedException
+   * @throws IOException
    */
 
   public void loadJalviewDataFile(Object file, DataSourceType sourceType,
@@ -5103,7 +5353,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Change the display state for the given feature groups -- Added by BH from
    * JalviewLite
-   * 
+   *
    * @param groups
    *          list of group strings
    * @param state
@@ -5248,7 +5498,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showDbRefs_actionPerformed(java.awt.event.ActionEvent
    * )
@@ -5262,7 +5512,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @seejalview.jbgui.GAlignFrame#showNpFeats_actionPerformed(java.awt.event.
    * ActionEvent)
    */
@@ -5276,7 +5526,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * find the viewport amongst the tabs in this alignment frame and close that
    * tab
-   * 
+   *
    * @param av
    */
 
@@ -5650,7 +5900,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showUnconservedMenuItem_actionPerformed(java.
    * awt.event.ActionEvent)
@@ -5665,7 +5915,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showGroupConsensus_actionPerformed(java.awt.event
    * .ActionEvent)
@@ -5681,7 +5931,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showGroupConservation_actionPerformed(java.awt
    * .event.ActionEvent)
@@ -5696,7 +5946,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showConsensusHistogram_actionPerformed(java.awt
    * .event.ActionEvent)
@@ -5711,7 +5961,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#showConsensusProfile_actionPerformed(java.awt
    * .event.ActionEvent)
@@ -5741,7 +5991,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /*
    * (non-Javadoc)
-   * 
+   *
    * @see
    * jalview.jbgui.GAlignFrame#makeGrpsFromSelection_actionPerformed(java.awt
    * .event.ActionEvent)
@@ -5795,7 +6045,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * make the given alignmentPanel the currently selected tab
-   * 
+   *
    * @param alignmentPanel
    */
 
@@ -5816,7 +6066,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * Action on selection of menu options to Show or Hide annotations.
-   * 
+   *
    * @param visible
    * @param forSequences
    *          update sequence-related annotations
@@ -5868,7 +6118,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * 
+   *
    * @return alignment panels in this alignment frame
    */
 
@@ -5931,7 +6181,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Set visibility of dna/protein complement view (available when shown in a
    * split frame).
-   * 
+   *
    * @param show
    */
 
@@ -6004,7 +6254,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   /**
    * Hides columns containing (or not containing) a specified feature, provided
    * that would not leave all columns hidden
-   * 
+   *
    * @param featureType
    * @param columnsContaining
    * @return
@@ -6079,6 +6329,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
   }
 
+  /**
+   * Sets the status of the HMMER menu
+   */
+  public void updateHMMERStatus()
+  {
+    hmmerMenu.setEnabled(HmmerCommand.isHmmerAvailable());
+  }
+
   @Override
   protected void loadVcf_actionPerformed()
   {
@@ -6135,9 +6393,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   /**
    * BH 2019 from JalviewLite
-   * 
+   *
    * get sequence feature groups that are hidden or shown
-   * 
+   *
    * @param visible
    *          true is visible
    * @return list
@@ -6157,7 +6415,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * 
+   *
    * @return list of feature groups on the view
    */
 
@@ -6222,5 +6480,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
     }
   }
+
 }