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.ApplicationSingletonProvider;
25 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
26 import java.lang.invoke.MethodHandles;
27 import java.net.MalformedURLException;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Objects;
35 import javax.ws.rs.core.MediaType;
37 import com.sun.jersey.api.client.Client;
38 import com.sun.jersey.api.client.ClientResponse;
39 import com.sun.jersey.api.client.WebResource;
40 import com.sun.jersey.api.client.config.DefaultClientConfig;
42 import jalview.bin.Cache;
43 import jalview.bin.Console;
44 import jalview.fts.api.FTSData;
45 import jalview.fts.api.FTSDataColumnI;
46 import jalview.fts.api.FTSRestClientI;
47 import jalview.fts.core.FTSRestClient;
48 import jalview.fts.core.FTSRestRequest;
49 import jalview.fts.core.FTSRestResponse;
50 import jalview.util.ChannelProperties;
51 import jalview.util.MessageManager;
52 import jalview.util.Platform;
56 * See https://issues.jalview.org/browse/JAL-4036
57 * The new Uniprot API is not dissimilar to the old one, but has some important changes.
58 * Some group names have changed slightly, some old groups have gone and there are quite a few new groups.
60 * Most changes are mappings of old column ids to new field ids. There are a handful of old
61 * columns not mapped to new fields, and new fields without an old column.
62 * [aside: not all possible columns were listed in the resources/fts/uniprot_data_columns.txt file.
63 * These were presumably additions after the file was created]
64 * For existing/mapped fields, the same preferences found in the resource file have been migrated to
65 * the new file with the new field name, id and group.
67 * The new mapped groups and files are stored and read from resources/fts/uniprot_data_columns-2022.txt.
69 * There is now no "sort" query string parameter.
71 * See https://www.uniprot.org/help/api_queries
73 * SIGNIFICANT CHANGE: Pagination is no longer performed using a record offset, but with a "cursor"
74 * query string parameter that is not really a cursor. The value is an opaque string that is passed (or
75 * rather a whole URL is passed) in the "Link" header of the HTTP response of the previous page.
76 * Where such a link is passed it is put into the cursors ArrayList.
77 * There are @Overridden methods in UniprotFTSPanel.
79 public class UniProtFTSRestClient extends FTSRestClient
80 implements ApplicationSingletonI,FTSRestClientI
82 public static UniProtFTSRestClient getInstance()
84 return (UniProtFTSRestClient) ApplicationSingletonProvider
85 .getInstance(UniProtFTSRestClient.class);
87 private static final String DEFAULT_UNIPROT_DOMAIN = "https://rest.uniprot.org";
89 private static final String USER_AGENT = ChannelProperties
90 .getProperty("app_name", "Jalview") + " "
91 + Cache.getDefault("VERSION", "Unknown") + " "
92 + MethodHandles.lookup().lookupClass() + " help@jalview.org";
96 Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN);
100 public final String uniprotSearchEndpoint;
102 private UniProtFTSRestClient()
106 uniprotSearchEndpoint = Cache.getDefault("UNIPROT_2022_DOMAIN",
107 DEFAULT_UNIPROT_DOMAIN) + "/uniprotkb/search";
110 @SuppressWarnings("unchecked")
112 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest)
115 return executeRequest(uniprotRestRequest, null);
118 public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest,
119 String cursor) throws Exception
123 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
124 uniprotRestRequest.getWantedFields());
125 int responseSize = (uniprotRestRequest.getResponseSize() == 0)
126 ? getDefaultResponsePageSize()
127 : uniprotRestRequest.getResponseSize();
129 int offSet = uniprotRestRequest.getOffSet();
131 if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
133 query = uniprotRestRequest.getSearchTerm();
137 query = uniprotRestRequest.getFieldToSearchBy().equalsIgnoreCase(
138 "Search All") ? uniprotRestRequest.getSearchTerm()
140 // + uniprotRestRequest.getSearchTerm()
141 : uniprotRestRequest.getFieldToSearchBy() + ":"
142 + uniprotRestRequest.getSearchTerm();
145 // BH 2018 the trick here is to coerce the classes in Javascript to be
146 // different from the ones in Java yet still allow this to be correct for
149 Class<ClientResponse> clientResponseClass;
152 // JavaScript only -- coerce types to Java types for Java
153 client = (Client) (Object) new jalview.javascript.web.Client();
154 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
164 client = Client.create(new DefaultClientConfig());
165 clientResponseClass = ClientResponse.class;
168 WebResource webResource = null;
169 webResource = client.resource(uniprotSearchEndpoint)
170 .queryParam("format", "tsv")
171 .queryParam("fields", wantedFields)
172 .queryParam("size", String.valueOf(responseSize))
173 /* 2022 new api has no "sort"
174 * .queryParam("sort", "score")
176 .queryParam("query", query);
177 if (offSet != 0 && cursor != null && cursor.length() > 0)
178 // 2022 new api does not do pagination with an offset, it requires a
179 // "cursor" parameter with a key (given for the next page).
180 // (see https://www.uniprot.org/help/pagination)
182 webResource = webResource.queryParam("cursor", cursor);
185 "Uniprot FTS Request: " + webResource.getURI().toString());
186 // Execute the REST request
187 WebResource.Builder wrBuilder = webResource
188 .accept(MediaType.TEXT_PLAIN);
189 if (!Platform.isJS())
196 wrBuilder.header("User-Agent", USER_AGENT);
198 ClientResponse clientResponse = wrBuilder.get(clientResponseClass);
200 if (!Platform.isJS())
207 if (clientResponse.getHeaders().containsKey("Link"))
209 // extract the URL from the 'Link: <URL>; ref="stuff"' header
210 String linkHeader = clientResponse.getHeaders().get("Link")
212 if (linkHeader.indexOf("<") > -1)
214 String temp = linkHeader.substring(linkHeader.indexOf("<") + 1);
215 if (temp.indexOf(">") > -1)
217 String nextUrl = temp.substring(0, temp.indexOf(">"));
218 // then get the cursor value from the query string parameters
219 String nextCursor = getQueryParam("cursor", nextUrl);
220 setCursor(cursorPage + 1, nextCursor);
225 String uniProtTabDelimittedResponseString = clientResponse
226 .getEntity(String.class);
227 // Make redundant objects eligible for garbage collection to conserve
229 // System.out.println(">>>>> response : "
230 // + uniProtTabDelimittedResponseString);
231 if (clientResponse.getStatus() != 200)
233 String errorMessage = getMessageByHTTPStatusCode(
234 clientResponse.getStatus(), "Uniprot");
235 throw new Exception(errorMessage);
238 // new Uniprot API is not including a "X-Total-Results" header when there
240 List<String> resultsHeaders = clientResponse.getHeaders()
241 .get("X-Total-Results");
242 int xTotalResults = 0;
247 else if (resultsHeaders != null && resultsHeaders.size() >= 1)
249 xTotalResults = Integer.valueOf(resultsHeaders.get(0));
251 clientResponse = null;
253 return parseUniprotResponse(uniProtTabDelimittedResponseString,
254 uniprotRestRequest, xTotalResults);
255 } catch (Exception e)
257 Console.warn("Problem with the query: " + e.getMessage());
258 Console.debug("Exception stacktrace:", e);
259 String exceptionMsg = e.getMessage();
260 if (exceptionMsg.contains("SocketException"))
262 // No internet connection
263 throw new Exception(MessageManager.getString(
264 "exception.unable_to_detect_internet_connection"));
266 else if (exceptionMsg.contains("UnknownHostException"))
268 // The server 'http://www.uniprot.org' is unreachable
269 throw new Exception(MessageManager.formatMessage(
270 "exception.fts_server_unreachable", "Uniprot"));
279 public boolean isAdvancedQuery(String query)
281 if (query.contains(" AND ") || query.contains(" OR ")
282 || query.contains(" NOT ") || query.contains(" ! ")
283 || query.contains(" || ") || query.contains(" && ")
284 || query.contains(":") || query.contains("-"))
291 public FTSRestResponse parseUniprotResponse(
292 String uniProtTabDelimittedResponseString,
293 FTSRestRequest uniprotRestRequest, int xTotalResults)
295 FTSRestResponse searchResult = new FTSRestResponse();
296 List<FTSData> result = null;
297 if (uniProtTabDelimittedResponseString == null
298 || uniProtTabDelimittedResponseString.trim().isEmpty())
300 searchResult.setNumberOfItemsFound(0);
303 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
304 if (foundDataRow != null && foundDataRow.length > 0)
306 result = new ArrayList<>();
307 boolean firstRow = true;
308 for (String dataRow : foundDataRow)
310 // The first data row is usually the header data. This should be
311 // filtered out from the rest of the data See: JAL-2485
317 // System.out.println(dataRow);
318 result.add(getFTSData(dataRow, uniprotRestRequest));
320 searchResult.setNumberOfItemsFound(xTotalResults);
321 searchResult.setSearchSummary(result);
327 // * Takes a collection of FTSDataColumnI and converts its 'code' values into
329 // * tab delimited string.
331 // * @param dataColumnFields
332 // * the collection of FTSDataColumnI to process
333 // * @return the generated comma delimited string from the supplied
334 // * FTSDataColumnI collection
336 // private String getDataColumnsFieldsAsTabDelimitedString(
337 // Collection<FTSDataColumnI> dataColumnFields)
339 // String result = "";
340 // if (dataColumnFields != null && !dataColumnFields.isEmpty())
342 // StringBuilder returnedFields = new StringBuilder();
343 // for (FTSDataColumnI field : dataColumnFields)
345 // if (field.getName().equalsIgnoreCase("Uniprot Id"))
347 // returnedFields.append("\t").append("Entry");
351 // returnedFields.append("\t").append(field.getName());
354 // returnedFields.deleteCharAt(0);
355 // result = returnedFields.toString();
360 public static FTSData getFTSData(String tabDelimittedDataStr,
361 FTSRestRequest request)
363 String primaryKey = null;
365 Object[] summaryRowData;
367 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
369 summaryRowData = new Object[diplayFields.size()];
370 String[] columns = tabDelimittedDataStr.split("\t");
371 for (FTSDataColumnI field : diplayFields)
375 String fieldData = columns[colCounter];
376 if (field.isPrimaryKeyColumn())
378 primaryKey = fieldData;
379 summaryRowData[colCounter++] = primaryKey;
381 else if (fieldData == null || fieldData.isEmpty())
383 summaryRowData[colCounter++] = null;
389 summaryRowData[colCounter++] = (field.getDataType()
390 .getDataTypeClass() == Integer.class)
391 ? Integer.valueOf(fieldData.replace(",", ""))
392 : (field.getDataType()
393 .getDataTypeClass() == Double.class)
394 ? Double.valueOf(fieldData)
396 } catch (Exception e)
399 System.out.println("offending value:" + fieldData);
402 } catch (Exception e)
404 // e.printStackTrace();
408 final String primaryKey1 = primaryKey;
410 final Object[] summaryRowData1 = summaryRowData;
414 public Object[] getSummaryData()
416 return summaryRowData1;
420 public Object getPrimaryKey()
426 * Returns a string representation of this object;
429 public String toString()
431 StringBuilder summaryFieldValues = new StringBuilder();
432 for (Object summaryField : summaryRowData1)
434 summaryFieldValues.append(
435 summaryField == null ? " " : summaryField.toString())
438 return summaryFieldValues.toString();
442 * Returns hash code value for this object
445 public int hashCode()
447 return Objects.hash(primaryKey1, this.toString());
451 public boolean equals(Object that)
453 return this.toString().equals(that.toString());
460 public String getColumnDataConfigFileName()
462 return "/fts/uniprot_data_columns-2022.txt";
465 /* 2022-07-20 bsoares
466 * used for the new API "cursor" pagination. See https://www.uniprot.org/help/pagination
468 private ArrayList<String> cursors;
470 private int cursorPage = 0;
472 protected int getCursorPage()
477 protected void setCursorPage(int i)
482 protected void setPrevCursorPage()
488 protected void setNextCursorPage()
493 protected void clearCursors()
495 cursors = new ArrayList(10);
498 protected String getCursor(int i)
500 return cursors.get(i);
503 protected String getNextCursor()
505 if (cursors.size() < cursorPage + 2)
507 return cursors.get(cursorPage + 1);
510 protected String getPrevCursor()
514 return cursors.get(cursorPage - 1);
517 protected void setCursor(int i, String c)
519 cursors.ensureCapacity(i + 1);
520 while (cursors.size() <= i)
526 "Set UniprotFRSRestClient cursors[" + i + "] to '" + c + "'");
530 public static String getQueryParam(String param, String u)
532 if (param == null || u == null)
536 URL url = new URL(u);
537 String[] kevs = url.getQuery().split("&");
538 for (int j = 0; j < kevs.length; j++)
540 String[] kev = kevs[j].split("=", 2);
541 if (param.equals(kev[0]))
546 } catch (MalformedURLException e)
548 Console.warn("Could not obtain next page 'cursor' value from 'u");