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;
24 import com.sun.jersey.api.json.JSONConfiguration;
26 public class PDBRestClient
28 private String pdbSearchEndpoint = "http://wwwdev.ebi.ac.uk/pdbe/search/pdb/select?";
31 * Takes a PDBRestRequest object and returns a response upon execution
33 * @param pdbRestRequest
34 * the pdbRequest to be sent
35 * @return the pdbResponse object for the given pdbRequest
37 public PDBRestResponse executeRequest(PDBRestRequest pdbRestRequest)
39 ClientConfig clientConfig = new DefaultClientConfig();
40 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
42 Client client = Client.create(clientConfig);
44 String query = pdbRestRequest.getFieldToSearchBy()
45 + pdbRestRequest.getSearchTerm()
46 + ((pdbRestRequest.isAllowEmptySeq()) ? ""
47 : " AND molecule_sequence:['' TO *]");
49 String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
52 String responseSize = (pdbRestRequest.getResponseSize() == 0) ? "200"
53 : String.valueOf(pdbRestRequest.getResponseSize());
54 String sortParam = (pdbRestRequest.getFieldToSortBy() == null || pdbRestRequest
55 .getFieldToSortBy().trim().isEmpty()) ? ""
57 .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
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);
68 String responseString = clientResponse.getEntity(String.class);
69 if (clientResponse.getStatus() != 200)
71 if (clientResponse.getStatus() == 400)
73 throw new RuntimeException(parseJsonExceptionString(responseString));
77 throw new RuntimeException("Failed : HTTP error code : "
78 + clientResponse.getStatus());
81 clientResponse = null;
83 return parsePDBJsonResponse(responseString, pdbRestRequest);
87 * Process error response from PDB server if/when one occurs.
90 * the json string containing error message from the server
91 * @return the processed error message from the json string
93 public static String parseJsonExceptionString(String jsonErrorResponse)
95 String errorMessage = "RunTime error";
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();
103 JSONObject responseHeader = (JSONObject) jsonObj
104 .get("responseHeader");
105 errorMessage += responseHeader.get("params").toString();
106 } catch (ParseException e)
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
118 * @param pdbJsonResponseString
119 * the json string to be parsed
120 * @param pdbRestRequest
121 * the request object which contains parameters used to process the
125 @SuppressWarnings("unchecked")
126 public static PDBRestResponse parsePDBJsonResponse(
127 String pdbJsonResponseString,
128 PDBRestRequest pdbRestRequest)
130 PDBRestResponse searchResult = new PDBRestResponse();
131 List<PDBResponseSummary> result = null;
134 JSONParser jsonParser = new JSONParser();
135 JSONObject jsonObj = (JSONObject) jsonParser
136 .parse(pdbJsonResponseString);
138 JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
139 String queryTime = ((JSONObject) jsonObj.get("responseHeader")).get(
141 int numFound = Integer
142 .valueOf(pdbResponse.get("numFound").toString());
145 result = new ArrayList<PDBResponseSummary>();
146 JSONArray docs = (JSONArray) pdbResponse.get("docs");
147 for (Iterator<JSONObject> docIter = docs.iterator(); docIter
150 JSONObject doc = docIter.next();
151 // if (doc.get("molecule_sequence") != null)
153 result.add(searchResult.new PDBResponseSummary(doc, pdbRestRequest));
156 searchResult.setNumberOfItemsFound(numFound);
157 searchResult.setResponseTime(queryTime);
158 searchResult.setSearchSummary(result);
160 } catch (ParseException e)
169 * Takes a collection of PDBDocField and converts it into a comma delimited
172 * @param pdbDocfields
175 public static String getPDBDocFieldsAsCommaDelimitedString(
176 Collection<PDBDocField> pdbDocfields)
179 if (pdbDocfields != null && !pdbDocfields.isEmpty())
181 StringBuilder returnedFields = new StringBuilder();
182 for (PDBDocField field : pdbDocfields)
184 returnedFields.append(",").append(field.getCode());
186 returnedFields.deleteCharAt(0);
187 result = returnedFields.toString();
193 * Determines the column index for the pdb id in the summary table. The pdb id
194 * serves as a unique identifier for a given row in the summary table
196 * @param wantedFeilds
197 * the available table columns in no particular order
198 * @return the pdb id field column index
200 public static int getPDBIdColumIndex(
201 Collection<PDBDocField> wantedFeilds, boolean hasRefSeq)
203 int pdbFeildIndex = hasRefSeq ? 1 : 0;
204 for (PDBDocField feild : wantedFeilds)
206 if (feild.equals(PDBDocField.PDB_ID))
212 return pdbFeildIndex;
216 * Represents the fields retrievable from a PDB Document response
219 public enum PDBDocField
221 PDB_ID("PDB Id", "pdb_id"), TITLE("Title", "title"), MOLECULE_NAME(
222 "Molecule", "molecule_name"), MOLECULE_TYPE("Molecule Type",
223 "molecule_type"), MOLECULE_SEQUENCE("Sequence",
224 "molecule_sequence"), PFAM_ACCESSION("PFAM Accession",
225 "pfam_accession"), PFAM_NAME("PFAM Name", "pfam_name"), INTERPRO_NAME(
226 "InterPro Name", "interpro_name"), INTERPRO_ACCESSION(
227 "InterPro Accession", "interpro_accession"), UNIPROT_ID(
228 "UniProt Id", "uniprot_id"), UNIPROT_ACCESSION(
229 "UniProt Accession", "uniprot_accession"), UNIPROT_COVERAGE(
230 "UniProt Coverage", "uniprot_coverage"), UNIPROT_FEATURES(
231 "Uniprot Features", "uniprot_features"), R_FACTOR("R Factor",
232 "r_factor"), RESOLUTION("Resolution", "resolution"), DATA_QUALITY(
233 "Data Quality", "data_quality"), OVERALL_QUALITY(
234 "Overall Quality", "overall_quality"), POLYMER_COUNT(
235 "Number of Polymers", "number_of_polymers"), PROTEIN_CHAIN_COUNT(
236 "Number of Protein Chains", "number_of_protein_chains"), BOUND_MOLECULE_COUNT(
237 "Number of Bound Molecule", "number_of_bound_molecules"), POLYMER_RESIDUE_COUNT(
238 "Number of Polymer Residue", "number_of_polymer_residues"), GENUS(
239 "GENUS", "genus"), GENE_NAME("Gene Name", "gene_name"), EXPERIMENTAL_METHOD(
240 "Experimental Method", "experimental_method"), GO_ID("GO Id",
241 "go_id"), ASSEMBLY_ID("Assembly Id", "assembly_form"), ASSEMBLY_FORM(
242 "Assembly Form", "assembly_id"), ASSEMBLY_TYPE("Assembly Type",
243 "assembly_type"), SPACE_GROUP("Space Group", "spacegroup"), CATH_CODE(
244 "Cath Code", "cath_code"), TAX_ID("Tax Id", "tax_id"), TAX_QUERY(
245 "Tax Query", "tax_query"), INTERACTING_ENTRY_ID(
246 "Interacting Entry Id", "interacting_entry_id"), INTERACTING_ENTITY_ID(
247 "Interacting Entity Id", "interacting_entity_id"), INTERACTING_MOLECULES(
248 "Interacting Molecules", "interacting_molecules"), PUBMED_ID(
249 "Pubmed Id", "pubmed_id"), STATUS("Status", "status"), MODEL_QUALITY(
250 "Model Quality", "model_quality"), PIVOT_RESOLUTION(
251 "Pivot Resolution", "pivot_resolution"), DATA_REDUCTION_SOFTWARE(
252 "Data reduction software", "data_reduction_software"), MAX_OBSERVED_RES(
253 "Max observed residues", "max_observed_residues"), ORG_SCI_NAME(
254 "Organism scientific name", "organism_scientific_name"), SUPER_KINGDOM(
255 "Super kingdom", "superkingdom"), RANK("Rank", "rank"), CRYSTALLISATION_PH(
256 "Crystallisation Ph", "crystallisation_ph"), BIO_FUNCTION(
257 "Biological Function", "biological_function"), BIO_PROCESS(
258 "Biological Process", "biological_process"), BIO_CELL_COMP(
259 "Biological Cell Component", "biological_cell_component"), COMPOUND_NAME(
260 "Compound Name", "compound_name"), COMPOUND_ID("Compound Id",
261 "compound_id"), COMPOUND_WEIGHT("Compound Weight",
262 "compound_weight"), COMP_SYS_NAME("Compound Systematic Name",
263 "compound_systematic_name"), INTERACTING_LIG(
264 "Interacting Ligands", "interacting_ligands"), JOURNAL(
265 "Journal", "journal"), ALL_AUTHORS("All Authors", "all_authors"), EXPERIMENTAL_DATA_AVAILABLE(
266 "Experiment Data Available", "experiment_data_available"), DIFFRACTION_PROTOCOL(
267 "Diffraction Protocol", "diffraction_protocol"), REFINEMENT_SOFTWARE(
268 "Refinement Software", "refinement_software"), STRUCTURE_DETERMINATION_METHOD(
269 "Structure Determination Method",
270 "structure_determination_method"), SYNCHROTON_SITE(
271 "Synchrotron Site", "synchrotron_site"), SAMPLE_PREP_METHOD(
272 "Sample Preparation Method", "sample_preparation_method"), ENTRY_AUTHORS(
273 "Entry Authors", "entry_authors"), CITATION_TITLE(
274 "Citation Title", "citation_title"), STRUCTURE_SOLUTION_SOFTWARE(
275 "Structure Solution Software", "structure_solution_software"), ENTRY_ENTITY(
276 "Entry Entity", "entry_entity"), R_FREE("R Free", "r_free"), NO_OF_POLYMER_ENTITIES(
277 "Number of Polymer Entities", "number_of_polymer_entities"), NO_OF_BOUND_ENTITIES(
278 "Number of Bound Entities", "number_of_bound_entities"), CRYSTALLISATION_RESERVOIR(
279 "Crystallisation Reservoir", "crystallisation_reservoir"), DATA_SCALING_SW(
280 "Data Scalling Software", "data_scaling_software"), DETECTOR(
281 "Detector", "detector"), DETECTOR_TYPE("Detector Type",
282 "detector_type"), MODIFIED_RESIDUE_FLAG(
283 "Modified Residue Flag", "modified_residue_flag"), NUMBER_OF_COPIES(
284 "Number of Copies", "number_of_copies"), STRUCT_ASYM_ID(
285 "Struc Asym Id", "struct_asym_id"), HOMOLOGUS_PDB_ENTITY_ID(
286 "Homologus PDB Entity Id", "homologus_pdb_entity_id"), MOLECULE_SYNONYM(
287 "Molecule Synonym", "molecule_synonym"), DEPOSITION_SITE(
288 "Deposition Site", "deposition_site"), SYNCHROTRON_BEAMLINE(
289 "Synchrotron Beamline", "synchrotron_beamline"), ENTITY_ID(
290 "Entity Id", "entity_id"), BEAM_SOURCE_NAME("Beam Source Name",
291 "beam_source_name"), PROCESSING_SITE("Processing Site",
292 "processing_site"), ENTITY_WEIGHT("Entity Weight",
293 "entity_weight"), VERSION("Version", "_version_"), ALL("ALL",
300 PDBDocField(String name, String code)
306 public String getName()
311 public String getCode()
316 public String toString()