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;
78 WebResource webResource;
79 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
81 URI uri = webResource.getURI();
82 System.out.println(uri.toString());
84 // Execute the REST request
85 ClientResponse clientResponse;
87 clientResponse = null;
91 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
92 .get(clientResponseClass);
95 // Get the JSON string from the response object or directly from the
96 // client (JavaScript)
97 Map<String, Object> jsonObj = null;
98 String responseString = null;
100 // Check the response status and report exception if one occurs
101 int responseStatus = isMocked() ? (mockQuery.equals(query) ? 200 : 404) : clientResponse.getStatus();
102 switch (responseStatus)
108 jsonObj = clientResponse.getEntity(Map.class);
112 responseString = isMocked() ? mockResponse: clientResponse.getEntity(String.class);
116 throw new Exception(parseJsonExceptionString(responseString));
118 return emptyTDBeaconsJsonResponse();
121 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
123 // Process the response and return the result to the caller.
124 return parseTDBeaconsJsonResponse(responseString, jsonObj,
126 } catch (Exception e)
128 String exceptionMsg = e.getMessage();
129 if (exceptionMsg.contains("SocketException"))
131 // No internet connection
132 throw new Exception(MessageManager.getString(
133 "exception.unable_to_detect_internet_connection"));
135 else if (exceptionMsg.contains("UnknownHostException"))
137 // The server is unreachable
138 throw new Exception(MessageManager.formatMessage(
139 "exception.fts_server_unreachable", "3DB Hub"));
150 * returns response for when the 3D-Beacons service doesn't have a record for
151 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
155 private FTSRestResponse emptyTDBeaconsJsonResponse()
160 public String setSearchTerm(String term)
165 public static FTSRestResponse parseTDBeaconsJsonResponse(
166 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
168 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
169 (Map<String, Object>) null, tdbRestRequest);
172 @SuppressWarnings("unchecked")
173 public static FTSRestResponse parseTDBeaconsJsonResponse(
174 String tdbJsonResponseString, Map<String, Object> jsonObj,
175 FTSRestRequest tdbRestRequest)
177 FTSRestResponse searchResult = new FTSRestResponse();
178 List<FTSData> result = null;
184 jsonObj = (Map<String, Object>) JSONUtils
185 .parse(tdbJsonResponseString);
188 Object uniprot_entry = jsonObj.get("uniprot_entry");
189 // TODO: decide if anything from uniprot_entry needs to be reported via
190 // the FTSRestResponse object
191 // Arnaud added seqLength = (Long) ((Map<String, Object>)
192 // jsonObj.get("uniprot_entry")).get("sequence_length");
194 List<Object> structures = (List<Object>) jsonObj.get("structures");
195 result = new ArrayList<>();
198 for (Iterator<Object> strucIter = structures.iterator(); strucIter
201 Map<String, Object> structure = (Map<String, Object>) strucIter
203 result.add(getFTSData(structure, tdbRestRequest));
207 searchResult.setNumberOfItemsFound(numFound);
208 searchResult.setSearchSummary(result);
210 } catch (ParseException e)
217 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
218 FTSRestRequest tdbRequest)
220 // TODO: consider reusing PDBFTSRestClient.getFTSData ?
222 String primaryKey = null;
223 Object[] summaryRowData;
225 SequenceI associatedSequence;
227 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
228 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
230 summaryRowData = new Object[(associatedSeq != null)
231 ? displayFields.size() + 1
232 : displayFields.size()];
233 if (associatedSeq != null)
235 associatedSequence = associatedSeq;
236 summaryRowData[0] = associatedSequence;
240 for (FTSDataColumnI field : displayFields)
242 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
244 : tdbJsonStructure.get(field.getCode()).toString();
245 // System.out.println("Field : " + field + " Data : " + fieldData);
246 if (field.isPrimaryKeyColumn())
248 primaryKey = fieldData;
249 summaryRowData[colCounter++] = primaryKey;
251 else if (fieldData == null || fieldData.trim().isEmpty())
253 summaryRowData[colCounter++] = null;
259 summaryRowData[colCounter++] = (field.getDataType()
260 .getDataTypeClass() == Integer.class)
261 ? Integer.valueOf(fieldData)
262 : (field.getDataType()
263 .getDataTypeClass() == Double.class)
264 ? Double.valueOf(fieldData)
266 } catch (Exception e)
268 // e.printStackTrace();
269 System.out.println("offending value:" + fieldData + fieldData);
273 final String primaryKey1 = primaryKey;
274 final Object[] summaryRowData1 = summaryRowData;
280 public Object[] getSummaryData()
282 return summaryRowData1;
286 public Object getPrimaryKey()
292 * Returns a string representation of this object;
295 public String toString()
297 StringBuilder summaryFieldValues = new StringBuilder();
298 for (Object summaryField : summaryRowData1)
300 summaryFieldValues.append(
301 summaryField == null ? " " : summaryField.toString())
304 return summaryFieldValues.toString();
308 * Returns hash code value for this object
311 public int hashCode()
313 return Objects.hash(primaryKey1, this.toString());
317 public boolean equals(Object that)
319 return this.toString().equals(that.toString());
324 // private static FTSData getFTSData(Map<String, Object> doc,
325 // FTSRestRequest tdbRestRequest)
327 // String primaryKey = null;
329 // Object[] summaryRowData;
331 // Collection<FTSDataColumnI> displayFields =
332 // tdbRestRequest.getWantedFields();
333 // int colCounter = 0;
334 // summaryRowData = new Object[displayFields.size() + 1];
339 private String parseJsonExceptionString(String jsonErrorString)
341 // TODO Auto-generated method stub
346 public String getColumnDataConfigFileName()
348 return "/fts/tdbeacons_data_columns.txt";
351 public static FTSRestClientI getInstance()
353 if (instance == null)
355 instance = new TDBeaconsFTSRestClient();
360 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
362 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
364 if (allDefaultDisplayedStructureDataColumns == null
365 || allDefaultDisplayedStructureDataColumns.isEmpty())
367 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
368 allDefaultDisplayedStructureDataColumns
369 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
371 return allDefaultDisplayedStructureDataColumns;
375 public String[] getPreferencesColumnsFor(PreferenceSource source)
377 String[] columnNames = null;
381 columnNames = new String[] { "", "Display", "Group" };
383 case STRUCTURE_CHOOSER:
384 columnNames = new String[] { "", "Display", "Group" };
387 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
388 "Show in structure summary" };