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.bin.Jalview;
26 import jalview.fts.api.FTSData;
27 import jalview.fts.api.FTSDataColumnI;
28 import jalview.fts.api.FTSRestClientI;
29 import jalview.fts.core.FTSRestClient;
30 import jalview.fts.core.FTSRestRequest;
31 import jalview.fts.core.FTSRestResponse;
32 import jalview.util.MessageManager;
33 import jalview.util.Platform;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.Objects;
40 import javax.ws.rs.core.MediaType;
42 import com.sun.jersey.api.client.Client;
43 import com.sun.jersey.api.client.ClientResponse;
44 import com.sun.jersey.api.client.WebResource;
45 import com.sun.jersey.api.client.config.DefaultClientConfig;
47 public class UniProtFTSRestClient extends FTSRestClient
49 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
53 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
56 public static FTSRestClientI getInstance()
58 Jalview j = Jalview.getInstance();
59 return (j.uniprotFTSRestClient == null ? j.uniprotFTSRestClient = new UniProtFTSRestClient()
60 : j.uniprotFTSRestClient);
63 public final String uniprotSearchEndpoint;
65 public UniProtFTSRestClient()
68 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_DOMAIN",
69 DEFAULT_UNIPROT_DOMAIN) + "/uniprot/";
72 @SuppressWarnings("unchecked")
74 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
79 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
80 uniprotRestRequest.getWantedFields());
81 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
82 ? getDefaultResponsePageSize()
83 : uniprotRestRequest.getResponseSize();
85 int offSet = uniprotRestRequest.getOffSet();
87 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
89 query = uniprotRestRequest.getSearchTerm();
93 query = uniprotRestRequest.getFieldToSearchBy()
94 .equalsIgnoreCase("Search All")
95 ? uniprotRestRequest.getSearchTerm()
97 + uniprotRestRequest.getSearchTerm()
98 : uniprotRestRequest.getFieldToSearchBy() + ":"
99 + uniprotRestRequest.getSearchTerm();
102 // BH 2018 the trick here is to coerce the classes in Javascript to be
103 // different from the ones in Java yet still allow this to be correct for
106 Class<ClientResponse> clientResponseClass;
109 // JavaScript only -- coerce types to Java types for Java
110 client = (Client) (Object) new jalview.javascript.web.Client();
111 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
121 client = Client.create(new DefaultClientConfig());
122 clientResponseClass = ClientResponse.class;
125 WebResource webResource = null;
126 webResource = client.resource(uniprotSearchEndpoint)
127 .queryParam("format", "tab")
128 .queryParam("columns", wantedFields)
129 .queryParam("limit", String.valueOf(responseSize))
130 .queryParam("offset", String.valueOf(offSet))
131 .queryParam("sort", "score").queryParam("query", query);
132 // Execute the REST request
133 ClientResponse clientResponse = webResource
134 .accept(MediaType.TEXT_PLAIN).get(clientResponseClass);
135 String uniProtTabDelimittedResponseString = clientResponse
136 .getEntity(String.class);
137 // Make redundant objects eligible for garbage collection to conserve
139 // System.out.println(">>>>> response : "
140 // + uniProtTabDelimittedResponseString);
141 if (clientResponse.getStatus() != 200)
143 String errorMessage = getMessageByHTTPStatusCode(
144 clientResponse.getStatus(), "Uniprot");
145 throw new Exception(errorMessage);
148 int xTotalResults = Platform.isJS() ? 1
149 : Integer.valueOf(clientResponse.getHeaders()
150 .get("X-Total-Results").get(0));
151 clientResponse = null;
153 return parseUniprotResponse(uniProtTabDelimittedResponseString,
154 uniprotRestRequest, xTotalResults);
155 } catch (Exception e)
157 String exceptionMsg = e.getMessage();
158 if (exceptionMsg.contains("SocketException"))
160 // No internet connection
161 throw new Exception(MessageManager.getString(
162 "exception.unable_to_detect_internet_connection"));
164 else if (exceptionMsg.contains("UnknownHostException"))
166 // The server 'http://www.uniprot.org' is unreachable
167 throw new Exception(MessageManager.formatMessage(
168 "exception.fts_server_unreachable", "Uniprot"));
177 public boolean isAdvancedQuery(String query)
179 if (query.contains(" AND ") || query.contains(" OR ")
180 || query.contains(" NOT ") || query.contains(" ! ")
181 || query.contains(" || ") || query.contains(" && ")
182 || query.contains(":") || query.contains("-"))
189 public FTSRestResponse parseUniprotResponse(
190 String uniProtTabDelimittedResponseString,
191 FTSRestRequest uniprotRestRequest, int xTotalResults)
193 FTSRestResponse searchResult = new FTSRestResponse();
194 List<FTSData> result = null;
195 if (uniProtTabDelimittedResponseString == null
196 || uniProtTabDelimittedResponseString.trim().isEmpty())
198 searchResult.setNumberOfItemsFound(0);
201 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
202 if (foundDataRow != null && foundDataRow.length > 0)
204 result = new ArrayList<>();
205 boolean firstRow = true;
206 for (String dataRow : foundDataRow)
208 // The first data row is usually the header data. This should be
209 // filtered out from the rest of the data See: JAL-2485
215 // System.out.println(dataRow);
216 result.add(getFTSData(dataRow, uniprotRestRequest));
218 searchResult.setNumberOfItemsFound(xTotalResults);
219 searchResult.setSearchSummary(result);
225 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
227 // * tab delimited string.
229 // * @param dataColumnFields
230 // * the collection of FTSDataColumnI to process
231 // * @return the generated comma delimited string from the supplied
232 // * FTSDataColumnI collection
234 // private String getDataColumnsFieldsAsTabDelimitedString(
235 // Collection<FTSDataColumnI> dataColumnFields)
237 // String result = "";
238 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
240 // StringBuilder returnedFields = new StringBuilder();
241 // for (FTSDataColumnI field : dataColumnFields)
243 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
245 // returnedFields.append("\t").append("Entry");
249 // returnedFields.append("\t").append(field.getName());
252 // returnedFields.deleteCharAt(0);
253 // result = returnedFields.toString();
258 public static FTSData getFTSData(String tabDelimittedDataStr,
259 FTSRestRequest request)
261 String primaryKey = null;
263 Object[] summaryRowData;
265 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
267 summaryRowData = new Object[diplayFields.size()];
268 String[] columns = tabDelimittedDataStr.split("\t");
269 for (FTSDataColumnI field : diplayFields)
273 String fieldData = columns[colCounter];
274 if (field.isPrimaryKeyColumn())
276 primaryKey = fieldData;
277 summaryRowData[colCounter++] = primaryKey;
279 else if (fieldData == null || fieldData.isEmpty())
281 summaryRowData[colCounter++] = null;
287 summaryRowData[colCounter++] = (field.getDataType()
288 .getDataTypeClass() == Integer.class)
289 ? Integer.valueOf(fieldData.replace(",", ""))
290 : (field.getDataType()
291 .getDataTypeClass() == Double.class)
292 ? Double.valueOf(fieldData)
294 } catch (Exception e)
297 System.out.println("offending value:" + fieldData);
300 } catch (Exception e)
302 // e.printStackTrace();
306 final String primaryKey1 = primaryKey;
308 final Object[] summaryRowData1 = summaryRowData;
312 public Object[] getSummaryData()
314 return summaryRowData1;
318 public Object getPrimaryKey()
324 * Returns a string representation of this object;
327 public String toString()
329 StringBuilder summaryFieldValues = new StringBuilder();
330 for (Object summaryField : summaryRowData1)
332 summaryFieldValues.append(
333 summaryField == null ? " " : summaryField.toString())
336 return summaryFieldValues.toString();
340 * Returns hash code value for this object
343 public int hashCode()
345 return Objects.hash(primaryKey1, this.toString());
349 public boolean equals(Object that)
351 return this.toString().equals(that.toString());
357 public String getColumnDataConfigFileName()
359 return "/fts/uniprot_data_columns.txt";