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;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Objects;
38 import javax.ws.rs.core.MediaType;
40 import org.json.simple.JSONArray;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
43 import org.json.simple.parser.ParseException;
45 import com.sun.jersey.api.client.Client;
46 import com.sun.jersey.api.client.ClientResponse;
47 import com.sun.jersey.api.client.WebResource;
48 import com.sun.jersey.api.client.config.ClientConfig;
49 import com.sun.jersey.api.client.config.DefaultClientConfig;
52 * A rest client for querying the Search endpoint of the PDB API
57 public class PDBFTSRestClient extends FTSRestClient
60 private static FTSRestClientI instance = null;
62 public static final String PDB_SEARCH_ENDPOINT = "https://www.ebi.ac.uk/pdbe/search/pdb/select?";
64 protected PDBFTSRestClient()
69 * Takes a PDBRestRequest object and returns a response upon execution
71 * @param pdbRestRequest
72 * the PDBRestRequest instance to be processed
73 * @return the pdbResponse object for the given request
77 public FTSRestResponse executeRequest(FTSRestRequest pdbRestRequest)
82 ClientConfig clientConfig = new DefaultClientConfig();
83 Client client = Client.create(clientConfig);
85 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
86 pdbRestRequest.getWantedFields());
87 int responseSize = (pdbRestRequest.getResponseSize() == 0)
88 ? getDefaultResponsePageSize()
89 : pdbRestRequest.getResponseSize();
90 int offSet = pdbRestRequest.getOffSet();
91 String sortParam = null;
92 if (pdbRestRequest.getFieldToSortBy() == null
93 || pdbRestRequest.getFieldToSortBy().trim().isEmpty())
99 if (pdbRestRequest.getFieldToSortBy()
100 .equalsIgnoreCase("Resolution"))
102 sortParam = pdbRestRequest.getFieldToSortBy()
103 + (pdbRestRequest.isAscending() ? " asc" : " desc");
107 sortParam = pdbRestRequest.getFieldToSortBy()
108 + (pdbRestRequest.isAscending() ? " desc" : " asc");
112 String facetPivot = (pdbRestRequest.getFacetPivot() == null
113 || pdbRestRequest.getFacetPivot().isEmpty()) ? ""
114 : pdbRestRequest.getFacetPivot();
115 String facetPivotMinCount = String
116 .valueOf(pdbRestRequest.getFacetPivotMinCount());
118 String query = pdbRestRequest.getFieldToSearchBy()
119 + pdbRestRequest.getSearchTerm()
120 + (pdbRestRequest.isAllowEmptySeq() ? ""
121 : " AND molecule_sequence:['' TO *]")
122 + (pdbRestRequest.isAllowUnpublishedEntries() ? ""
123 : " AND status:REL");
125 // Build request parameters for the REST Request
126 WebResource webResource = null;
127 if (pdbRestRequest.isFacet())
129 webResource = client.resource(PDB_SEARCH_ENDPOINT)
130 .queryParam("wt", "json").queryParam("fl", wantedFields)
131 .queryParam("rows", String.valueOf(responseSize))
132 .queryParam("q", query)
133 .queryParam("start", String.valueOf(offSet))
134 .queryParam("sort", sortParam).queryParam("facet", "true")
135 .queryParam("facet.pivot", facetPivot)
136 .queryParam("facet.pivot.mincount", facetPivotMinCount);
140 webResource = client.resource(PDB_SEARCH_ENDPOINT)
141 .queryParam("wt", "json").queryParam("fl", wantedFields)
142 .queryParam("rows", String.valueOf(responseSize))
143 .queryParam("start", String.valueOf(offSet))
144 .queryParam("q", query).queryParam("sort", sortParam);
146 // Execute the REST request
147 ClientResponse clientResponse = webResource
148 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
150 // Get the JSON string from the response object
151 String responseString = clientResponse.getEntity(String.class);
152 // System.out.println("query >>>>>>> " + pdbRestRequest.toString());
154 // Check the response status and report exception if one occurs
155 int responseStatus = clientResponse.getStatus();
156 if (responseStatus != 200)
158 String errorMessage = "";
159 if (responseStatus == 400)
161 errorMessage = parseJsonExceptionString(responseString);
162 throw new Exception(errorMessage);
166 errorMessage = getMessageByHTTPStatusCode(
167 responseStatus, "PDB");
168 throw new Exception(errorMessage);
172 // Make redundant objects eligible for garbage collection to conserve
174 clientResponse = null;
177 // Process the response and return the result to the caller.
178 return parsePDBJsonResponse(responseString, pdbRestRequest);
179 } catch (Exception e)
181 String exceptionMsg = e.getMessage();
182 if (exceptionMsg.contains("SocketException"))
184 // No internet connection
185 throw new Exception(MessageManager.getString(
186 "exception.unable_to_detect_internet_connection"));
188 else if (exceptionMsg.contains("UnknownHostException"))
190 // The server 'www.ebi.ac.uk' is unreachable
191 throw new Exception(MessageManager.formatMessage(
192 "exception.fts_server_unreachable", "PDB Solr"));
202 * Process error response from PDB server if/when one occurs.
204 * @param jsonResponse
205 * the JSON string containing error message from the server
206 * @return the processed error message from the JSON string
208 public static String parseJsonExceptionString(String jsonErrorResponse)
210 StringBuilder errorMessage = new StringBuilder(
211 "\n============= PDB Rest Client RunTime error =============\n");
215 JSONParser jsonParser = new JSONParser();
216 JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonErrorResponse);
217 JSONObject errorResponse = (JSONObject) jsonObj.get("error");
219 JSONObject responseHeader = (JSONObject) jsonObj
220 .get("responseHeader");
221 JSONObject paramsObj = (JSONObject) responseHeader.get("params");
222 String status = responseHeader.get("status").toString();
223 String message = errorResponse.get("msg").toString();
224 String query = paramsObj.get("q").toString();
225 String fl = paramsObj.get("fl").toString();
227 errorMessage.append("Status: ").append(status).append("\n");
228 errorMessage.append("Message: ").append(message).append("\n");
229 errorMessage.append("query: ").append(query).append("\n");
230 errorMessage.append("fl: ").append(fl).append("\n");
232 } catch (ParseException e)
236 return errorMessage.toString();
240 * Parses the JSON response string from PDB REST API. The response is dynamic
241 * hence, only fields specifically requested for in the 'wantedFields'
242 * parameter is fetched/processed
244 * @param pdbJsonResponseString
245 * the JSON string to be parsed
246 * @param pdbRestRequest
247 * the request object which contains parameters used to process the
251 @SuppressWarnings("unchecked")
252 public static FTSRestResponse parsePDBJsonResponse(
253 String pdbJsonResponseString, FTSRestRequest pdbRestRequest)
255 FTSRestResponse searchResult = new FTSRestResponse();
256 List<FTSData> result = null;
259 JSONParser jsonParser = new JSONParser();
260 JSONObject jsonObj = (JSONObject) jsonParser
261 .parse(pdbJsonResponseString);
263 JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
264 String queryTime = ((JSONObject) jsonObj.get("responseHeader"))
265 .get("QTime").toString();
266 int numFound = Integer
267 .valueOf(pdbResponse.get("numFound").toString());
270 result = new ArrayList<FTSData>();
271 JSONArray docs = (JSONArray) pdbResponse.get("docs");
272 for (Iterator<JSONObject> docIter = docs.iterator(); docIter
275 JSONObject doc = docIter.next();
276 result.add(getFTSData(doc, pdbRestRequest));
278 searchResult.setNumberOfItemsFound(numFound);
279 searchResult.setResponseTime(queryTime);
280 searchResult.setSearchSummary(result);
282 } catch (ParseException e)
289 public static FTSData getFTSData(JSONObject pdbJsonDoc,
290 FTSRestRequest request)
293 String primaryKey = null;
295 Object[] summaryRowData;
297 SequenceI associatedSequence;
299 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
300 SequenceI associatedSeq = request.getAssociatedSequence();
302 summaryRowData = new Object[(associatedSeq != null)
303 ? diplayFields.size() + 1
304 : diplayFields.size()];
305 if (associatedSeq != null)
307 associatedSequence = associatedSeq;
308 summaryRowData[0] = associatedSequence;
312 for (FTSDataColumnI field : diplayFields)
314 String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
315 : pdbJsonDoc.get(field.getCode()).toString();
316 if (field.isPrimaryKeyColumn())
318 primaryKey = fieldData;
319 summaryRowData[colCounter++] = primaryKey;
321 else if (fieldData == null || fieldData.isEmpty())
323 summaryRowData[colCounter++] = null;
329 summaryRowData[colCounter++] = (field.getDataType()
330 .getDataTypeClass() == Integer.class)
331 ? Integer.valueOf(fieldData)
332 : (field.getDataType()
333 .getDataTypeClass() == Double.class)
334 ? Double.valueOf(fieldData)
335 : sanitiseData(fieldData);
336 } catch (Exception e)
339 System.out.println("offending value:" + fieldData);
344 final String primaryKey1 = primaryKey;
346 final Object[] summaryRowData1 = summaryRowData;
350 public Object[] getSummaryData()
352 return summaryRowData1;
356 public Object getPrimaryKey()
362 * Returns a string representation of this object;
365 public String toString()
367 StringBuilder summaryFieldValues = new StringBuilder();
368 for (Object summaryField : summaryRowData1)
370 summaryFieldValues.append(
371 summaryField == null ? " " : summaryField.toString())
374 return summaryFieldValues.toString();
378 * Returns hash code value for this object
381 public int hashCode()
383 return Objects.hash(primaryKey1, this.toString());
387 public boolean equals(Object that)
389 return this.toString().equals(that.toString());
394 private static String sanitiseData(String data)
396 String cleanData = data.replaceAll("\\[\"", "").replaceAll("\\]\"", "")
397 .replaceAll("\\[", "").replaceAll("\\]", "")
398 .replaceAll("\",\"", ", ").replaceAll("\"", "");
403 public String getColumnDataConfigFileName()
405 return "/fts/pdb_data_columns.txt";
408 public static FTSRestClientI getInstance()
410 if (instance == null)
412 instance = new PDBFTSRestClient();
417 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
419 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
421 if (allDefaultDisplayedStructureDataColumns == null
422 || allDefaultDisplayedStructureDataColumns.isEmpty())
424 allDefaultDisplayedStructureDataColumns = new ArrayList<FTSDataColumnI>();
425 allDefaultDisplayedStructureDataColumns
426 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
428 return allDefaultDisplayedStructureDataColumns;