1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package jalview.fts.service.uniprot; |
23 |
|
|
24 |
|
import jalview.bin.Cache; |
25 |
|
import jalview.fts.api.FTSData; |
26 |
|
import jalview.fts.api.FTSDataColumnI; |
27 |
|
import jalview.fts.api.FTSRestClientI; |
28 |
|
import jalview.fts.core.FTSRestClient; |
29 |
|
import jalview.fts.core.FTSRestRequest; |
30 |
|
import jalview.fts.core.FTSRestResponse; |
31 |
|
import jalview.util.MessageManager; |
32 |
|
import jalview.util.Platform; |
33 |
|
|
34 |
|
import java.util.ArrayList; |
35 |
|
import java.util.Collection; |
36 |
|
import java.util.List; |
37 |
|
import java.util.Objects; |
38 |
|
|
39 |
|
import javax.ws.rs.core.MediaType; |
40 |
|
|
41 |
|
import com.sun.jersey.api.client.Client; |
42 |
|
import com.sun.jersey.api.client.ClientResponse; |
43 |
|
import com.sun.jersey.api.client.WebResource; |
44 |
|
import com.sun.jersey.api.client.config.DefaultClientConfig; |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 137 (137) |
Complexity: 44 |
Complexity Density: 0.5 |
|
46 |
|
public class UniProtFTSRestClient extends FTSRestClient |
47 |
|
{ |
48 |
|
private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org"; |
49 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
50 |
0 |
static... |
51 |
|
{ |
52 |
0 |
Platform.addJ2SDirectDatabaseCall(DEFAULT_UNIPROT_DOMAIN); |
53 |
|
} |
54 |
|
|
55 |
|
private static FTSRestClientI instance = null; |
56 |
|
|
57 |
|
public final String uniprotSearchEndpoint; |
58 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
59 |
0 |
public UniProtFTSRestClient()... |
60 |
|
{ |
61 |
0 |
super(); |
62 |
0 |
uniprotSearchEndpoint = Cache.getDefault("UNIPROT_DOMAIN", |
63 |
|
DEFAULT_UNIPROT_DOMAIN) + "/uniprot/"; |
64 |
|
} |
65 |
|
|
|
|
| 0% |
Uncovered Elements: 48 (48) |
Complexity: 10 |
Complexity Density: 0.31 |
|
66 |
0 |
@SuppressWarnings("unchecked")... |
67 |
|
@Override |
68 |
|
public FTSRestResponse executeRequest(FTSRestRequest uniprotRestRequest) |
69 |
|
throws Exception |
70 |
|
{ |
71 |
0 |
try |
72 |
|
{ |
73 |
0 |
String wantedFields = getDataColumnsFieldsAsCommaDelimitedString( |
74 |
|
uniprotRestRequest.getWantedFields()); |
75 |
0 |
int responseSize = (uniprotRestRequest.getResponseSize() == 0) |
76 |
|
? getDefaultResponsePageSize() |
77 |
|
: uniprotRestRequest.getResponseSize(); |
78 |
|
|
79 |
0 |
int offSet = uniprotRestRequest.getOffSet(); |
80 |
0 |
String query; |
81 |
0 |
if (isAdvancedQuery(uniprotRestRequest.getSearchTerm())) |
82 |
|
{ |
83 |
0 |
query = uniprotRestRequest.getSearchTerm(); |
84 |
|
} |
85 |
|
else |
86 |
|
{ |
87 |
0 |
query = uniprotRestRequest.getFieldToSearchBy() |
88 |
|
.equalsIgnoreCase("Search All") |
89 |
|
? uniprotRestRequest.getSearchTerm() |
90 |
|
+ " or mnemonic:" |
91 |
|
+ uniprotRestRequest.getSearchTerm() |
92 |
|
: uniprotRestRequest.getFieldToSearchBy() + ":" |
93 |
|
+ uniprotRestRequest.getSearchTerm(); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
0 |
Client client; |
100 |
0 |
Class<ClientResponse> clientResponseClass; |
101 |
0 |
if (Platform.isJS()) |
102 |
|
{ |
103 |
|
|
104 |
0 |
client = (Client) (Object) new jalview.javascript.web.Client(); |
105 |
0 |
clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class; |
106 |
|
} |
107 |
|
else |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
{ |
114 |
|
|
115 |
0 |
client = Client.create(new DefaultClientConfig()); |
116 |
0 |
clientResponseClass = ClientResponse.class; |
117 |
|
} |
118 |
|
|
119 |
0 |
WebResource webResource = null; |
120 |
0 |
webResource = client.resource(uniprotSearchEndpoint) |
121 |
|
.queryParam("format", "tab") |
122 |
|
.queryParam("columns", wantedFields) |
123 |
|
.queryParam("limit", String.valueOf(responseSize)) |
124 |
|
.queryParam("offset", String.valueOf(offSet)) |
125 |
|
.queryParam("sort", "score").queryParam("query", query); |
126 |
|
|
127 |
0 |
ClientResponse clientResponse = webResource |
128 |
|
.accept(MediaType.TEXT_PLAIN).get(clientResponseClass); |
129 |
0 |
String uniProtTabDelimittedResponseString = clientResponse |
130 |
|
.getEntity(String.class); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
0 |
if (clientResponse.getStatus() != 200) |
136 |
|
{ |
137 |
0 |
String errorMessage = getMessageByHTTPStatusCode( |
138 |
|
clientResponse.getStatus(), "Uniprot"); |
139 |
0 |
throw new Exception(errorMessage); |
140 |
|
|
141 |
|
} |
142 |
0 |
int xTotalResults = Platform.isJS() ? 1 |
143 |
|
: Integer.valueOf(clientResponse.getHeaders() |
144 |
|
.get("X-Total-Results").get(0)); |
145 |
0 |
clientResponse = null; |
146 |
0 |
client = null; |
147 |
0 |
return parseUniprotResponse(uniProtTabDelimittedResponseString, |
148 |
|
uniprotRestRequest, xTotalResults); |
149 |
|
} catch (Exception e) |
150 |
|
{ |
151 |
0 |
String exceptionMsg = e.getMessage(); |
152 |
0 |
if (exceptionMsg.contains("SocketException")) |
153 |
|
{ |
154 |
|
|
155 |
0 |
throw new Exception(MessageManager.getString( |
156 |
|
"exception.unable_to_detect_internet_connection")); |
157 |
|
} |
158 |
0 |
else if (exceptionMsg.contains("UnknownHostException")) |
159 |
|
{ |
160 |
|
|
161 |
0 |
throw new Exception(MessageManager.formatMessage( |
162 |
|
"exception.fts_server_unreachable", "Uniprot")); |
163 |
|
} |
164 |
|
else |
165 |
|
{ |
166 |
0 |
throw e; |
167 |
|
} |
168 |
|
} |
169 |
|
} |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 9 |
Complexity Density: 3 |
|
171 |
0 |
public boolean isAdvancedQuery(String query)... |
172 |
|
{ |
173 |
0 |
if (query.contains(" AND ") || query.contains(" OR ") |
174 |
|
|| query.contains(" NOT ") || query.contains(" ! ") |
175 |
|
|| query.contains(" || ") || query.contains(" && ") |
176 |
|
|| query.contains(":") || query.contains("-")) |
177 |
|
{ |
178 |
0 |
return true; |
179 |
|
} |
180 |
0 |
return false; |
181 |
|
} |
182 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 6 |
Complexity Density: 0.35 |
|
183 |
0 |
public FTSRestResponse parseUniprotResponse(... |
184 |
|
String uniProtTabDelimittedResponseString, |
185 |
|
FTSRestRequest uniprotRestRequest, int xTotalResults) |
186 |
|
{ |
187 |
0 |
FTSRestResponse searchResult = new FTSRestResponse(); |
188 |
0 |
List<FTSData> result = null; |
189 |
0 |
if (uniProtTabDelimittedResponseString == null |
190 |
|
|| uniProtTabDelimittedResponseString.trim().isEmpty()) |
191 |
|
{ |
192 |
0 |
searchResult.setNumberOfItemsFound(0); |
193 |
0 |
return searchResult; |
194 |
|
} |
195 |
0 |
String[] foundDataRow = uniProtTabDelimittedResponseString.split("\n"); |
196 |
0 |
if (foundDataRow != null && foundDataRow.length > 0) |
197 |
|
{ |
198 |
0 |
result = new ArrayList<>(); |
199 |
0 |
boolean firstRow = true; |
200 |
0 |
for (String dataRow : foundDataRow) |
201 |
|
{ |
202 |
|
|
203 |
|
|
204 |
0 |
if (firstRow) |
205 |
|
{ |
206 |
0 |
firstRow = false; |
207 |
0 |
continue; |
208 |
|
} |
209 |
|
|
210 |
0 |
result.add(getFTSData(dataRow, uniprotRestRequest)); |
211 |
|
} |
212 |
0 |
searchResult.setNumberOfItemsFound(xTotalResults); |
213 |
0 |
searchResult.setSearchSummary(result); |
214 |
|
} |
215 |
0 |
return searchResult; |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0.38 |
|
252 |
0 |
public static FTSData getFTSData(String tabDelimittedDataStr,... |
253 |
|
FTSRestRequest request) |
254 |
|
{ |
255 |
0 |
String primaryKey = null; |
256 |
|
|
257 |
0 |
Object[] summaryRowData; |
258 |
|
|
259 |
0 |
Collection<FTSDataColumnI> diplayFields = request.getWantedFields(); |
260 |
0 |
int colCounter = 0; |
261 |
0 |
summaryRowData = new Object[diplayFields.size()]; |
262 |
0 |
String[] columns = tabDelimittedDataStr.split("\t"); |
263 |
0 |
for (FTSDataColumnI field : diplayFields) |
264 |
|
{ |
265 |
0 |
try |
266 |
|
{ |
267 |
0 |
String fieldData = columns[colCounter]; |
268 |
0 |
if (field.isPrimaryKeyColumn()) |
269 |
|
{ |
270 |
0 |
primaryKey = fieldData; |
271 |
0 |
summaryRowData[colCounter++] = primaryKey; |
272 |
|
} |
273 |
0 |
else if (fieldData == null || fieldData.isEmpty()) |
274 |
|
{ |
275 |
0 |
summaryRowData[colCounter++] = null; |
276 |
|
} |
277 |
|
else |
278 |
|
{ |
279 |
0 |
try |
280 |
|
{ |
281 |
0 |
summaryRowData[colCounter++] = (field.getDataType() |
282 |
|
.getDataTypeClass() == Integer.class) |
283 |
|
? Integer.valueOf(fieldData.replace(",", "")) |
284 |
0 |
: (field.getDataType() |
285 |
|
.getDataTypeClass() == Double.class) |
286 |
|
? Double.valueOf(fieldData) |
287 |
|
: fieldData; |
288 |
|
} catch (Exception e) |
289 |
|
{ |
290 |
0 |
e.printStackTrace(); |
291 |
0 |
System.out.println("offending value:" + fieldData); |
292 |
|
} |
293 |
|
} |
294 |
|
} catch (Exception e) |
295 |
|
{ |
296 |
|
|
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
0 |
final String primaryKey1 = primaryKey; |
301 |
|
|
302 |
0 |
final Object[] summaryRowData1 = summaryRowData; |
303 |
0 |
return new FTSData() |
304 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
305 |
0 |
@Override... |
306 |
|
public Object[] getSummaryData() |
307 |
|
{ |
308 |
0 |
return summaryRowData1; |
309 |
|
} |
310 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
311 |
0 |
@Override... |
312 |
|
public Object getPrimaryKey() |
313 |
|
{ |
314 |
0 |
return primaryKey1; |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
320 |
0 |
@Override... |
321 |
|
public String toString() |
322 |
|
{ |
323 |
0 |
StringBuilder summaryFieldValues = new StringBuilder(); |
324 |
0 |
for (Object summaryField : summaryRowData1) |
325 |
|
{ |
326 |
0 |
summaryFieldValues.append( |
327 |
0 |
summaryField == null ? " " : summaryField.toString()) |
328 |
|
.append("\t"); |
329 |
|
} |
330 |
0 |
return summaryFieldValues.toString(); |
331 |
|
} |
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
336 |
0 |
@Override... |
337 |
|
public int hashCode() |
338 |
|
{ |
339 |
0 |
return Objects.hash(primaryKey1, this.toString()); |
340 |
|
} |
341 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
342 |
0 |
@Override... |
343 |
|
public boolean equals(Object that) |
344 |
|
{ |
345 |
0 |
return this.toString().equals(that.toString()); |
346 |
|
} |
347 |
|
}; |
348 |
|
} |
349 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
350 |
0 |
public static FTSRestClientI getInstance()... |
351 |
|
{ |
352 |
0 |
if (instance == null) |
353 |
|
{ |
354 |
0 |
instance = new UniProtFTSRestClient(); |
355 |
|
} |
356 |
0 |
return instance; |
357 |
|
} |
358 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
359 |
0 |
@Override... |
360 |
|
public String getColumnDataConfigFileName() |
361 |
|
{ |
362 |
0 |
return "/fts/uniprot_data_columns.txt"; |
363 |
|
} |
364 |
|
|
365 |
|
} |