039d23fe2be00d3bef626fa9c53f89efbcb0c5c5
[jalview.git] / src / jalview / ws / dbsources / PDBRestClient.java
1 package jalview.ws.dbsources;
2
3 import jalview.ws.uimodel.PDBRestRequest;
4 import jalview.ws.uimodel.PDBRestResponse;
5 import jalview.ws.uimodel.PDBRestResponse.PDBResponseSummary;
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import javax.ws.rs.core.MediaType;
13
14 import org.json.simple.JSONArray;
15 import org.json.simple.JSONObject;
16 import org.json.simple.parser.JSONParser;
17 import org.json.simple.parser.ParseException;
18
19 import com.sun.jersey.api.client.Client;
20 import com.sun.jersey.api.client.ClientResponse;
21 import com.sun.jersey.api.client.WebResource;
22 import com.sun.jersey.api.client.config.ClientConfig;
23 import com.sun.jersey.api.client.config.DefaultClientConfig;
24 import com.sun.jersey.api.json.JSONConfiguration;
25
26 public class PDBRestClient
27 {
28   private String pdbSearchEndpoint = "http://wwwdev.ebi.ac.uk/pdbe/search/pdb/select?";
29
30   /**
31    * Takes a PDBRestRequest object and returns a response upon execution
32    * 
33    * @param pdbRestRequest
34    *          the pdbRequest to be sent
35    * @return the pdbResponse object for the given pdbRequest
36    */
37   public PDBRestResponse executeRequest(PDBRestRequest pdbRestRequest)
38   {
39     ClientConfig clientConfig = new DefaultClientConfig();
40     clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
41             Boolean.TRUE);
42     Client client = Client.create(clientConfig);
43
44     String query = pdbRestRequest.getFieldToSearchBy()
45             + pdbRestRequest.getSearchTerm()
46             + ((pdbRestRequest.isAllowEmptySeq()) ? ""
47                     : " AND molecule_sequence:['' TO *]");
48
49     String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
50             .getWantedFields());
51
52     String responseSize = (pdbRestRequest.getResponseSize() == 0) ? "200"
53             : String.valueOf(pdbRestRequest.getResponseSize());
54     String sortParam = (pdbRestRequest.getFieldToSortBy() == null || pdbRestRequest
55             .getFieldToSortBy().trim().isEmpty()) ? ""
56  : (pdbRestRequest
57             .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
58             : " desc"));
59
60     WebResource webResource = client.resource(pdbSearchEndpoint)
61             .queryParam("wt", "json").queryParam("fl", wantedFields)
62             .queryParam("rows", responseSize)
63             .queryParam("q", query)
64             .queryParam("sort", sortParam);
65     ClientResponse clientResponse = webResource.accept(
66             MediaType.APPLICATION_JSON).get(ClientResponse.class);
67
68     String responseString = clientResponse.getEntity(String.class);
69     if (clientResponse.getStatus() != 200)
70     {
71       if (clientResponse.getStatus() == 400)
72       {
73         throw new RuntimeException(parseJsonExceptionString(responseString));
74       }
75       else
76       {
77       throw new RuntimeException("Failed : HTTP error code : "
78               + clientResponse.getStatus());
79       }
80     }
81     clientResponse = null;
82     client = null;
83     return parsePDBJsonResponse(responseString, pdbRestRequest);
84   }
85
86   /**
87    * Process error response from PDB server if/when one occurs.
88    * 
89    * @param jsonResponse
90    *          the json string containing error message from the server
91    * @return the processed error message from the json string
92    */
93   public static String parseJsonExceptionString(String jsonErrorResponse)
94   {
95     String errorMessage = "RunTime error";
96     try
97     {
98       JSONParser jsonParser = new JSONParser();
99       JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonErrorResponse);
100       JSONObject errorResponse = (JSONObject) jsonObj.get("error");
101       errorMessage = errorResponse.get("msg").toString();
102
103       JSONObject responseHeader = (JSONObject) jsonObj
104               .get("responseHeader");
105       errorMessage += responseHeader.get("params").toString();
106     } catch (ParseException e)
107     {
108       e.printStackTrace();
109     }
110     return errorMessage;
111   }
112
113   /**
114    * Parses json response string from PDB REST API to a PDBRestResponse
115    * instance. The parsed response is dynamic and based upon some of the request
116    * parameters.
117    * 
118    * @param pdbJsonResponseString
119    *          the json string to be parsed
120    * @param pdbRestRequest
121    *          the request object which contains parameters used to process the
122    *          json string
123    * @return
124    */
125   @SuppressWarnings("unchecked")
126   public static PDBRestResponse parsePDBJsonResponse(
127           String pdbJsonResponseString,
128           PDBRestRequest pdbRestRequest)
129   {
130     PDBRestResponse searchResult = new PDBRestResponse();
131     List<PDBResponseSummary> result = null;
132     try
133     {
134       JSONParser jsonParser = new JSONParser();
135       JSONObject jsonObj = (JSONObject) jsonParser
136               .parse(pdbJsonResponseString);
137
138       JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
139       String queryTime = ((JSONObject) jsonObj.get("responseHeader")).get(
140               "QTime").toString();
141       int numFound = Integer
142               .valueOf(pdbResponse.get("numFound").toString());
143       if (numFound > 0)
144       {
145         result = new ArrayList<PDBResponseSummary>();
146         JSONArray docs = (JSONArray) pdbResponse.get("docs");
147         for (Iterator<JSONObject> docIter = docs.iterator(); docIter
148                 .hasNext();)
149         {
150           JSONObject doc = docIter.next();
151           result.add(searchResult.new PDBResponseSummary(doc, pdbRestRequest));
152         }
153         searchResult.setNumberOfItemsFound(numFound);
154         searchResult.setResponseTime(queryTime);
155         searchResult.setSearchSummary(result);
156       }
157     } catch (ParseException e)
158     {
159       e.printStackTrace();
160     }
161     return searchResult;
162   }
163
164   /**
165    * Takes a collection of PDBDocField and converts its code values into a comma
166    * delimited string.
167    * 
168    * @param pdbDocfields
169    * @return
170    */
171   public static String getPDBDocFieldsAsCommaDelimitedString(
172           Collection<PDBDocField> pdbDocfields)
173   {
174     String result = "";
175     if (pdbDocfields != null && !pdbDocfields.isEmpty())
176     {
177       StringBuilder returnedFields = new StringBuilder();
178       for (PDBDocField field : pdbDocfields)
179       {
180         returnedFields.append(",").append(field.getCode());
181       }
182       returnedFields.deleteCharAt(0); 
183       result = returnedFields.toString();
184     }
185     return result;
186   }
187
188   /**
189    * Determines the column index for 'PDB Id' Fields in the dynamic summary
190    * table. The PDB Id serves as a unique identifier for a given row in the
191    * summary table
192    * 
193    * @param wantedFeilds
194    *          the available table columns in no particular order
195    * @return the pdb id field column index
196    */
197   public static int getPDBIdColumIndex(
198           Collection<PDBDocField> wantedFeilds, boolean hasRefSeq)
199   {
200     int pdbFeildIndexCounter = hasRefSeq ? 1 : 0; // If a reference sequence is
201                                            // attached then start counting from
202                                            // 1 else start from zero
203     for (PDBDocField feild : wantedFeilds)
204     {
205       if (feild.equals(PDBDocField.PDB_ID))
206       {
207         break; // once PDB Id index is determined exit iteration
208       }
209       ++pdbFeildIndexCounter;
210     }
211     return pdbFeildIndexCounter;
212   }
213
214   /**
215    * Represents the fields retrievable from a PDB Document response
216    *
217    */
218   public enum PDBDocField
219   {
220     PDB_ID("PDB Id", "pdb_id"), TITLE("Title", "title"), MOLECULE_NAME(
221             "Molecule", "molecule_name"), MOLECULE_TYPE("Molecule Type",
222             "molecule_type"), MOLECULE_SEQUENCE("Sequence",
223             "molecule_sequence"), PFAM_ACCESSION("PFAM Accession",
224             "pfam_accession"), PFAM_NAME("PFAM Name", "pfam_name"), INTERPRO_NAME(
225             "InterPro Name", "interpro_name"), INTERPRO_ACCESSION(
226             "InterPro Accession", "interpro_accession"), UNIPROT_ID(
227             "UniProt Id", "uniprot_id"), UNIPROT_ACCESSION(
228             "UniProt Accession", "uniprot_accession"), UNIPROT_COVERAGE(
229             "UniProt Coverage", "uniprot_coverage"), UNIPROT_FEATURES(
230             "Uniprot Features", "uniprot_features"), R_FACTOR("R Factor",
231             "r_factor"), RESOLUTION("Resolution", "resolution"), DATA_QUALITY(
232             "Data Quality", "data_quality"), OVERALL_QUALITY(
233             "Overall Quality", "overall_quality"), POLYMER_COUNT(
234             "Number of Polymers", "number_of_polymers"), PROTEIN_CHAIN_COUNT(
235             "Number of Protein Chains", "number_of_protein_chains"), BOUND_MOLECULE_COUNT(
236             "Number of Bound Molecule", "number_of_bound_molecules"), POLYMER_RESIDUE_COUNT(
237             "Number of Polymer Residue", "number_of_polymer_residues"), GENUS(
238             "GENUS", "genus"), GENE_NAME("Gene Name", "gene_name"), EXPERIMENTAL_METHOD(
239             "Experimental Method", "experimental_method"), GO_ID("GO Id",
240             "go_id"), ASSEMBLY_ID("Assembly Id", "assembly_form"), ASSEMBLY_FORM(
241             "Assembly Form", "assembly_id"), ASSEMBLY_TYPE("Assembly Type",
242             "assembly_type"), SPACE_GROUP("Space Group", "spacegroup"), CATH_CODE(
243             "Cath Code", "cath_code"), TAX_ID("Tax Id", "tax_id"), TAX_QUERY(
244             "Tax Query", "tax_query"), INTERACTING_ENTRY_ID(
245             "Interacting Entry Id", "interacting_entry_id"), INTERACTING_ENTITY_ID(
246             "Interacting Entity Id", "interacting_entity_id"), INTERACTING_MOLECULES(
247             "Interacting Molecules", "interacting_molecules"), PUBMED_ID(
248             "Pubmed Id", "pubmed_id"), STATUS("Status", "status"), MODEL_QUALITY(
249             "Model Quality", "model_quality"), PIVOT_RESOLUTION(
250             "Pivot Resolution", "pivot_resolution"), DATA_REDUCTION_SOFTWARE(
251             "Data reduction software", "data_reduction_software"), MAX_OBSERVED_RES(
252             "Max observed residues", "max_observed_residues"), ORG_SCI_NAME(
253             "Organism scientific name", "organism_scientific_name"), SUPER_KINGDOM(
254             "Super kingdom", "superkingdom"), RANK("Rank", "rank"), CRYSTALLISATION_PH(
255             "Crystallisation Ph", "crystallisation_ph"), BIO_FUNCTION(
256             "Biological Function", "biological_function"), BIO_PROCESS(
257             "Biological Process", "biological_process"), BIO_CELL_COMP(
258             "Biological Cell Component", "biological_cell_component"), COMPOUND_NAME(
259             "Compound Name", "compound_name"), COMPOUND_ID("Compound Id",
260             "compound_id"), COMPOUND_WEIGHT("Compound Weight",
261             "compound_weight"), COMP_SYS_NAME("Compound Systematic Name",
262             "compound_systematic_name"), INTERACTING_LIG(
263             "Interacting Ligands", "interacting_ligands"), JOURNAL(
264             "Journal", "journal"), ALL_AUTHORS("All Authors", "all_authors"), EXPERIMENTAL_DATA_AVAILABLE(
265             "Experiment Data Available", "experiment_data_available"), DIFFRACTION_PROTOCOL(
266             "Diffraction Protocol", "diffraction_protocol"), REFINEMENT_SOFTWARE(
267             "Refinement Software", "refinement_software"), STRUCTURE_DETERMINATION_METHOD(
268             "Structure Determination Method",
269             "structure_determination_method"), SYNCHROTON_SITE(
270             "Synchrotron Site", "synchrotron_site"), SAMPLE_PREP_METHOD(
271             "Sample Preparation Method", "sample_preparation_method"), ENTRY_AUTHORS(
272             "Entry Authors", "entry_authors"), CITATION_TITLE(
273             "Citation Title", "citation_title"), STRUCTURE_SOLUTION_SOFTWARE(
274             "Structure Solution Software", "structure_solution_software"), ENTRY_ENTITY(
275             "Entry Entity", "entry_entity"), R_FREE("R Free", "r_free"), NO_OF_POLYMER_ENTITIES(
276             "Number of Polymer Entities", "number_of_polymer_entities"), NO_OF_BOUND_ENTITIES(
277             "Number of Bound Entities", "number_of_bound_entities"), CRYSTALLISATION_RESERVOIR(
278             "Crystallisation Reservoir", "crystallisation_reservoir"), DATA_SCALING_SW(
279             "Data Scalling Software", "data_scaling_software"), DETECTOR(
280             "Detector", "detector"), DETECTOR_TYPE("Detector Type",
281             "detector_type"), MODIFIED_RESIDUE_FLAG(
282             "Modified Residue Flag", "modified_residue_flag"), NUMBER_OF_COPIES(
283             "Number of Copies", "number_of_copies"), STRUCT_ASYM_ID(
284             "Struc Asym Id", "struct_asym_id"), HOMOLOGUS_PDB_ENTITY_ID(
285             "Homologus PDB Entity Id", "homologus_pdb_entity_id"), MOLECULE_SYNONYM(
286             "Molecule Synonym", "molecule_synonym"), DEPOSITION_SITE(
287             "Deposition Site", "deposition_site"), SYNCHROTRON_BEAMLINE(
288             "Synchrotron Beamline", "synchrotron_beamline"), ENTITY_ID(
289             "Entity Id", "entity_id"), BEAM_SOURCE_NAME("Beam Source Name",
290             "beam_source_name"), PROCESSING_SITE("Processing Site",
291             "processing_site"), ENTITY_WEIGHT("Entity Weight",
292             "entity_weight"), VERSION("Version", "_version_"), ALL("ALL",
293             "text");
294
295     private String name;
296
297     private String code;
298
299     PDBDocField(String name, String code)
300     {
301       this.name = name;
302       this.code = code;
303     }
304
305     public String getName()
306     {
307       return name;
308     }
309
310     public String getCode()
311     {
312       return code;
313     }
314
315     public String toString()
316     {
317       return name;
318     }
319   }
320 }