JAL-3829 do better at detecting structure type, prompt to retrieve Uniprot ids for...
[jalview.git] / src / jalview / gui / StructureChooser.java
index 6bcac24..2e7019a 100644 (file)
@@ -37,6 +37,7 @@ import jalview.fts.core.FTSRestResponse;
 import jalview.fts.service.pdb.PDBFTSRestClient;
 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
 import jalview.gui.structurechooser.StructureChooserQuerySource;
+import jalview.gui.structurechooser.ThreeDBStructureChooserQuerySource;
 import jalview.io.DataSourceType;
 import jalview.jbgui.FilterOption;
 import jalview.jbgui.GStructureChooser;
@@ -55,6 +56,9 @@ import java.util.List;
 import java.util.Objects;
 import java.util.Set;
 import java.util.Vector;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
 
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
@@ -131,19 +135,21 @@ public class StructureChooser extends GStructureChooser
 
     // ensure a filter option is in force for search
     populateFilterComboBox(true, cachedPDBExists);
-    Thread discoverPDBStructuresThread = new Thread(new Runnable()
+    // todo change to futures I guess
+
+    final Runnable discoverPDBStructures = new Runnable()
     {
       @Override
       public void run()
       {
-        // looks for any existing structures already loaded 
-        // for the sequences (the cached ones) 
-        // then queries the StructureChooserQuerySource to 
+        // looks for any existing structures already loaded
+        // for the sequences (the cached ones)
+        // then queries the StructureChooserQuerySource to
         // discover more structures.
-        // 
+        //
         // Possible optimisation is to only begin querying
         // the structure chooser if there are no cached structures.
-        
+
         long startTime = System.currentTimeMillis();
         updateProgressIndicator(MessageManager
                 .getString("status.loading_cached_pdb_entries"), startTime);
@@ -159,8 +165,68 @@ public class StructureChooser extends GStructureChooser
         mainFrame.setVisible(true);
         updateCurrentView();
       }
-    });
-    discoverPDBStructuresThread.start();
+    };
+    final List<SequenceI> seqsWithoutSourceDBRef = new ArrayList<SequenceI>();
+
+    final Runnable discoverCanonicalDBrefs = new Runnable()
+    {
+      @Override
+      public void run()
+      {
+        long progressId = System.currentTimeMillis();
+
+        int y = seqsWithoutSourceDBRef.size();
+        setProgressBar(MessageManager.formatMessage(
+                "status.fetching_dbrefs_for_sequences_without_valid_refs",
+                y), progressId);
+        SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
+                .toArray(new SequenceI[y]);
+        DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef);
+        dbRefFetcher.fetchDBRefs(true);
+
+        setProgressBar("Fetch complete.", progressId); // todo i18n
+
+        SwingUtilities.invokeLater(discoverPDBStructures);
+      }
+    };
+
+    Executors.defaultThreadFactory().newThread(new Runnable()
+    {
+      public void run()
+      {
+
+        for (SequenceI seq : selectedSequences)
+        {
+          if (seq.isProtein())
+          {
+            int dbRef = ThreeDBStructureChooserQuerySource
+                    .checkUniprotRefs(seq.getDBRefs());
+            if (dbRef < 0)
+            {
+              seqsWithoutSourceDBRef.add(seq);
+            }
+          }
+        }
+        // retrieve database refs for protein sequences
+        if (!seqsWithoutSourceDBRef.isEmpty())
+        {
+          JvOptionPane.newOptionDialog(Desktop.getDesktop())
+                  .setResponseHandler(0, discoverCanonicalDBrefs)
+                  .setResponseHandler(1, discoverPDBStructures)
+                  .showDialog(MessageManager.formatMessage("label.fetch_references_for",seqsWithoutSourceDBRef.size()), MessageManager.getString(
+                          "label.fetch_uniprot_references"),
+                          JvOptionPane.YES_NO_OPTION,
+                          JvOptionPane.PLAIN_MESSAGE, null, new Object[]
+                          { MessageManager.getString("action.ok"),
+                              MessageManager.getString("action.cancel") },
+                          MessageManager.getString("action.ok"));
+        } else {
+          // get structures directly
+          Executors.defaultThreadFactory().newThread(discoverPDBStructures).start();
+        }
+      };
+    }).start();;
+
   }
 
   /**
@@ -799,8 +865,8 @@ public class StructureChooser extends GStructureChooser
           List<SequenceI> selectedSeqsToView = new ArrayList<>();
           for (int row : selectedRows)
           {
-            PDBEntry pdbEntry = (PDBEntry) tbl_local_pdb.getValueAt(row,
-                    pdbIdColIndex);
+            PDBEntry pdbEntry = ((PDBEntryTableModel) tbl_local_pdb.getModel()).getPDBEntryAt(row).getPdbEntry();
+            
             pdbEntriesToView[count++] = pdbEntry;
             SequenceI selectedSeq = (SequenceI) tbl_local_pdb
                     .getValueAt(row, refSeqColIndex);
@@ -1159,7 +1225,7 @@ public class StructureChooser extends GStructureChooser
         value = entry.getSequence();
         break;
       case 1:
-        value = entry.getPdbEntry();
+        value = entry.getQualifiedId();
         break;
       case 2:
         value = entry.getPdbEntry().getChainCode() == null ? "_"
@@ -1200,6 +1266,15 @@ public class StructureChooser extends GStructureChooser
       this.pdbEntry = pdbEntry;
     }
 
+    public String getQualifiedId()
+    {
+      if (pdbEntry.hasProvider())
+      {
+        return pdbEntry.getProvider()+":"+pdbEntry.getId();
+      } 
+      return pdbEntry.toString();
+    }
+
     public SequenceI getSequence()
     {
       return sequence;