JAL-1668 added filtering and sorting capabilites
[jalview.git] / src / jalview / ws / dbsources / PDBRestClient.java
index bdc507f..e00d9ac 100644 (file)
@@ -2,8 +2,7 @@ package jalview.ws.dbsources;
 
 import jalview.ws.uimodel.PDBSearchRequest;
 import jalview.ws.uimodel.PDBSearchResponse;
-import jalview.ws.uimodel.PDBSummaryListModel;
-import jalview.ws.uimodel.PDBSummaryListModel.PDBDocField;
+import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -32,7 +31,7 @@ public class PDBRestClient
     PDBSearchRequest request = new PDBSearchRequest();
     request.setAllowEmptySeq(false);
     request.setResponseSize(100);
-    request.setSearchTarget("pfam_name");
+    request.setFieldToSearchBy("pfam_name");
     request.setSearchTerm("Lipoc*");
     List<PDBDocField> wantedFields = new ArrayList<PDBDocField>();
     wantedFields.add(PDBDocField.MOLECULE_TYPE);
@@ -51,7 +50,7 @@ public class PDBRestClient
             Boolean.TRUE);
     Client client = Client.create(clientConfig);
 
-    String query = request.getSearchTarget()
+    String query = request.getFieldToSearchBy()
             + request.getSearchTerm()
             + ((request.isAllowEmptySeq()) ? ""
                     : " AND molecule_sequence:['' TO *]");
@@ -61,28 +60,63 @@ public class PDBRestClient
 
     String responseSize = (request.getResponseSize() == 0) ? "200" : String
             .valueOf(request.getResponseSize());
+    String sortParam = (request.getFieldToSortBy() == null || request
+            .getFieldToSortBy().trim().isEmpty()) ? ""
+            : (request
+            .getFieldToSortBy() + (request.isAscending() ? " asc" : " desc"));
+
     WebResource webResource = client.resource(pdbSearchEndpoint)
             .queryParam("wt", "json").queryParam("fl", wantedFields)
             .queryParam("rows", responseSize)
-            .queryParam("q", query);
+            .queryParam("q", query)
+            .queryParam("sort", sortParam);
     ClientResponse clientResponse = webResource.accept(
             MediaType.APPLICATION_JSON).get(ClientResponse.class);
 
     String responseString = clientResponse.getEntity(String.class);
     if (clientResponse.getStatus() != 200)
     {
+      if (clientResponse.getStatus() == 400)
+      {
+        throw new RuntimeException(parseException(responseString));
+      }
+      else
+      {
       throw new RuntimeException("Failed : HTTP error code : "
               + clientResponse.getStatus());
+      }
     }
     // System.out.println("--------------> " + responseString);
-    return parseResponse(responseString, request.getWantedFields());
+    return parseResponse(responseString, request.getWantedFields(),
+            request.getAssociatedSequence());
   }
 
+  private String parseException(String jsonResponse)
+  {
+    String errorMessage = "RunTime error";
+    try
+    {
+      JSONParser jsonParser = new JSONParser();
+      JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonResponse);
+      JSONObject errorResponse = (JSONObject) jsonObj.get("error");
+      errorMessage = errorResponse.get("msg").toString();
+
+      JSONObject responseHeader = (JSONObject) jsonObj
+              .get("responseHeader");
+      errorMessage += responseHeader.get("params").toString();
+    } catch (ParseException e)
+    {
+      e.printStackTrace();
+    }
+    return errorMessage;
+  }
+
+  @SuppressWarnings("unchecked")
   private PDBSearchResponse parseResponse(String jsonResponse,
-          List<PDBDocField> wantedFields)
+          List<PDBDocField> wantedFields, String associatedSequence)
   {
     PDBSearchResponse searchResult = new PDBSearchResponse();
-    List<PDBSummaryListModel> result = null;
+    List<PDBResponseSummary> result = null;
     try
     {
       JSONParser jsonParser = new JSONParser();
@@ -96,7 +130,7 @@ public class PDBRestClient
               .valueOf(pdbResponse.get("numFound").toString());
       if (numFound > 0)
       {
-        result = new ArrayList<PDBSummaryListModel>();
+        result = new ArrayList<PDBResponseSummary>();
         JSONArray docs = (JSONArray) pdbResponse.get("docs");
         for (Iterator<JSONObject> docIter = docs.iterator(); docIter
                 .hasNext();)
@@ -104,7 +138,8 @@ public class PDBRestClient
           JSONObject doc = docIter.next();
           // if (doc.get("molecule_sequence") != null)
           // {
-          result.add(new PDBSummaryListModel(doc, wantedFields));
+          result.add(searchResult.new PDBResponseSummary(doc, wantedFields,
+                  associatedSequence));
           // }
         }
         searchResult.setItemsFound(numFound);
@@ -136,4 +171,49 @@ public class PDBRestClient
   }
 
 
+  public enum PDBDocField
+  {
+    PDB_ID("PDB Id", "pdb_id"), TITLE("Title", "title"), MOLECULE_NAME(
+            "Molecule", "molecule_name"), MOLECULE_TYPE("Molecule Type",
+            "molecule_type"), MOLECULE_SEQUENCE("Sequence",
+            "molecule_sequence"), UNIPROT_FEATURES("Uniprot Features",
+            "uniprot_features"), PFAM_ACCESSION("PFAM Accession",
+            "pfam_accession"), INTERPRO_ACCESSION("InterPro Accession",
+            "interpro_accession"), UNIPROT_ACCESSION("UniProt Accession",
+            "uniprot_accession"), R_FACTOR("R Factor", "r_factor"), RESOLUTION(
+            "Resolution", "resolution"), DATA_QUALITY("Data Quality",
+            "data_quality"), OVERALL_QUALITY("Overall Quality",
+            "overall_quality"), POLYMER_COUNT("Polymer Count",
+            "number_of_polymers"), PROTEIN_CHAIN_COUNT(
+            "Protein Chain Count", "number_of_protein_chains"), BOUND_MOLECULE_COUNT(
+            "Bound Molecule Count", "number_of_bound_molecules"), POLYMER_RESIDUE_COUNT(
+            "Polymer Residue Count", "number_of_polymer_residues"), UNIPROT_COVERAGE(
+            "UniProt Coverage", "uniprot_coverage"), GENUS("GENUS", "genus"), GENE_NAME(
+            "Gene Name", "gene_name"), ALL("ALL", "text");
+
+    private String name;
+
+    private String code;
+
+    PDBDocField(String name, String code)
+    {
+      this.name = name;
+      this.code = code;
+    }
+
+    public String getName()
+    {
+      return name;
+    }
+
+    public String getCode()
+    {
+      return code;
+    }
+
+    public String toString()
+    {
+      return name;
+    }
+  }
 }