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.
22 package jalview.fts.service.uniprot;
24 import jalview.bin.ApplicationSingletonProvider;
25 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
26 import jalview.bin.Cache;
27 import jalview.fts.api.FTSData;
28 import jalview.fts.api.FTSDataColumnI;
29 import jalview.fts.api.FTSRestClientI;
30 import jalview.fts.core.FTSRestClient;
31 import jalview.fts.core.FTSRestRequest;
32 import jalview.fts.core.FTSRestResponse;
33 import jalview.gui.Preferences;
34 import jalview.util.MessageManager;
35 import jalview.util.Platform;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.Objects;
42 import javax.ws.rs.core.MediaType;
44 import com.sun.jersey.api.client.Client;
45 import com.sun.jersey.api.client.ClientResponse;
46 import com.sun.jersey.api.client.WebResource;
47 import com.sun.jersey.api.client.config.DefaultClientConfig;
49 public class UniProtFTSRestClient extends FTSRestClient
50 implements ApplicationSingletonI
53 public static FTSRestClientI getInstance()
55 return (FTSRestClientI) ApplicationSingletonProvider
56 .getInstance(UniProtFTSRestClient.class);
59 private UniProtFTSRestClient()
61 // singleton -- use getInstance()
63 uniprotSearchEndpoint = Cache.getDefault(Preferences.UNIPROT_DOMAIN,
64 DEFAULT_UNIPROT_DOMAIN) + "/uniprot/";
67 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
69 public final String uniprotSearchEndpoint;
73 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
75 @SuppressWarnings("unchecked")
77 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
82 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
83 uniprotRestRequest.getWantedFields());
84 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
85 ? getDefaultResponsePageSize()
86 : uniprotRestRequest.getResponseSize();
88 int offSet = uniprotRestRequest.getOffSet();
90 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
92 query = uniprotRestRequest.getSearchTerm();
96 query = uniprotRestRequest.getFieldToSearchBy()
97 .equalsIgnoreCase("Search All")
98 ? uniprotRestRequest.getSearchTerm()
100 + uniprotRestRequest.getSearchTerm()
101 : uniprotRestRequest.getFieldToSearchBy() + ":"
102 + uniprotRestRequest.getSearchTerm();
105 // BH 2018 the trick here is to coerce the classes in Javascript to be
106 // different from the ones in Java yet still allow this to be correct for
109 Class<ClientResponse> clientResponseClass;
112 // JavaScript only -- coerce types to Java types for Java
113 client = (Client) (Object) new jalview.javascript.web.Client();
114 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
124 client = Client.create(new DefaultClientConfig());
125 clientResponseClass = ClientResponse.class;
128 WebResource webResource = null;
129 webResource = client.resource(uniprotSearchEndpoint)
130 .queryParam("format", "tab")
131 .queryParam("columns", wantedFields)
132 .queryParam("limit", String.valueOf(responseSize))
133 .queryParam("offset", String.valueOf(offSet))
134 .queryParam("sort", "score").queryParam("query", query);
135 // Execute the REST request
136 ClientResponse clientResponse = webResource
137 .accept(MediaType.TEXT_PLAIN).get(clientResponseClass);
138 String uniProtTabDelimittedResponseString = clientResponse
139 .getEntity(String.class);
140 // Make redundant objects eligible for garbage collection to conserve
142 // System.out.println(">>>>> response : "
143 // + uniProtTabDelimittedResponseString);
144 if (clientResponse.getStatus() != 200)
146 String errorMessage = getMessageByHTTPStatusCode(
147 clientResponse.getStatus(), "Uniprot");
148 throw new Exception(errorMessage);
151 int xTotalResults = Platform.isJS() ? 1
152 : Integer.valueOf(clientResponse.getHeaders()
153 .get("X-Total-Results").get(0));
154 clientResponse = null;
156 return parseUniprotResponse(uniProtTabDelimittedResponseString,
157 uniprotRestRequest, xTotalResults);
158 } catch (Exception e)
160 String exceptionMsg = e.getMessage();
161 if (exceptionMsg.contains("SocketException"))
163 // No internet connection
164 throw new Exception(MessageManager.getString(
165 "exception.unable_to_detect_internet_connection"));
167 else if (exceptionMsg.contains("UnknownHostException"))
169 // The server 'http://www.uniprot.org' is unreachable
170 throw new Exception(MessageManager.formatMessage(
171 "exception.fts_server_unreachable", "Uniprot"));
180 public boolean isAdvancedQuery(String query)
182 if (query.contains(" AND ") || query.contains(" OR ")
183 || query.contains(" NOT ") || query.contains(" ! ")
184 || query.contains(" || ") || query.contains(" && ")
185 || query.contains(":") || query.contains("-"))
192 public FTSRestResponse parseUniprotResponse(
193 String uniProtTabDelimittedResponseString,
194 FTSRestRequest uniprotRestRequest, int xTotalResults)
196 FTSRestResponse searchResult = new FTSRestResponse();
197 List<FTSData> result = null;
198 if (uniProtTabDelimittedResponseString == null
199 || uniProtTabDelimittedResponseString.trim().isEmpty())
201 searchResult.setNumberOfItemsFound(0);
204 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
205 if (foundDataRow != null && foundDataRow.length > 0)
207 result = new ArrayList<>();
208 boolean firstRow = true;
209 for (String dataRow : foundDataRow)
211 // The first data row is usually the header data. This should be
212 // filtered out from the rest of the data See: JAL-2485
218 // System.out.println(dataRow);
219 result.add(getFTSData(dataRow, uniprotRestRequest));
221 searchResult.setNumberOfItemsFound(xTotalResults);
222 searchResult.setSearchSummary(result);
228 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
230 // * tab delimited string.
232 // * @param dataColumnFields
233 // * the collection of FTSDataColumnI to process
234 // * @return the generated comma delimited string from the supplied
235 // * FTSDataColumnI collection
237 // private String getDataColumnsFieldsAsTabDelimitedString(
238 // Collection<FTSDataColumnI> dataColumnFields)
240 // String result = "";
241 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
243 // StringBuilder returnedFields = new StringBuilder();
244 // for (FTSDataColumnI field : dataColumnFields)
246 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
248 // returnedFields.append("\t").append("Entry");
252 // returnedFields.append("\t").append(field.getName());
255 // returnedFields.deleteCharAt(0);
256 // result = returnedFields.toString();
261 public static FTSData getFTSData(String tabDelimittedDataStr,
262 FTSRestRequest request)
264 String primaryKey = null;
266 Object[] summaryRowData;
268 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
270 summaryRowData = new Object[diplayFields.size()];
271 String[] columns = tabDelimittedDataStr.split("\t");
272 for (FTSDataColumnI field : diplayFields)
276 String fieldData = columns[colCounter];
277 if (field.isPrimaryKeyColumn())
279 primaryKey = fieldData;
280 summaryRowData[colCounter++] = primaryKey;
282 else if (fieldData == null || fieldData.isEmpty())
284 summaryRowData[colCounter++] = null;
290 summaryRowData[colCounter++] = (field.getDataType()
291 .getDataTypeClass() == Integer.class)
292 ? Integer.valueOf(fieldData.replace(",", ""))
293 : (field.getDataType()
294 .getDataTypeClass() == Double.class)
295 ? Double.valueOf(fieldData)
297 } catch (Exception e)
300 System.out.println("offending value:" + fieldData);
303 } catch (Exception e)
305 // e.printStackTrace();
309 final String primaryKey1 = primaryKey;
311 final Object[] summaryRowData1 = summaryRowData;
315 public Object[] getSummaryData()
317 return summaryRowData1;
321 public Object getPrimaryKey()
327 * Returns a string representation of this object;
330 public String toString()
332 StringBuilder summaryFieldValues = new StringBuilder();
333 for (Object summaryField : summaryRowData1)
335 summaryFieldValues.append(
336 summaryField == null ? " " : summaryField.toString())
339 return summaryFieldValues.toString();
343 * Returns hash code value for this object
346 public int hashCode()
348 return Objects.hash(primaryKey1, this.toString());
352 public boolean equals(Object that)
354 return this.toString().equals(that.toString());
360 public String getColumnDataConfigFileName()
362 return "/fts/uniprot_data_columns.txt";