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.contains("SocketException"))
132 // No internet connection
133 throw new Exception(MessageManager.getString(
134 "exception.unable_to_detect_internet_connection"));
136 else if (exceptionMsg.contains("UnknownHostException"))
138 // The server is unreachable
139 throw new Exception(MessageManager.formatMessage(
140 "exception.fts_server_unreachable", "3DB Hub"));
151 * returns response for when the 3D-Beacons service doesn't have a record for
152 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
156 private FTSRestResponse emptyTDBeaconsJsonResponse()
161 public String setSearchTerm(String term)
166 public static FTSRestResponse parseTDBeaconsJsonResponse(
167 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
169 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
170 (Map<String, Object>) null, tdbRestRequest);
173 @SuppressWarnings("unchecked")
174 public static FTSRestResponse parseTDBeaconsJsonResponse(
175 String tdbJsonResponseString, Map<String, Object> jsonObj,
176 FTSRestRequest tdbRestRequest)
178 FTSRestResponse searchResult = new FTSRestResponse();
179 List<FTSData> result = null;
185 jsonObj = (Map<String, Object>) JSONUtils
186 .parse(tdbJsonResponseString);
189 Object uniprot_entry = jsonObj.get("uniprot_entry");
190 // TODO: decide if anything from uniprot_entry needs to be reported via
191 // the FTSRestResponse object
192 // Arnaud added seqLength = (Long) ((Map<String, Object>)
193 // jsonObj.get("uniprot_entry")).get("sequence_length");
195 List<Object> structures = (List<Object>) jsonObj.get("structures");
196 result = new ArrayList<>();
199 for (Iterator<Object> strucIter = structures.iterator(); strucIter
202 Map<String, Object> structure = (Map<String, Object>) strucIter
204 result.add(getFTSData(structure, tdbRestRequest));
208 searchResult.setNumberOfItemsFound(numFound);
209 searchResult.setSearchSummary(result);
211 } catch (ParseException e)
218 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
219 FTSRestRequest tdbRequest)
221 // TODO: consider reusing PDBFTSRestClient.getFTSData ?
223 String primaryKey = null;
224 Object[] summaryRowData;
226 SequenceI associatedSequence;
228 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
229 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
231 summaryRowData = new Object[(associatedSeq != null)
232 ? displayFields.size() + 1
233 : displayFields.size()];
234 if (associatedSeq != null)
236 associatedSequence = associatedSeq;
237 summaryRowData[0] = associatedSequence;
241 for (FTSDataColumnI field : displayFields)
243 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
245 : tdbJsonStructure.get(field.getCode()).toString();
246 // System.out.println("Field : " + field + " Data : " + fieldData);
247 if (field.isPrimaryKeyColumn())
249 primaryKey = fieldData;
250 summaryRowData[colCounter++] = primaryKey;
252 else if (fieldData == null || fieldData.trim().isEmpty())
254 summaryRowData[colCounter++] = null;
260 summaryRowData[colCounter++] = (field.getDataType()
261 .getDataTypeClass() == Integer.class)
262 ? Integer.valueOf(fieldData)
263 : (field.getDataType()
264 .getDataTypeClass() == Double.class)
265 ? Double.valueOf(fieldData)
267 } catch (Exception e)
269 // e.printStackTrace();
270 System.out.println("offending value:" + fieldData + fieldData);
274 final String primaryKey1 = primaryKey;
275 final Object[] summaryRowData1 = summaryRowData;
281 public Object[] getSummaryData()
283 return summaryRowData1;
287 public Object getPrimaryKey()
293 * Returns a string representation of this object;
296 public String toString()
298 StringBuilder summaryFieldValues = new StringBuilder();
299 for (Object summaryField : summaryRowData1)
301 summaryFieldValues.append(
302 summaryField == null ? " " : summaryField.toString())
305 return summaryFieldValues.toString();
309 * Returns hash code value for this object
312 public int hashCode()
314 return Objects.hash(primaryKey1, this.toString());
318 public boolean equals(Object that)
320 return this.toString().equals(that.toString());
325 // private static FTSData getFTSData(Map<String, Object> doc,
326 // FTSRestRequest tdbRestRequest)
328 // String primaryKey = null;
330 // Object[] summaryRowData;
332 // Collection<FTSDataColumnI> displayFields =
333 // tdbRestRequest.getWantedFields();
334 // int colCounter = 0;
335 // summaryRowData = new Object[displayFields.size() + 1];
340 private String parseJsonExceptionString(String jsonErrorString)
342 // TODO Auto-generated method stub
347 public String getColumnDataConfigFileName()
349 return "/fts/tdbeacons_data_columns.txt";
352 public static FTSRestClientI getInstance()
354 if (instance == null)
356 instance = new TDBeaconsFTSRestClient();
361 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
363 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
365 if (allDefaultDisplayedStructureDataColumns == null
366 || allDefaultDisplayedStructureDataColumns.isEmpty())
368 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
369 allDefaultDisplayedStructureDataColumns
370 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
372 return allDefaultDisplayedStructureDataColumns;
376 public String[] getPreferencesColumnsFor(PreferenceSource source)
378 String[] columnNames = null;
382 columnNames = new String[] { "", "Display", "Group" };
384 case STRUCTURE_CHOOSER:
385 columnNames = new String[] { "", "Display", "Group" };
388 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
389 "Show in structure summary" };