Merge branch 'features/r2_11_2_alphafold/JAL-629' into features/JAL-3858_PAEsInProjects
[jalview.git] / src / jalview / gui / StructureChooser.java
index 07eec2b..9f259bb 100644 (file)
@@ -24,12 +24,14 @@ package jalview.gui;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Locale;
+import java.util.concurrent.Callable;
 import java.util.concurrent.Executors;
 
 import javax.swing.JCheckBox;
@@ -64,6 +66,7 @@ import jalview.jbgui.GStructureChooser;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 import jalview.ws.DBRefFetcher;
 import jalview.ws.DBRefFetcher.FetchFinishedListenerI;
 import jalview.ws.seqfetcher.DbSourceProxy;
@@ -116,11 +119,19 @@ public class StructureChooser extends GStructureChooser
 
   List<SequenceI> seqsWithoutSourceDBRef = null;
 
+  private boolean noChooserGUI = false;
+
   private static StructureViewer lastTargetedView = null;
 
   public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
           AlignmentPanel ap)
   {
+    this(selectedSeqs, selectedSeq, ap, false);
+  }
+
+  public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
+          AlignmentPanel ap, boolean noChooserGUI)
+  {
     // which FTS engine to use
     data = StructureChooserQuerySource.getQuerySourceFor(selectedSeqs);
     initDialog();
@@ -129,6 +140,7 @@ public class StructureChooser extends GStructureChooser
     this.selectedSequence = selectedSeq;
     this.selectedSequences = selectedSeqs;
     this.progressIndicator = (ap == null) ? null : ap.alignFrame;
+    this.noChooserGUI = noChooserGUI;
     init();
 
   }
@@ -244,7 +256,7 @@ public class StructureChooser extends GStructureChooser
     populateFilterComboBox(isStructuresDiscovered(), cachedPDBExists);
     discoverStructureViews();
     updateProgressIndicator(null, startTime);
-    mainFrame.setVisible(true);
+    mainFrame.setVisible(noChooserGUI);
     updateCurrentView();
   }
 
@@ -316,53 +328,43 @@ public class StructureChooser extends GStructureChooser
     };
 
     // fetch db refs if OK pressed
-    final Runnable discoverCanonicalDBrefs = new Runnable()
-    {
-      @Override
-      public void run()
+    final Callable discoverCanonicalDBrefs = () -> {
+      btn_queryTDB.setEnabled(false);
+      populateSeqsWithoutSourceDBRef();
+
+      final int y = seqsWithoutSourceDBRef.size();
+      if (y > 0)
       {
-        btn_queryTDB.setEnabled(false);
-        populateSeqsWithoutSourceDBRef();
+        final SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
+                .toArray(new SequenceI[y]);
+        DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef,
+                progressBar, new DbSourceProxy[]
+                { new jalview.ws.dbsources.Uniprot() }, null, false);
+        dbRefFetcher.addListener(afterDbRefFetch);
+        // ideally this would also gracefully run with callbacks
 
-        final int y = seqsWithoutSourceDBRef.size();
-        if (y > 0)
-        {
-          final SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
-                  .toArray(new SequenceI[y]);
-          DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef,
-                  progressBar, new DbSourceProxy[]
-                  { new jalview.ws.dbsources.Uniprot() }, null, false);
-          dbRefFetcher.addListener(afterDbRefFetch);
-          // ideally this would also gracefully run with callbacks
-
-          dbRefFetcher.fetchDBRefs(true);
-        }
-        else
-        {
-          // call finished action directly
-          afterDbRefFetch.finished();
-        }
+        dbRefFetcher.fetchDBRefs(true);
       }
-
+      else
+      {
+        // call finished action directly
+        afterDbRefFetch.finished();
+      }
+      return null;
     };
-    final Runnable revertview = new Runnable()
-    {
-      @Override
-      public void run()
+    final Callable revertview = () -> {
+      if (lastSelected != null)
       {
-        if (lastSelected != null)
-        {
-          cmb_filterOption.setSelectedItem(lastSelected);
-        }
-      };
+        cmb_filterOption.setSelectedItem(lastSelected);
+      }
+      return null;
     };
     int threshold = Cache.getDefault("UNIPROT_AUTOFETCH_THRESHOLD",
             THRESHOLD_WARN_UNIPROT_FETCH_NEEDED);
     Console.debug("Using Uniprot fetch threshold of " + threshold);
     if (ignoreGui || seqsWithoutSourceDBRef.size() < threshold)
     {
-      Executors.defaultThreadFactory().newThread(discoverCanonicalDBrefs)
-              .start();
+      Executors.newSingleThreadExecutor().submit(discoverCanonicalDBrefs);
       return;
     }
     // need cancel and no to result in the discoverPDB action - mocked is
@@ -1524,7 +1526,8 @@ public class StructureChooser extends GStructureChooser
   @Override
   public void setProgressBar(String message, long id)
   {
-    progressBar.setProgressBar(message, id);
+    if (!Platform.isHeadless())
+      progressBar.setProgressBar(message, id);
   }
 
   @Override
@@ -1576,4 +1579,34 @@ public class StructureChooser extends GStructureChooser
   {
     return notQueriedTDBYet;
   }
+
+  /**
+   * Open a single structure file for a given sequence
+   */
+  public static void openStructureFileForSequence(AlignmentPanel ap,
+          SequenceI seq, File sFile)
+  {
+    StructureChooser sc = new StructureChooser(new SequenceI[] { seq }, seq,
+            ap, true);
+    StructureSelectionManager ssm = ap.getStructureSelectionManager();
+    PDBEntry fileEntry = null;
+    try
+    {
+      fileEntry = new AssociatePdbFileWithSeq().associatePdbWithSeq(
+              sFile.getAbsolutePath(), DataSourceType.FILE, seq, true,
+              Desktop.instance);
+    } catch (Exception e)
+    {
+      Console.error("Could not open structure file '"
+              + sFile.getAbsolutePath() + "'");
+      return;
+    }
+
+    StructureViewer sViewer = sc.launchStructureViewer(ssm,
+            new PDBEntry[]
+            { fileEntry }, ap, new SequenceI[] { seq });
+
+    sc.mainFrame.dispose();
+    sc.noChooserGUI = false;
+  }
 }