Adding tests for 3DB, currently 2/4 corrects on PanelTest
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSPanel.java
index 21034ac..0d79d9b 100644 (file)
@@ -1,9 +1,18 @@
 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 org.json.JSONArray;
+import org.json.JSONObject;
+
 import jalview.fts.api.FTSDataColumnI;
 import jalview.fts.api.FTSRestClientI;
 import jalview.fts.core.FTSRestRequest;
@@ -17,7 +26,7 @@ import jalview.util.MessageManager;
 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<>();
 
@@ -25,6 +34,8 @@ public class TDBeaconsFTSPanel extends GFTSPanel
 
   private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH";
   
+  private static HttpURLConnection connection;
+  
   public TDBeaconsFTSPanel(SequenceFetcher fetcher)
   {
     super(fetcher);
@@ -33,6 +44,70 @@ public class TDBeaconsFTSPanel extends GFTSPanel
     this.progressIndicator = (fetcher == null) ? null
             : fetcher.getProgressIndicator();
   }
+  
+  public int TDBeaconsFTSPanel2(String result)
+  {
+    int seqlength = executeParse(result);
+    return seqlength;
+  }
+  
+  public static int executeParse(String query) {
+    BufferedReader reader;
+    String line;
+    StringBuffer responseContent = new StringBuffer();
+    try {
+      URL url = new URL("https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/summary/" + query + ".json");
+      connection = (HttpURLConnection) url.openConnection();
+      connection.setRequestMethod("GET");
+      connection.setConnectTimeout(5000); // <=>5seconds
+      connection.setReadTimeout(5000);
+      // Status check up, 200 = connection succesful
+      int status = connection.getResponseCode();
+      // System.out.println(status);
+
+      if (status > 299) {
+              reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
+              while ((line = reader.readLine()) != null) {
+                      responseContent.append(line);
+              }
+              reader.close();
+      } else {
+              reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+              while ((line = reader.readLine()) != null) {
+                      responseContent.append(line);
+              }
+              reader.close();
+      }
+      //System.out.println(responseContent.toString());
+      int seq = parse(responseContent.toString());
+      return seq;
+
+      } catch (MalformedURLException e) {
+        e.printStackTrace();
+      } catch (IOException e) {
+        e.printStackTrace();
+      } finally {
+      connection.disconnect();
+      }
+      return (Integer) null;
+      
+  }
+  
+  public static int parse(String jsonString) {
+    JSONObject entry = new JSONObject(jsonString);
+    System.out.println(entry);
+    int length = entry.getJSONObject("uniprot_entry").getInt("sequence_length");
+    String  ac = entry.getJSONObject("uniprot_entry").getString("ac");
+    
+    JSONArray structures = entry.getJSONArray("structures");
+    for (int i=0 ; i < structures.length() ; i++) {
+            String id = structures.getJSONObject(i).getString("model_identifier");
+            System.out.println("identifier num " + i + " : " + id);
+    }
+    System.out.println("length : " + length + "; access name : " + ac);
+    return length;
+    
+}
 
   @Override
   public void searchAction(boolean isFreshSearch)
@@ -209,5 +284,9 @@ public class TDBeaconsFTSPanel extends GFTSPanel
     System.out.println("No help implemented yet.");
     
   }
-
+  
+  public static String decodeSearchTerm(String enteredText) {
+    // no multiple query support yet
+    return enteredText;
+  }
 }