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/";
45 private static String TDB_DEV_API = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
47 private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API;
49 public static FTSRestClientI instance = null;
51 protected TDBeaconsFTSRestClient()
55 @SuppressWarnings("unchecked")
57 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
62 String query = tdbRestRequest.getSearchTerm();
64 Class<ClientResponse> clientResponseClass;
68 client = (Client) (Object) new jalview.javascript.web.Client();
69 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
78 client = Client.create(new DefaultClientConfig());
79 clientResponseClass = ClientResponse.class;
82 WebResource webResource;
83 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN + query);
85 URI uri = webResource.getURI();
86 System.out.println(uri.toString());
88 // Execute the REST request
89 ClientResponse clientResponse;
92 clientResponse = null;
96 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
97 .get(clientResponseClass);
100 // Get the JSON string from the response object or directly from the
101 // client (JavaScript)
102 Map<String, Object> jsonObj = null;
103 String responseString = null;
105 // Check the response status and report exception if one occurs
106 int responseStatus = isMocked()
107 ? (mockQueries.containsKey(query) ? 200 : 404)
108 : clientResponse.getStatus();
109 switch (responseStatus)
115 jsonObj = clientResponse.getEntity(Map.class);
119 responseString = isMocked() ? mockQueries.get(query)
120 : clientResponse.getEntity(String.class);
124 throw new Exception(parseJsonExceptionString(responseString));
126 return emptyTDBeaconsJsonResponse();
129 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
131 // Process the response and return the result to the caller.
132 return parseTDBeaconsJsonResponse(responseString, jsonObj,
134 } catch (Exception e)
136 String exceptionMsg = e.getMessage();
137 if (exceptionMsg != null)
139 if (exceptionMsg.contains("SocketException"))
141 // No internet connection
142 throw new Exception(MessageManager.getString(
143 "exception.unable_to_detect_internet_connection"));
145 else if (exceptionMsg.contains("UnknownHostException"))
147 // The server is unreachable
148 throw new Exception(MessageManager.formatMessage(
149 "exception.fts_server_unreachable", "3DB Hub"));
159 * returns response for when the 3D-Beacons service doesn't have a record for
160 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
164 private FTSRestResponse emptyTDBeaconsJsonResponse()
169 public String setSearchTerm(String term)
174 public static FTSRestResponse parseTDBeaconsJsonResponse(
175 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
177 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
178 (Map<String, Object>) null, tdbRestRequest);
181 @SuppressWarnings("unchecked")
182 public static FTSRestResponse parseTDBeaconsJsonResponse(
183 String tdbJsonResponseString, Map<String, Object> jsonObj,
184 FTSRestRequest tdbRestRequest)
186 FTSRestResponse searchResult = new FTSRestResponse();
187 List<FTSData> result = null;
193 jsonObj = (Map<String, Object>) JSONUtils
194 .parse(tdbJsonResponseString);
197 Object uniprot_entry = jsonObj.get("uniprot_entry");
198 // TODO: decide if anything from uniprot_entry needs to be reported via
199 // the FTSRestResponse object
200 // Arnaud added seqLength = (Long) ((Map<String, Object>)
201 // jsonObj.get("uniprot_entry")).get("sequence_length");
203 List<Object> structures = (List<Object>) jsonObj.get("structures");
204 result = new ArrayList<>();
207 for (Iterator<Object> strucIter = structures.iterator(); strucIter
210 Map<String, Object> structure = (Map<String, Object>) strucIter
212 result.add(getFTSData(structure, tdbRestRequest));
216 searchResult.setNumberOfItemsFound(numFound);
217 searchResult.setSearchSummary(result);
219 } catch (ParseException e)
226 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
227 FTSRestRequest tdbRequest)
229 String primaryKey = null;
230 Object[] summaryRowData;
232 SequenceI associatedSequence;
234 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
235 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
237 summaryRowData = new Object[(associatedSeq != null)
238 ? displayFields.size() + 1
239 : displayFields.size()];
240 if (associatedSeq != null)
242 associatedSequence = associatedSeq;
243 summaryRowData[0] = associatedSequence;
247 for (FTSDataColumnI field : displayFields)
249 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
251 : tdbJsonStructure.get(field.getCode()).toString();
252 // System.out.println("Field : " + field + " Data : " + fieldData);
253 if (field.isPrimaryKeyColumn())
255 primaryKey = fieldData;
256 summaryRowData[colCounter++] = primaryKey;
258 else if (fieldData == null || fieldData.trim().isEmpty())
260 summaryRowData[colCounter++] = null;
266 summaryRowData[colCounter++] = (field.getDataType()
267 .getDataTypeClass() == Integer.class)
268 ? Integer.valueOf(fieldData)
269 : (field.getDataType()
270 .getDataTypeClass() == Double.class)
271 ? Double.valueOf(fieldData)
273 } catch (Exception e)
275 // e.printStackTrace();
276 System.out.println("offending value:" + fieldData + fieldData);
280 final String primaryKey1 = primaryKey;
281 final Object[] summaryRowData1 = summaryRowData;
283 return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
286 // private static FTSData getFTSData(Map<String, Object> doc,
287 // FTSRestRequest tdbRestRequest)
289 // String primaryKey = null;
291 // Object[] summaryRowData;
293 // Collection<FTSDataColumnI> displayFields =
294 // tdbRestRequest.getWantedFields();
295 // int colCounter = 0;
296 // summaryRowData = new Object[displayFields.size() + 1];
301 private String parseJsonExceptionString(String jsonErrorString)
303 // TODO Auto-generated method stub
308 public String getColumnDataConfigFileName()
310 return "/fts/tdbeacons_data_columns.txt";
313 public static FTSRestClientI getInstance()
315 if (instance == null)
317 instance = new TDBeaconsFTSRestClient();
322 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
324 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
326 if (allDefaultDisplayedStructureDataColumns == null
327 || allDefaultDisplayedStructureDataColumns.isEmpty())
329 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
330 allDefaultDisplayedStructureDataColumns
331 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
333 return allDefaultDisplayedStructureDataColumns;
337 public String[] getPreferencesColumnsFor(PreferenceSource source)
339 String[] columnNames = null;
343 columnNames = new String[] { "", "Display", "Group" };
345 case STRUCTURE_CHOOSER:
346 columnNames = new String[] { "", "Display", "Group" };
349 columnNames = new String[] { "3DB Beacons Field",
350 "Show in search summary", "Show in structure summary" };