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 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;
276 return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
279 // private static FTSData getFTSData(Map<String, Object> doc,
280 // FTSRestRequest tdbRestRequest)
282 // String primaryKey = null;
284 // Object[] summaryRowData;
286 // Collection<FTSDataColumnI> displayFields =
287 // tdbRestRequest.getWantedFields();
288 // int colCounter = 0;
289 // summaryRowData = new Object[displayFields.size() + 1];
294 private String parseJsonExceptionString(String jsonErrorString)
296 // TODO Auto-generated method stub
301 public String getColumnDataConfigFileName()
303 return "/fts/tdbeacons_data_columns.txt";
306 public static FTSRestClientI getInstance()
308 if (instance == null)
310 instance = new TDBeaconsFTSRestClient();
315 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
317 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
319 if (allDefaultDisplayedStructureDataColumns == null
320 || allDefaultDisplayedStructureDataColumns.isEmpty())
322 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
323 allDefaultDisplayedStructureDataColumns
324 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
326 return allDefaultDisplayedStructureDataColumns;
330 public String[] getPreferencesColumnsFor(PreferenceSource source)
332 String[] columnNames = null;
336 columnNames = new String[] { "", "Display", "Group" };
338 case STRUCTURE_CHOOSER:
339 columnNames = new String[] { "", "Display", "Group" };
342 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
343 "Show in structure summary" };