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";
51 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
54 private static FTSRestClientI instance = null;
56 public final String uniprotSearchEndpoint;
58 public UniProtFTSRestClient()
61 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_DOMAIN",
62 DEFAULT_UNIPROT_DOMAIN) + "/uniprot/";
65 @SuppressWarnings("unchecked")
67 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
72 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
73 uniprotRestRequest.getWantedFields());
74 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
75 ? getDefaultResponsePageSize()
76 : uniprotRestRequest.getResponseSize();
78 int offSet = uniprotRestRequest.getOffSet();
80 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
82 query = uniprotRestRequest.getSearchTerm();
86 query = uniprotRestRequest.getFieldToSearchBy()
87 .equalsIgnoreCase("Search All")
88 ? uniprotRestRequest.getSearchTerm()
90 + uniprotRestRequest.getSearchTerm()
91 : uniprotRestRequest.getFieldToSearchBy() + ":"
92 + uniprotRestRequest.getSearchTerm();
95 // BH 2018 the trick here is to coerce the classes in Javascript to be
96 // different from the ones in Java yet still allow this to be correct for
99 Class<ClientResponse> clientResponseClass;
102 // JavaScript only -- coerce types to Java types for Java
103 client = (Client) (Object) new jalview.javascript.web.Client();
104 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
114 client = Client.create(new DefaultClientConfig());
115 clientResponseClass = ClientResponse.class;
118 WebResource webResource = null;
119 webResource = client.resource(uniprotSearchEndpoint)
120 .queryParam("format", "tab")
121 .queryParam("columns", wantedFields)
122 .queryParam("limit", String.valueOf(responseSize))
123 .queryParam("offset", String.valueOf(offSet))
124 .queryParam("sort", "score").queryParam("query", query);
125 // Execute the REST request
126 ClientResponse clientResponse = webResource
127 .accept(MediaType.TEXT_PLAIN).get(clientResponseClass);
128 String uniProtTabDelimittedResponseString = clientResponse
129 .getEntity(String.class);
130 // Make redundant objects eligible for garbage collection to conserve
132 // System.out.println(">>>>> response : "
133 // + uniProtTabDelimittedResponseString);
134 if (clientResponse.getStatus() != 200)
136 String errorMessage = getMessageByHTTPStatusCode(
137 clientResponse.getStatus(), "Uniprot");
138 throw new Exception(errorMessage);
141 int xTotalResults = Platform.isJS() ? 1
142 : Integer.valueOf(clientResponse.getHeaders()
143 .get("X-Total-Results").get(0));
144 clientResponse = null;
146 return parseUniprotResponse(uniProtTabDelimittedResponseString,
147 uniprotRestRequest, xTotalResults);
148 } catch (Exception e)
150 String exceptionMsg = e.getMessage();
151 if (exceptionMsg.contains("SocketException"))
153 // No internet connection
154 throw new Exception(MessageManager.getString(
155 "exception.unable_to_detect_internet_connection"));
157 else if (exceptionMsg.contains("UnknownHostException"))
159 // The server 'http://www.uniprot.org' is unreachable
160 throw new Exception(MessageManager.formatMessage(
161 "exception.fts_server_unreachable", "Uniprot"));
170 public boolean isAdvancedQuery(String query)
172 if (query.contains(" AND ") || query.contains(" OR ")
173 || query.contains(" NOT ") || query.contains(" ! ")
174 || query.contains(" || ") || query.contains(" && ")
175 || query.contains(":") || query.contains("-"))
182 public FTSRestResponse parseUniprotResponse(
183 String uniProtTabDelimittedResponseString,
184 FTSRestRequest uniprotRestRequest, int xTotalResults)
186 FTSRestResponse searchResult = new FTSRestResponse();
187 List<FTSData> result = null;
188 if (uniProtTabDelimittedResponseString == null
189 || uniProtTabDelimittedResponseString.trim().isEmpty())
191 searchResult.setNumberOfItemsFound(0);
194 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
195 if (foundDataRow != null && foundDataRow.length > 0)
197 result = new ArrayList<>();
198 boolean firstRow = true;
199 for (String dataRow : foundDataRow)
201 // The first data row is usually the header data. This should be
202 // filtered out from the rest of the data See: JAL-2485
208 // System.out.println(dataRow);
209 result.add(getFTSData(dataRow, uniprotRestRequest));
211 searchResult.setNumberOfItemsFound(xTotalResults);
212 searchResult.setSearchSummary(result);
218 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
220 // * tab delimited string.
222 // * @param dataColumnFields
223 // * the collection of FTSDataColumnI to process
224 // * @return the generated comma delimited string from the supplied
225 // * FTSDataColumnI collection
227 // private String getDataColumnsFieldsAsTabDelimitedString(
228 // Collection<FTSDataColumnI> dataColumnFields)
230 // String result = "";
231 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
233 // StringBuilder returnedFields = new StringBuilder();
234 // for (FTSDataColumnI field : dataColumnFields)
236 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
238 // returnedFields.append("\t").append("Entry");
242 // returnedFields.append("\t").append(field.getName());
245 // returnedFields.deleteCharAt(0);
246 // result = returnedFields.toString();
251 public static FTSData getFTSData(String tabDelimittedDataStr,
252 FTSRestRequest request)
254 String primaryKey = null;
256 Object[] summaryRowData;
258 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
260 summaryRowData = new Object[diplayFields.size()];
261 String[] columns = tabDelimittedDataStr.split("\t");
262 for (FTSDataColumnI field : diplayFields)
266 String fieldData = columns[colCounter];
267 if (field.isPrimaryKeyColumn())
269 primaryKey = fieldData;
270 summaryRowData[colCounter++] = primaryKey;
272 else if (fieldData == null || fieldData.isEmpty())
274 summaryRowData[colCounter++] = null;
280 summaryRowData[colCounter++] = (field.getDataType()
281 .getDataTypeClass() == Integer.class)
282 ? Integer.valueOf(fieldData.replace(",", ""))
283 : (field.getDataType()
284 .getDataTypeClass() == Double.class)
285 ? Double.valueOf(fieldData)
287 } catch (Exception e)
290 System.out.println("offending value:" + fieldData);
293 } catch (Exception e)
295 // e.printStackTrace();
299 final String primaryKey1 = primaryKey;
301 final Object[] summaryRowData1 = summaryRowData;
305 public Object[] getSummaryData()
307 return summaryRowData1;
311 public Object getPrimaryKey()
317 * Returns a string representation of this object;
320 public String toString()
322 StringBuilder summaryFieldValues = new StringBuilder();
323 for (Object summaryField : summaryRowData1)
325 summaryFieldValues.append(
326 summaryField == null ? " " : summaryField.toString())
329 return summaryFieldValues.toString();
333 * Returns hash code value for this object
336 public int hashCode()
338 return Objects.hash(primaryKey1, this.toString());
342 public boolean equals(Object that)
344 return this.toString().equals(that.toString());
349 public static FTSRestClientI getInstance()
351 if (instance == null)
353 instance = new UniProtFTSRestClient();
359 public String getColumnDataConfigFileName()
361 return "/fts/uniprot_data_columns.txt";