JAL-1667 PDBe Search Interface implementation
[jalview.git] / src / jalview / ws / dbsources / PDBRestClient.java
diff --git a/src/jalview/ws/dbsources/PDBRestClient.java b/src/jalview/ws/dbsources/PDBRestClient.java
new file mode 100644 (file)
index 0000000..c26c88f
--- /dev/null
@@ -0,0 +1,97 @@
+package jalview.ws.dbsources;
+
+import jalview.ws.uimodel.PDBSearchResultPojo;
+import jalview.ws.uimodel.PDBSummaryListModel;
+
+import java.util.Iterator;
+
+import javax.swing.DefaultListModel;
+import javax.ws.rs.core.MediaType;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.json.JSONConfiguration;
+
+public class PDBRestClient
+{
+  private String pdbSearchEndpoint = "http://wwwdev.ebi.ac.uk/pdbe/search/pdb/select?";
+
+
+  public static void main(String[] args)
+  {
+    new PDBRestClient().searchResult("pfam_name", "Lipoc*");
+  }
+
+  private String executeRestSearch(String qParam,
+          String searchTerm)
+  {
+    ClientConfig clientConfig = new DefaultClientConfig();
+    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
+            Boolean.TRUE);
+    Client client = Client.create(clientConfig);
+    WebResource webResource = client.resource(pdbSearchEndpoint)
+            .queryParam("wt", "json")
+            .queryParam("q", qParam + ":" + searchTerm);
+    ClientResponse clientResponse = webResource.accept(
+            MediaType.APPLICATION_JSON).get(ClientResponse.class);
+
+    String responseString = clientResponse.getEntity(String.class);
+    if (clientResponse.getStatus() != 200)
+    {
+      throw new RuntimeException("Failed : HTTP error code : "
+              + clientResponse.getStatus());
+    }
+    return responseString;
+  }
+
+  public PDBSearchResultPojo searchResult(String qParam,
+          String searchTerm)
+  {
+    String jsonResponseString = executeRestSearch(qParam, searchTerm);
+    PDBSearchResultPojo searchResult = new PDBSearchResultPojo();
+    DefaultListModel<PDBSummaryListModel> result = null;
+    try
+    {
+      JSONParser jsonParser = new JSONParser();
+      JSONObject jsonObj = (JSONObject) jsonParser
+              .parse(jsonResponseString);
+
+      JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
+      String queryTime = ((JSONObject) jsonObj.get("responseHeader")).get(
+              "QTime").toString();
+      int numFound = Integer
+              .valueOf(pdbResponse.get("numFound").toString());
+      if (numFound > 0)
+      {
+        result = new DefaultListModel<PDBSummaryListModel>();
+        JSONArray docs = (JSONArray) pdbResponse.get("docs");
+        for (Iterator<JSONObject> docIter = docs.iterator(); docIter
+                .hasNext();)
+        {
+          JSONObject doc = docIter.next();
+          if (doc.get("molecule_sequence") != null)
+          {
+            result.addElement(new PDBSummaryListModel(doc));
+          }
+        }
+        searchResult.setItemFound(numFound);
+        searchResult.setResponseTime(queryTime);
+        searchResult.setSearchSummary(result);
+      }
+    } catch (ParseException e)
+    {
+      e.printStackTrace();
+    }
+
+    return searchResult;
+  }
+
+}