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;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.Objects;
38 import javax.ws.rs.core.MediaType;
40 import com.sun.jersey.api.client.Client;
41 import com.sun.jersey.api.client.ClientResponse;
42 import com.sun.jersey.api.client.WebResource;
43 import com.sun.jersey.api.client.config.ClientConfig;
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 ClientConfig clientConfig = new DefaultClientConfig();
68 Client client = Client.create(clientConfig);
70 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
71 uniportRestRequest.getWantedFields());
72 int responseSize = (uniportRestRequest.getResponseSize() == 0)
73 ? getDefaultResponsePageSize()
74 : uniportRestRequest.getResponseSize();
76 int offSet = uniportRestRequest.getOffSet();
78 if (isAdvancedQuery(uniportRestRequest.getSearchTerm()))
80 query = uniportRestRequest.getSearchTerm();
84 query = uniportRestRequest.getFieldToSearchBy()
85 .equalsIgnoreCase("Search All")
86 ? uniportRestRequest.getSearchTerm()
88 + uniportRestRequest.getSearchTerm()
89 : uniportRestRequest.getFieldToSearchBy() + ":"
90 + uniportRestRequest.getSearchTerm();
93 WebResource webResource = null;
94 webResource = client.resource(uniprotSearchEndpoint)
95 .queryParam("format", "tab")
96 .queryParam("columns", wantedFields)
97 .queryParam("limit", String.valueOf(responseSize))
98 .queryParam("offset", String.valueOf(offSet))
99 .queryParam("sort", "score").queryParam("query", query);
100 // Execute the REST request
101 ClientResponse clientResponse = webResource
102 .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
103 String uniProtTabDelimittedResponseString = clientResponse
104 .getEntity(String.class);
105 // Make redundant objects eligible for garbage collection to conserve
107 // System.out.println(">>>>> response : "
108 // + uniProtTabDelimittedResponseString);
109 if (clientResponse.getStatus() != 200)
111 String errorMessage = getMessageByHTTPStatusCode(
112 clientResponse.getStatus(), "Uniprot");
113 throw new Exception(errorMessage);
116 int xTotalResults = Integer.valueOf(
117 clientResponse.getHeaders().get("X-Total-Results").get(0));
118 clientResponse = null;
120 return parseUniprotResponse(uniProtTabDelimittedResponseString,
121 uniportRestRequest, xTotalResults);
122 } catch (Exception e)
124 String exceptionMsg = e.getMessage();
125 if (exceptionMsg.contains("SocketException"))
127 // No internet connection
128 throw new Exception(MessageManager.getString(
129 "exception.unable_to_detect_internet_connection"));
131 else if (exceptionMsg.contains("UnknownHostException"))
133 // The server 'http://www.uniprot.org' is unreachable
134 throw new Exception(MessageManager.formatMessage(
135 "exception.fts_server_unreachable", "Uniprot"));
144 public boolean isAdvancedQuery(String query)
146 if (query.contains(" AND ") || query.contains(" OR ")
147 || query.contains(" NOT ") || query.contains(" ! ")
148 || query.contains(" || ") || query.contains(" && ")
149 || query.contains(":") || query.contains("-"))
156 public FTSRestResponse parseUniprotResponse(
157 String uniProtTabDelimittedResponseString,
158 FTSRestRequest uniprotRestRequest, int xTotalResults)
160 FTSRestResponse searchResult = new FTSRestResponse();
161 List<FTSData> result = null;
162 if (uniProtTabDelimittedResponseString == null
163 || uniProtTabDelimittedResponseString.trim().isEmpty())
165 searchResult.setNumberOfItemsFound(0);
168 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
169 if (foundDataRow != null && foundDataRow.length > 0)
171 result = new ArrayList<>();
172 boolean firstRow = true;
173 for (String dataRow : foundDataRow)
175 // The first data row is usually the header data. This should be
176 // filtered out from the rest of the data See: JAL-2485
182 // System.out.println(dataRow);
183 result.add(getFTSData(dataRow, uniprotRestRequest));
185 searchResult.setNumberOfItemsFound(xTotalResults);
186 searchResult.setSearchSummary(result);
192 * Takes a collection of FTSDataColumnI and converts its 'code' values into a
193 * tab delimited string.
195 * @param dataColumnFields
196 * the collection of FTSDataColumnI to process
197 * @return the generated comma delimited string from the supplied
198 * FTSDataColumnI collection
200 private String getDataColumnsFieldsAsTabDelimitedString(
201 Collection<FTSDataColumnI> dataColumnFields)
204 if (dataColumnFields != null && !dataColumnFields.isEmpty())
206 StringBuilder returnedFields = new StringBuilder();
207 for (FTSDataColumnI field : dataColumnFields)
209 if (field.getName().equalsIgnoreCase("Uniprot Id"))
211 returnedFields.append("\t").append("Entry");
215 returnedFields.append("\t").append(field.getName());
218 returnedFields.deleteCharAt(0);
219 result = returnedFields.toString();
224 public static FTSData getFTSData(String tabDelimittedDataStr,
225 FTSRestRequest request)
227 String primaryKey = null;
229 Object[] summaryRowData;
231 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
233 summaryRowData = new Object[diplayFields.size()];
234 String[] columns = tabDelimittedDataStr.split("\t");
235 for (FTSDataColumnI field : diplayFields)
239 String fieldData = columns[colCounter];
240 if (field.isPrimaryKeyColumn())
242 primaryKey = fieldData;
243 summaryRowData[colCounter++] = primaryKey;
245 else if (fieldData == null || fieldData.isEmpty())
247 summaryRowData[colCounter++] = null;
253 summaryRowData[colCounter++] = (field.getDataType()
254 .getDataTypeClass() == Integer.class)
255 ? Integer.valueOf(fieldData.replace(",", ""))
256 : (field.getDataType()
257 .getDataTypeClass() == Double.class)
258 ? Double.valueOf(fieldData)
260 } catch (Exception e)
263 System.out.println("offending value:" + fieldData);
266 } catch (Exception e)
268 // e.printStackTrace();
272 final String primaryKey1 = primaryKey;
274 final Object[] summaryRowData1 = summaryRowData;
278 public Object[] getSummaryData()
280 return summaryRowData1;
284 public Object getPrimaryKey()
290 * Returns a string representation of this object;
293 public String toString()
295 StringBuilder summaryFieldValues = new StringBuilder();
296 for (Object summaryField : summaryRowData1)
298 summaryFieldValues.append(
299 summaryField == null ? " " : summaryField.toString())
302 return summaryFieldValues.toString();
306 * Returns hash code value for this object
309 public int hashCode()
311 return Objects.hash(primaryKey1, this.toString());
315 public boolean equals(Object that)
317 return this.toString().equals(that.toString());
322 public static FTSRestClientI getInstance()
324 if (instance == null)
326 instance = new UniProtFTSRestClient();
332 public String getColumnDataConfigFileName()
334 return "/fts/uniprot_data_columns.txt";