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;
24 import java.io.FileInputStream;
25 import java.io.FileReader;
27 import java.nio.CharBuffer;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.List;
33 import java.util.Objects;
35 import javax.ws.rs.core.MediaType;
37 import org.json.simple.parser.ParseException;
39 import com.sun.jersey.api.client.Client;
40 import com.sun.jersey.api.client.ClientResponse;
41 import com.sun.jersey.api.client.WebResource;
42 import com.sun.jersey.api.client.config.DefaultClientConfig;
44 import jalview.bin.ApplicationSingletonProvider;
45 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
46 import jalview.datamodel.SequenceI;
47 import jalview.fts.api.FTSData;
48 import jalview.fts.api.FTSDataColumnI;
49 import jalview.fts.api.FTSRestClientI;
50 import jalview.fts.api.StructureFTSRestClientI;
51 import jalview.fts.core.FTSDataColumnPreferences;
52 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
53 import jalview.fts.core.FTSRestClient;
54 import jalview.fts.core.FTSRestRequest;
55 import jalview.fts.core.FTSRestResponse;
56 import jalview.fts.service.alphafold.AlphafoldRestClient;
57 import jalview.util.JSONUtils;
58 import jalview.util.MessageManager;
59 import jalview.util.Platform;
62 * A rest client for querying the Search endpoint of the PDB API
66 public class PDBFTSRestClient extends FTSRestClient
67 implements StructureFTSRestClientI,ApplicationSingletonI
70 private static FTSRestClientI instance = null;
72 public static final String PDB_SEARCH_ENDPOINT = "https://www.ebi.ac.uk/pdbe/search/pdb/select?";
74 public static FTSRestClientI getInstance()
76 return (FTSRestClientI) ApplicationSingletonProvider
77 .getInstance(PDBFTSRestClient.class);
79 protected PDBFTSRestClient()
84 * Takes a PDBRestRequest object and returns a response upon execution
86 * @param pdbRestRequest
87 * the PDBRestRequest instance to be processed
88 * @return the pdbResponse object for the given request
91 @SuppressWarnings({ "unused", "unchecked" })
93 public FTSRestResponse executeRequest(FTSRestRequest pdbRestRequest)
98 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
99 pdbRestRequest.getWantedFields());
100 int responseSize = (pdbRestRequest.getResponseSize() == 0)
101 ? getDefaultResponsePageSize()
102 : pdbRestRequest.getResponseSize();
103 int offSet = pdbRestRequest.getOffSet();
104 String sortParam = null;
105 if (pdbRestRequest.getFieldToSortBy() == null
106 || pdbRestRequest.getFieldToSortBy().trim().isEmpty())
112 if (pdbRestRequest.getFieldToSortBy()
113 .equalsIgnoreCase("Resolution"))
115 sortParam = pdbRestRequest.getFieldToSortBy()
116 + (pdbRestRequest.isAscending() ? " asc" : " desc");
120 sortParam = pdbRestRequest.getFieldToSortBy()
121 + (pdbRestRequest.isAscending() ? " desc" : " asc");
125 String facetPivot = (pdbRestRequest.getFacetPivot() == null
126 || pdbRestRequest.getFacetPivot().isEmpty()) ? ""
127 : pdbRestRequest.getFacetPivot();
128 String facetPivotMinCount = String
129 .valueOf(pdbRestRequest.getFacetPivotMinCount());
131 String query = pdbRestRequest.getFieldToSearchBy()
132 + pdbRestRequest.getSearchTerm()
133 + (pdbRestRequest.isAllowEmptySeq() ? ""
134 : " AND molecule_sequence:['' TO *]")
135 + (pdbRestRequest.isAllowUnpublishedEntries() ? ""
136 : " AND status:REL");
138 // Build request parameters for the REST Request
140 // BH 2018 the trick here is to coerce the classes in Javascript to be
141 // different from the ones in Java yet still allow this to be correct for
144 Class<ClientResponse> clientResponseClass;
147 // JavaScript only -- coerce types to Java types for Java
148 client = (Client) (Object) new jalview.javascript.web.Client();
149 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
158 client = Client.create(new DefaultClientConfig());
159 clientResponseClass = ClientResponse.class;
162 WebResource webResource;
163 if (pdbRestRequest.isFacet())
165 webResource = client.resource(PDB_SEARCH_ENDPOINT)
166 .queryParam("wt", "json").queryParam("fl", wantedFields)
167 .queryParam("rows", String.valueOf(responseSize))
168 .queryParam("q", query)
169 .queryParam("start", String.valueOf(offSet))
170 .queryParam("sort", sortParam).queryParam("facet", "true")
171 .queryParam("facet.pivot", facetPivot)
172 .queryParam("facet.pivot.mincount", facetPivotMinCount);
176 webResource = client.resource(PDB_SEARCH_ENDPOINT)
177 .queryParam("wt", "json").queryParam("fl", wantedFields)
178 .queryParam("rows", String.valueOf(responseSize))
179 .queryParam("start", String.valueOf(offSet))
180 .queryParam("q", query).queryParam("sort", sortParam);
183 URI uri = webResource.getURI();
185 System.out.println(uri);
186 ClientResponse clientResponse = null;
187 int responseStatus = -1;
189 // Get the JSON string from the response object or directly from the
190 // client (JavaScript)
191 Map<String, Object> jsonObj = null;
192 String responseString = null;
194 System.out.println("query >>>>>>> " + pdbRestRequest.toString());
198 // Execute the REST request
199 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
200 .get(clientResponseClass);
201 responseStatus = clientResponse.getStatus();
206 if (mockQueries.containsKey(uri.toString()))
208 responseStatus = 200;
212 // FIXME - may cause unexpected exceptions for callers when mocked
213 responseStatus = 400;
217 // Check the response status and report exception if one occurs
218 switch (responseStatus)
223 responseString = mockQueries.get(uri.toString());
229 jsonObj = clientResponse.getEntity(Map.class);
233 responseString = clientResponse.getEntity(String.class);
238 throw new Exception(isMocked() ? "400 response (Mocked)"
239 : parseJsonExceptionString(responseString));
242 getMessageByHTTPStatusCode(responseStatus, "PDB"));
245 // Process the response and return the result to the caller.
246 return parsePDBJsonResponse(responseString, jsonObj, pdbRestRequest);
247 } catch (Exception e)
249 if (e.getMessage() == null)
253 String exceptionMsg = e.getMessage();
254 if (exceptionMsg.contains("SocketException"))
256 // No internet connection
257 throw new Exception(MessageManager.getString(
258 "exception.unable_to_detect_internet_connection"));
260 else if (exceptionMsg.contains("UnknownHostException"))
262 // The server 'www.ebi.ac.uk' is unreachable
263 throw new Exception(MessageManager.formatMessage(
264 "exception.fts_server_unreachable", "PDB Solr"));
274 * Process error response from PDB server if/when one occurs.
276 * @param jsonResponse
277 * the JSON string containing error message from the server
278 * @return the processed error message from the JSON string
280 @SuppressWarnings("unchecked")
281 public static String parseJsonExceptionString(String jsonErrorResponse)
283 StringBuilder errorMessage = new StringBuilder(
284 "\n============= PDB Rest Client RunTime error =============\n");
287 // "responseHeader":{
291 // "q":"(text:q93xj9_soltu) AND molecule_sequence:['' TO *] AND status:REL",
292 // "fl":"pdb_id,title,experimental_method,resolution",
294 // "sort":"overall_quality desc",
297 // "response":{"numFound":1,"start":0,"docs":[
299 // "experimental_method":["X-ray diffraction"],
301 // "resolution":2.46,
302 // "title":"The crystal structure of Potato ferredoxin I with 2Fe-2S
308 Map<String, Object> jsonObj = (Map<String, Object>) JSONUtils
309 .parse(jsonErrorResponse);
310 Map<String, Object> errorResponse = (Map<String, Object>) jsonObj
313 Map<String, Object> responseHeader = (Map<String, Object>) jsonObj
314 .get("responseHeader");
315 Map<String, Object> paramsObj = (Map<String, Object>) responseHeader
317 String status = responseHeader.get("status").toString();
318 String message = errorResponse.get("msg").toString();
319 String query = paramsObj.get("q").toString();
320 String fl = paramsObj.get("fl").toString();
322 errorMessage.append("Status: ").append(status).append("\n");
323 errorMessage.append("Message: ").append(message).append("\n");
324 errorMessage.append("query: ").append(query).append("\n");
325 errorMessage.append("fl: ").append(fl).append("\n");
327 } catch (ParseException e)
331 return errorMessage.toString();
335 * Parses the JSON response string from PDB REST API. The response is dynamic
336 * hence, only fields specifically requested for in the 'wantedFields'
337 * parameter is fetched/processed
339 * @param pdbJsonResponseString
340 * the JSON string to be parsed
341 * @param pdbRestRequest
342 * the request object which contains parameters used to process the
346 public static FTSRestResponse parsePDBJsonResponse(
347 String pdbJsonResponseString, FTSRestRequest pdbRestRequest)
349 return parsePDBJsonResponse(pdbJsonResponseString,
350 (Map<String, Object>) null, pdbRestRequest);
353 @SuppressWarnings("unchecked")
354 public static FTSRestResponse parsePDBJsonResponse(
355 String pdbJsonResponseString, Map<String, Object> jsonObj,
356 FTSRestRequest pdbRestRequest)
358 FTSRestResponse searchResult = new FTSRestResponse();
359 List<FTSData> result = null;
364 jsonObj = (Map<String, Object>) JSONUtils
365 .parse(pdbJsonResponseString);
367 Map<String, Object> pdbResponse = (Map<String, Object>) jsonObj
369 String queryTime = ((Map<String, Object>) jsonObj
370 .get("responseHeader")).get("QTime").toString();
371 int numFound = Integer
372 .valueOf(pdbResponse.get("numFound").toString());
373 List<Object> docs = (List<Object>) pdbResponse.get("docs");
375 result = new ArrayList<FTSData>();
378 for (Iterator<Object> docIter = docs.iterator(); docIter.hasNext();)
380 Map<String, Object> doc = (Map<String, Object>) docIter.next();
381 result.add(getFTSData(doc, pdbRestRequest));
384 // this is the total number found by the query,
385 // rather than the set returned in SearchSummary
386 searchResult.setNumberOfItemsFound(numFound);
387 searchResult.setResponseTime(queryTime);
388 searchResult.setSearchSummary(result);
389 } catch (ParseException e)
396 public static FTSData getFTSData(Map<String, Object> pdbJsonDoc,
397 FTSRestRequest request)
400 String primaryKey = null;
402 Object[] summaryRowData;
404 SequenceI associatedSequence;
406 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
407 SequenceI associatedSeq = request.getAssociatedSequence();
409 summaryRowData = new Object[(associatedSeq != null)
410 ? diplayFields.size() + 1
411 : diplayFields.size()];
412 if (associatedSeq != null)
414 associatedSequence = associatedSeq;
415 summaryRowData[0] = associatedSequence;
419 for (FTSDataColumnI field : diplayFields)
421 // System.out.println("Field " + field);
422 String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
423 : pdbJsonDoc.get(field.getCode()).toString();
424 // System.out.println("Field Data : " + fieldData);
425 if (field.isPrimaryKeyColumn())
427 primaryKey = fieldData;
428 summaryRowData[colCounter++] = primaryKey;
430 else if (fieldData == null || fieldData.isEmpty())
432 summaryRowData[colCounter++] = null;
438 summaryRowData[colCounter++] = (field.getDataType()
439 .getDataTypeClass() == Integer.class)
440 ? Integer.valueOf(fieldData)
441 : (field.getDataType()
442 .getDataTypeClass() == Double.class)
443 ? Double.valueOf(fieldData)
444 : sanitiseData(fieldData);
445 } catch (Exception e)
448 System.out.println("offending value:" + fieldData);
453 final String primaryKey1 = primaryKey;
455 final Object[] summaryRowData1 = summaryRowData;
459 public Object[] getSummaryData()
461 return summaryRowData1;
465 public Object getPrimaryKey()
471 * Returns a string representation of this object;
474 public String toString()
476 StringBuilder summaryFieldValues = new StringBuilder();
477 for (Object summaryField : summaryRowData1)
479 summaryFieldValues.append(
480 summaryField == null ? " " : summaryField.toString())
483 return summaryFieldValues.toString();
487 * Returns hash code value for this object
490 public int hashCode()
492 return Objects.hash(primaryKey1, this.toString());
496 public boolean equals(Object that)
498 return this.toString().equals(that.toString());
503 private static String sanitiseData(String data)
505 String cleanData = data.replaceAll("\\[\"", "").replaceAll("\\]\"", "")
506 .replaceAll("\\[", "").replaceAll("\\]", "")
507 .replaceAll("\",\"", ", ").replaceAll("\"", "");
512 public String getColumnDataConfigFileName()
514 return "/fts/pdb_data_columns.txt";
518 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
521 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
523 if (allDefaultDisplayedStructureDataColumns == null
524 || allDefaultDisplayedStructureDataColumns.isEmpty())
526 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
527 allDefaultDisplayedStructureDataColumns
528 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
530 return allDefaultDisplayedStructureDataColumns;
533 public String[] getPreferencesColumnsFor(PreferenceSource source)
535 String[] columnNames = null;
539 columnNames = new String[] { "", "Display", "Group" };
541 case STRUCTURE_CHOOSER:
542 columnNames = new String[] { "", "Display", "Group" };
545 columnNames = new String[] { "PDB Field", "Show in search summary",
546 "Show in structure summary" };