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 result.add(searchResult.new PDBResponseSummary(doc, pdbRestRequest));
153 searchResult.setNumberOfItemsFound(numFound);
154 searchResult.setResponseTime(queryTime);
155 searchResult.setSearchSummary(result);
157 } catch (ParseException e)
165 * Takes a collection of PDBDocField and converts its code values into a comma
168 * @param pdbDocfields
171 public static String getPDBDocFieldsAsCommaDelimitedString(
172 Collection<PDBDocField> pdbDocfields)
175 if (pdbDocfields != null && !pdbDocfields.isEmpty())
177 StringBuilder returnedFields = new StringBuilder();
178 for (PDBDocField field : pdbDocfields)
180 returnedFields.append(",").append(field.getCode());
182 returnedFields.deleteCharAt(0);
183 result = returnedFields.toString();
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
193 * @param wantedFeilds
194 * the available table columns in no particular order
195 * @return the pdb id field column index
197 public static int getPDBIdColumIndex(
198 Collection<PDBDocField> wantedFeilds, boolean hasRefSeq)
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)
205 if (feild.equals(PDBDocField.PDB_ID))
207 break; // once PDB Id index is determined exit iteration
209 ++pdbFeildIndexCounter;
211 return pdbFeildIndexCounter;
215 * Represents the fields retrievable from a PDB Document response
218 public enum PDBDocField
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",
299 PDBDocField(String name, String code)
305 public String getName()
310 public String getCode()
315 public String toString()