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
37 private static final String DEFAULT_THREEDBEACONS_DOMAIN = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
39 public static FTSRestClientI instance = null;
41 protected TDBeaconsFTSRestClient()
44 @SuppressWarnings("unchecked")
46 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
51 String query = tdbRestRequest.getSearchTerm();
53 Class<ClientResponse> clientResponseClass;
57 client = (Client) (Object) new jalview.javascript.web.Client();
58 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
67 client = Client.create(new DefaultClientConfig());
68 clientResponseClass = ClientResponse.class;
70 WebResource webResource;
71 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
73 URI uri = webResource.getURI();
74 System.out.println(uri.toString());
76 // Execute the REST request
77 ClientResponse clientResponse;
79 clientResponse = null;
83 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
84 .get(clientResponseClass);
87 // Get the JSON string from the response object or directly from the
88 // client (JavaScript)
89 Map<String, Object> jsonObj = null;
90 String responseString = null;
92 // Check the response status and report exception if one occurs
93 int responseStatus = isMocked() ? (mockQuery.equals(query) ? 200 : 404) : clientResponse.getStatus();
94 switch (responseStatus)
100 jsonObj = clientResponse.getEntity(Map.class);
104 responseString = isMocked() ? mockResponse: clientResponse.getEntity(String.class);
108 throw new Exception(parseJsonExceptionString(responseString));
110 return emptyTDBeaconsJsonResponse();
113 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
115 // Process the response and return the result to the caller.
116 return parseTDBeaconsJsonResponse(responseString, jsonObj,
118 } catch (Exception e)
120 String exceptionMsg = e.getMessage();
121 if (exceptionMsg.contains("SocketException"))
123 // No internet connection
124 throw new Exception(MessageManager.getString(
125 "exception.unable_to_detect_internet_connection"));
127 else if (exceptionMsg.contains("UnknownHostException"))
129 // The server is unreachable
130 throw new Exception(MessageManager.formatMessage(
131 "exception.fts_server_unreachable", "3DB Hub"));
142 * returns response for when the 3D-Beacons service doesn't have a record for
143 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
147 private FTSRestResponse emptyTDBeaconsJsonResponse()
152 public String setSearchTerm(String term)
157 public static FTSRestResponse parseTDBeaconsJsonResponse(
158 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
160 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
161 (Map<String, Object>) null, tdbRestRequest);
164 @SuppressWarnings("unchecked")
165 public static FTSRestResponse parseTDBeaconsJsonResponse(
166 String tdbJsonResponseString, Map<String, Object> jsonObj,
167 FTSRestRequest tdbRestRequest)
169 FTSRestResponse searchResult = new FTSRestResponse();
170 List<FTSData> result = null;
176 jsonObj = (Map<String, Object>) JSONUtils
177 .parse(tdbJsonResponseString);
180 Object uniprot_entry = jsonObj.get("uniprot_entry");
181 // TODO: decide if anything from uniprot_entry needs to be reported via
182 // the FTSRestResponse object
183 // Arnaud added seqLength = (Long) ((Map<String, Object>)
184 // jsonObj.get("uniprot_entry")).get("sequence_length");
186 List<Object> structures = (List<Object>) jsonObj.get("structures");
187 result = new ArrayList<>();
190 for (Iterator<Object> strucIter = structures.iterator(); strucIter
193 Map<String, Object> structure = (Map<String, Object>) strucIter
195 result.add(getFTSData(structure, tdbRestRequest));
199 searchResult.setNumberOfItemsFound(numFound);
200 searchResult.setSearchSummary(result);
202 } catch (ParseException e)
209 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
210 FTSRestRequest tdbRequest)
212 // TODO: consider reusing PDBFTSRestClient.getFTSData ?
214 String primaryKey = null;
215 Object[] summaryRowData;
217 SequenceI associatedSequence;
219 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
220 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
222 summaryRowData = new Object[(associatedSeq != null)
223 ? displayFields.size() + 1
224 : displayFields.size()];
225 if (associatedSeq != null)
227 associatedSequence = associatedSeq;
228 summaryRowData[0] = associatedSequence;
232 for (FTSDataColumnI field : displayFields)
234 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
236 : tdbJsonStructure.get(field.getCode()).toString();
237 // System.out.println("Field : " + field + " Data : " + fieldData);
238 if (field.isPrimaryKeyColumn())
240 primaryKey = fieldData;
241 summaryRowData[colCounter++] = primaryKey;
243 else if (fieldData == null || fieldData.trim().isEmpty())
245 summaryRowData[colCounter++] = null;
251 summaryRowData[colCounter++] = (field.getDataType()
252 .getDataTypeClass() == Integer.class)
253 ? Integer.valueOf(fieldData)
254 : (field.getDataType()
255 .getDataTypeClass() == Double.class)
256 ? Double.valueOf(fieldData)
258 } catch (Exception e)
260 // e.printStackTrace();
261 System.out.println("offending value:" + fieldData + fieldData);
265 final String primaryKey1 = primaryKey;
266 final Object[] summaryRowData1 = summaryRowData;
272 public Object[] getSummaryData()
274 return summaryRowData1;
278 public Object getPrimaryKey()
284 * Returns a string representation of this object;
287 public String toString()
289 StringBuilder summaryFieldValues = new StringBuilder();
290 for (Object summaryField : summaryRowData1)
292 summaryFieldValues.append(
293 summaryField == null ? " " : summaryField.toString())
296 return summaryFieldValues.toString();
300 * Returns hash code value for this object
303 public int hashCode()
305 return Objects.hash(primaryKey1, this.toString());
309 public boolean equals(Object that)
311 return this.toString().equals(that.toString());
316 // private static FTSData getFTSData(Map<String, Object> doc,
317 // FTSRestRequest tdbRestRequest)
319 // String primaryKey = null;
321 // Object[] summaryRowData;
323 // Collection<FTSDataColumnI> displayFields =
324 // tdbRestRequest.getWantedFields();
325 // int colCounter = 0;
326 // summaryRowData = new Object[displayFields.size() + 1];
331 private String parseJsonExceptionString(String jsonErrorString)
333 // TODO Auto-generated method stub
338 public String getColumnDataConfigFileName()
340 return "/fts/tdbeacons_data_columns.txt";
343 public static FTSRestClientI getInstance()
345 if (instance == null)
347 instance = new TDBeaconsFTSRestClient();
352 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
354 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
356 if (allDefaultDisplayedStructureDataColumns == null
357 || allDefaultDisplayedStructureDataColumns.isEmpty())
359 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
360 allDefaultDisplayedStructureDataColumns
361 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
363 return allDefaultDisplayedStructureDataColumns;
367 public String[] getPreferencesColumnsFor(PreferenceSource source)
369 String[] columnNames = null;
373 columnNames = new String[] { "", "Display", "Group" };
375 case STRUCTURE_CHOOSER:
376 columnNames = new String[] { "", "Display", "Group" };
379 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
380 "Show in structure summary" };