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.core.FTSRestClient;
25 import jalview.fts.core.FTSRestRequest;
26 import jalview.fts.core.FTSRestResponse;
27 import jalview.fts.service.pdb.PDBFTSRestClient;
28 import jalview.util.JSONUtils;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
32 public class TDBeaconsFTSRestClient extends FTSRestClient
34 private static final String DEFAULT_THREEDBEACONS_DOMAIN =
35 "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
37 private static FTSRestClientI instance = null;
39 protected TDBeaconsFTSRestClient()
43 @SuppressWarnings("unchecked")
45 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
50 String query = tdbRestRequest.getSearchTerm();
52 Class<ClientResponse> clientResponseClass;
56 client = (Client) (Object) new jalview.javascript.web.Client();
57 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
65 client = Client.create(new DefaultClientConfig());
66 clientResponseClass = ClientResponse.class;
68 WebResource webResource;
69 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
71 URI uri = webResource.getURI();
72 System.out.println(uri.toString());
74 // Execute the REST request
75 ClientResponse clientResponse = webResource
76 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
78 // Get the JSON string from the response object or directly from the
79 // client (JavaScript)
80 Map<String, Object> jsonObj = null;
81 String responseString = null;
83 // Check the response status and report exception if one occurs
84 int responseStatus = clientResponse.getStatus();
85 switch (responseStatus)
91 jsonObj = clientResponse.getEntity(Map.class);
95 responseString = clientResponse.getEntity(String.class);
99 throw new Exception(parseJsonExceptionString(responseString));
102 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
104 // Process the response and return the result to the caller.
105 return parseTDBeaconsJsonResponse(responseString, jsonObj, tdbRestRequest);
106 } catch (Exception e)
108 String exceptionMsg = e.getMessage();
109 if (exceptionMsg.contains("SocketException"))
111 // No internet connection
112 throw new Exception(MessageManager.getString(
113 "exception.unable_to_detect_internet_connection"));
115 else if (exceptionMsg.contains("UnknownHostException"))
117 // The server is unreachable
118 throw new Exception(MessageManager.formatMessage(
119 "exception.fts_server_unreachable", "3DB Hub"));
129 public String setSearchTerm(String term) {
133 public static FTSRestResponse parseTDBeaconsJsonResponse(
134 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
136 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
137 (Map<String, Object>) null, tdbRestRequest);
140 @SuppressWarnings("unchecked")
141 public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString,
142 Map<String, Object> jsonObj, FTSRestRequest tdbRestRequest)
144 FTSRestResponse searchResult = new FTSRestResponse();
145 List<FTSData> result = null;
151 jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
154 Object uniprot_entry = jsonObj.get("uniprot_entry");
155 // TODO: decide if anything from uniprot_entry needs to be reported via the FTSRestResponse object
156 // Arnaud added seqLength = (Long) ((Map<String, Object>) jsonObj.get("uniprot_entry")).get("sequence_length");
158 List<Object> structures = (List<Object>) jsonObj.get("structures");
159 result = new ArrayList<>();
162 for (Iterator<Object> strucIter = structures.iterator(); strucIter.hasNext();)
164 Map<String, Object> structure = (Map<String, Object>) strucIter.next();
165 result.add(getFTSData(structure, tdbRestRequest));
169 searchResult.setNumberOfItemsFound(numFound);
170 searchResult.setSearchSummary(result);
172 } catch (ParseException e)
179 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
180 FTSRestRequest tdbRequest)
182 // TODO Auto-generated method stub
183 String primaryKey = null;
184 Object[] summaryRowData;
185 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
187 summaryRowData = new Object[displayFields.size()];
189 for (FTSDataColumnI field : displayFields) {
190 String fieldData = (tdbJsonStructure.get(field.getCode()) == null) ? " "
191 : tdbJsonStructure.get(field.getCode()).toString();
192 // System.out.println("Field : " + field + " Data : " + fieldData);
193 if (field.isPrimaryKeyColumn())
195 primaryKey = fieldData;
196 summaryRowData[colCounter++] = primaryKey;
198 else if (fieldData == null || fieldData.trim().isEmpty())
200 summaryRowData[colCounter++] = null;
206 summaryRowData[colCounter++] = (field.getDataType()
207 .getDataTypeClass() == Integer.class)
208 ? Integer.valueOf(fieldData)
209 : (field.getDataType()
210 .getDataTypeClass() == Double.class)
211 ? Double.valueOf(fieldData)
213 } catch (Exception e)
215 //e.printStackTrace();
216 System.out.println("offending value:" + fieldData + fieldData);
220 final String primaryKey1 = primaryKey;
221 final Object[] summaryRowData1 = summaryRowData;
227 public Object[] getSummaryData()
229 return summaryRowData1;
233 public Object getPrimaryKey()
239 * Returns a string representation of this object;
242 public String toString()
244 StringBuilder summaryFieldValues = new StringBuilder();
245 for (Object summaryField : summaryRowData1)
247 summaryFieldValues.append(
248 summaryField == null ? " " : summaryField.toString())
251 return summaryFieldValues.toString();
255 * Returns hash code value for this object
258 public int hashCode()
260 return Objects.hash(primaryKey1, this.toString());
264 public boolean equals(Object that)
266 return this.toString().equals(that.toString());
271 // private static FTSData getFTSData(Map<String, Object> doc,
272 // FTSRestRequest tdbRestRequest)
274 // String primaryKey = null;
276 // Object[] summaryRowData;
278 // Collection<FTSDataColumnI> displayFields = tdbRestRequest.getWantedFields();
279 // int colCounter = 0;
280 // summaryRowData = new Object[displayFields.size() + 1];
285 private String parseJsonExceptionString(String jsonErrorString)
287 // TODO Auto-generated method stub
292 public String getColumnDataConfigFileName()
294 return "/fts/tdbeacons_data_columns.txt";
297 public static FTSRestClientI getInstance()
299 if (instance == null)
301 instance = new TDBeaconsFTSRestClient();
306 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
308 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
310 if (allDefaultDisplayedStructureDataColumns == null
311 || allDefaultDisplayedStructureDataColumns.isEmpty())
313 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
314 allDefaultDisplayedStructureDataColumns
315 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
317 return allDefaultDisplayedStructureDataColumns;