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.util.MessageManager;
34 import jalview.util.Platform;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
39 import java.util.Objects;
41 import javax.ws.rs.core.MediaType;
43 import com.sun.jersey.api.client.Client;
44 import com.sun.jersey.api.client.ClientResponse;
45 import com.sun.jersey.api.client.WebResource;
46 import com.sun.jersey.api.client.config.DefaultClientConfig;
48 public class UniProtFTSRestClient extends FTSRestClient
49 implements ApplicationSingletonI
52 public static FTSRestClientI getInstance()
54 return (FTSRestClientI) ApplicationSingletonProvider
55 .getInstance(UniProtFTSRestClient.class);
58 private UniProtFTSRestClient()
60 // singleton -- use getInstance()
62 uniprotSearchEndpoint = Cache.getDefault(Preferences.UNIPROT_DOMAIN,
63 DEFAULT_UNIPROT_DOMAIN) + "/uniprot/";
66 private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
68 public final String uniprotSearchEndpoint;
72 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
74 @SuppressWarnings("unchecked")
76 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
81 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
82 uniprotRestRequest.getWantedFields());
83 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
84 ? getDefaultResponsePageSize()
85 : uniprotRestRequest.getResponseSize();
87 int offSet = uniprotRestRequest.getOffSet();
89 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
91 query = uniprotRestRequest.getSearchTerm();
95 query = uniprotRestRequest.getFieldToSearchBy()
96 .equalsIgnoreCase("Search All")
97 ? uniprotRestRequest.getSearchTerm()
99 + uniprotRestRequest.getSearchTerm()
100 : uniprotRestRequest.getFieldToSearchBy() + ":"
101 + uniprotRestRequest.getSearchTerm();
104 // BH 2018 the trick here is to coerce the classes in Javascript to be
105 // different from the ones in Java yet still allow this to be correct for
108 Class<ClientResponse> clientResponseClass;
111 // JavaScript only -- coerce types to Java types for Java
112 client = (Client) (Object) new jalview.javascript.web.Client();
113 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
123 client = Client.create(new DefaultClientConfig());
124 clientResponseClass = ClientResponse.class;
127 WebResource webResource = null;
128 webResource = client.resource(uniprotSearchEndpoint)
129 .queryParam("format", "tab")
130 .queryParam("columns", wantedFields)
131 .queryParam("limit", String.valueOf(responseSize))
132 .queryParam("offset", String.valueOf(offSet))
133 .queryParam("sort", "score").queryParam("query", query);
134 // Execute the REST request
135 ClientResponse clientResponse = webResource
136 .accept(MediaType.TEXT_PLAIN).get(clientResponseClass);
137 String uniProtTabDelimittedResponseString = clientResponse
138 .getEntity(String.class);
139 // Make redundant objects eligible for garbage collection to conserve
141 // System.out.println(">>>>> response : "
142 // + uniProtTabDelimittedResponseString);
143 if (clientResponse.getStatus() != 200)
145 String errorMessage = getMessageByHTTPStatusCode(
146 clientResponse.getStatus(), "Uniprot");
147 throw new Exception(errorMessage);
150 int xTotalResults = Platform.isJS() ? 1
151 : Integer.valueOf(clientResponse.getHeaders()
152 .get("X-Total-Results").get(0));
153 clientResponse = null;
155 return parseUniprotResponse(uniProtTabDelimittedResponseString,
156 uniprotRestRequest, xTotalResults);
157 } catch (Exception e)
159 String exceptionMsg = e.getMessage();
160 if (exceptionMsg.contains("SocketException"))
162 // No internet connection
163 throw new Exception(MessageManager.getString(
164 "exception.unable_to_detect_internet_connection"));
166 else if (exceptionMsg.contains("UnknownHostException"))
168 // The server 'http://www.uniprot.org' is unreachable
169 throw new Exception(MessageManager.formatMessage(
170 "exception.fts_server_unreachable", "Uniprot"));
179 public boolean isAdvancedQuery(String query)
181 if (query.contains(" AND ") || query.contains(" OR ")
182 || query.contains(" NOT ") || query.contains(" ! ")
183 || query.contains(" || ") || query.contains(" && ")
184 || query.contains(":") || query.contains("-"))
191 public FTSRestResponse parseUniprotResponse(
192 String uniProtTabDelimittedResponseString,
193 FTSRestRequest uniprotRestRequest, int xTotalResults)
195 FTSRestResponse searchResult = new FTSRestResponse();
196 List<FTSData> result = null;
197 if (uniProtTabDelimittedResponseString == null
198 || uniProtTabDelimittedResponseString.trim().isEmpty())
200 searchResult.setNumberOfItemsFound(0);
203 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
204 if (foundDataRow != null && foundDataRow.length > 0)
206 result = new ArrayList<>();
207 boolean firstRow = true;
208 for (String dataRow : foundDataRow)
210 // The first data row is usually the header data. This should be
211 // filtered out from the rest of the data See: JAL-2485
217 // System.out.println(dataRow);
218 result.add(getFTSData(dataRow, uniprotRestRequest));
220 searchResult.setNumberOfItemsFound(xTotalResults);
221 searchResult.setSearchSummary(result);
227 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
229 // * tab delimited string.
231 // * @param dataColumnFields
232 // * the collection of FTSDataColumnI to process
233 // * @return the generated comma delimited string from the supplied
234 // * FTSDataColumnI collection
236 // private String getDataColumnsFieldsAsTabDelimitedString(
237 // Collection<FTSDataColumnI> dataColumnFields)
239 // String result = "";
240 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
242 // StringBuilder returnedFields = new StringBuilder();
243 // for (FTSDataColumnI field : dataColumnFields)
245 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
247 // returnedFields.append("\t").append("Entry");
251 // returnedFields.append("\t").append(field.getName());
254 // returnedFields.deleteCharAt(0);
255 // result = returnedFields.toString();
260 public static FTSData getFTSData(String tabDelimittedDataStr,
261 FTSRestRequest request)
263 String primaryKey = null;
265 Object[] summaryRowData;
267 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
269 summaryRowData = new Object[diplayFields.size()];
270 String[] columns = tabDelimittedDataStr.split("\t");
271 for (FTSDataColumnI field : diplayFields)
275 String fieldData = columns[colCounter];
276 if (field.isPrimaryKeyColumn())
278 primaryKey = fieldData;
279 summaryRowData[colCounter++] = primaryKey;
281 else if (fieldData == null || fieldData.isEmpty())
283 summaryRowData[colCounter++] = null;
289 summaryRowData[colCounter++] = (field.getDataType()
290 .getDataTypeClass() == Integer.class)
291 ? Integer.valueOf(fieldData.replace(",", ""))
292 : (field.getDataType()
293 .getDataTypeClass() == Double.class)
294 ? Double.valueOf(fieldData)
296 } catch (Exception e)
299 System.out.println("offending value:" + fieldData);
302 } catch (Exception e)
304 // e.printStackTrace();
308 final String primaryKey1 = primaryKey;
310 final Object[] summaryRowData1 = summaryRowData;
314 public Object[] getSummaryData()
316 return summaryRowData1;
320 public Object getPrimaryKey()
326 * Returns a string representation of this object;
329 public String toString()
331 StringBuilder summaryFieldValues = new StringBuilder();
332 for (Object summaryField : summaryRowData1)
334 summaryFieldValues.append(
335 summaryField == null ? " " : summaryField.toString())
338 return summaryFieldValues.toString();
342 * Returns hash code value for this object
345 public int hashCode()
347 return Objects.hash(primaryKey1, this.toString());
351 public boolean equals(Object that)
353 return this.toString().equals(that.toString());
359 public String getColumnDataConfigFileName()
361 return "/fts/uniprot_data_columns.txt";