1 package jalview.ws.dbsources;
3 import jalview.ws.uimodel.PDBRestRequest;
4 import jalview.ws.uimodel.PDBRestResponse;
5 import jalview.ws.uimodel.PDBRestResponse.PDBResponseSummary;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Iterator;
10 import java.util.List;
12 import javax.ws.rs.core.MediaType;
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;
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;
27 * A rest client for querying the Search endpoing of the PDB REST API
32 public class PDBRestClient
34 public static final String PDB_SEARCH_ENDPOINT = "http://www.ebi.ac.uk/pdbe/search/pdb/select?";
36 private static int DEFAULT_RESPONSE_SIZE = 200;
39 * Takes a PDBRestRequest object and returns a response upon execution
41 * @param pdbRestRequest
42 * the PDBRestRequest instance to be processed
43 * @return the pdbResponse object for the given request
46 public PDBRestResponse executeRequest(PDBRestRequest pdbRestRequest)
51 ClientConfig clientConfig = new DefaultClientConfig();
52 Client client = Client.create(clientConfig);
54 String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
56 int responseSize = (pdbRestRequest.getResponseSize() == 0) ? DEFAULT_RESPONSE_SIZE
57 : pdbRestRequest.getResponseSize();
58 String sortParam = (pdbRestRequest.getFieldToSortBy() == null || pdbRestRequest
59 .getFieldToSortBy().trim().isEmpty()) ? "" : (pdbRestRequest
60 .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
63 // Build request parameters for the REST Request
64 WebResource webResource = client.resource(PDB_SEARCH_ENDPOINT)
65 .queryParam("wt", "json").queryParam("fl", wantedFields)
66 .queryParam("rows", String.valueOf(responseSize))
67 .queryParam("q", pdbRestRequest.getQuery())
68 .queryParam("sort", sortParam);
70 // Execute the REST request
71 ClientResponse clientResponse = webResource.accept(
72 MediaType.APPLICATION_JSON).get(ClientResponse.class);
74 // Get the JSON string from the response object
75 String responseString = clientResponse.getEntity(String.class);
77 // Check the response status and report exception if one occurs
78 if (clientResponse.getStatus() != 200)
80 String errorMessage = "";
81 if (clientResponse.getStatus() == 400)
83 errorMessage = parseJsonExceptionString(responseString);
84 throw new Exception(errorMessage);
88 errorMessage = getMessageByHTTPStatusCode(clientResponse
90 throw new Exception(errorMessage);
94 // Make redundant objects eligible for garbage collection to conserve
96 clientResponse = null;
99 // Process the response and return the result to the caller.
100 return parsePDBJsonResponse(responseString, pdbRestRequest);
101 } catch (Exception e)
103 String exceptionMsg = e.getMessage();
104 if (exceptionMsg.contains("SocketException"))
107 "Jalview is unable to detect an internet connection");
108 // No internet connection
110 else if (exceptionMsg.contains("UnknownHostException"))
113 "Jalview is unable to reach the host server \'www.ebi.ac.uk\'. "
114 + "\nPlease ensure that you are connected to the internet and try again.");
115 // The server 'www.ebi.ac.uk' is unreachable
124 public String getMessageByHTTPStatusCode(int code)
130 message = "PDB Rest Service no longer exists!";
134 message = "The requested resource could not be found";
144 message = "There seems to be an error from the PDB Rest API server.";
154 * Process error response from PDB server if/when one occurs.
156 * @param jsonResponse
157 * the JSON string containing error message from the server
158 * @return the processed error message from the JSON string
160 public static String parseJsonExceptionString(String jsonErrorResponse)
162 StringBuilder errorMessage = new StringBuilder(
163 "\n============= PDB Rest Client RunTime error =============\n");
167 JSONParser jsonParser = new JSONParser();
168 JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonErrorResponse);
169 JSONObject errorResponse = (JSONObject) jsonObj.get("error");
171 JSONObject responseHeader = (JSONObject) jsonObj
172 .get("responseHeader");
173 JSONObject paramsObj = (JSONObject) responseHeader.get("params");
174 String status = responseHeader.get("status").toString();
175 String message = errorResponse.get("msg").toString();
176 String query = paramsObj.get("q").toString();
177 String fl = paramsObj.get("fl").toString();
179 errorMessage.append("Status: ").append(status).append("\n");
180 errorMessage.append("Message: ").append(message).append("\n");
181 errorMessage.append("query: ").append(query).append("\n");
182 errorMessage.append("fl: ").append(fl).append("\n");
184 } catch (ParseException e)
188 return errorMessage.toString();
192 * Parses the JSON response string from PDB REST API. The response is dynamic
193 * hence, only fields specifically requested for in the 'wantedFields'
194 * parameter is fetched/processed
196 * @param pdbJsonResponseString
197 * the JSON string to be parsed
198 * @param pdbRestRequest
199 * the request object which contains parameters used to process the
203 @SuppressWarnings("unchecked")
204 public static PDBRestResponse parsePDBJsonResponse(
205 String pdbJsonResponseString, PDBRestRequest pdbRestRequest)
207 PDBRestResponse searchResult = new PDBRestResponse();
208 List<PDBResponseSummary> result = null;
211 JSONParser jsonParser = new JSONParser();
212 JSONObject jsonObj = (JSONObject) jsonParser
213 .parse(pdbJsonResponseString);
215 JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
216 String queryTime = ((JSONObject) jsonObj.get("responseHeader")).get(
218 int numFound = Integer
219 .valueOf(pdbResponse.get("numFound").toString());
222 result = new ArrayList<PDBResponseSummary>();
223 JSONArray docs = (JSONArray) pdbResponse.get("docs");
224 for (Iterator<JSONObject> docIter = docs.iterator(); docIter
227 JSONObject doc = docIter.next();
228 result.add(searchResult.new PDBResponseSummary(doc,
231 searchResult.setNumberOfItemsFound(numFound);
232 searchResult.setResponseTime(queryTime);
233 searchResult.setSearchSummary(result);
235 } catch (ParseException e)
243 * Takes a collection of PDBDocField and converts its 'code' Field values into
244 * a comma delimited string.
246 * @param pdbDocfields
247 * the collection of PDBDocField to process
248 * @return the comma delimited string from the pdbDocFields collection
250 public static String getPDBDocFieldsAsCommaDelimitedString(
251 Collection<PDBDocField> pdbDocfields)
254 if (pdbDocfields != null && !pdbDocfields.isEmpty())
256 StringBuilder returnedFields = new StringBuilder();
257 for (PDBDocField field : pdbDocfields)
259 returnedFields.append(",").append(field.getCode());
261 returnedFields.deleteCharAt(0);
262 result = returnedFields.toString();
268 * Determines the column index for 'PDB Id' Fields in the dynamic summary
269 * table. The PDB Id serves as a unique identifier for a given row in the
272 * @param wantedFeilds
273 * the available table columns in no particular order
274 * @return the pdb id field column index
276 public static int getPDBIdColumIndex(
277 Collection<PDBDocField> wantedFeilds, boolean hasRefSeq)
280 // If a reference sequence is attached then start counting from 1 else
282 int pdbFeildIndexCounter = hasRefSeq ? 1 : 0;
284 for (PDBDocField feild : wantedFeilds)
286 if (feild.equals(PDBDocField.PDB_ID))
288 break; // Once PDB Id index is determined exit iteration
290 ++pdbFeildIndexCounter;
292 return pdbFeildIndexCounter;
296 * This enum represents the fields available in the PDB JSON response
299 public enum PDBDocField
301 PDB_ID("PDB Id", "pdb_id"), TITLE("Title", "title"), MOLECULE_NAME(
302 "Molecule", "molecule_name"), MOLECULE_TYPE("Molecule Type",
303 "molecule_type"), MOLECULE_SEQUENCE("Sequence",
304 "molecule_sequence"), PFAM_ACCESSION("PFAM Accession",
305 "pfam_accession"), PFAM_NAME("PFAM Name", "pfam_name"), INTERPRO_NAME(
306 "InterPro Name", "interpro_name"), INTERPRO_ACCESSION(
307 "InterPro Accession", "interpro_accession"), UNIPROT_ID(
308 "UniProt Id", "uniprot_id"), UNIPROT_ACCESSION(
309 "UniProt Accession", "uniprot_accession"), UNIPROT_COVERAGE(
310 "UniProt Coverage", "uniprot_coverage"), UNIPROT_FEATURES(
311 "Uniprot Features", "uniprot_features"), R_FACTOR("R Factor",
312 "r_factor"), RESOLUTION("Resolution", "resolution"), DATA_QUALITY(
313 "Data Quality", "data_quality"), OVERALL_QUALITY(
314 "Overall Quality", "overall_quality"), POLYMER_COUNT(
315 "Number of Polymers", "number_of_polymers"), PROTEIN_CHAIN_COUNT(
316 "Number of Protein Chains", "number_of_protein_chains"), BOUND_MOLECULE_COUNT(
317 "Number of Bound Molecule", "number_of_bound_molecules"), POLYMER_RESIDUE_COUNT(
318 "Number of Polymer Residue", "number_of_polymer_residues"), GENUS(
319 "GENUS", "genus"), GENE_NAME("Gene Name", "gene_name"), EXPERIMENTAL_METHOD(
320 "Experimental Method", "experimental_method"), GO_ID("GO Id",
321 "go_id"), ASSEMBLY_ID("Assembly Id", "assembly_form"), ASSEMBLY_FORM(
322 "Assembly Form", "assembly_id"), ASSEMBLY_TYPE("Assembly Type",
323 "assembly_type"), SPACE_GROUP("Space Group", "spacegroup"), CATH_CODE(
324 "Cath Code", "cath_code"), TAX_ID("Tax Id", "tax_id"), TAX_QUERY(
325 "Tax Query", "tax_query"), INTERACTING_ENTRY_ID(
326 "Interacting Entry Id", "interacting_entry_id"), INTERACTING_ENTITY_ID(
327 "Interacting Entity Id", "interacting_entity_id"), INTERACTING_MOLECULES(
328 "Interacting Molecules", "interacting_molecules"), PUBMED_ID(
329 "Pubmed Id", "pubmed_id"), STATUS("Status", "status"), MODEL_QUALITY(
330 "Model Quality", "model_quality"), PIVOT_RESOLUTION(
331 "Pivot Resolution", "pivot_resolution"), DATA_REDUCTION_SOFTWARE(
332 "Data reduction software", "data_reduction_software"), MAX_OBSERVED_RES(
333 "Max observed residues", "max_observed_residues"), ORG_SCI_NAME(
334 "Organism scientific name", "organism_scientific_name"), SUPER_KINGDOM(
335 "Super kingdom", "superkingdom"), RANK("Rank", "rank"), CRYSTALLISATION_PH(
336 "Crystallisation Ph", "crystallisation_ph"), BIOLOGICAL_FUNCTION(
337 "Biological Function", "biological_function"), BIOLOGICAL_PROCESS(
338 "Biological Process", "biological_process"), BIOLOGICAL_CELL_COMPONENT(
339 "Biological Cell Component", "biological_cell_component"), COMPOUND_NAME(
340 "Compound Name", "compound_name"), COMPOUND_ID("Compound Id",
341 "compound_id"), COMPOUND_WEIGHT("Compound Weight",
342 "compound_weight"), COMPOUND_SYSTEMATIC_NAME(
343 "Compound Systematic Name", "compound_systematic_name"), INTERACTING_LIG(
344 "Interacting Ligands", "interacting_ligands"), JOURNAL(
345 "Journal", "journal"), ALL_AUTHORS("All Authors", "all_authors"), EXPERIMENTAL_DATA_AVAILABLE(
346 "Experiment Data Available", "experiment_data_available"), DIFFRACTION_PROTOCOL(
347 "Diffraction Protocol", "diffraction_protocol"), REFINEMENT_SOFTWARE(
348 "Refinement Software", "refinement_software"), STRUCTURE_DETERMINATION_METHOD(
349 "Structure Determination Method",
350 "structure_determination_method"), SYNCHROTON_SITE(
351 "Synchrotron Site", "synchrotron_site"), SAMPLE_PREP_METHOD(
352 "Sample Preparation Method", "sample_preparation_method"), ENTRY_AUTHORS(
353 "Entry Authors", "entry_authors"), CITATION_TITLE(
354 "Citation Title", "citation_title"), STRUCTURE_SOLUTION_SOFTWARE(
355 "Structure Solution Software", "structure_solution_software"), ENTRY_ENTITY(
356 "Entry Entity", "entry_entity"), R_FREE("R Free", "r_free"), NO_OF_POLYMER_ENTITIES(
357 "Number of Polymer Entities", "number_of_polymer_entities"), NO_OF_BOUND_ENTITIES(
358 "Number of Bound Entities", "number_of_bound_entities"), CRYSTALLISATION_RESERVOIR(
359 "Crystallisation Reservoir", "crystallisation_reservoir"), DATA_SCALING_SW(
360 "Data Scalling Software", "data_scaling_software"), DETECTOR(
361 "Detector", "detector"), DETECTOR_TYPE("Detector Type",
362 "detector_type"), MODIFIED_RESIDUE_FLAG(
363 "Modified Residue Flag", "modified_residue_flag"), NUMBER_OF_COPIES(
364 "Number of Copies", "number_of_copies"), STRUCT_ASYM_ID(
365 "Struc Asym Id", "struct_asym_id"), HOMOLOGUS_PDB_ENTITY_ID(
366 "Homologus PDB Entity Id", "homologus_pdb_entity_id"), MOLECULE_SYNONYM(
367 "Molecule Synonym", "molecule_synonym"), DEPOSITION_SITE(
368 "Deposition Site", "deposition_site"), SYNCHROTRON_BEAMLINE(
369 "Synchrotron Beamline", "synchrotron_beamline"), ENTITY_ID(
370 "Entity Id", "entity_id"), BEAM_SOURCE_NAME("Beam Source Name",
371 "beam_source_name"), PROCESSING_SITE("Processing Site",
372 "processing_site"), ENTITY_WEIGHT("Entity Weight",
373 "entity_weight"), VERSION("Version", "_version_"), ALL("ALL",
380 PDBDocField(String name, String code)
386 public String getName()
391 public String getCode()
396 public String toString()