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 java.lang.invoke.MethodHandles;
25 import java.net.MalformedURLException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Objects;
32 import javax.ws.rs.core.MediaType;
34 import com.sun.jersey.api.client.Client;
35 import com.sun.jersey.api.client.ClientResponse;
36 import com.sun.jersey.api.client.WebResource;
37 import com.sun.jersey.api.client.config.DefaultClientConfig;
39 import jalview.bin.Cache;
40 import jalview.bin.Console;
41 import jalview.fts.api.FTSData;
42 import jalview.fts.api.FTSDataColumnI;
43 import jalview.fts.core.FTSRestClient;
44 import jalview.fts.core.FTSRestRequest;
45 import jalview.fts.core.FTSRestResponse;
46 import jalview.util.ChannelProperties;
47 import jalview.util.MessageManager;
48 import jalview.util.Platform;
52 * See https://issues.jalview.org/browse/JAL-4036
53 * The new Uniprot API is not dissimilar to the old one, but has some important changes.
54 * Some group names have changed slightly, some old groups have gone and there are quite a few new groups.
56 * Most changes are mappings of old column ids to new field ids. There are a handful of old
57 * columns not mapped to new fields, and new fields without an old column.
58 * [aside: not all possible columns were listed in the resources/fts/uniprot_data_columns.txt file.
59 * These were presumably additions after the file was created]
60 * For existing/mapped fields, the same preferences found in the resource file have been migrated to
61 * the new file with the new field name, id and group.
63 * The new mapped groups and files are stored and read from resources/fts/uniprot_data_columns-2022.txt.
65 * There is now no "sort" query string parameter.
67 * See https://www.uniprot.org/help/api_queries
69 * SIGNIFICANT CHANGE: Pagination is no longer performed using a record offset, but with a "cursor"
70 * query string parameter that is not really a cursor. The value is an opaque string that is passed (or
71 * rather a whole URL is passed) in the "Link" header of the HTTP response of the previous page.
72 * Where such a link is passed it is put into the cursors ArrayList.
73 * There are @Overridden methods in UniprotFTSPanel.
76 public class UniProtFTSRestClient extends FTSRestClient
78 private static final String DEFAULT_UNIPROT_DOMAIN = "https://rest.uniprot.org";
80 private static final String USER_AGENT = ChannelProperties
81 .getProperty("app_name", "Jalview") + " "
82 + Cache.getDefault("VERSION", "Unknown") + " "
83 + MethodHandles.lookup().lookupClass() + " help@jalview.org";
87 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
90 private static UniProtFTSRestClient instance = null;
92 public final String uniprotSearchEndpoint;
94 public UniProtFTSRestClient()
98 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_2022_DOMAIN",
99 DEFAULT_UNIPROT_DOMAIN) + "/uniprotkb/search";
102 @SuppressWarnings("unchecked")
104 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
107 return executeRequest(uniprotRestRequest, null);
110 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest,
111 String cursor) throws Exception
115 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
116 uniprotRestRequest.getWantedFields());
117 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
118 ? getDefaultResponsePageSize()
119 : uniprotRestRequest.getResponseSize();
121 int offSet = uniprotRestRequest.getOffSet();
123 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
125 query = uniprotRestRequest.getSearchTerm();
129 query = uniprotRestRequest.getFieldToSearchBy().equalsIgnoreCase(
130 "Search All") ? uniprotRestRequest.getSearchTerm()
132 // + uniprotRestRequest.getSearchTerm()
133 : uniprotRestRequest.getFieldToSearchBy() + ":"
134 + uniprotRestRequest.getSearchTerm();
137 // BH 2018 the trick here is to coerce the classes in Javascript to be
138 // different from the ones in Java yet still allow this to be correct for
141 Class<ClientResponse> clientResponseClass;
144 // JavaScript only -- coerce types to Java types for Java
145 client = (Client) (Object) new jalview.javascript.web.Client();
146 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
156 client = Client.create(new DefaultClientConfig());
157 clientResponseClass = ClientResponse.class;
160 WebResource webResource = null;
161 webResource = client.resource(uniprotSearchEndpoint)
162 .queryParam("format", "tsv")
163 .queryParam("fields", wantedFields)
164 .queryParam("size", String.valueOf(responseSize))
165 /* 2022 new api has no "sort"
166 * .queryParam("sort", "score")
168 .queryParam("query", query);
169 if (offSet != 0 && cursor != null && cursor.length() > 0)
170 // 2022 new api does not do pagination with an offset, it requires a
171 // "cursor" parameter with a key (given for the next page).
172 // (see https://www.uniprot.org/help/pagination)
174 webResource = webResource.queryParam("cursor", cursor);
177 "Uniprot FTS Request: " + webResource.getURI().toString());
178 // Execute the REST request
179 WebResource.Builder wrBuilder = webResource
180 .accept(MediaType.TEXT_PLAIN);
181 if (!Platform.isJS())
188 wrBuilder.header("User-Agent", USER_AGENT);
190 ClientResponse clientResponse = wrBuilder.get(clientResponseClass);
192 if (!Platform.isJS())
199 if (clientResponse.getHeaders().containsKey("Link"))
201 // extract the URL from the 'Link: <URL>; ref="stuff"' header
202 String linkHeader = clientResponse.getHeaders().get("Link")
204 if (linkHeader.indexOf("<") > -1)
206 String temp = linkHeader.substring(linkHeader.indexOf("<") + 1);
207 if (temp.indexOf(">") > -1)
209 String nextUrl = temp.substring(0, temp.indexOf(">"));
210 // then get the cursor value from the query string parameters
211 String nextCursor = getQueryParam("cursor", nextUrl);
212 setCursor(cursorPage + 1, nextCursor);
218 String uniProtTabDelimittedResponseString = clientResponse
219 .getEntity(String.class);
220 // Make redundant objects eligible for garbage collection to conserve
222 // jalview.bin.Console.outPrintln(">>>>> response : "
223 // + uniProtTabDelimittedResponseString);
224 if (clientResponse.getStatus() != 200)
226 String errorMessage = getMessageByHTTPStatusCode(
227 clientResponse.getStatus(), "Uniprot");
228 throw new Exception(errorMessage);
231 // new Uniprot API is not including a "X-Total-Results" header when there
233 List<String> resultsHeaders = clientResponse.getHeaders()
234 .get("X-Total-Results");
235 int xTotalResults = 0;
240 else if (resultsHeaders != null && resultsHeaders.size() >= 1)
242 xTotalResults = Integer.valueOf(resultsHeaders.get(0));
244 clientResponse = null;
246 return parseUniprotResponse(uniProtTabDelimittedResponseString,
247 uniprotRestRequest, xTotalResults);
248 } catch (Exception e)
250 Console.warn("Problem with the query: " + e.getMessage());
251 Console.debug("Exception stacktrace:", e);
252 String exceptionMsg = e.getMessage();
253 if (exceptionMsg.contains("SocketException"))
255 // No internet connection
256 throw new Exception(MessageManager.getString(
257 "exception.unable_to_detect_internet_connection"));
259 else if (exceptionMsg.contains("UnknownHostException"))
261 // The server 'http://www.uniprot.org' is unreachable
262 throw new Exception(MessageManager.formatMessage(
263 "exception.fts_server_unreachable", "Uniprot"));
272 public boolean isAdvancedQuery(String query)
274 if (query.contains(" AND ") || query.contains(" OR ")
275 || query.contains(" NOT ") || query.contains(" ! ")
276 || query.contains(" || ") || query.contains(" && ")
277 || query.contains(":") || query.contains("-"))
284 public FTSRestResponse parseUniprotResponse(
285 String uniProtTabDelimittedResponseString,
286 FTSRestRequest uniprotRestRequest, int xTotalResults)
288 FTSRestResponse searchResult = new FTSRestResponse();
289 List<FTSData> result = null;
290 if (uniProtTabDelimittedResponseString == null
291 || uniProtTabDelimittedResponseString.trim().isEmpty())
293 searchResult.setNumberOfItemsFound(0);
296 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
297 if (foundDataRow != null && foundDataRow.length > 0)
299 result = new ArrayList<>();
300 boolean firstRow = true;
301 for (String dataRow : foundDataRow)
303 // The first data row is usually the header data. This should be
304 // filtered out from the rest of the data See: JAL-2485
310 // jalview.bin.Console.outPrintln(dataRow);
311 result.add(getFTSData(dataRow, uniprotRestRequest));
313 searchResult.setNumberOfItemsFound(xTotalResults);
314 searchResult.setSearchSummary(result);
320 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
322 // * tab delimited string.
324 // * @param dataColumnFields
325 // * the collection of FTSDataColumnI to process
326 // * @return the generated comma delimited string from the supplied
327 // * FTSDataColumnI collection
329 // private String getDataColumnsFieldsAsTabDelimitedString(
330 // Collection<FTSDataColumnI> dataColumnFields)
332 // String result = "";
333 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
335 // StringBuilder returnedFields = new StringBuilder();
336 // for (FTSDataColumnI field : dataColumnFields)
338 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
340 // returnedFields.append("\t").append("Entry");
344 // returnedFields.append("\t").append(field.getName());
347 // returnedFields.deleteCharAt(0);
348 // result = returnedFields.toString();
353 public static FTSData getFTSData(String tabDelimittedDataStr,
354 FTSRestRequest request)
356 String primaryKey = null;
358 Object[] summaryRowData;
360 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
362 summaryRowData = new Object[diplayFields.size()];
363 String[] columns = tabDelimittedDataStr.split("\t");
364 for (FTSDataColumnI field : diplayFields)
368 String fieldData = columns[colCounter];
369 if (field.isPrimaryKeyColumn())
371 primaryKey = fieldData;
372 summaryRowData[colCounter++] = primaryKey;
374 else if (fieldData == null || fieldData.isEmpty())
376 summaryRowData[colCounter++] = null;
382 summaryRowData[colCounter++] = (field.getDataType()
383 .getDataTypeClass() == Integer.class)
384 ? Integer.valueOf(fieldData.replace(",", ""))
385 : (field.getDataType()
386 .getDataTypeClass() == Double.class)
387 ? Double.valueOf(fieldData)
389 } catch (Exception e)
392 jalview.bin.Console.outPrintln("offending value:" + fieldData);
395 } catch (Exception e)
397 // e.printStackTrace();
401 final String primaryKey1 = primaryKey;
403 final Object[] summaryRowData1 = summaryRowData;
407 public Object[] getSummaryData()
409 return summaryRowData1;
413 public Object getPrimaryKey()
419 * Returns a string representation of this object;
422 public String toString()
424 StringBuilder summaryFieldValues = new StringBuilder();
425 for (Object summaryField : summaryRowData1)
427 summaryFieldValues.append(
428 summaryField == null ? " " : summaryField.toString())
431 return summaryFieldValues.toString();
435 * Returns hash code value for this object
438 public int hashCode()
440 return Objects.hash(primaryKey1, this.toString());
444 public boolean equals(Object that)
446 return this.toString().equals(that.toString());
451 public static UniProtFTSRestClient getInstance()
453 if (instance == null)
455 instance = new UniProtFTSRestClient();
461 public String getColumnDataConfigFileName()
463 return "/fts/uniprot_data_columns-2022.txt";
466 /* 2022-07-20 bsoares
467 * used for the new API "cursor" pagination. See https://www.uniprot.org/help/pagination
469 private ArrayList<String> cursors;
471 private int cursorPage = 0;
473 protected int getCursorPage()
478 protected void setCursorPage(int i)
483 protected void setPrevCursorPage()
489 protected void setNextCursorPage()
494 protected void clearCursors()
496 cursors = new ArrayList(10);
499 protected String getCursor(int i)
501 return cursors.get(i);
504 protected String getNextCursor()
506 if (cursors.size() < cursorPage + 2)
508 return cursors.get(cursorPage + 1);
511 protected String getPrevCursor()
515 return cursors.get(cursorPage - 1);
518 protected void setCursor(int i, String c)
520 cursors.ensureCapacity(i + 1);
521 while (cursors.size() <= i)
527 "Set UniprotFRSRestClient cursors[" + i + "] to '" + c + "'");
531 public static String getQueryParam(String param, String u)
533 if (param == null || u == null)
537 URL url = new URL(u);
538 String[] kevs = url.getQuery().split("&");
539 for (int j = 0; j < kevs.length; j++)
541 String[] kev = kevs[j].split("=", 2);
542 if (param.equals(kev[0]))
547 } catch (MalformedURLException e)
549 Console.warn("Could not obtain next page 'cursor' value from 'u");