1 package jalview.fts.service.threedbeacons;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
9 import java.util.Objects;
11 import javax.ws.rs.core.MediaType;
13 import org.json.simple.parser.ParseException;
15 import com.sun.jersey.api.client.Client;
16 import com.sun.jersey.api.client.ClientResponse;
17 import com.sun.jersey.api.client.WebResource;
18 import com.sun.jersey.api.client.config.DefaultClientConfig;
20 import jalview.datamodel.SequenceI;
21 import jalview.fts.api.FTSData;
22 import jalview.fts.api.FTSDataColumnI;
23 import jalview.fts.api.FTSRestClientI;
24 import jalview.fts.api.StructureFTSRestClientI;
25 import jalview.fts.core.FTSRestClient;
26 import jalview.fts.core.FTSRestRequest;
27 import jalview.fts.core.FTSRestResponse;
28 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
29 import jalview.fts.service.pdb.PDBFTSRestClient;
30 import jalview.util.JSONUtils;
31 import jalview.util.MessageManager;
32 import jalview.util.Platform;
34 public class TDBeaconsFTSRestClient extends FTSRestClient
35 implements StructureFTSRestClientI
38 * production server URI
40 private static String TDB_PROD_API="https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
44 private static String TDB_DEV_API="https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
45 private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API;
47 public static FTSRestClientI instance = null;
49 protected TDBeaconsFTSRestClient()
52 @SuppressWarnings("unchecked")
54 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
59 String query = tdbRestRequest.getSearchTerm();
61 Class<ClientResponse> clientResponseClass;
65 client = (Client) (Object) new jalview.javascript.web.Client();
66 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
75 client = Client.create(new DefaultClientConfig());
76 clientResponseClass = ClientResponse.class;
79 WebResource webResource;
80 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN+query);
82 URI uri = webResource.getURI();
83 System.out.println(uri.toString());
85 // Execute the REST request
86 ClientResponse clientResponse;
88 clientResponse = null;
92 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
93 .get(clientResponseClass);
96 // Get the JSON string from the response object or directly from the
97 // client (JavaScript)
98 Map<String, Object> jsonObj = null;
99 String responseString = null;
101 // Check the response status and report exception if one occurs
102 int responseStatus = isMocked() ? (mockQueries.containsKey(query) ? 200 : 404) : clientResponse.getStatus();
103 switch (responseStatus)
109 jsonObj = clientResponse.getEntity(Map.class);
113 responseString = isMocked() ? mockQueries.get(query): clientResponse.getEntity(String.class);
117 throw new Exception(parseJsonExceptionString(responseString));
119 return emptyTDBeaconsJsonResponse();
122 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
124 // Process the response and return the result to the caller.
125 return parseTDBeaconsJsonResponse(responseString, jsonObj,
127 } catch (Exception e)
129 String exceptionMsg = e.getMessage();
130 if (exceptionMsg != null)
132 if (exceptionMsg.contains("SocketException"))
134 // No internet connection
135 throw new Exception(MessageManager.getString(
136 "exception.unable_to_detect_internet_connection"));
138 else if (exceptionMsg.contains("UnknownHostException"))
140 // The server is unreachable
141 throw new Exception(MessageManager.formatMessage(
142 "exception.fts_server_unreachable", "3DB Hub"));
152 * returns response for when the 3D-Beacons service doesn't have a record for
153 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
157 private FTSRestResponse emptyTDBeaconsJsonResponse()
162 public String setSearchTerm(String term)
167 public static FTSRestResponse parseTDBeaconsJsonResponse(
168 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
170 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
171 (Map<String, Object>) null, tdbRestRequest);
174 @SuppressWarnings("unchecked")
175 public static FTSRestResponse parseTDBeaconsJsonResponse(
176 String tdbJsonResponseString, Map<String, Object> jsonObj,
177 FTSRestRequest tdbRestRequest)
179 FTSRestResponse searchResult = new FTSRestResponse();
180 List<FTSData> result = null;
186 jsonObj = (Map<String, Object>) JSONUtils
187 .parse(tdbJsonResponseString);
190 Object uniprot_entry = jsonObj.get("uniprot_entry");
191 // TODO: decide if anything from uniprot_entry needs to be reported via
192 // the FTSRestResponse object
193 // Arnaud added seqLength = (Long) ((Map<String, Object>)
194 // jsonObj.get("uniprot_entry")).get("sequence_length");
196 List<Object> structures = (List<Object>) jsonObj.get("structures");
197 result = new ArrayList<>();
200 for (Iterator<Object> strucIter = structures.iterator(); strucIter
203 Map<String, Object> structure = (Map<String, Object>) strucIter
205 result.add(getFTSData(structure, tdbRestRequest));
209 searchResult.setNumberOfItemsFound(numFound);
210 searchResult.setSearchSummary(result);
212 } catch (ParseException e)
219 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
220 FTSRestRequest tdbRequest)
222 // TODO: consider reusing PDBFTSRestClient.getFTSData ?
224 String primaryKey = null;
225 Object[] summaryRowData;
227 SequenceI associatedSequence;
229 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
230 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
232 summaryRowData = new Object[(associatedSeq != null)
233 ? displayFields.size() + 1
234 : displayFields.size()];
235 if (associatedSeq != null)
237 associatedSequence = associatedSeq;
238 summaryRowData[0] = associatedSequence;
242 for (FTSDataColumnI field : displayFields)
244 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
246 : tdbJsonStructure.get(field.getCode()).toString();
247 // System.out.println("Field : " + field + " Data : " + fieldData);
248 if (field.isPrimaryKeyColumn())
250 primaryKey = fieldData;
251 summaryRowData[colCounter++] = primaryKey;
253 else if (fieldData == null || fieldData.trim().isEmpty())
255 summaryRowData[colCounter++] = null;
261 summaryRowData[colCounter++] = (field.getDataType()
262 .getDataTypeClass() == Integer.class)
263 ? Integer.valueOf(fieldData)
264 : (field.getDataType()
265 .getDataTypeClass() == Double.class)
266 ? Double.valueOf(fieldData)
268 } catch (Exception e)
270 // e.printStackTrace();
271 System.out.println("offending value:" + fieldData + fieldData);
275 final String primaryKey1 = primaryKey;
276 final Object[] summaryRowData1 = summaryRowData;
282 public Object[] getSummaryData()
284 return summaryRowData1;
288 public Object getPrimaryKey()
294 * Returns a string representation of this object;
297 public String toString()
299 StringBuilder summaryFieldValues = new StringBuilder();
300 for (Object summaryField : summaryRowData1)
302 summaryFieldValues.append(
303 summaryField == null ? " " : summaryField.toString())
306 return summaryFieldValues.toString();
310 * Returns hash code value for this object
313 public int hashCode()
315 return Objects.hash(primaryKey1, this.toString());
319 public boolean equals(Object that)
321 return this.toString().equals(that.toString());
326 // private static FTSData getFTSData(Map<String, Object> doc,
327 // FTSRestRequest tdbRestRequest)
329 // String primaryKey = null;
331 // Object[] summaryRowData;
333 // Collection<FTSDataColumnI> displayFields =
334 // tdbRestRequest.getWantedFields();
335 // int colCounter = 0;
336 // summaryRowData = new Object[displayFields.size() + 1];
341 private String parseJsonExceptionString(String jsonErrorString)
343 // TODO Auto-generated method stub
348 public String getColumnDataConfigFileName()
350 return "/fts/tdbeacons_data_columns.txt";
353 public static FTSRestClientI getInstance()
355 if (instance == null)
357 instance = new TDBeaconsFTSRestClient();
362 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
364 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
366 if (allDefaultDisplayedStructureDataColumns == null
367 || allDefaultDisplayedStructureDataColumns.isEmpty())
369 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
370 allDefaultDisplayedStructureDataColumns
371 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
373 return allDefaultDisplayedStructureDataColumns;
377 public String[] getPreferencesColumnsFor(PreferenceSource source)
379 String[] columnNames = null;
383 columnNames = new String[] { "", "Display", "Group" };
385 case STRUCTURE_CHOOSER:
386 columnNames = new String[] { "", "Display", "Group" };
389 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
390 "Show in structure summary" };