2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.fts.service.threedbeacons;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
32 import org.json.simple.parser.ParseException;
34 import com.sun.jersey.api.client.Client;
35 import com.sun.jersey.api.client.ClientResponse;
36 import com.sun.jersey.api.client.WebResource;
37 import com.sun.jersey.api.client.config.DefaultClientConfig;
39 import jalview.datamodel.SequenceI;
40 import jalview.fts.api.FTSData;
41 import jalview.fts.api.FTSDataColumnI;
42 import jalview.fts.api.FTSRestClientI;
43 import jalview.fts.api.StructureFTSRestClientI;
44 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
45 import jalview.fts.core.FTSRestClient;
46 import jalview.fts.core.FTSRestRequest;
47 import jalview.fts.core.FTSRestResponse;
48 import jalview.util.JSONUtils;
49 import jalview.util.MessageManager;
50 import jalview.util.Platform;
52 public class TDBeaconsFTSRestClient extends FTSRestClient
53 implements StructureFTSRestClientI
56 * production server URI
58 private static String TDB_PROD_API = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
63 private static String TDB_DEV_API = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
65 private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API;
67 public static FTSRestClientI instance = null;
69 protected TDBeaconsFTSRestClient()
73 @SuppressWarnings("unchecked")
75 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
80 String query = tdbRestRequest.getSearchTerm();
82 Class<ClientResponse> clientResponseClass;
86 client = (Client) (Object) new jalview.javascript.web.Client();
87 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
96 client = Client.create(new DefaultClientConfig());
97 clientResponseClass = ClientResponse.class;
100 WebResource webResource;
101 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN + query);
103 URI uri = webResource.getURI();
104 jalview.bin.Console.outPrintln(uri.toString());
106 // Execute the REST request
107 ClientResponse clientResponse;
110 clientResponse = null;
114 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
115 .get(clientResponseClass);
118 // Get the JSON string from the response object or directly from the
119 // client (JavaScript)
120 Map<String, Object> jsonObj = null;
121 String responseString = null;
123 // Check the response status and report exception if one occurs
124 int responseStatus = isMocked()
125 ? (mockQueries.containsKey(query) ? 200 : 404)
126 : clientResponse.getStatus();
127 switch (responseStatus)
133 jsonObj = clientResponse.getEntity(Map.class);
137 responseString = isMocked() ? mockQueries.get(query)
138 : clientResponse.getEntity(String.class);
142 throw new Exception(parseJsonExceptionString(responseString));
144 return emptyTDBeaconsJsonResponse();
147 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
149 // Process the response and return the result to the caller.
150 return parseTDBeaconsJsonResponse(responseString, jsonObj,
152 } catch (Exception e)
154 String exceptionMsg = e.getMessage();
155 if (exceptionMsg != null)
157 if (exceptionMsg.contains("SocketException"))
159 // No internet connection
160 throw new Exception(MessageManager.getString(
161 "exception.unable_to_detect_internet_connection"));
163 else if (exceptionMsg.contains("UnknownHostException"))
165 // The server is unreachable
166 throw new Exception(MessageManager.formatMessage(
167 "exception.fts_server_unreachable", "3DB Hub"));
177 * returns response for when the 3D-Beacons service doesn't have a record for
178 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
182 private FTSRestResponse emptyTDBeaconsJsonResponse()
187 public String setSearchTerm(String term)
192 public static FTSRestResponse parseTDBeaconsJsonResponse(
193 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
195 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
196 (Map<String, Object>) null, tdbRestRequest);
199 @SuppressWarnings("unchecked")
200 public static FTSRestResponse parseTDBeaconsJsonResponse(
201 String tdbJsonResponseString, Map<String, Object> jsonObj,
202 FTSRestRequest tdbRestRequest)
204 FTSRestResponse searchResult = new FTSRestResponse();
205 List<FTSData> result = null;
211 jsonObj = (Map<String, Object>) JSONUtils
212 .parse(tdbJsonResponseString);
215 Object uniprot_entry = jsonObj.get("uniprot_entry");
216 // TODO: decide if anything from uniprot_entry needs to be reported via
217 // the FTSRestResponse object
218 // Arnaud added seqLength = (Long) ((Map<String, Object>)
219 // jsonObj.get("uniprot_entry")).get("sequence_length");
221 List<Object> structures = (List<Object>) jsonObj.get("structures");
222 result = new ArrayList<>();
225 for (Iterator<Object> strucIter = structures.iterator(); strucIter
228 Map<String, Object> structure = (Map<String, Object>) strucIter
230 result.add(getFTSData(structure, tdbRestRequest));
234 searchResult.setNumberOfItemsFound(numFound);
235 searchResult.setSearchSummary(result);
237 } catch (ParseException e)
244 private static FTSData getFTSData(
245 Map<String, Object> tdbJsonStructureSummary,
246 FTSRestRequest tdbRequest)
248 String primaryKey = null;
249 Object[] summaryRowData;
251 SequenceI associatedSequence;
253 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
254 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
256 summaryRowData = new Object[(associatedSeq != null)
257 ? displayFields.size() + 1
258 : displayFields.size()];
259 if (associatedSeq != null)
261 associatedSequence = associatedSeq;
262 summaryRowData[0] = associatedSequence;
265 Map<String, Object> tdbJsonStructure = (Map<String, Object>) tdbJsonStructureSummary
267 for (FTSDataColumnI field : displayFields)
269 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
271 : tdbJsonStructure.get(field.getCode()).toString();
272 // jalview.bin.Console.outPrintln("Field : " + field + " Data : " +
274 if (field.isPrimaryKeyColumn())
276 primaryKey = fieldData;
277 summaryRowData[colCounter++] = primaryKey;
279 else if (fieldData == null || fieldData.trim().isEmpty())
281 summaryRowData[colCounter++] = null;
287 summaryRowData[colCounter++] = (field.getDataType()
288 .getDataTypeClass() == Integer.class)
289 ? Integer.valueOf(fieldData)
290 : (field.getDataType()
291 .getDataTypeClass() == Double.class)
292 ? Double.valueOf(fieldData)
294 } catch (Exception e)
296 // e.printStackTrace();
298 .outPrintln("offending value:" + fieldData + fieldData);
302 final String primaryKey1 = primaryKey;
303 final Object[] summaryRowData1 = summaryRowData;
305 return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
308 // private static FTSData getFTSData(Map<String, Object> doc,
309 // FTSRestRequest tdbRestRequest)
311 // String primaryKey = null;
313 // Object[] summaryRowData;
315 // Collection<FTSDataColumnI> displayFields =
316 // tdbRestRequest.getWantedFields();
317 // int colCounter = 0;
318 // summaryRowData = new Object[displayFields.size() + 1];
323 private String parseJsonExceptionString(String jsonErrorString)
325 // TODO Auto-generated method stub
330 public String getColumnDataConfigFileName()
332 return "/fts/tdbeacons_data_columns.txt";
335 public static FTSRestClientI getInstance()
337 if (instance == null)
339 instance = new TDBeaconsFTSRestClient();
344 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
347 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
349 if (allDefaultDisplayedStructureDataColumns == null
350 || allDefaultDisplayedStructureDataColumns.isEmpty())
352 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
353 allDefaultDisplayedStructureDataColumns
354 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
356 return allDefaultDisplayedStructureDataColumns;
360 public String[] getPreferencesColumnsFor(PreferenceSource source)
362 String[] columnNames = null;
366 columnNames = new String[] { "", "Display", "Group" };
368 case STRUCTURE_CHOOSER:
369 columnNames = new String[] { "", "Display", "Group" };
372 columnNames = new String[] { "3DB Beacons Field",
373 "Show in search summary", "Show in structure summary" };