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()
45 @SuppressWarnings("unchecked")
47 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
52 String query = tdbRestRequest.getSearchTerm();
54 Class<ClientResponse> clientResponseClass;
58 client = (Client) (Object) new jalview.javascript.web.Client();
59 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
68 client = Client.create(new DefaultClientConfig());
69 clientResponseClass = ClientResponse.class;
71 WebResource webResource;
72 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
74 URI uri = webResource.getURI();
75 System.out.println(uri.toString());
77 // Execute the REST request
78 ClientResponse clientResponse = webResource
79 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
81 // Get the JSON string from the response object or directly from the
82 // client (JavaScript)
83 Map<String, Object> jsonObj = null;
84 String responseString = null;
86 // Check the response status and report exception if one occurs
87 int responseStatus = clientResponse.getStatus();
88 switch (responseStatus)
94 jsonObj = clientResponse.getEntity(Map.class);
98 responseString = clientResponse.getEntity(String.class);
102 throw new Exception(parseJsonExceptionString(responseString));
105 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
107 // Process the response and return the result to the caller.
108 return parseTDBeaconsJsonResponse(responseString, jsonObj,
110 } catch (Exception e)
112 String exceptionMsg = e.getMessage();
113 if (exceptionMsg.contains("SocketException"))
115 // No internet connection
116 throw new Exception(MessageManager.getString(
117 "exception.unable_to_detect_internet_connection"));
119 else if (exceptionMsg.contains("UnknownHostException"))
121 // The server is unreachable
122 throw new Exception(MessageManager.formatMessage(
123 "exception.fts_server_unreachable", "3DB Hub"));
133 public String setSearchTerm(String term)
138 public static FTSRestResponse parseTDBeaconsJsonResponse(
139 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
141 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
142 (Map<String, Object>) null, tdbRestRequest);
145 @SuppressWarnings("unchecked")
146 public static FTSRestResponse parseTDBeaconsJsonResponse(
147 String tdbJsonResponseString, Map<String, Object> jsonObj,
148 FTSRestRequest tdbRestRequest)
150 FTSRestResponse searchResult = new FTSRestResponse();
151 List<FTSData> result = null;
157 jsonObj = (Map<String, Object>) JSONUtils
158 .parse(tdbJsonResponseString);
161 Object uniprot_entry = jsonObj.get("uniprot_entry");
162 // TODO: decide if anything from uniprot_entry needs to be reported via
163 // the FTSRestResponse object
164 // Arnaud added seqLength = (Long) ((Map<String, Object>)
165 // jsonObj.get("uniprot_entry")).get("sequence_length");
167 List<Object> structures = (List<Object>) jsonObj.get("structures");
168 result = new ArrayList<>();
171 for (Iterator<Object> strucIter = structures.iterator(); strucIter
174 Map<String, Object> structure = (Map<String, Object>) strucIter
176 result.add(getFTSData(structure, tdbRestRequest));
180 searchResult.setNumberOfItemsFound(numFound);
181 searchResult.setSearchSummary(result);
183 } catch (ParseException e)
190 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
191 FTSRestRequest tdbRequest)
193 // TODO: consider reusing PDBFTSRestClient.getFTSData ?
195 String primaryKey = null;
196 Object[] summaryRowData;
198 SequenceI associatedSequence;
200 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
201 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
203 summaryRowData = new Object[(associatedSeq != null)
204 ? displayFields.size() + 1
205 : displayFields.size()];
206 if (associatedSeq != null)
208 associatedSequence = associatedSeq;
209 summaryRowData[0] = associatedSequence;
213 for (FTSDataColumnI field : displayFields)
215 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
217 : tdbJsonStructure.get(field.getCode()).toString();
218 // System.out.println("Field : " + field + " Data : " + fieldData);
219 if (field.isPrimaryKeyColumn())
221 primaryKey = fieldData;
222 summaryRowData[colCounter++] = primaryKey;
224 else if (fieldData == null || fieldData.trim().isEmpty())
226 summaryRowData[colCounter++] = null;
232 summaryRowData[colCounter++] = (field.getDataType()
233 .getDataTypeClass() == Integer.class)
234 ? Integer.valueOf(fieldData)
235 : (field.getDataType()
236 .getDataTypeClass() == Double.class)
237 ? Double.valueOf(fieldData)
239 } catch (Exception e)
241 // e.printStackTrace();
242 System.out.println("offending value:" + fieldData + fieldData);
246 final String primaryKey1 = primaryKey;
247 final Object[] summaryRowData1 = summaryRowData;
253 public Object[] getSummaryData()
255 return summaryRowData1;
259 public Object getPrimaryKey()
265 * Returns a string representation of this object;
268 public String toString()
270 StringBuilder summaryFieldValues = new StringBuilder();
271 for (Object summaryField : summaryRowData1)
273 summaryFieldValues.append(
274 summaryField == null ? " " : summaryField.toString())
277 return summaryFieldValues.toString();
281 * Returns hash code value for this object
284 public int hashCode()
286 return Objects.hash(primaryKey1, this.toString());
290 public boolean equals(Object that)
292 return this.toString().equals(that.toString());
297 // private static FTSData getFTSData(Map<String, Object> doc,
298 // FTSRestRequest tdbRestRequest)
300 // String primaryKey = null;
302 // Object[] summaryRowData;
304 // Collection<FTSDataColumnI> displayFields =
305 // tdbRestRequest.getWantedFields();
306 // int colCounter = 0;
307 // summaryRowData = new Object[displayFields.size() + 1];
312 private String parseJsonExceptionString(String jsonErrorString)
314 // TODO Auto-generated method stub
319 public String getColumnDataConfigFileName()
321 return "/fts/tdbeacons_data_columns.txt";
324 public static FTSRestClientI getInstance()
326 if (instance == null)
328 instance = new TDBeaconsFTSRestClient();
333 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
335 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
337 if (allDefaultDisplayedStructureDataColumns == null
338 || allDefaultDisplayedStructureDataColumns.isEmpty())
340 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
341 allDefaultDisplayedStructureDataColumns
342 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
344 return allDefaultDisplayedStructureDataColumns;
348 public String[] getPreferencesColumnsFor(PreferenceSource source)
350 String[] columnNames = null;
354 columnNames = new String[] { "", "Display", "Group" };
356 case STRUCTURE_CHOOSER:
357 columnNames = new String[] { "", "Display", "Group" };
360 columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
361 "Show in structure summary" };