Merge branch 'merge/develop_JAL-1260' into develop
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSPanel.java
index 21034ac..7fa6aa5 100644 (file)
@@ -1,9 +1,22 @@
 package jalview.fts.service.threedbeacons;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 
+import javax.swing.SwingUtilities;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
 import jalview.fts.api.FTSDataColumnI;
 import jalview.fts.api.FTSRestClientI;
 import jalview.fts.core.FTSRestRequest;
@@ -11,24 +24,33 @@ import jalview.fts.core.FTSRestResponse;
 import jalview.fts.core.GFTSPanel;
 import jalview.fts.service.pdb.PDBFTSRestClient;
 import jalview.gui.SequenceFetcher;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
+import jalview.io.FileFormatI;
+import jalview.io.FileLoader;
+import jalview.io.FormatAdapter;
 import jalview.util.MessageManager;
 
 @SuppressWarnings("serial")
 public class TDBeaconsFTSPanel extends GFTSPanel
-{       
+{
   private static String defaultFTSFrameTitle = MessageManager
-          .getString("label.threedb_sequence_fetcher");
+          .getString("label.pdb_sequence_fetcher");
 
   private static Map<String, Integer> tempUserPrefs = new HashMap<>();
 
   private static final String THREEDB_FTS_CACHE_KEY = "CACHE.THREEDB_FTS";
 
   private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH";
-  
+
+  private static HttpURLConnection connection;
+
   public TDBeaconsFTSPanel(SequenceFetcher fetcher)
   {
-    super(fetcher);
-    pageLimit = TDBeaconsFTSRestClient.getInstance().getDefaultResponsePageSize();
+    // no ID retrieval option for TD Beacons just now
+    super(null);
+    pageLimit = TDBeaconsFTSRestClient.getInstance()
+            .getDefaultResponsePageSize();
     this.seqFetcher = fetcher;
     this.progressIndicator = (fetcher == null) ? null
             : fetcher.getProgressIndicator();
@@ -63,11 +85,12 @@ public class TDBeaconsFTSPanel extends GFTSPanel
           FTSRestRequest request = new FTSRestRequest();
           request.setAllowEmptySeq(allowEmptySequence);
           request.setResponseSize(100);
-          request.setFieldToSearchBy("(" + searchTarget + ":");
-          request.setSearchTerm(searchTerm + ")");
+          // expect it to be uniprot accesssion
+          request.setSearchTerm(searchTerm + ".json");
           request.setOffSet(offSet);
           request.setWantedFields(wantedFields);
-          FTSRestClientI tdbRestClient = TDBeaconsFTSRestClient.getInstance();
+          FTSRestClientI tdbRestClient = TDBeaconsFTSRestClient
+                  .getInstance();
           FTSRestResponse resultList;
           try
           {
@@ -100,7 +123,8 @@ public class TDBeaconsFTSPanel extends GFTSPanel
 
           if (isPaginationEnabled() && resultSetCount > 0)
           {
-            String f1 = totalNumberformatter.format(Integer.valueOf(offSet + 1));
+            String f1 = totalNumberformatter
+                    .format(Integer.valueOf(offSet + 1));
             String f2 = totalNumberformatter
                     .format(Integer.valueOf(offSet + resultSetCount));
             String f3 = totalNumberformatter
@@ -124,14 +148,14 @@ public class TDBeaconsFTSPanel extends GFTSPanel
       }
     }.start();
   }
-  
+
   @Override
   public void okAction()
   {
     // mainFrame.dispose();
     disableActionButtons();
     StringBuilder selectedIds = new StringBuilder();
-    HashSet<String> selectedIdsSet = new HashSet<>();
+    final HashSet<String> selectedIdsSet = new HashSet<>();
     int primaryKeyColIndex = 0;
     try
     {
@@ -147,12 +171,12 @@ public class TDBeaconsFTSPanel extends GFTSPanel
     {
       String idStr = getResultTable()
               .getValueAt(summaryRow, primaryKeyColIndex).toString();
-      selectedIdsSet.add(searchTerm);
+      selectedIdsSet.add(idStr);
     }
 
     for (String idStr : paginatorCart)
     {
-      selectedIdsSet.add(searchTerm);
+      selectedIdsSet.add(idStr);
     }
 
     for (String selectedId : selectedIdsSet)
@@ -160,19 +184,57 @@ public class TDBeaconsFTSPanel extends GFTSPanel
       selectedIds.append(selectedId).append(";");
     }
 
-    String ids = selectedIds.toString();
-    seqFetcher.setQuery(ids);
-    Thread worker = new Thread(seqFetcher);
-    worker.start();
+    SwingUtilities.invokeLater(new Runnable()
+    {
+      @Override
+      public void run()
+      {
+        AlignmentI allSeqs = null;
+        FormatAdapter fl = new jalview.io.FormatAdapter();
+        for (String tdbURL : selectedIdsSet)
+        {
+          try
+          {
+            // retrieve the structure via its URL
+            AlignmentI tdbAl = fl.readFile(tdbURL, DataSourceType.URL,
+                    FileFormat.MMCif);
+
+            // TODO: pad structure according to its Uniprot Start so all line up w.r.t. the Uniprot reference sequence
+            // TODO: give the structure a sensible name (not the giant URL *:o) )
+            if (tdbAl != null)
+            {
+              if (allSeqs != null)
+              {
+                allSeqs.append(tdbAl);
+              }
+              else
+              {
+                allSeqs = tdbAl;
+              }
+            }
+          } catch (Exception x)
+          {
+            Cache.log.warn(
+                    "Couldn't retrieve 3d-beacons model for uniprot id"
+                            + searchTerm + " : " + tdbURL,
+                    x);
+          }
+        }
+        seqFetcher.parseResult(allSeqs,
+                "3D-Beacons models for " + searchTerm, FileFormat.MMCif,
+                null);
+
+      }
+    });
     delayAndEnableActionButtons();
   }
-  
+
   @Override
   public FTSRestClientI getFTSRestClient()
   {
     return TDBeaconsFTSRestClient.getInstance();
   }
-  
+
   @Override
   public String getFTSFrameTitle()
   {
@@ -207,7 +269,12 @@ public class TDBeaconsFTSPanel extends GFTSPanel
   protected void showHelp()
   {
     System.out.println("No help implemented yet.");
-    
+
   }
 
+  public static String decodeSearchTerm(String enteredText)
+  {
+    // no multiple query support yet
+    return enteredText;
+  }
 }