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 private static FTSRestClientI instance = null;
41 protected TDBeaconsFTSRestClient()
44 public static void createMockTDBeaconsFSRestClient(String workingQuery, String jsonResponse) {
45 instance = new TDBeaconsFTSRestClient();
46 ((TDBeaconsFTSRestClient) instance).setMock(workingQuery,jsonResponse);
48 private String mockQuery = null;
50 private String mockResponse = null;
51 protected void setMock(String workingQuery,String jsonResponse) {
52 mockQuery=workingQuery;
53 mockResponse = jsonResponse;
55 private boolean isMocked()
57 return mockQuery != null && mockResponse!=null;
59 @SuppressWarnings("unchecked")
61 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
66 String query = tdbRestRequest.getSearchTerm();
68 Class<ClientResponse> clientResponseClass;
72 client = (Client) (Object) new jalview.javascript.web.Client();
73 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
82 client = Client.create(new DefaultClientConfig());
83 clientResponseClass = ClientResponse.class;
85 WebResource webResource;
86 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
88 URI uri = webResource.getURI();
89 System.out.println(uri.toString());
91 // Execute the REST request
92 ClientResponse clientResponse;
94 clientResponse = null;
98 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
99 .get(clientResponseClass);
102 // Get the JSON string from the response object or directly from the
103 // client (JavaScript)
104 Map<String, Object> jsonObj = null;
105 String responseString = null;
107 // Check the response status and report exception if one occurs
108 int responseStatus = isMocked() ? (mockQuery.equals(query) ? 200 : 404) : clientResponse.getStatus();
109 switch (responseStatus)
115 jsonObj = clientResponse.getEntity(Map.class);
119 responseString = isMocked() ? mockResponse: clientResponse.getEntity(String.class);
123 throw new Exception(parseJsonExceptionString(responseString));
125 return emptyTDBeaconsJsonResponse();
128 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
130 // Process the response and return the result to the caller.
131 return parseTDBeaconsJsonResponse(responseString, jsonObj,
133 } catch (Exception e)
135 String exceptionMsg = e.getMessage();
136 if (exceptionMsg.contains("SocketException"))
138 // No internet connection
139 throw new Exception(MessageManager.getString(
140 "exception.unable_to_detect_internet_connection"));
142 else if (exceptionMsg.contains("UnknownHostException"))
144 // The server is unreachable
145 throw new Exception(MessageManager.formatMessage(
146 "exception.fts_server_unreachable", "3DB Hub"));
157 * returns response for when the 3D-Beacons service doesn't have a record for
158 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
162 private FTSRestResponse emptyTDBeaconsJsonResponse()
167 public String setSearchTerm(String term)
172 public static FTSRestResponse parseTDBeaconsJsonResponse(
173 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
175 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
176 (Map<String, Object>) null, tdbRestRequest);
179 @SuppressWarnings("unchecked")
180 public static FTSRestResponse parseTDBeaconsJsonResponse(
181 String tdbJsonResponseString, Map<String, Object> jsonObj,
182 FTSRestRequest tdbRestRequest)
184 FTSRestResponse searchResult = new FTSRestResponse();
185 List<FTSData> result = null;
191 jsonObj = (Map<String, Object>) JSONUtils
192 .parse(tdbJsonResponseString);
195 Object uniprot_entry = jsonObj.get("uniprot_entry");
196 // TODO: decide if anything from uniprot_entry needs to be reported via
197 // the FTSRestResponse object
198 // Arnaud added seqLength = (Long) ((Map<String, Object>)
199 // jsonObj.get("uniprot_entry")).get("sequence_length");
201 List<Object> structures = (List<Object>) jsonObj.get("structures");
202 result = new ArrayList<>();
205 for (Iterator<Object> strucIter = structures.iterator(); strucIter
208 Map<String, Object> structure = (Map<String, Object>) strucIter
210 result.add(getFTSData(structure, tdbRestRequest));
214 searchResult.setNumberOfItemsFound(numFound);
215 searchResult.setSearchSummary(result);
217 } catch (ParseException e)
224 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
225 FTSRestRequest tdbRequest)
227 // TODO: consider reusing PDBFTSRestClient.getFTSData ?
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;
287 public Object[] getSummaryData()
289 return summaryRowData1;
293 public Object getPrimaryKey()
299 * Returns a string representation of this object;
302 public String toString()
304 StringBuilder summaryFieldValues = new StringBuilder();
305 for (Object summaryField : summaryRowData1)
307 summaryFieldValues.append(
308 summaryField == null ? " " : summaryField.toString())
311 return summaryFieldValues.toString();
315 * Returns hash code value for this object
318 public int hashCode()
320 return Objects.hash(primaryKey1, this.toString());
324 public boolean equals(Object that)
326 return this.toString().equals(that.toString());
331 // private static FTSData getFTSData(Map<String, Object> doc,
332 // FTSRestRequest tdbRestRequest)
334 // String primaryKey = null;
336 // Object[] summaryRowData;
338 // Collection<FTSDataColumnI> displayFields =
339 // tdbRestRequest.getWantedFields();
340 // int colCounter = 0;
341 // summaryRowData = new Object[displayFields.size() + 1];
346 private String parseJsonExceptionString(String jsonErrorString)
348 // TODO Auto-generated method stub
353 public String getColumnDataConfigFileName()
355 return "/fts/tdbeacons_data_columns.txt";
358 public static FTSRestClientI getInstance()
360 if (instance == null)
362 instance = new TDBeaconsFTSRestClient();
367 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
369 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
371 if (allDefaultDisplayedStructureDataColumns == null
372 || allDefaultDisplayedStructureDataColumns.isEmpty())
374 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
375 allDefaultDisplayedStructureDataColumns
376 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
378 return allDefaultDisplayedStructureDataColumns;
382 public String[] getPreferencesColumnsFor(PreferenceSource source)
384 String[] columnNames = null;
388 columnNames = new String[] { "", "Display", "Group" };
390 case STRUCTURE_CHOOSER:
391 columnNames = new String[] { "", "Display", "Group" };
394 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
395 "Show in structure summary" };