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.Cache;
25 import jalview.fts.api.FTSData;
26 import jalview.fts.api.FTSDataColumnI;
27 import jalview.fts.api.FTSRestClientI;
28 import jalview.fts.core.FTSRestClient;
29 import jalview.fts.core.FTSRestRequest;
30 import jalview.fts.core.FTSRestResponse;
31 import jalview.util.MessageManager;
32 import jalview.util.Platform;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.Objects;
39 import javax.ws.rs.core.MediaType;
41 import com.sun.jersey.api.client.Client;
42 import com.sun.jersey.api.client.ClientResponse;
43 import com.sun.jersey.api.client.WebResource;
44 import com.sun.jersey.api.client.config.DefaultClientConfig;
46 public class UniProtFTSRestClient extends FTSRestClient
48 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
52 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
55 private static FTSRestClientI instance = null;
57 public static FTSRestClientI getInstance()
60 // BH 2019.05.08 need to isolate static fields in JavaScript
62 FTSRestClientI i = instance;
63 @SuppressWarnings("unused")
67 g = Thread.currentThread().getThreadGroup();
69 * @j2sNative i = g._jalviewUniProtFTSRestClientInstance;
75 i = new UniProtFTSRestClient();
80 * @j2sNative g._jalviewUniProtFTSRestClientInstance = i;
92 public final String uniprotSearchEndpoint;
94 public UniProtFTSRestClient()
97 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_DOMAIN",
98 DEFAULT_UNIPROT_DOMAIN) + "/uniprot/";
101 @SuppressWarnings("unchecked")
103 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
108 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
109 uniprotRestRequest.getWantedFields());
110 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
111 ? getDefaultResponsePageSize()
112 : uniprotRestRequest.getResponseSize();
114 int offSet = uniprotRestRequest.getOffSet();
116 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
118 query = uniprotRestRequest.getSearchTerm();
122 query = uniprotRestRequest.getFieldToSearchBy()
123 .equalsIgnoreCase("Search All")
124 ? uniprotRestRequest.getSearchTerm()
126 + uniprotRestRequest.getSearchTerm()
127 : uniprotRestRequest.getFieldToSearchBy() + ":"
128 + uniprotRestRequest.getSearchTerm();
131 // BH 2018 the trick here is to coerce the classes in Javascript to be
132 // different from the ones in Java yet still allow this to be correct for
135 Class<ClientResponse> clientResponseClass;
138 // JavaScript only -- coerce types to Java types for Java
139 client = (Client) (Object) new jalview.javascript.web.Client();
140 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
150 client = Client.create(new DefaultClientConfig());
151 clientResponseClass = ClientResponse.class;
154 WebResource webResource = null;
155 webResource = client.resource(uniprotSearchEndpoint)
156 .queryParam("format", "tab")
157 .queryParam("columns", wantedFields)
158 .queryParam("limit", String.valueOf(responseSize))
159 .queryParam("offset", String.valueOf(offSet))
160 .queryParam("sort", "score").queryParam("query", query);
161 // Execute the REST request
162 ClientResponse clientResponse = webResource
163 .accept(MediaType.TEXT_PLAIN).get(clientResponseClass);
164 String uniProtTabDelimittedResponseString = clientResponse
165 .getEntity(String.class);
166 // Make redundant objects eligible for garbage collection to conserve
168 // System.out.println(">>>>> response : "
169 // + uniProtTabDelimittedResponseString);
170 if (clientResponse.getStatus() != 200)
172 String errorMessage = getMessageByHTTPStatusCode(
173 clientResponse.getStatus(), "Uniprot");
174 throw new Exception(errorMessage);
177 int xTotalResults = Platform.isJS() ? 1
178 : Integer.valueOf(clientResponse.getHeaders()
179 .get("X-Total-Results").get(0));
180 clientResponse = null;
182 return parseUniprotResponse(uniProtTabDelimittedResponseString,
183 uniprotRestRequest, xTotalResults);
184 } catch (Exception e)
186 String exceptionMsg = e.getMessage();
187 if (exceptionMsg.contains("SocketException"))
189 // No internet connection
190 throw new Exception(MessageManager.getString(
191 "exception.unable_to_detect_internet_connection"));
193 else if (exceptionMsg.contains("UnknownHostException"))
195 // The server 'http://www.uniprot.org' is unreachable
196 throw new Exception(MessageManager.formatMessage(
197 "exception.fts_server_unreachable", "Uniprot"));
206 public boolean isAdvancedQuery(String query)
208 if (query.contains(" AND ") || query.contains(" OR ")
209 || query.contains(" NOT ") || query.contains(" ! ")
210 || query.contains(" || ") || query.contains(" && ")
211 || query.contains(":") || query.contains("-"))
218 public FTSRestResponse parseUniprotResponse(
219 String uniProtTabDelimittedResponseString,
220 FTSRestRequest uniprotRestRequest, int xTotalResults)
222 FTSRestResponse searchResult = new FTSRestResponse();
223 List<FTSData> result = null;
224 if (uniProtTabDelimittedResponseString == null
225 || uniProtTabDelimittedResponseString.trim().isEmpty())
227 searchResult.setNumberOfItemsFound(0);
230 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
231 if (foundDataRow != null && foundDataRow.length > 0)
233 result = new ArrayList<>();
234 boolean firstRow = true;
235 for (String dataRow : foundDataRow)
237 // The first data row is usually the header data. This should be
238 // filtered out from the rest of the data See: JAL-2485
244 // System.out.println(dataRow);
245 result.add(getFTSData(dataRow, uniprotRestRequest));
247 searchResult.setNumberOfItemsFound(xTotalResults);
248 searchResult.setSearchSummary(result);
254 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
256 // * tab delimited string.
258 // * @param dataColumnFields
259 // * the collection of FTSDataColumnI to process
260 // * @return the generated comma delimited string from the supplied
261 // * FTSDataColumnI collection
263 // private String getDataColumnsFieldsAsTabDelimitedString(
264 // Collection<FTSDataColumnI> dataColumnFields)
266 // String result = "";
267 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
269 // StringBuilder returnedFields = new StringBuilder();
270 // for (FTSDataColumnI field : dataColumnFields)
272 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
274 // returnedFields.append("\t").append("Entry");
278 // returnedFields.append("\t").append(field.getName());
281 // returnedFields.deleteCharAt(0);
282 // result = returnedFields.toString();
287 public static FTSData getFTSData(String tabDelimittedDataStr,
288 FTSRestRequest request)
290 String primaryKey = null;
292 Object[] summaryRowData;
294 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
296 summaryRowData = new Object[diplayFields.size()];
297 String[] columns = tabDelimittedDataStr.split("\t");
298 for (FTSDataColumnI field : diplayFields)
302 String fieldData = columns[colCounter];
303 if (field.isPrimaryKeyColumn())
305 primaryKey = fieldData;
306 summaryRowData[colCounter++] = primaryKey;
308 else if (fieldData == null || fieldData.isEmpty())
310 summaryRowData[colCounter++] = null;
316 summaryRowData[colCounter++] = (field.getDataType()
317 .getDataTypeClass() == Integer.class)
318 ? Integer.valueOf(fieldData.replace(",", ""))
319 : (field.getDataType()
320 .getDataTypeClass() == Double.class)
321 ? Double.valueOf(fieldData)
323 } catch (Exception e)
326 System.out.println("offending value:" + fieldData);
329 } catch (Exception e)
331 // e.printStackTrace();
335 final String primaryKey1 = primaryKey;
337 final Object[] summaryRowData1 = summaryRowData;
341 public Object[] getSummaryData()
343 return summaryRowData1;
347 public Object getPrimaryKey()
353 * Returns a string representation of this object;
356 public String toString()
358 StringBuilder summaryFieldValues = new StringBuilder();
359 for (Object summaryField : summaryRowData1)
361 summaryFieldValues.append(
362 summaryField == null ? " " : summaryField.toString())
365 return summaryFieldValues.toString();
369 * Returns hash code value for this object
372 public int hashCode()
374 return Objects.hash(primaryKey1, this.toString());
378 public boolean equals(Object that)
380 return this.toString().equals(that.toString());
386 public String getColumnDataConfigFileName()
388 return "/fts/uniprot_data_columns.txt";