package jalview.fts.service.uniprot;
+import jalview.bin.Cache;
import jalview.fts.api.FTSData;
import jalview.fts.api.FTSDataColumnI;
import jalview.fts.api.FTSRestClientI;
public class UniProtFTSRestClient extends FTSRestClient
{
+ private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
+
private static FTSRestClientI instance = null;
- public static final String UNIPROT_SEARCH_ENDPOINT = "http://www.uniprot.org/uniprot/?";
+ public final String uniprotSearchEndpoint;
+
+ public UniProtFTSRestClient()
+ {
+ super();
+ uniprotSearchEndpoint = Cache.getDefault("UNIPROT_DOMAIN",
+ DEFAULT_UNIPROT_DOMAIN) + "/uniprot/?";
+ }
@Override
public FTSRestResponse executeRequest(FTSRestRequest uniportRestRequest)
ClientConfig clientConfig = new DefaultClientConfig();
Client client = Client.create(clientConfig);
- String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(uniportRestRequest
- .getWantedFields());
- int responseSize = (uniportRestRequest.getResponseSize() == 0) ? getDefaultResponsePageSize()
+ String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
+ uniportRestRequest.getWantedFields());
+ int responseSize = (uniportRestRequest.getResponseSize() == 0)
+ ? getDefaultResponsePageSize()
: uniportRestRequest.getResponseSize();
int offSet = uniportRestRequest.getOffSet();
+ String query;
+ if (isAdvancedQuery(uniportRestRequest.getSearchTerm()))
+ {
+ query = uniportRestRequest.getSearchTerm();
+ }
+ else
+ {
+ query = uniportRestRequest.getFieldToSearchBy()
+ .equalsIgnoreCase("Search All")
+ ? uniportRestRequest.getSearchTerm()
+ + " or mnemonic:"
+ + uniportRestRequest.getSearchTerm()
+ : uniportRestRequest.getFieldToSearchBy() + ":"
+ + uniportRestRequest.getSearchTerm();
+ }
- String query = uniportRestRequest.getFieldToSearchBy()
- .equalsIgnoreCase("Search All") ? uniportRestRequest
- .getSearchTerm() : uniportRestRequest.getFieldToSearchBy()
- + ":" + uniportRestRequest.getSearchTerm();
-
- // + (uniportRestRequest.isAllowUnpublishedEntries() ? ""
- // : " AND status:REL");
- // System.out.println(">>>>> Query : " + query);
- // System.out.println(">>>>> Columns : " + wantedFields);
- // System.out.println(">>>>> Response size: " + responseSize
- // + " offset : "
- // + offSet);
WebResource webResource = null;
- webResource = client.resource(UNIPROT_SEARCH_ENDPOINT)
+ webResource = client.resource(uniprotSearchEndpoint)
.queryParam("format", "tab")
.queryParam("columns", wantedFields)
.queryParam("limit", String.valueOf(responseSize))
.queryParam("offset", String.valueOf(offSet))
.queryParam("sort", "score").queryParam("query", query);
// Execute the REST request
- ClientResponse clientResponse = webResource.accept(
- MediaType.TEXT_PLAIN).get(ClientResponse.class);
+ ClientResponse clientResponse = webResource
+ .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
String uniProtTabDelimittedResponseString = clientResponse
.getEntity(String.class);
// Make redundant objects eligible for garbage collection to conserve
throw new Exception(errorMessage);
}
- int xTotalResults = Integer.valueOf(clientResponse.getHeaders()
- .get("X-Total-Results").get(0));
+ int xTotalResults = Integer.valueOf(
+ clientResponse.getHeaders().get("X-Total-Results").get(0));
clientResponse = null;
client = null;
return parseUniprotResponse(uniProtTabDelimittedResponseString,
if (exceptionMsg.contains("SocketException"))
{
// No internet connection
- throw new Exception(
- MessageManager
- .getString("exception.unable_to_detect_internet_connection"));
+ throw new Exception(MessageManager.getString(
+ "exception.unable_to_detect_internet_connection"));
}
else if (exceptionMsg.contains("UnknownHostException"))
{
}
}
+ public boolean isAdvancedQuery(String query)
+ {
+ if (query.contains(" AND ") || query.contains(" OR ")
+ || query.contains(" NOT ") || query.contains(" ! ")
+ || query.contains(" || ") || query.contains(" && ")
+ || query.contains(":") || query.contains("-"))
+ {
+ return true;
+ }
+ return false;
+ }
public FTSRestResponse parseUniprotResponse(
String uniProtTabDelimittedResponseString,
String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
if (foundDataRow != null && foundDataRow.length > 0)
{
- result = new ArrayList<FTSData>();
- String titleRow = getDataColumnsFieldsAsTabDelimitedString(uniprotRestRequest
- .getWantedFields());
- // System.out.println(">>>>Title row : " + titleRow);
+ result = new ArrayList<>();
+ boolean firstRow = true;
for (String dataRow : foundDataRow)
{
- if (dataRow.equalsIgnoreCase(titleRow))
+ // The first data row is usually the header data. This should be
+ // filtered out from the rest of the data See: JAL-2485
+ if (firstRow)
{
- // System.out.println(">>>>>>>>>> matched!!!");
+ firstRow = false;
continue;
}
// System.out.println(dataRow);
{
try
{
- summaryRowData[colCounter++] = (field.getDataColumnClass() == Integer.class) ? Integer
- .valueOf(fieldData)
- : (field.getDataColumnClass() == Double.class) ? Double
- .valueOf(fieldData) : fieldData;
+ summaryRowData[colCounter++] = (field.getDataType()
+ .getDataTypeClass() == Integer.class)
+ ? Integer.valueOf(fieldData.replace(",", ""))
+ : (field.getDataType()
+ .getDataTypeClass() == Double.class)
+ ? Double.valueOf(fieldData)
+ : fieldData;
} catch (Exception e)
{
e.printStackTrace();
- System.out.println("offending value:" + fieldData);
+ System.out.println("offending value:" + fieldData);
}
}
} catch (Exception e)
};
}
-
public static FTSRestClientI getInstance()
{
if (instance == null)