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.fts.api.FTSData;
25 import jalview.fts.api.FTSDataColumnI;
26 import jalview.fts.api.FTSRestClientI;
27 import jalview.fts.core.FTSRestClient;
28 import jalview.fts.core.FTSRestRequest;
29 import jalview.fts.core.FTSRestResponse;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Objects;
36 import javax.ws.rs.core.MediaType;
38 import com.sun.jersey.api.client.Client;
39 import com.sun.jersey.api.client.ClientResponse;
40 import com.sun.jersey.api.client.WebResource;
41 import com.sun.jersey.api.client.config.ClientConfig;
42 import com.sun.jersey.api.client.config.DefaultClientConfig;
44 public class UniProtFTSRestClient extends FTSRestClient
46 private static FTSRestClientI instance = null;
48 public static final String UNIPROT_SEARCH_ENDPOINT = "http://www.uniprot.org/uniprot/?";
51 public FTSRestResponse executeRequest(FTSRestRequest uniportRestRequest)
53 ClientConfig clientConfig = new DefaultClientConfig();
54 Client client = Client.create(clientConfig);
56 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(uniportRestRequest
58 int responseSize = (uniportRestRequest.getResponseSize() == 0) ? getDefaultResponsePageSize()
59 : uniportRestRequest.getResponseSize();
61 int offSet = uniportRestRequest.getOffSet();
63 String query = uniportRestRequest.getFieldToSearchBy()
64 .equalsIgnoreCase("Search All") ? uniportRestRequest
66 : uniportRestRequest.getFieldToSearchBy() + ":"
67 + uniportRestRequest.getSearchTerm();
69 // + (uniportRestRequest.isAllowUnpublishedEntries() ? ""
70 // : " AND status:REL");
71 // System.out.println(">>>>> Query : " + query);
72 // System.out.println(">>>>> Columns : " + wantedFields);
73 // System.out.println(">>>>> Response size: " + responseSize
76 WebResource webResource = null;
77 webResource = client.resource(UNIPROT_SEARCH_ENDPOINT)
78 .queryParam("format", "tab")
79 .queryParam("columns", wantedFields)
80 .queryParam("limit", String.valueOf(responseSize))
81 .queryParam("offset", String.valueOf(offSet))
82 .queryParam("sort", "score")
83 .queryParam("query", query);
84 // Execute the REST request
85 ClientResponse clientResponse = webResource
86 .accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
87 String uniProtTabDelimittedResponseString = clientResponse
88 .getEntity(String.class);
89 // Make redundant objects eligible for garbage collection to conserve
91 clientResponse = null;
93 // System.out.println(">>>>> response : "
94 // + uniProtTabDelimittedResponseString);
95 return parseUniprotResponse(uniProtTabDelimittedResponseString,
100 public FTSRestResponse parseUniprotResponse(
101 String uniProtTabDelimittedResponseString,
102 FTSRestRequest uniprotRestRequest)
104 FTSRestResponse searchResult = new FTSRestResponse();
105 List<FTSData> result = null;
106 if (uniProtTabDelimittedResponseString == null
107 || uniProtTabDelimittedResponseString.trim().isEmpty())
109 searchResult.setNumberOfItemsFound(0);
112 String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n");
113 if (foundDataRow != null && foundDataRow.length > 0)
115 result = new ArrayList<FTSData>();
116 String titleRow = getDataColumnsFieldsAsTabDelimitedString(uniprotRestRequest
118 // System.out.println(">>>>Title row : " + titleRow);
119 for (String dataRow : foundDataRow)
121 if (dataRow.equalsIgnoreCase(titleRow))
123 // System.out.println(">>>>>>>>>> matched!!!");
126 // System.out.println(dataRow);
127 result.add(getFTSData(dataRow, uniprotRestRequest));
129 searchResult.setNumberOfItemsFound(result.size());
130 searchResult.setSearchSummary(result);
136 * Takes a collection of FTSDataColumnI and converts its 'code' values into a
137 * tab delimited string.
139 * @param dataColumnFields
140 * the collection of FTSDataColumnI to process
141 * @return the generated comma delimited string from the supplied
142 * FTSDataColumnI collection
144 private String getDataColumnsFieldsAsTabDelimitedString(
145 Collection<FTSDataColumnI> dataColumnFields)
148 if (dataColumnFields != null && !dataColumnFields.isEmpty())
150 StringBuilder returnedFields = new StringBuilder();
151 for (FTSDataColumnI field : dataColumnFields)
153 if (field.getName().equalsIgnoreCase("Uniprot Id"))
155 returnedFields.append("\t").append("Entry");
159 returnedFields.append("\t").append(field.getName());
162 returnedFields.deleteCharAt(0);
163 result = returnedFields.toString();
167 public static FTSData getFTSData(String tabDelimittedDataStr,
168 FTSRestRequest request)
170 String primaryKey = null;
172 Object[] summaryRowData;
174 Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
176 summaryRowData = new Object[diplayFields.size()];
177 String[] columns = tabDelimittedDataStr.split("\t");
178 for (FTSDataColumnI field : diplayFields)
182 String fieldData = columns[colCounter];
183 if (field.isPrimaryKeyColumn())
185 primaryKey = fieldData;
186 summaryRowData[colCounter++] = primaryKey;
188 else if (fieldData == null || fieldData.isEmpty())
190 summaryRowData[colCounter++] = null;
196 summaryRowData[colCounter++] = (field.getDataColumnClass() == Integer.class) ? Integer
198 : (field.getDataColumnClass() == Double.class) ? Double
199 .valueOf(fieldData) : fieldData;
200 } catch (Exception e)
203 System.out.println("offending value:" + fieldData);
206 } catch (Exception e)
208 // e.printStackTrace();
212 final String primaryKey1 = primaryKey;
214 final Object[] summaryRowData1 = summaryRowData;
218 public Object[] getSummaryData()
220 return summaryRowData1;
224 public Object getPrimaryKey()
230 * Returns a string representation of this object;
233 public String toString()
235 StringBuilder summaryFieldValues = new StringBuilder();
236 for (Object summaryField : summaryRowData1)
238 summaryFieldValues.append(
239 summaryField == null ? " " : summaryField.toString())
242 return summaryFieldValues.toString();
246 * Returns hash code value for this object
249 public int hashCode()
251 return Objects.hash(primaryKey1, this.toString());
257 public static FTSRestClientI getInstance()
259 if (instance == null)
261 instance = new UniProtFTSRestClient();
267 public String getColumnDataConfigFileName()
269 return "/fts/uniprot_data_columns.txt";