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.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Objects;
31 import javax.ws.rs.core.MediaType;
33 import com.sun.jersey.api.client.Client;
34 import com.sun.jersey.api.client.ClientResponse;
35 import com.sun.jersey.api.client.WebResource;
36 import com.sun.jersey.api.client.config.DefaultClientConfig;
38 import jalview.bin.Cache;
39 import jalview.bin.Console;
40 import jalview.fts.api.FTSData;
41 import jalview.fts.api.FTSDataColumnI;
42 import jalview.fts.core.FTSRestClient;
43 import jalview.fts.core.FTSRestRequest;
44 import jalview.fts.core.FTSRestResponse;
45 import jalview.util.ChannelProperties;
46 import jalview.util.MessageManager;
47 import jalview.util.Platform;
51 * See https://issues.jalview.org/browse/JAL-4036
52 * The new Uniprot API is not dissimilar to the old one, but has some important changes.
53 * Some group names have changed slightly, some old groups have gone and there are quite a few new groups.
55 * Most changes are mappings of old column ids to new field ids. There are a handful of old
56 * columns not mapped to new fields, and new fields without an old column.
57 * [aside: not all possible columns were listed in the resources/fts/uniprot_data_columns.txt file.
58 * These were presumably additions after the file was created]
59 * For existing/mapped fields, the same preferences found in the resource file have been migrated to
60 * the new file with the new field name, id and group.
62 * The new mapped groups and files are stored and read from resources/fts/uniprot_data_columns-2022.txt.
64 * There is now no "sort" query string parameter.
66 * See https://www.uniprot.org/help/api_queries
68 * SIGNIFICANT CHANGE: Pagination is no longer performed using a record offset, but with a "cursor"
69 * query string parameter that is not really a cursor. The value is an opaque string that is passed (or
70 * rather a whole URL is passed) in the "Link" header of the HTTP response of the previous page.
71 * Where such a link is passed it is put into the cursors ArrayList.
72 * There are @Overridden methods in UniprotFTSPanel.
75 public class UniProtFTSRestClient extends FTSRestClient
77 private static final String DEFAULT_UNIPROT_DOMAIN = "https://rest.uniprot.org";
79 private static final String USER_AGENT = ChannelProperties
80 .getProperty("app_name", "Jalview") + " "
81 + Cache.getDefault("VERSION", "Unknown") + " "
82 + UniProtFTSRestClient.class.toString() + " help@jalview.org";
86 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
89 private static UniProtFTSRestClient instance = null;
91 public final String uniprotSearchEndpoint;
93 public UniProtFTSRestClient()
97 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_2022_DOMAIN",
98 DEFAULT_UNIPROT_DOMAIN) + "/uniprotkb/search";
101 @SuppressWarnings("unchecked")
103 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
106 return executeRequest(uniprotRestRequest, null);
109 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest,
110 String cursor) throws Exception
114 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
115 uniprotRestRequest.getWantedFields());
116 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
117 ? getDefaultResponsePageSize()
118 : uniprotRestRequest.getResponseSize();
120 int offSet = uniprotRestRequest.getOffSet();
122 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
124 query = uniprotRestRequest.getSearchTerm();
128 query = uniprotRestRequest.getFieldToSearchBy().equalsIgnoreCase(
129 "Search All") ? uniprotRestRequest.getSearchTerm()
131 // + uniprotRestRequest.getSearchTerm()
132 : uniprotRestRequest.getFieldToSearchBy() + ":"
133 + uniprotRestRequest.getSearchTerm();
136 // BH 2018 the trick here is to coerce the classes in Javascript to be
137 // different from the ones in Java yet still allow this to be correct for
140 Class<ClientResponse> clientResponseClass;
143 // JavaScript only -- coerce types to Java types for Java
144 client = (Client) (Object) new jalview.javascript.web.Client();
145 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
155 client = Client.create(new DefaultClientConfig());
156 clientResponseClass = ClientResponse.class;
159 WebResource webResource = null;
160 webResource = client.resource(uniprotSearchEndpoint)
161 .queryParam("format", "tsv")
162 .queryParam("fields", wantedFields)
163 .queryParam("size", String.valueOf(responseSize))
164 /* 2022 new api has no "sort"
165 * .queryParam("sort", "score")
167 .queryParam("query", query);
168 if (offSet != 0 && cursor != null && cursor.length() > 0)
169 // 2022 new api does not do pagination with an offset, it requires a
170 // "cursor" parameter with a key (given for the next page).
171 // (see https://www.uniprot.org/help/pagination)
173 webResource = webResource.queryParam("cursor", cursor);
176 "Uniprot FTS Request: " + webResource.getURI().toString());
177 // Execute the REST request
178 WebResource.Builder wrBuilder = webResource
179 .accept(MediaType.TEXT_PLAIN);
180 if (!Platform.isJS())
187 wrBuilder.header("User-Agent", USER_AGENT);
189 ClientResponse clientResponse = wrBuilder.get(clientResponseClass);
191 if (!Platform.isJS())
198 if (clientResponse.getHeaders().containsKey("Link"))
200 // extract the URL from the 'Link: <URL>; ref="stuff"' header
201 String linkHeader = clientResponse.getHeaders().get("Link")
203 if (linkHeader.indexOf("<") > -1)
205 String temp = linkHeader.substring(linkHeader.indexOf("<") + 1);
206 if (temp.indexOf(">") > -1)
208 String nextUrl = temp.substring(0, temp.indexOf(">"));
209 // then get the cursor value from the query string parameters
210 String nextCursor = getQueryParam("cursor", nextUrl);
211 setCursor(cursorPage + 1, nextCursor);
217 String uniProtTabDelimittedResponseString = clientResponse
218 .getEntity(String.class);
219 // Make redundant objects eligible for garbage collection to conserve
221 // jalview.bin.Console.outPrintln(">>>>> response : "
222 // + uniProtTabDelimittedResponseString);
223 if (clientResponse.getStatus() != 200)
225 String errorMessage = getMessageByHTTPStatusCode(
226 clientResponse.getStatus(), "Uniprot");
227 throw new Exception(errorMessage);
230 int xTotalResults = 0;
237 // new Uniprot API is not including a "X-Total-Results" header when
240 List<String> resultsHeaders = clientResponse.getHeaders()
241 .get("X-Total-Results");
242 if (resultsHeaders != null && resultsHeaders.size() >= 1)
244 xTotalResults = Integer.valueOf(resultsHeaders.get(0));
247 clientResponse = null;
249 return parseUniprotResponse(uniProtTabDelimittedResponseString,
250 uniprotRestRequest, xTotalResults);
251 } catch (Exception e)
253 Console.warn("Problem with the query: " + e.getMessage());
254 Console.debug("Exception stacktrace:", e);
255 String exceptionMsg = e.getMessage();
256 if (exceptionMsg.contains("SocketException"))
258 // No internet connection
259 throw new Exception(MessageManager.getString(
260 "exception.unable_to_detect_internet_connection"));
262 else if (exceptionMsg.contains("UnknownHostException"))
264 // The server 'http://www.uniprot.org' is unreachable
265 throw new Exception(MessageManager.formatMessage(
266 "exception.fts_server_unreachable", "Uniprot"));
275 public boolean isAdvancedQuery(String query)
277 if (query.contains(" AND ") || query.contains(" OR ")
278 || query.contains(" NOT ") || query.contains(" ! ")
279 || query.contains(" || ") || query.contains(" && ")
280 || query.contains(":") || query.contains("-"))
287 public FTSRestResponse parseUniprotResponse(
288 String uniProtTabDelimittedResponseString,
289 FTSRestRequest uniprotRestRequest, int xTotalResults)
291 FTSRestResponse searchResult = new FTSRestResponse();
292 List<FTSData> result = null;
293 if (uniProtTabDelimittedResponseString == null
294 || uniProtTabDelimittedResponseString.trim().isEmpty())
296 searchResult.setNumberOfItemsFound(0);
299 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
300 if (foundDataRow != null && foundDataRow.length > 0)
302 result = new ArrayList<>();
303 boolean firstRow = true;
304 for (String dataRow : foundDataRow)
306 // The first data row is usually the header data. This should be
307 // filtered out from the rest of the data See: JAL-2485
313 // jalview.bin.Console.outPrintln(dataRow);
314 result.add(getFTSData(dataRow, uniprotRestRequest));
316 searchResult.setNumberOfItemsFound(xTotalResults);
317 searchResult.setSearchSummary(result);
323 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
325 // * tab delimited string.
327 // * @param dataColumnFields
328 // * the collection of FTSDataColumnI to process
329 // * @return the generated comma delimited string from the supplied
330 // * FTSDataColumnI collection
332 // private String getDataColumnsFieldsAsTabDelimitedString(
333 // Collection<FTSDataColumnI> dataColumnFields)
335 // String result = "";
336 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
338 // StringBuilder returnedFields = new StringBuilder();
339 // for (FTSDataColumnI field : dataColumnFields)
341 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
343 // returnedFields.append("\t").append("Entry");
347 // returnedFields.append("\t").append(field.getName());
350 // returnedFields.deleteCharAt(0);
351 // result = returnedFields.toString();
356 public static FTSData getFTSData(String tabDelimittedDataStr,
357 FTSRestRequest request)
359 String primaryKey = null;
361 Object[] summaryRowData;
363 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
365 summaryRowData = new Object[diplayFields.size()];
366 String[] columns = tabDelimittedDataStr.split("\t");
367 for (FTSDataColumnI field : diplayFields)
371 String fieldData = columns[colCounter];
372 if (field.isPrimaryKeyColumn())
374 primaryKey = fieldData;
375 summaryRowData[colCounter++] = primaryKey;
377 else if (fieldData == null || fieldData.isEmpty())
379 summaryRowData[colCounter++] = null;
385 summaryRowData[colCounter++] = (field.getDataType()
386 .getDataTypeClass() == Integer.class)
387 ? Integer.valueOf(fieldData.replace(",", ""))
388 : (field.getDataType()
389 .getDataTypeClass() == Double.class)
390 ? Double.valueOf(fieldData)
392 } catch (Exception e)
395 jalview.bin.Console.outPrintln("offending value:" + fieldData);
398 } catch (Exception e)
400 // e.printStackTrace();
404 final String primaryKey1 = primaryKey;
406 final Object[] summaryRowData1 = summaryRowData;
410 public Object[] getSummaryData()
412 return summaryRowData1;
416 public Object getPrimaryKey()
422 * Returns a string representation of this object;
425 public String toString()
427 StringBuilder summaryFieldValues = new StringBuilder();
428 for (Object summaryField : summaryRowData1)
430 summaryFieldValues.append(
431 summaryField == null ? " " : summaryField.toString())
434 return summaryFieldValues.toString();
438 * Returns hash code value for this object
441 public int hashCode()
443 return Objects.hash(primaryKey1, this.toString());
447 public boolean equals(Object that)
449 return this.toString().equals(that.toString());
454 public static UniProtFTSRestClient getInstance()
456 if (instance == null)
458 instance = new UniProtFTSRestClient();
464 public String getColumnDataConfigFileName()
466 return "/fts/uniprot_data_columns-2022.txt";
469 /* 2022-07-20 bsoares
470 * used for the new API "cursor" pagination. See https://www.uniprot.org/help/pagination
472 private ArrayList<String> cursors;
474 private int cursorPage = 0;
476 protected int getCursorPage()
481 protected void setCursorPage(int i)
486 protected void setPrevCursorPage()
492 protected void setNextCursorPage()
497 protected void clearCursors()
499 cursors = new ArrayList(10);
502 protected String getCursor(int i)
504 return cursors.get(i);
507 protected String getNextCursor()
509 if (cursors.size() < cursorPage + 2)
511 return cursors.get(cursorPage + 1);
514 protected String getPrevCursor()
518 return cursors.get(cursorPage - 1);
521 protected void setCursor(int i, String c)
523 cursors.ensureCapacity(i + 1);
524 while (cursors.size() <= i)
530 "Set UniprotFRSRestClient cursors[" + i + "] to '" + c + "'");
534 public static String getQueryParam(String param, String u)
536 if (param == null || u == null)
540 URL url = new URL(u);
541 String[] kevs = url.getQuery().split("&");
542 for (int j = 0; j < kevs.length; j++)
544 String[] kev = kevs[j].split("=", 2);
545 if (param.equals(kev[0]))
550 } catch (MalformedURLException e)
552 Console.warn("Could not obtain next page 'cursor' value from 'u");