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;
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";
50 private static FTSRestClientI instance = null;
52 public final String uniprotSearchEndpoint;
54 public UniProtFTSRestClient()
57 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_DOMAIN",
58 DEFAULT_UNIPROT_DOMAIN) + "/uniprot/";
62 public FTSRestResponse executeRequest(FTSRestRequest uniportRestRequest)
67 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
68 uniportRestRequest.getWantedFields());
69 int responseSize = (uniportRestRequest.getResponseSize() == 0)
70 ? getDefaultResponsePageSize()
71 : uniportRestRequest.getResponseSize();
73 int offSet = uniportRestRequest.getOffSet();
75 if (isAdvancedQuery(uniportRestRequest.getSearchTerm()))
77 query = uniportRestRequest.getSearchTerm();
81 query = uniportRestRequest.getFieldToSearchBy()
82 .equalsIgnoreCase("Search All")
83 ? uniportRestRequest.getSearchTerm()
85 + uniportRestRequest.getSearchTerm()
86 : uniportRestRequest.getFieldToSearchBy() + ":"
87 + uniportRestRequest.getSearchTerm();
90 // BH 2018 the trick here is to coerce the classes in Javascript to be
91 // different from the ones in Java yet still allow this to be correct for
94 Class<ClientResponse> clientResponseClass;
95 if (/** @j2sNative true || */
98 // JavaScript only -- coerce types to Java types for Java
99 client = (Client) (Object) new jalview.javascript.web.Client();
100 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
105 client = Client.create(new DefaultClientConfig());
106 clientResponseClass = ClientResponse.class;
109 WebResource webResource = null;
110 webResource = client.resource(uniprotSearchEndpoint)
111 .queryParam("format", "tab")
112 .queryParam("columns", wantedFields)
113 .queryParam("limit", String.valueOf(responseSize))
114 .queryParam("offset", String.valueOf(offSet))
115 .queryParam("sort", "score").queryParam("query", query);
116 // Execute the REST request
117 ClientResponse clientResponse = webResource
118 .accept(MediaType.TEXT_PLAIN).get(clientResponseClass);
119 String uniProtTabDelimittedResponseString = clientResponse
120 .getEntity(String.class);
121 // Make redundant objects eligible for garbage collection to conserve
123 // System.out.println(">>>>> response : "
124 // + uniProtTabDelimittedResponseString);
125 if (clientResponse.getStatus() != 200)
127 String errorMessage = getMessageByHTTPStatusCode(
128 clientResponse.getStatus(), "Uniprot");
129 throw new Exception(errorMessage);
132 int xTotalResults = Jalview.isJS() ? 1
133 : Integer.valueOf(clientResponse.getHeaders()
134 .get("X-Total-Results").get(0));
135 clientResponse = null;
137 return parseUniprotResponse(uniProtTabDelimittedResponseString,
138 uniportRestRequest, xTotalResults);
139 } catch (Exception e)
141 String exceptionMsg = e.getMessage();
142 if (exceptionMsg.contains("SocketException"))
144 // No internet connection
145 throw new Exception(MessageManager.getString(
146 "exception.unable_to_detect_internet_connection"));
148 else if (exceptionMsg.contains("UnknownHostException"))
150 // The server 'http://www.uniprot.org' is unreachable
151 throw new Exception(MessageManager.formatMessage(
152 "exception.fts_server_unreachable", "Uniprot"));
161 public boolean isAdvancedQuery(String query)
163 if (query.contains(" AND ") || query.contains(" OR ")
164 || query.contains(" NOT ") || query.contains(" ! ")
165 || query.contains(" || ") || query.contains(" && ")
166 || query.contains(":") || query.contains("-"))
173 public FTSRestResponse parseUniprotResponse(
174 String uniProtTabDelimittedResponseString,
175 FTSRestRequest uniprotRestRequest, int xTotalResults)
177 FTSRestResponse searchResult = new FTSRestResponse();
178 List<FTSData> result = null;
179 if (uniProtTabDelimittedResponseString == null
180 || uniProtTabDelimittedResponseString.trim().isEmpty())
182 searchResult.setNumberOfItemsFound(0);
185 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
186 if (foundDataRow != null && foundDataRow.length > 0)
188 result = new ArrayList<>();
189 boolean firstRow = true;
190 for (String dataRow : foundDataRow)
192 // The first data row is usually the header data. This should be
193 // filtered out from the rest of the data See: JAL-2485
199 // System.out.println(dataRow);
200 result.add(getFTSData(dataRow, uniprotRestRequest));
202 searchResult.setNumberOfItemsFound(xTotalResults);
203 searchResult.setSearchSummary(result);
209 * Takes a collection of FTSDataColumnI and converts its 'code' values into a
210 * tab delimited string.
212 * @param dataColumnFields
213 * the collection of FTSDataColumnI to process
214 * @return the generated comma delimited string from the supplied
215 * FTSDataColumnI collection
217 private String getDataColumnsFieldsAsTabDelimitedString(
218 Collection<FTSDataColumnI> dataColumnFields)
221 if (dataColumnFields != null && !dataColumnFields.isEmpty())
223 StringBuilder returnedFields = new StringBuilder();
224 for (FTSDataColumnI field : dataColumnFields)
226 if (field.getName().equalsIgnoreCase("Uniprot Id"))
228 returnedFields.append("\t").append("Entry");
232 returnedFields.append("\t").append(field.getName());
235 returnedFields.deleteCharAt(0);
236 result = returnedFields.toString();
241 public static FTSData getFTSData(String tabDelimittedDataStr,
242 FTSRestRequest request)
244 String primaryKey = null;
246 Object[] summaryRowData;
248 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
250 summaryRowData = new Object[diplayFields.size()];
251 String[] columns = tabDelimittedDataStr.split("\t");
252 for (FTSDataColumnI field : diplayFields)
256 String fieldData = columns[colCounter];
257 if (field.isPrimaryKeyColumn())
259 primaryKey = fieldData;
260 summaryRowData[colCounter++] = primaryKey;
262 else if (fieldData == null || fieldData.isEmpty())
264 summaryRowData[colCounter++] = null;
270 summaryRowData[colCounter++] = (field.getDataType()
271 .getDataTypeClass() == Integer.class)
272 ? Integer.valueOf(fieldData.replace(",", ""))
273 : (field.getDataType()
274 .getDataTypeClass() == Double.class)
275 ? Double.valueOf(fieldData)
277 } catch (Exception e)
280 System.out.println("offending value:" + fieldData);
283 } catch (Exception e)
285 // e.printStackTrace();
289 final String primaryKey1 = primaryKey;
291 final Object[] summaryRowData1 = summaryRowData;
295 public Object[] getSummaryData()
297 return summaryRowData1;
301 public Object getPrimaryKey()
307 * Returns a string representation of this object;
310 public String toString()
312 StringBuilder summaryFieldValues = new StringBuilder();
313 for (Object summaryField : summaryRowData1)
315 summaryFieldValues.append(
316 summaryField == null ? " " : summaryField.toString())
319 return summaryFieldValues.toString();
323 * Returns hash code value for this object
326 public int hashCode()
328 return Objects.hash(primaryKey1, this.toString());
332 public boolean equals(Object that)
334 return this.toString().equals(that.toString());
339 public static FTSRestClientI getInstance()
341 if (instance == null)
343 instance = new UniProtFTSRestClient();
349 public String getColumnDataConfigFileName()
351 return "/fts/uniprot_data_columns.txt";