X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Ffts%2Fservice%2Funiprot%2FUniProtFTSRestClient.java;fp=src%2Fjalview%2Ffts%2Fservice%2Funiprot%2FUniProtFTSRestClient.java;h=f92016631cdba330a1ff01662277ad7b1790144e;hb=659cdd6d20a562ff4a856f2325399f2dbb398126;hp=0000000000000000000000000000000000000000;hpb=49a16230c3aa7081afe8a6c502bac36b46b25eee;p=jalview.git diff --git a/src/jalview/fts/service/uniprot/UniProtFTSRestClient.java b/src/jalview/fts/service/uniprot/UniProtFTSRestClient.java new file mode 100644 index 0000000..f920166 --- /dev/null +++ b/src/jalview/fts/service/uniprot/UniProtFTSRestClient.java @@ -0,0 +1,223 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +package jalview.fts.service.uniprot; + +import jalview.fts.api.FTSData; +import jalview.fts.api.FTSDataColumnI; +import jalview.fts.api.FTSRestClientI; +import jalview.fts.core.FTSRestClient; +import jalview.fts.core.FTSRestRequest; +import jalview.fts.core.FTSRestResponse; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +import javax.ws.rs.core.MediaType; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; + +public class UniProtFTSRestClient extends FTSRestClient +{ + private static FTSRestClientI instance = null; + + public static final String UNIPROT_SEARCH_ENDPOINT = "http://www.uniprot.org/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() + : uniportRestRequest.getResponseSize(); + + String query = uniportRestRequest.getFieldToSearchBy() + ":" + + uniportRestRequest.getSearchTerm(); + // + (uniportRestRequest.isAllowUnpublishedEntries() ? "" + // : " AND status:REL"); + // System.out.println(">>>>> Query : " + query); + // System.out.println(">>>>> Columns : " + wantedFields); + WebResource webResource = null; + webResource = client.resource(UNIPROT_SEARCH_ENDPOINT) + .queryParam("format", "tab") + .queryParam("columns", wantedFields) + .queryParam("limit", String.valueOf(responseSize)) + .queryParam("query", query); + // Execute the REST request + 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 + // memory + clientResponse = null; + client = null; + // System.out.println(">>>>> response : " + // + uniProtTabDelimittedResponseString); + return parseUniprotResponse(uniProtTabDelimittedResponseString, + uniportRestRequest); + + } + + public FTSRestResponse parseUniprotResponse( + String uniProtTabDelimittedResponseString, + FTSRestRequest uniprotRestRequest) + { + FTSRestResponse searchResult = new FTSRestResponse(); + List result = null; + String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n"); + if (foundDataRow != null && foundDataRow.length > 0) + { + result = new ArrayList(); + String titleRow = getDataColumnsFieldsAsTabDelimitedString(uniprotRestRequest + .getWantedFields()); + // System.out.println(">>>>Title row : " + titleRow); + for (String dataRow : foundDataRow) + { + if (dataRow.equalsIgnoreCase(titleRow)) + { + // System.out.println(">>>>>>>>>> matched!!!"); + continue; + } + // System.out.println(dataRow); + result.add(getFTSData(dataRow, uniprotRestRequest)); + } + searchResult.setNumberOfItemsFound(result.size()); + searchResult.setSearchSummary(result); + } + return searchResult; + } + + public static FTSData getFTSData(String tabDelimittedDataStr, + FTSRestRequest request) + { + String primaryKey = null; + + Object[] summaryRowData; + + Collection diplayFields = request.getWantedFields(); + int colCounter = 0; + summaryRowData = new Object[diplayFields.size()]; + String[] columns = tabDelimittedDataStr.split("\t"); + for (FTSDataColumnI field : diplayFields) + { + try + { + String fieldData = columns[colCounter]; + if (field.isPrimaryKeyColumn()) + { + primaryKey = fieldData; + summaryRowData[colCounter++] = primaryKey; + } + else if (fieldData == null || fieldData.isEmpty()) + { + summaryRowData[colCounter++] = null; + } + else + { + try + { + summaryRowData[colCounter++] = (field.getDataColumnClass() == Integer.class) ? Integer + .valueOf(fieldData) + : (field.getDataColumnClass() == Double.class) ? Double + .valueOf(fieldData) : fieldData; + } catch (Exception e) + { + e.printStackTrace(); + System.out.println("offending value:" + fieldData); + } + } + } catch (Exception e) + { + // e.printStackTrace(); + } + } + + final String primaryKey1 = primaryKey; + + final Object[] summaryRowData1 = summaryRowData; + return new FTSData() + { + @Override + public Object[] getSummaryData() + { + return summaryRowData1; + } + + @Override + public Object getPrimaryKey() + { + return primaryKey1; + } + + /** + * Returns a string representation of this object; + */ + @Override + public String toString() + { + StringBuilder summaryFieldValues = new StringBuilder(); + for (Object summaryField : summaryRowData1) + { + summaryFieldValues.append( + summaryField == null ? " " : summaryField.toString()) + .append("\t"); + } + return summaryFieldValues.toString(); + } + + /** + * Returns hash code value for this object + */ + @Override + public int hashCode() + { + return Objects.hash(primaryKey1, this.toString()); + } + }; + } + + + public static FTSRestClientI getInstance() + { + if (instance == null) + { + instance = new UniProtFTSRestClient(); + } + return instance; + } + + @Override + public String getColumnDataConfigFileName() + { + return getResourceFile("/fts/uniprot_data_columns.conf"); + } + +}