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.bin.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.datamodel.SequenceI;
26 import jalview.fts.api.FTSData;
27 import jalview.fts.api.FTSDataColumnI;
28 import jalview.fts.api.FTSRestClientI;
29 import jalview.fts.core.FTSRestClient;
30 import jalview.fts.core.FTSRestRequest;
31 import jalview.fts.core.FTSRestResponse;
32 import jalview.util.JSONUtils;
33 import jalview.util.MessageManager;
34 import jalview.util.Platform;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Iterator;
40 import java.util.List;
42 import java.util.Objects;
44 import javax.ws.rs.core.MediaType;
46 import org.json.simple.parser.ParseException;
48 import com.sun.jersey.api.client.Client;
49 import com.sun.jersey.api.client.ClientResponse;
50 import com.sun.jersey.api.client.WebResource;
51 import com.sun.jersey.api.client.config.DefaultClientConfig;
54 * A rest client for querying the Search endpoint of the PDB API
58 public class PDBFTSRestClient extends FTSRestClient implements ApplicationSingletonI
60 public static final String PDB_SEARCH_ENDPOINT = "https://www.ebi.ac.uk/pdbe/search/pdb/select?";
62 public static FTSRestClientI getInstance()
64 return (FTSRestClientI) ApplicationSingletonProvider
65 .getInstance(PDBFTSRestClient.class);
68 private PDBFTSRestClient()
70 // singleton -- use getInstance()
74 * Takes a PDBRestRequest object and returns a response upon execution
76 * @param pdbRestRequest
77 * the PDBRestRequest instance to be processed
78 * @return the pdbResponse object for the given request
81 @SuppressWarnings({ "unused", "unchecked" })
83 public FTSRestResponse executeRequest(FTSRestRequest pdbRestRequest)
88 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
89 pdbRestRequest.getWantedFields());
90 int responseSize = (pdbRestRequest.getResponseSize() == 0)
91 ? getDefaultResponsePageSize()
92 : pdbRestRequest.getResponseSize();
93 int offSet = pdbRestRequest.getOffSet();
94 String sortParam = null;
95 if (pdbRestRequest.getFieldToSortBy() == null
96 || pdbRestRequest.getFieldToSortBy().trim().isEmpty())
102 if (pdbRestRequest.getFieldToSortBy()
103 .equalsIgnoreCase("Resolution"))
105 sortParam = pdbRestRequest.getFieldToSortBy()
106 + (pdbRestRequest.isAscending() ? " asc" : " desc");
110 sortParam = pdbRestRequest.getFieldToSortBy()
111 + (pdbRestRequest.isAscending() ? " desc" : " asc");
115 String facetPivot = (pdbRestRequest.getFacetPivot() == null
116 || pdbRestRequest.getFacetPivot().isEmpty()) ? ""
117 : pdbRestRequest.getFacetPivot();
118 String facetPivotMinCount = String
119 .valueOf(pdbRestRequest.getFacetPivotMinCount());
121 String query = pdbRestRequest.getFieldToSearchBy()
122 + pdbRestRequest.getSearchTerm()
123 + (pdbRestRequest.isAllowEmptySeq() ? ""
124 : " AND molecule_sequence:['' TO *]")
125 + (pdbRestRequest.isAllowUnpublishedEntries() ? ""
126 : " AND status:REL");
128 // Build request parameters for the REST Request
130 // BH 2018 the trick here is to coerce the classes in Javascript to be
131 // different from the ones in Java yet still allow this to be correct for Java
133 Class<ClientResponse> clientResponseClass;
136 // JavaScript only -- coerce types to Java types for Java
137 client = (Client) (Object) new jalview.javascript.web.Client();
138 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
147 client = Client.create(new DefaultClientConfig());
148 clientResponseClass = ClientResponse.class;
151 WebResource webResource;
152 if (pdbRestRequest.isFacet())
154 webResource = client.resource(PDB_SEARCH_ENDPOINT)
155 .queryParam("wt", "json").queryParam("fl", wantedFields)
156 .queryParam("rows", String.valueOf(responseSize))
157 .queryParam("q", query)
158 .queryParam("start", String.valueOf(offSet))
159 .queryParam("sort", sortParam).queryParam("facet", "true")
160 .queryParam("facet.pivot", facetPivot)
161 .queryParam("facet.pivot.mincount", facetPivotMinCount);
165 webResource = client.resource(PDB_SEARCH_ENDPOINT)
166 .queryParam("wt", "json").queryParam("fl", wantedFields)
167 .queryParam("rows", String.valueOf(responseSize))
168 .queryParam("start", String.valueOf(offSet))
169 .queryParam("q", query).queryParam("sort", sortParam);
172 URI uri = webResource.getURI();
174 System.out.println(uri);
176 // Execute the REST request
177 ClientResponse clientResponse = webResource
178 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass );
180 int status = clientResponse.getStatus();
182 // Get the JSON string from the response object or directly from the
183 // client (JavaScript)
184 Map<String, Object> jsonObj = (Platform.isJS() && status == 200
185 ? clientResponse.getEntity(Map.class)
187 String responseString = (jsonObj == null
188 ? clientResponse.getEntity(String.class)
191 // System.out.println("query >>>>>>> " + pdbRestRequest.toString());
193 // Check the response status and report exception if one occurs
199 throw new Exception(parseJsonExceptionString(responseString));
201 throw new Exception(getMessageByHTTPStatusCode(status, "PDB"));
204 // Make redundant objects eligible for garbage collection to conserve
206 clientResponse = null;
209 // Process the response and return the result to the caller.
210 return parsePDBJsonResponse(responseString, jsonObj, pdbRestRequest);
211 } catch (Exception e)
213 String exceptionMsg = e.getMessage();
214 if (exceptionMsg.contains("SocketException"))
216 // No internet connection
217 throw new Exception(MessageManager.getString(
218 "exception.unable_to_detect_internet_connection"));
220 else if (exceptionMsg.contains("UnknownHostException"))
222 // The server 'www.ebi.ac.uk' is unreachable
223 throw new Exception(MessageManager.formatMessage(
224 "exception.fts_server_unreachable", "PDB Solr"));
234 * Process error response from PDB server if/when one occurs.
236 * @param jsonResponse
237 * the JSON string containing error message from the server
238 * @return the processed error message from the JSON string
240 @SuppressWarnings("unchecked")
241 public static String parseJsonExceptionString(String jsonErrorResponse)
243 StringBuilder errorMessage = new StringBuilder(
244 "\n============= PDB Rest Client RunTime error =============\n");
248 // "responseHeader":{
252 // "q":"(text:q93xj9_soltu) AND molecule_sequence:['' TO *] AND status:REL",
253 // "fl":"pdb_id,title,experimental_method,resolution",
255 // "sort":"overall_quality desc",
258 // "response":{"numFound":1,"start":0,"docs":[
260 // "experimental_method":["X-ray diffraction"],
262 // "resolution":2.46,
263 // "title":"The crystal structure of Potato ferredoxin I with 2Fe-2S cluster"}]
268 Map<String, Object> jsonObj = (Map<String, Object>) JSONUtils.parse(jsonErrorResponse);
269 Map<String, Object> errorResponse = (Map<String, Object>) jsonObj.get("error");
271 Map<String, Object> responseHeader = (Map<String, Object>) jsonObj
272 .get("responseHeader");
273 Map<String, Object> paramsObj = (Map<String, Object>) responseHeader.get("params");
274 String status = responseHeader.get("status").toString();
275 String message = errorResponse.get("msg").toString();
276 String query = paramsObj.get("q").toString();
277 String fl = paramsObj.get("fl").toString();
279 errorMessage.append("Status: ").append(status).append("\n");
280 errorMessage.append("Message: ").append(message).append("\n");
281 errorMessage.append("query: ").append(query).append("\n");
282 errorMessage.append("fl: ").append(fl).append("\n");
284 } catch (ParseException e)
288 return errorMessage.toString();
292 * Parses the JSON response string from PDB REST API. The response is dynamic
293 * hence, only fields specifically requested for in the 'wantedFields'
294 * parameter is fetched/processed
296 * @param pdbJsonResponseString
297 * the JSON string to be parsed
298 * @param pdbRestRequest
299 * the request object which contains parameters used to process the
303 public static FTSRestResponse parsePDBJsonResponse(
304 String pdbJsonResponseString, FTSRestRequest pdbRestRequest)
306 return parsePDBJsonResponse(pdbJsonResponseString,
307 (Map<String, Object>) null, pdbRestRequest);
310 @SuppressWarnings("unchecked")
311 public static FTSRestResponse parsePDBJsonResponse(
312 String pdbJsonResponseString, Map<String, Object> jsonObj,
313 FTSRestRequest pdbRestRequest)
315 FTSRestResponse searchResult = new FTSRestResponse();
316 List<FTSData> result = null;
321 jsonObj = (Map<String, Object>) JSONUtils.parse(pdbJsonResponseString);
323 Map<String, Object> pdbResponse = (Map<String, Object>) jsonObj.get("response");
324 String queryTime = ((Map<String, Object>) jsonObj.get("responseHeader"))
325 .get("QTime").toString();
326 int numFound = Integer
327 .valueOf(pdbResponse.get("numFound").toString());
330 result = new ArrayList<>();
331 List<Object> docs = (List<Object>) pdbResponse.get("docs");
332 for (Iterator<Object> docIter = docs.iterator(); docIter
335 Map<String, Object> doc = (Map<String, Object>) docIter.next();
336 result.add(getFTSData(doc, pdbRestRequest));
338 searchResult.setNumberOfItemsFound(numFound);
339 searchResult.setResponseTime(queryTime);
340 searchResult.setSearchSummary(result);
342 } catch (ParseException e)
349 public static FTSData getFTSData(Map<String, Object> pdbJsonDoc,
350 FTSRestRequest request)
353 String primaryKey = null;
355 Object[] summaryRowData;
357 SequenceI associatedSequence;
359 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
360 SequenceI associatedSeq = request.getAssociatedSequence();
362 summaryRowData = new Object[(associatedSeq != null)
363 ? diplayFields.size() + 1
364 : diplayFields.size()];
365 if (associatedSeq != null)
367 associatedSequence = associatedSeq;
368 summaryRowData[0] = associatedSequence;
372 for (FTSDataColumnI field : diplayFields)
374 String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
375 : pdbJsonDoc.get(field.getCode()).toString();
376 if (field.isPrimaryKeyColumn())
378 primaryKey = fieldData;
379 summaryRowData[colCounter++] = primaryKey;
381 else if (fieldData == null || fieldData.isEmpty())
383 summaryRowData[colCounter++] = null;
389 summaryRowData[colCounter++] = (field.getDataType()
390 .getDataTypeClass() == Integer.class)
391 ? Integer.valueOf(fieldData)
392 : (field.getDataType()
393 .getDataTypeClass() == Double.class)
394 ? Double.valueOf(fieldData)
395 : sanitiseData(fieldData);
396 } catch (Exception e)
399 System.out.println("offending value:" + fieldData);
404 final String primaryKey1 = primaryKey;
406 final Object[] summaryRowData1 = summaryRowData;
410 public Object[] getSummaryData()
412 return summaryRowData1;
416 public Object getPrimaryKey()
422 * Returns a string representation of this object;
425 public String toString()
427 StringBuilder summaryFieldValues = new StringBuilder();
428 for (Object summaryField : summaryRowData1)
430 summaryFieldValues.append(
431 summaryField == null ? " " : summaryField.toString())
434 return summaryFieldValues.toString();
438 * Returns hash code value for this object
441 public int hashCode()
443 return Objects.hash(primaryKey1, this.toString());
447 public boolean equals(Object that)
449 return this.toString().equals(that.toString());
454 private static String sanitiseData(String data)
456 String cleanData = data.replaceAll("\\[\"", "").replaceAll("\\]\"", "")
457 .replaceAll("\\[", "").replaceAll("\\]", "")
458 .replaceAll("\",\"", ", ").replaceAll("\"", "");
463 public String getColumnDataConfigFileName()
465 return "/fts/pdb_data_columns.txt";
468 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
470 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
472 if (allDefaultDisplayedStructureDataColumns == null
473 || allDefaultDisplayedStructureDataColumns.isEmpty())
475 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
476 allDefaultDisplayedStructureDataColumns
477 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
479 return allDefaultDisplayedStructureDataColumns;