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;
29 import java.util.Objects;
31 import javax.ws.rs.core.MediaType;
33 import org.json.simple.parser.ParseException;
35 import com.sun.jersey.api.client.Client;
36 import com.sun.jersey.api.client.ClientResponse;
37 import com.sun.jersey.api.client.WebResource;
38 import com.sun.jersey.api.client.config.DefaultClientConfig;
40 import jalview.datamodel.SequenceI;
41 import jalview.fts.api.FTSData;
42 import jalview.fts.api.FTSDataColumnI;
43 import jalview.fts.api.FTSRestClientI;
44 import jalview.fts.api.StructureFTSRestClientI;
45 import jalview.fts.core.FTSRestClient;
46 import jalview.fts.core.FTSRestRequest;
47 import jalview.fts.core.FTSRestResponse;
48 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
49 import jalview.fts.service.pdb.PDBFTSRestClient;
50 import jalview.util.JSONUtils;
51 import jalview.util.MessageManager;
52 import jalview.util.Platform;
54 public class TDBeaconsFTSRestClient extends FTSRestClient
55 implements StructureFTSRestClientI
58 * production server URI
60 private static String TDB_PROD_API = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
65 private static String TDB_DEV_API = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
67 private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API;
69 public static FTSRestClientI instance = null;
71 protected TDBeaconsFTSRestClient()
75 @SuppressWarnings("unchecked")
77 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
82 String query = tdbRestRequest.getSearchTerm();
84 Class<ClientResponse> clientResponseClass;
88 client = (Client) (Object) new jalview.javascript.web.Client();
89 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
98 client = Client.create(new DefaultClientConfig());
99 clientResponseClass = ClientResponse.class;
102 WebResource webResource;
103 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN + query);
105 URI uri = webResource.getURI();
106 System.out.println(uri.toString());
108 // Execute the REST request
109 ClientResponse clientResponse;
112 clientResponse = null;
116 clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
117 .get(clientResponseClass);
120 // Get the JSON string from the response object or directly from the
121 // client (JavaScript)
122 Map<String, Object> jsonObj = null;
123 String responseString = null;
125 // Check the response status and report exception if one occurs
126 int responseStatus = isMocked()
127 ? (mockQueries.containsKey(query) ? 200 : 404)
128 : clientResponse.getStatus();
129 switch (responseStatus)
135 jsonObj = clientResponse.getEntity(Map.class);
139 responseString = isMocked() ? mockQueries.get(query)
140 : clientResponse.getEntity(String.class);
144 throw new Exception(parseJsonExceptionString(responseString));
146 return emptyTDBeaconsJsonResponse();
149 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
151 // Process the response and return the result to the caller.
152 return parseTDBeaconsJsonResponse(responseString, jsonObj,
154 } catch (Exception e)
156 String exceptionMsg = e.getMessage();
157 if (exceptionMsg != null)
159 if (exceptionMsg.contains("SocketException"))
161 // No internet connection
162 throw new Exception(MessageManager.getString(
163 "exception.unable_to_detect_internet_connection"));
165 else if (exceptionMsg.contains("UnknownHostException"))
167 // The server is unreachable
168 throw new Exception(MessageManager.formatMessage(
169 "exception.fts_server_unreachable", "3DB Hub"));
179 * returns response for when the 3D-Beacons service doesn't have a record for
180 * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
184 private FTSRestResponse emptyTDBeaconsJsonResponse()
189 public String setSearchTerm(String term)
194 public static FTSRestResponse parseTDBeaconsJsonResponse(
195 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
197 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
198 (Map<String, Object>) null, tdbRestRequest);
201 @SuppressWarnings("unchecked")
202 public static FTSRestResponse parseTDBeaconsJsonResponse(
203 String tdbJsonResponseString, Map<String, Object> jsonObj,
204 FTSRestRequest tdbRestRequest)
206 FTSRestResponse searchResult = new FTSRestResponse();
207 List<FTSData> result = null;
213 jsonObj = (Map<String, Object>) JSONUtils
214 .parse(tdbJsonResponseString);
217 Object uniprot_entry = jsonObj.get("uniprot_entry");
218 // TODO: decide if anything from uniprot_entry needs to be reported via
219 // the FTSRestResponse object
220 // Arnaud added seqLength = (Long) ((Map<String, Object>)
221 // jsonObj.get("uniprot_entry")).get("sequence_length");
223 List<Object> structures = (List<Object>) jsonObj.get("structures");
224 result = new ArrayList<>();
227 for (Iterator<Object> strucIter = structures.iterator(); strucIter
230 Map<String, Object> structure = (Map<String, Object>) strucIter
232 result.add(getFTSData(structure, tdbRestRequest));
236 searchResult.setNumberOfItemsFound(numFound);
237 searchResult.setSearchSummary(result);
239 } catch (ParseException e)
246 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
247 FTSRestRequest tdbRequest)
249 String primaryKey = null;
250 Object[] summaryRowData;
252 SequenceI associatedSequence;
254 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
255 SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
257 summaryRowData = new Object[(associatedSeq != null)
258 ? displayFields.size() + 1
259 : displayFields.size()];
260 if (associatedSeq != null)
262 associatedSequence = associatedSeq;
263 summaryRowData[0] = associatedSequence;
267 for (FTSDataColumnI field : displayFields)
269 String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
271 : tdbJsonStructure.get(field.getCode()).toString();
272 // System.out.println("Field : " + field + " Data : " + fieldData);
273 if (field.isPrimaryKeyColumn())
275 primaryKey = fieldData;
276 summaryRowData[colCounter++] = primaryKey;
278 else if (fieldData == null || fieldData.trim().isEmpty())
280 summaryRowData[colCounter++] = null;
286 summaryRowData[colCounter++] = (field.getDataType()
287 .getDataTypeClass() == Integer.class)
288 ? Integer.valueOf(fieldData)
289 : (field.getDataType()
290 .getDataTypeClass() == Double.class)
291 ? Double.valueOf(fieldData)
293 } catch (Exception e)
295 // e.printStackTrace();
296 System.out.println("offending value:" + fieldData + fieldData);
300 final String primaryKey1 = primaryKey;
301 final Object[] summaryRowData1 = summaryRowData;
303 return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
306 // private static FTSData getFTSData(Map<String, Object> doc,
307 // FTSRestRequest tdbRestRequest)
309 // String primaryKey = null;
311 // Object[] summaryRowData;
313 // Collection<FTSDataColumnI> displayFields =
314 // tdbRestRequest.getWantedFields();
315 // int colCounter = 0;
316 // summaryRowData = new Object[displayFields.size() + 1];
321 private String parseJsonExceptionString(String jsonErrorString)
323 // TODO Auto-generated method stub
328 public String getColumnDataConfigFileName()
330 return "/fts/tdbeacons_data_columns.txt";
333 public static FTSRestClientI getInstance()
335 if (instance == null)
337 instance = new TDBeaconsFTSRestClient();
342 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
344 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
346 if (allDefaultDisplayedStructureDataColumns == null
347 || allDefaultDisplayedStructureDataColumns.isEmpty())
349 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
350 allDefaultDisplayedStructureDataColumns
351 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
353 return allDefaultDisplayedStructureDataColumns;
357 public String[] getPreferencesColumnsFor(PreferenceSource source)
359 String[] columnNames = null;
363 columnNames = new String[] { "", "Display", "Group" };
365 case STRUCTURE_CHOOSER:
366 columnNames = new String[] { "", "Display", "Group" };
369 columnNames = new String[] { "3DB Beacons Field",
370 "Show in search summary", "Show in structure summary" };