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.fts.api.FTSData;
25 import jalview.fts.api.FTSDataColumnI;
26 import jalview.fts.api.FTSRestClientI;
27 import jalview.fts.core.FTSRestClient;
28 import jalview.fts.core.FTSRestRequest;
29 import jalview.fts.core.FTSRestResponse;
30 import jalview.util.MessageManager;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Objects;
37 import javax.ws.rs.core.MediaType;
39 import com.sun.jersey.api.client.Client;
40 import com.sun.jersey.api.client.ClientResponse;
41 import com.sun.jersey.api.client.WebResource;
42 import com.sun.jersey.api.client.config.ClientConfig;
43 import com.sun.jersey.api.client.config.DefaultClientConfig;
45 public class UniProtFTSRestClient extends FTSRestClient
47 private static FTSRestClientI instance = null;
49 public static final String UNIPROT_SEARCH_ENDPOINT = "http://www.uniprot.org/uniprot/?";
52 public FTSRestResponse executeRequest(FTSRestRequest uniportRestRequest)
57 ClientConfig clientConfig = new DefaultClientConfig();
58 Client client = Client.create(clientConfig);
60 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
61 uniportRestRequest.getWantedFields());
62 int responseSize = (uniportRestRequest.getResponseSize() == 0)
63 ? getDefaultResponsePageSize()
64 : uniportRestRequest.getResponseSize();
66 int offSet = uniportRestRequest.getOffSet();
68 if (isAdvancedQuery(uniportRestRequest.getSearchTerm()))
70 query = uniportRestRequest.getSearchTerm();
74 query = uniportRestRequest.getFieldToSearchBy()
75 .equalsIgnoreCase("Search All")
76 ? uniportRestRequest.getSearchTerm()
78 + uniportRestRequest.getSearchTerm()
79 : uniportRestRequest.getFieldToSearchBy() + ":"
80 + uniportRestRequest.getSearchTerm();
83 WebResource webResource = null;
84 webResource = client.resource(UNIPROT_SEARCH_ENDPOINT)
85 .queryParam("format", "tab")
86 .queryParam("columns", wantedFields)
87 .queryParam("limit", String.valueOf(responseSize))
88 .queryParam("offset", String.valueOf(offSet))
89 .queryParam("sort", "score").queryParam("query", query);
90 // Execute the REST request
91 ClientResponse clientResponse = webResource
92 .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
93 String uniProtTabDelimittedResponseString = clientResponse
94 .getEntity(String.class);
95 // Make redundant objects eligible for garbage collection to conserve
97 // System.out.println(">>>>> response : "
98 // + uniProtTabDelimittedResponseString);
99 if (clientResponse.getStatus() != 200)
101 String errorMessage = getMessageByHTTPStatusCode(
102 clientResponse.getStatus(), "Uniprot");
103 throw new Exception(errorMessage);
106 int xTotalResults = Integer.valueOf(
107 clientResponse.getHeaders().get("X-Total-Results").get(0));
108 clientResponse = null;
110 return parseUniprotResponse(uniProtTabDelimittedResponseString,
111 uniportRestRequest, xTotalResults);
112 } catch (Exception e)
114 String exceptionMsg = e.getMessage();
115 if (exceptionMsg.contains("SocketException"))
117 // No internet connection
118 throw new Exception(MessageManager.getString(
119 "exception.unable_to_detect_internet_connection"));
121 else if (exceptionMsg.contains("UnknownHostException"))
123 // The server 'http://www.uniprot.org' is unreachable
124 throw new Exception(MessageManager.formatMessage(
125 "exception.fts_server_unreachable", "Uniprot"));
134 public boolean isAdvancedQuery(String query)
136 if (query.contains(" AND ") || query.contains(" OR ")
137 || query.contains(" NOT ") || query.contains(" ! ")
138 || query.contains(" || ") || query.contains(" && ")
139 || query.contains(":") || query.contains("-"))
146 public FTSRestResponse parseUniprotResponse(
147 String uniProtTabDelimittedResponseString,
148 FTSRestRequest uniprotRestRequest, int xTotalResults)
150 FTSRestResponse searchResult = new FTSRestResponse();
151 List<FTSData> result = null;
152 if (uniProtTabDelimittedResponseString == null
153 || uniProtTabDelimittedResponseString.trim().isEmpty())
155 searchResult.setNumberOfItemsFound(0);
158 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
159 if (foundDataRow != null && foundDataRow.length > 0)
161 result = new ArrayList<FTSData>();
162 boolean firstRow = true;
163 for (String dataRow : foundDataRow)
165 // The first data row is usually the header data. This should be
166 // filtered out from the rest of the data See: JAL-2485
172 // System.out.println(dataRow);
173 result.add(getFTSData(dataRow, uniprotRestRequest));
175 searchResult.setNumberOfItemsFound(xTotalResults);
176 searchResult.setSearchSummary(result);
182 * Takes a collection of FTSDataColumnI and converts its 'code' values into a
183 * tab delimited string.
185 * @param dataColumnFields
186 * the collection of FTSDataColumnI to process
187 * @return the generated comma delimited string from the supplied
188 * FTSDataColumnI collection
190 private String getDataColumnsFieldsAsTabDelimitedString(
191 Collection<FTSDataColumnI> dataColumnFields)
194 if (dataColumnFields != null && !dataColumnFields.isEmpty())
196 StringBuilder returnedFields = new StringBuilder();
197 for (FTSDataColumnI field : dataColumnFields)
199 if (field.getName().equalsIgnoreCase("Uniprot Id"))
201 returnedFields.append("\t").append("Entry");
205 returnedFields.append("\t").append(field.getName());
208 returnedFields.deleteCharAt(0);
209 result = returnedFields.toString();
214 public static FTSData getFTSData(String tabDelimittedDataStr,
215 FTSRestRequest request)
217 String primaryKey = null;
219 Object[] summaryRowData;
221 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
223 summaryRowData = new Object[diplayFields.size()];
224 String[] columns = tabDelimittedDataStr.split("\t");
225 for (FTSDataColumnI field : diplayFields)
229 String fieldData = columns[colCounter];
230 if (field.isPrimaryKeyColumn())
232 primaryKey = fieldData;
233 summaryRowData[colCounter++] = primaryKey;
235 else if (fieldData == null || fieldData.isEmpty())
237 summaryRowData[colCounter++] = null;
243 summaryRowData[colCounter++] = (field.getDataType()
244 .getDataTypeClass() == Integer.class)
245 ? Integer.valueOf(fieldData.replace(",", ""))
246 : (field.getDataType()
247 .getDataTypeClass() == Double.class)
248 ? Double.valueOf(fieldData)
250 } catch (Exception e)
253 System.out.println("offending value:" + fieldData);
256 } catch (Exception e)
258 // e.printStackTrace();
262 final String primaryKey1 = primaryKey;
264 final Object[] summaryRowData1 = summaryRowData;
268 public Object[] getSummaryData()
270 return summaryRowData1;
274 public Object getPrimaryKey()
280 * Returns a string representation of this object;
283 public String toString()
285 StringBuilder summaryFieldValues = new StringBuilder();
286 for (Object summaryField : summaryRowData1)
288 summaryFieldValues.append(
289 summaryField == null ? " " : summaryField.toString())
292 return summaryFieldValues.toString();
296 * Returns hash code value for this object
299 public int hashCode()
301 return Objects.hash(primaryKey1, this.toString());
305 public boolean equals(Object that)
307 return this.toString().equals(that.toString());
312 public static FTSRestClientI getInstance()
314 if (instance == null)
316 instance = new UniProtFTSRestClient();
322 public String getColumnDataConfigFileName()
324 return "/fts/uniprot_data_columns.txt";