2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.fts.service.pdb;
23 import jalview.datamodel.SequenceI;
24 import jalview.fts.api.FTSData;
25 import jalview.fts.api.FTSDataColumnI;
26 import jalview.fts.api.FTSRestClientI;
27 import jalview.fts.core.FTSRestClient;
28 import jalview.fts.core.FTSRestRequest;
29 import jalview.fts.core.FTSRestResponse;
30 import jalview.util.MessageManager;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Objects;
39 import javax.ws.rs.core.MediaType;
41 import org.json.simple.JSONArray;
42 import org.json.simple.JSONObject;
43 import org.json.simple.parser.JSONParser;
44 import org.json.simple.parser.ParseException;
46 //import jalview.javascript.web.Client;
47 //import jalview.javascript.web.ClientResponse;
48 //import jalview.javascript.web.WebResource;
50 import com.sun.jersey.api.client.Client;
51 import com.sun.jersey.api.client.ClientResponse;
52 import com.sun.jersey.api.client.WebResource;
54 import com.sun.jersey.api.client.config.ClientConfig;
55 import com.sun.jersey.api.client.config.DefaultClientConfig;
58 * A rest client for querying the Search endpoint of the PDB API
60 * BH 2018: just a tiny tweak here - for SwingJS, we coerce the classes to be
61 * from jalview.javascript.web instead of com.sun.jersey.api.client
67 public class PDBFTSRestClient extends FTSRestClient
70 private static FTSRestClientI instance = null;
72 public static final String PDB_SEARCH_ENDPOINT = "https://www.ebi.ac.uk/pdbe/search/pdb/select?";
74 protected PDBFTSRestClient()
79 * Takes a PDBRestRequest object and returns a response upon execution
81 * @param pdbRestRequest
82 * the PDBRestRequest instance to be processed
83 * @return the pdbResponse object for the given request
86 @SuppressWarnings({ "unused", "unchecked" })
88 public FTSRestResponse executeRequest(FTSRestRequest pdbRestRequest)
94 WebResource webResource;
96 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
97 pdbRestRequest.getWantedFields());
98 int responseSize = (pdbRestRequest.getResponseSize() == 0)
99 ? getDefaultResponsePageSize()
100 : pdbRestRequest.getResponseSize();
101 int offSet = pdbRestRequest.getOffSet();
102 String sortParam = null;
103 if (pdbRestRequest.getFieldToSortBy() == null
104 || pdbRestRequest.getFieldToSortBy().trim().isEmpty())
110 if (pdbRestRequest.getFieldToSortBy()
111 .equalsIgnoreCase("Resolution"))
113 sortParam = pdbRestRequest.getFieldToSortBy()
114 + (pdbRestRequest.isAscending() ? " asc" : " desc");
118 sortParam = pdbRestRequest.getFieldToSortBy()
119 + (pdbRestRequest.isAscending() ? " desc" : " asc");
123 String facetPivot = (pdbRestRequest.getFacetPivot() == null
124 || pdbRestRequest.getFacetPivot().isEmpty()) ? ""
125 : pdbRestRequest.getFacetPivot();
126 String facetPivotMinCount = String
127 .valueOf(pdbRestRequest.getFacetPivotMinCount());
129 String query = pdbRestRequest.getFieldToSearchBy()
130 + pdbRestRequest.getSearchTerm()
131 + (pdbRestRequest.isAllowEmptySeq() ? ""
132 : " AND molecule_sequence:['' TO *]")
133 + (pdbRestRequest.isAllowUnpublishedEntries() ? ""
134 : " AND status:REL");
136 // Build request parameters for the REST Request
138 // BH 2018 the trick here is to coerce the classes in Javascript to be
139 // different from the ones in Java yet still allow this to be correct for Java
140 Class<ClientResponse> clientResponseClass;
141 if (/** @j2sNative true || */
144 // JavaScript only -- coerce types to Java types for Java
145 client = (Client) (Object) new jalview.javascript.web.Client();
146 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
151 client = Client.create(new DefaultClientConfig());
152 clientResponseClass = ClientResponse.class;
155 if (pdbRestRequest.isFacet())
157 webResource = client.resource(PDB_SEARCH_ENDPOINT)
158 .queryParam("wt", "json").queryParam("fl", wantedFields)
159 .queryParam("rows", String.valueOf(responseSize))
160 .queryParam("q", query)
161 .queryParam("start", String.valueOf(offSet))
162 .queryParam("sort", sortParam).queryParam("facet", "true")
163 .queryParam("facet.pivot", facetPivot)
164 .queryParam("facet.pivot.mincount", facetPivotMinCount);
168 webResource = client.resource(PDB_SEARCH_ENDPOINT)
169 .queryParam("wt", "json").queryParam("fl", wantedFields)
170 .queryParam("rows", String.valueOf(responseSize))
171 .queryParam("start", String.valueOf(offSet))
172 .queryParam("q", query).queryParam("sort", sortParam);
175 URI uri = webResource.getURI();
177 // Execute the REST request
178 ClientResponse clientResponse = webResource
179 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass );
181 // Get the JSON string from the response object
182 String responseString = clientResponse.getEntity(String.class);
183 // System.out.println("query >>>>>>> " + pdbRestRequest.toString());
185 // Check the response status and report exception if one occurs
186 if (clientResponse.getStatus() != 200)
188 String errorMessage = "";
189 if (clientResponse.getStatus() == 400)
191 errorMessage = parseJsonExceptionString(responseString);
192 throw new Exception(errorMessage);
196 errorMessage = getMessageByHTTPStatusCode(
197 clientResponse.getStatus(), "PDB");
198 throw new Exception(errorMessage);
202 // Make redundant objects eligible for garbage collection to conserve
204 clientResponse = null;
207 // Process the response and return the result to the caller.
208 return parsePDBJsonResponse(responseString, pdbRestRequest);
209 } catch (Exception e)
211 String exceptionMsg = e.getMessage();
212 if (exceptionMsg.contains("SocketException"))
214 // No internet connection
215 throw new Exception(MessageManager.getString(
216 "exception.unable_to_detect_internet_connection"));
218 else if (exceptionMsg.contains("UnknownHostException"))
220 // The server 'www.ebi.ac.uk' is unreachable
221 throw new Exception(MessageManager.formatMessage(
222 "exception.fts_server_unreachable", "PDB Solr"));
232 * Process error response from PDB server if/when one occurs.
234 * @param jsonResponse
235 * the JSON string containing error message from the server
236 * @return the processed error message from the JSON string
238 public static String parseJsonExceptionString(String jsonErrorResponse)
240 StringBuilder errorMessage = new StringBuilder(
241 "\n============= PDB Rest Client RunTime error =============\n");
245 // "responseHeader":{
249 // "q":"(text:q93xj9_soltu) AND molecule_sequence:['' TO *] AND status:REL",
250 // "fl":"pdb_id,title,experimental_method,resolution",
252 // "sort":"overall_quality desc",
255 // "response":{"numFound":1,"start":0,"docs":[
257 // "experimental_method":["X-ray diffraction"],
259 // "resolution":2.46,
260 // "title":"The crystal structure of Potato ferredoxin I with 2Fe-2S cluster"}]
265 JSONParser jsonParser = new JSONParser();
266 JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonErrorResponse);
267 JSONObject errorResponse = (JSONObject) jsonObj.get("error");
269 JSONObject responseHeader = (JSONObject) jsonObj
270 .get("responseHeader");
271 JSONObject paramsObj = (JSONObject) responseHeader.get("params");
272 String status = responseHeader.get("status").toString();
273 String message = errorResponse.get("msg").toString();
274 String query = paramsObj.get("q").toString();
275 String fl = paramsObj.get("fl").toString();
277 errorMessage.append("Status: ").append(status).append("\n");
278 errorMessage.append("Message: ").append(message).append("\n");
279 errorMessage.append("query: ").append(query).append("\n");
280 errorMessage.append("fl: ").append(fl).append("\n");
282 } catch (ParseException e)
286 return errorMessage.toString();
290 * Parses the JSON response string from PDB REST API. The response is dynamic
291 * hence, only fields specifically requested for in the 'wantedFields'
292 * parameter is fetched/processed
294 * @param pdbJsonResponseString
295 * the JSON string to be parsed
296 * @param pdbRestRequest
297 * the request object which contains parameters used to process the
301 @SuppressWarnings("unchecked")
302 public static FTSRestResponse parsePDBJsonResponse(
303 String pdbJsonResponseString, FTSRestRequest pdbRestRequest)
305 FTSRestResponse searchResult = new FTSRestResponse();
306 List<FTSData> result = null;
309 JSONParser jsonParser = new JSONParser();
310 JSONObject jsonObj = (JSONObject) jsonParser
311 .parse(pdbJsonResponseString);
313 JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
314 String queryTime = ((JSONObject) jsonObj.get("responseHeader"))
315 .get("QTime").toString();
316 int numFound = Integer
317 .valueOf(pdbResponse.get("numFound").toString());
320 result = new ArrayList<FTSData>();
321 JSONArray docs = (JSONArray) pdbResponse.get("docs");
322 for (Iterator<JSONObject> docIter = docs.iterator(); docIter
325 JSONObject doc = docIter.next();
326 result.add(getFTSData(doc, pdbRestRequest));
328 searchResult.setNumberOfItemsFound(numFound);
329 searchResult.setResponseTime(queryTime);
330 searchResult.setSearchSummary(result);
332 } catch (ParseException e)
339 public static FTSData getFTSData(JSONObject pdbJsonDoc,
340 FTSRestRequest request)
343 String primaryKey = null;
345 Object[] summaryRowData;
347 SequenceI associatedSequence;
349 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
350 SequenceI associatedSeq = request.getAssociatedSequence();
352 summaryRowData = new Object[(associatedSeq != null)
353 ? diplayFields.size() + 1
354 : diplayFields.size()];
355 if (associatedSeq != null)
357 associatedSequence = associatedSeq;
358 summaryRowData[0] = associatedSequence;
362 for (FTSDataColumnI field : diplayFields)
364 String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
365 : pdbJsonDoc.get(field.getCode()).toString();
366 if (field.isPrimaryKeyColumn())
368 primaryKey = fieldData;
369 summaryRowData[colCounter++] = primaryKey;
371 else if (fieldData == null || fieldData.isEmpty())
373 summaryRowData[colCounter++] = null;
379 summaryRowData[colCounter++] = (field.getDataType()
380 .getDataTypeClass() == Integer.class)
381 ? Integer.valueOf(fieldData)
382 : (field.getDataType()
383 .getDataTypeClass() == Double.class)
384 ? Double.valueOf(fieldData)
385 : sanitiseData(fieldData);
386 } catch (Exception e)
389 System.out.println("offending value:" + fieldData);
394 final String primaryKey1 = primaryKey;
396 final Object[] summaryRowData1 = summaryRowData;
400 public Object[] getSummaryData()
402 return summaryRowData1;
406 public Object getPrimaryKey()
412 * Returns a string representation of this object;
415 public String toString()
417 StringBuilder summaryFieldValues = new StringBuilder();
418 for (Object summaryField : summaryRowData1)
420 summaryFieldValues.append(
421 summaryField == null ? " " : summaryField.toString())
424 return summaryFieldValues.toString();
428 * Returns hash code value for this object
431 public int hashCode()
433 return Objects.hash(primaryKey1, this.toString());
437 public boolean equals(Object that)
439 return this.toString().equals(that.toString());
444 private static String sanitiseData(String data)
446 String cleanData = data.replaceAll("\\[\"", "").replaceAll("\\]\"", "")
447 .replaceAll("\\[", "").replaceAll("\\]", "")
448 .replaceAll("\",\"", ", ").replaceAll("\"", "");
453 public String getColumnDataConfigFileName()
455 return "/fts/pdb_data_columns.txt";
458 public static FTSRestClientI getInstance()
460 if (instance == null)
462 instance = new PDBFTSRestClient();
467 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
469 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
471 if (allDefaultDisplayedStructureDataColumns == null
472 || allDefaultDisplayedStructureDataColumns.isEmpty())
474 allDefaultDisplayedStructureDataColumns = new ArrayList<FTSDataColumnI>();
475 allDefaultDisplayedStructureDataColumns
476 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
478 return allDefaultDisplayedStructureDataColumns;
481 public static void main(String[] args) {
484 // check for transpiler fix associated with JSONParser yylex.java use of charAt()
485 // instead of codePointAt()
494 String result = s +x + y + z;
495 assert (result == "e103982102");
496 JSONParser jsonParser = new JSONParser();
499 JSONObject jsonObj = (JSONObject) jsonParser.parse("{\"a\":3}");
500 System.out.println(jsonObj);
501 } catch (ParseException e)