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(uniportRestRequest
62 int responseSize = (uniportRestRequest.getResponseSize() == 0) ? getDefaultResponsePageSize()
63 : uniportRestRequest.getResponseSize();
65 int offSet = uniportRestRequest.getOffSet();
67 if (isAdvancedQuery(uniportRestRequest.getSearchTerm()))
69 query = uniportRestRequest.getSearchTerm();
73 query = uniportRestRequest.getFieldToSearchBy().equalsIgnoreCase(
74 "Search All") ? uniportRestRequest.getSearchTerm()
75 + " or mnemonic:" + uniportRestRequest.getSearchTerm()
76 : uniportRestRequest.getFieldToSearchBy() + ":"
77 + uniportRestRequest.getSearchTerm();
80 WebResource webResource = null;
81 webResource = client.resource(UNIPROT_SEARCH_ENDPOINT)
82 .queryParam("format", "tab")
83 .queryParam("columns", wantedFields)
84 .queryParam("limit", String.valueOf(responseSize))
85 .queryParam("offset", String.valueOf(offSet))
86 .queryParam("sort", "score").queryParam("query", query);
87 // Execute the REST request
88 ClientResponse clientResponse = webResource.accept(
89 MediaType.TEXT_PLAIN).get(ClientResponse.class);
90 String uniProtTabDelimittedResponseString = clientResponse
91 .getEntity(String.class);
92 // Make redundant objects eligible for garbage collection to conserve
94 // System.out.println(">>>>> response : "
95 // + uniProtTabDelimittedResponseString);
96 if (clientResponse.getStatus() != 200)
98 String errorMessage = getMessageByHTTPStatusCode(
99 clientResponse.getStatus(), "Uniprot");
100 throw new Exception(errorMessage);
103 int xTotalResults = Integer.valueOf(clientResponse.getHeaders()
104 .get("X-Total-Results").get(0));
105 clientResponse = null;
107 return parseUniprotResponse(uniProtTabDelimittedResponseString,
108 uniportRestRequest, xTotalResults);
109 } catch (Exception e)
111 String exceptionMsg = e.getMessage();
112 if (exceptionMsg.contains("SocketException"))
114 // No internet connection
117 .getString("exception.unable_to_detect_internet_connection"));
119 else if (exceptionMsg.contains("UnknownHostException"))
121 // The server 'http://www.uniprot.org' is unreachable
122 throw new Exception(MessageManager.formatMessage(
123 "exception.fts_server_unreachable", "Uniprot"));
132 public boolean isAdvancedQuery(String query)
134 if (query.contains(" AND ") || query.contains(" OR ")
135 || query.contains(" NOT ") || query.contains(" ! ")
136 || query.contains(" || ") || query.contains(" && ")
137 || query.contains(":") || query.contains("-"))
144 public FTSRestResponse parseUniprotResponse(
145 String uniProtTabDelimittedResponseString,
146 FTSRestRequest uniprotRestRequest, int xTotalResults)
148 FTSRestResponse searchResult = new FTSRestResponse();
149 List<FTSData> result = null;
150 if (uniProtTabDelimittedResponseString == null
151 || uniProtTabDelimittedResponseString.trim().isEmpty())
153 searchResult.setNumberOfItemsFound(0);
156 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
157 if (foundDataRow != null && foundDataRow.length > 0)
159 result = new ArrayList<FTSData>();
160 String titleRow = getDataColumnsFieldsAsTabDelimitedString(uniprotRestRequest
162 // System.out.println(">>>>Title row : " + titleRow);
163 for (String dataRow : foundDataRow)
165 if (dataRow.equalsIgnoreCase(titleRow))
167 // System.out.println(">>>>>>>>>> matched!!!");
170 // System.out.println(dataRow);
171 result.add(getFTSData(dataRow, uniprotRestRequest));
173 searchResult.setNumberOfItemsFound(xTotalResults);
174 searchResult.setSearchSummary(result);
180 * Takes a collection of FTSDataColumnI and converts its 'code' values into a
181 * tab delimited string.
183 * @param dataColumnFields
184 * the collection of FTSDataColumnI to process
185 * @return the generated comma delimited string from the supplied
186 * FTSDataColumnI collection
188 private String getDataColumnsFieldsAsTabDelimitedString(
189 Collection<FTSDataColumnI> dataColumnFields)
192 if (dataColumnFields != null && !dataColumnFields.isEmpty())
194 StringBuilder returnedFields = new StringBuilder();
195 for (FTSDataColumnI field : dataColumnFields)
197 if (field.getName().equalsIgnoreCase("Uniprot Id"))
199 returnedFields.append("\t").append("Entry");
203 returnedFields.append("\t").append(field.getName());
206 returnedFields.deleteCharAt(0);
207 result = returnedFields.toString();
212 public static FTSData getFTSData(String tabDelimittedDataStr,
213 FTSRestRequest request)
215 String primaryKey = null;
217 Object[] summaryRowData;
219 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
221 summaryRowData = new Object[diplayFields.size()];
222 String[] columns = tabDelimittedDataStr.split("\t");
223 for (FTSDataColumnI field : diplayFields)
227 String fieldData = columns[colCounter];
228 if (field.isPrimaryKeyColumn())
230 primaryKey = fieldData;
231 summaryRowData[colCounter++] = primaryKey;
233 else if (fieldData == null || fieldData.isEmpty())
235 summaryRowData[colCounter++] = null;
241 summaryRowData[colCounter++] = (field.getDataType()
242 .getDataTypeClass() == Integer.class) ? Integer
243 .valueOf(fieldData.replace(",", ""))
244 : (field.getDataType().getDataTypeClass() == Double.class) ? Double
245 .valueOf(fieldData) : fieldData;
246 } catch (Exception e)
249 System.out.println("offending value:" + fieldData);
252 } catch (Exception e)
254 // e.printStackTrace();
258 final String primaryKey1 = primaryKey;
260 final Object[] summaryRowData1 = summaryRowData;
264 public Object[] getSummaryData()
266 return summaryRowData1;
270 public Object getPrimaryKey()
276 * Returns a string representation of this object;
279 public String toString()
281 StringBuilder summaryFieldValues = new StringBuilder();
282 for (Object summaryField : summaryRowData1)
284 summaryFieldValues.append(
285 summaryField == null ? " " : summaryField.toString())
288 return summaryFieldValues.toString();
292 * Returns hash code value for this object
295 public int hashCode()
297 return Objects.hash(primaryKey1, this.toString());
301 public boolean equals(Object that)
303 return this.toString().equals(that.toString());
308 public static FTSRestClientI getInstance()
310 if (instance == null)
312 instance = new UniProtFTSRestClient();
318 public String getColumnDataConfigFileName()
320 return "/fts/uniprot_data_columns.txt";