1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ws.seqfetcher; |
22 |
|
|
23 |
|
import jalview.api.FeatureSettingsModelI; |
24 |
|
import jalview.bin.Console; |
25 |
|
import jalview.datamodel.AlignmentI; |
26 |
|
import jalview.datamodel.DBRefEntry; |
27 |
|
import jalview.datamodel.SequenceI; |
28 |
|
import jalview.util.DBRefUtils; |
29 |
|
import jalview.util.MessageManager; |
30 |
|
|
31 |
|
import java.util.ArrayList; |
32 |
|
import java.util.Collections; |
33 |
|
import java.util.Comparator; |
34 |
|
import java.util.Enumeration; |
35 |
|
import java.util.HashSet; |
36 |
|
import java.util.Hashtable; |
37 |
|
import java.util.List; |
38 |
|
import java.util.Map; |
39 |
|
import java.util.Stack; |
40 |
|
import java.util.Vector; |
41 |
|
|
|
|
| 0% |
Uncovered Elements: 225 (225) |
Complexity: 50 |
Complexity Density: 0.34 |
|
42 |
|
public class ASequenceFetcher |
43 |
|
{ |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
protected Hashtable<String, Map<String, DbSourceProxy>> fetchableDbs; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private Comparator<DbSourceProxy> proxyComparator; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
58 |
0 |
protected ASequenceFetcher()... |
59 |
|
{ |
60 |
0 |
super(); |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
0 |
proxyComparator = new Comparator<DbSourceProxy>() |
66 |
|
{ |
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
67 |
0 |
@Override... |
68 |
|
public int compare(DbSourceProxy o1, DbSourceProxy o2) |
69 |
|
{ |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
0 |
int compared = Integer.compare(o1.getTier(), o2.getTier()); |
74 |
0 |
if (compared == 0) |
75 |
|
{ |
76 |
|
|
77 |
0 |
String o1Name = o1.getDbName(); |
78 |
0 |
String o2Name = o2.getDbName(); |
79 |
0 |
if (o1Name != null && o2Name != null) |
80 |
|
{ |
81 |
0 |
compared = o1Name.compareToIgnoreCase(o2Name); |
82 |
|
} |
83 |
|
} |
84 |
0 |
return compared; |
85 |
|
} |
86 |
|
}; |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@return |
93 |
|
|
94 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
95 |
0 |
public String[] getSupportedDb()... |
96 |
|
{ |
97 |
0 |
if (fetchableDbs == null) |
98 |
|
{ |
99 |
0 |
return null; |
100 |
|
} |
101 |
0 |
String[] sf = fetchableDbs.keySet() |
102 |
|
.toArray(new String[fetchableDbs.size()]); |
103 |
0 |
return sf; |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
106 |
0 |
public boolean isFetchable(String source)... |
107 |
|
{ |
108 |
0 |
for (String db : fetchableDbs.keySet()) |
109 |
|
{ |
110 |
0 |
if (source.equalsIgnoreCase(db)) |
111 |
|
{ |
112 |
0 |
return true; |
113 |
|
} |
114 |
|
} |
115 |
0 |
Console.warn("isFetchable doesn't know about '" + source + "'"); |
116 |
0 |
return false; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
@param |
123 |
|
@param |
124 |
|
|
125 |
|
@return |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 103 (103) |
Complexity: 19 |
Complexity Density: 0.26 |
|
127 |
0 |
public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)... |
128 |
|
{ |
129 |
0 |
Vector<SequenceI> rseqs = new Vector<>(); |
130 |
0 |
Hashtable<String, List<String>> queries = new Hashtable<>(); |
131 |
0 |
for (DBRefEntry ref : refs) |
132 |
|
{ |
133 |
0 |
String canonical = DBRefUtils.getCanonicalName(ref.getSource()); |
134 |
0 |
if (!queries.containsKey(canonical)) |
135 |
|
{ |
136 |
0 |
queries.put(canonical, new ArrayList<String>()); |
137 |
|
} |
138 |
0 |
List<String> qset = queries.get(canonical); |
139 |
0 |
if (!qset.contains(ref.getAccessionId())) |
140 |
|
{ |
141 |
0 |
qset.add(ref.getAccessionId()); |
142 |
|
} |
143 |
|
} |
144 |
0 |
Enumeration<String> e = queries.keys(); |
145 |
0 |
while (e.hasMoreElements()) |
146 |
|
{ |
147 |
0 |
List<String> query = null; |
148 |
0 |
String db = null; |
149 |
0 |
db = e.nextElement(); |
150 |
0 |
query = queries.get(db); |
151 |
0 |
if (!isFetchable(db)) |
152 |
|
{ |
153 |
0 |
reportStdError(db, query, new Exception( |
154 |
|
"Don't know how to fetch from this database :" + db)); |
155 |
0 |
continue; |
156 |
|
} |
157 |
|
|
158 |
0 |
Stack<String> queriesLeft = new Stack<>(); |
159 |
0 |
queriesLeft.addAll(query); |
160 |
|
|
161 |
0 |
List<DbSourceProxy> proxies = getSourceProxy(db); |
162 |
0 |
for (DbSourceProxy fetcher : proxies) |
163 |
|
{ |
164 |
0 |
List<String> queriesMade = new ArrayList<>(); |
165 |
0 |
HashSet<String> queriesFound = new HashSet<>(); |
166 |
0 |
try |
167 |
|
{ |
168 |
0 |
if (fetcher.isDnaCoding() != dna) |
169 |
|
{ |
170 |
0 |
continue; |
171 |
|
} |
172 |
0 |
boolean doMultiple = fetcher.getMaximumQueryCount() > 1; |
173 |
0 |
while (!queriesLeft.isEmpty()) |
174 |
|
{ |
175 |
0 |
StringBuffer qsb = new StringBuffer(); |
176 |
0 |
do |
177 |
|
{ |
178 |
0 |
if (qsb.length() > 0) |
179 |
|
{ |
180 |
0 |
qsb.append(fetcher.getAccessionSeparator()); |
181 |
|
} |
182 |
0 |
String q = queriesLeft.pop(); |
183 |
0 |
queriesMade.add(q); |
184 |
0 |
qsb.append(q); |
185 |
0 |
} while (doMultiple && !queriesLeft.isEmpty()); |
186 |
|
|
187 |
0 |
AlignmentI seqset = null; |
188 |
0 |
try |
189 |
|
{ |
190 |
|
|
191 |
0 |
seqset = fetcher.getSequenceRecords(qsb.toString()); |
192 |
|
} catch (Exception ex) |
193 |
|
{ |
194 |
0 |
jalview.bin.Console.errPrintln( |
195 |
|
"Failed to retrieve the following from " + db); |
196 |
0 |
jalview.bin.Console.errPrintln(qsb); |
197 |
0 |
ex.printStackTrace(System.err); |
198 |
|
} |
199 |
|
|
200 |
0 |
if (seqset != null) |
201 |
|
{ |
202 |
0 |
SequenceI seqs[] = seqset.getSequencesArray(); |
203 |
0 |
if (seqs != null) |
204 |
|
{ |
205 |
0 |
for (int is = 0; is < seqs.length; is++) |
206 |
|
{ |
207 |
0 |
rseqs.addElement(seqs[is]); |
208 |
|
|
209 |
0 |
List<DBRefEntry> frefs = DBRefUtils.searchRefs( |
210 |
|
seqs[is].getDBRefs(), |
211 |
|
new DBRefEntry(db, null, null), |
212 |
|
DBRefUtils.SEARCH_MODE_FULL); |
213 |
0 |
for (DBRefEntry dbr : frefs) |
214 |
|
{ |
215 |
0 |
queriesFound.add(dbr.getAccessionId()); |
216 |
0 |
queriesMade.remove(dbr.getAccessionId()); |
217 |
|
} |
218 |
0 |
seqs[is] = null; |
219 |
|
} |
220 |
|
} |
221 |
|
else |
222 |
|
{ |
223 |
0 |
if (fetcher.getRawRecords() != null) |
224 |
|
{ |
225 |
0 |
jalview.bin.Console.outPrintln( |
226 |
|
"# Retrieved from " + db + ":" + qsb.toString()); |
227 |
0 |
StringBuffer rrb = fetcher.getRawRecords(); |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
0 |
String hdr; |
232 |
|
|
233 |
|
|
234 |
0 |
hdr = "# " + db + ":" + qsb.toString(); |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
0 |
jalview.bin.Console.outPrintln(hdr); |
239 |
0 |
if (rrb != null) |
240 |
|
{ |
241 |
0 |
jalview.bin.Console.outPrintln(rrb); |
242 |
|
} |
243 |
0 |
jalview.bin.Console.outPrintln("# end of " + hdr); |
244 |
|
} |
245 |
|
|
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
} |
250 |
|
} catch (Exception ex) |
251 |
|
{ |
252 |
0 |
reportStdError(db, queriesMade, ex); |
253 |
|
} |
254 |
0 |
if (queriesMade.size() > 0) |
255 |
|
{ |
256 |
0 |
jalview.bin.Console.outPrintln("# Adding " + queriesMade.size() |
257 |
|
+ " ids back to queries list for searching again (" + db |
258 |
|
+ ")"); |
259 |
0 |
queriesLeft.addAll(queriesMade); |
260 |
|
} |
261 |
|
} |
262 |
|
} |
263 |
|
|
264 |
0 |
SequenceI[] result = null; |
265 |
0 |
if (rseqs.size() > 0) |
266 |
|
{ |
267 |
0 |
result = new SequenceI[rseqs.size()]; |
268 |
0 |
int si = 0; |
269 |
0 |
for (SequenceI s : rseqs) |
270 |
|
{ |
271 |
0 |
result[si++] = s; |
272 |
0 |
s.updatePDBIds(); |
273 |
|
} |
274 |
|
} |
275 |
0 |
return result; |
276 |
|
} |
277 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
278 |
0 |
public void reportStdError(String db, List<String> queriesMade,... |
279 |
|
Exception ex) |
280 |
|
{ |
281 |
|
|
282 |
0 |
jalview.bin.Console.errPrintln( |
283 |
|
"Failed to retrieve the following references from " + db); |
284 |
0 |
int n = 0; |
285 |
0 |
for (String qv : queriesMade) |
286 |
|
{ |
287 |
0 |
System.err.print(" " + qv + ";"); |
288 |
0 |
if (n++ > 10) |
289 |
|
{ |
290 |
0 |
jalview.bin.Console.errPrintln(); |
291 |
0 |
n = 0; |
292 |
|
} |
293 |
|
} |
294 |
0 |
jalview.bin.Console.errPrintln(); |
295 |
0 |
ex.printStackTrace(); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
@param |
302 |
|
|
303 |
|
|
304 |
|
@return |
305 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
306 |
0 |
public List<DbSourceProxy> getSourceProxy(String db)... |
307 |
|
{ |
308 |
0 |
db = DBRefUtils.getCanonicalName(db); |
309 |
0 |
Map<String, DbSourceProxy> dblist = fetchableDbs.get(db); |
310 |
0 |
if (dblist == null) |
311 |
|
{ |
312 |
0 |
return new ArrayList<>(); |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
0 |
List<DbSourceProxy> dbs = new ArrayList<>(dblist.values()); |
319 |
0 |
Collections.sort(dbs, proxyComparator); |
320 |
0 |
return dbs; |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
@param |
327 |
|
|
328 |
|
|
329 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.43 |
|
330 |
0 |
protected void addDBRefSourceImpl(... |
331 |
|
Class<? extends DbSourceProxy> dbSourceProxy) |
332 |
|
throws IllegalArgumentException |
333 |
|
{ |
334 |
0 |
DbSourceProxy proxy = null; |
335 |
0 |
try |
336 |
|
{ |
337 |
0 |
DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance(); |
338 |
0 |
proxy = proxyObj; |
339 |
|
} catch (IllegalArgumentException e) |
340 |
|
{ |
341 |
0 |
throw e; |
342 |
|
} catch (Exception e) |
343 |
|
{ |
344 |
|
|
345 |
0 |
throw new Error(MessageManager |
346 |
|
.getString("error.dbrefsource_implementation_exception"), e); |
347 |
|
} |
348 |
0 |
addDbRefSourceImpl(proxy); |
349 |
|
} |
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
@param |
356 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
357 |
0 |
protected void addDbRefSourceImpl(DbSourceProxy proxy)... |
358 |
|
{ |
359 |
0 |
if (proxy != null) |
360 |
|
{ |
361 |
0 |
if (fetchableDbs == null) |
362 |
|
{ |
363 |
0 |
fetchableDbs = new Hashtable<>(); |
364 |
|
} |
365 |
0 |
Map<String, DbSourceProxy> slist = fetchableDbs |
366 |
|
.get(proxy.getDbSource()); |
367 |
0 |
if (slist == null) |
368 |
|
{ |
369 |
0 |
fetchableDbs.put(proxy.getDbSource(), slist = new Hashtable<>()); |
370 |
|
} |
371 |
0 |
slist.put(proxy.getDbName(), proxy); |
372 |
|
} |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
@param |
379 |
|
|
380 |
|
@return |
381 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 6 |
Complexity Density: 0.4 |
|
382 |
0 |
public String[] getDbInstances(Class class1)... |
383 |
|
{ |
384 |
0 |
if (!DbSourceProxy.class.isAssignableFrom(class1)) |
385 |
|
{ |
386 |
0 |
throw new Error(MessageManager.formatMessage( |
387 |
|
"error.implementation_error_dbinstance_must_implement_interface", |
388 |
|
new String[] |
389 |
|
{ class1.toString() })); |
390 |
|
} |
391 |
0 |
if (fetchableDbs == null) |
392 |
|
{ |
393 |
0 |
return null; |
394 |
|
} |
395 |
0 |
String[] sources = null; |
396 |
0 |
Vector<String> src = new Vector<>(); |
397 |
0 |
Enumeration<String> dbs = fetchableDbs.keys(); |
398 |
0 |
while (dbs.hasMoreElements()) |
399 |
|
{ |
400 |
0 |
String dbn = dbs.nextElement(); |
401 |
0 |
for (DbSourceProxy dbp : fetchableDbs.get(dbn).values()) |
402 |
|
{ |
403 |
0 |
if (class1.isAssignableFrom(dbp.getClass())) |
404 |
|
{ |
405 |
0 |
src.addElement(dbn); |
406 |
|
} |
407 |
|
} |
408 |
|
} |
409 |
0 |
if (src.size() > 0) |
410 |
|
{ |
411 |
0 |
src.copyInto(sources = new String[src.size()]); |
412 |
|
} |
413 |
0 |
return sources; |
414 |
|
} |
415 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
416 |
0 |
public DbSourceProxy[] getDbSourceProxyInstances(Class class1)... |
417 |
|
{ |
418 |
0 |
List<DbSourceProxy> prlist = new ArrayList<>(); |
419 |
0 |
for (String fetchable : getSupportedDb()) |
420 |
|
{ |
421 |
0 |
for (DbSourceProxy pr : getSourceProxy(fetchable)) |
422 |
|
{ |
423 |
0 |
if (class1.isInstance(pr)) |
424 |
|
{ |
425 |
0 |
prlist.add(pr); |
426 |
|
} |
427 |
|
} |
428 |
|
} |
429 |
0 |
if (prlist.size() == 0) |
430 |
|
{ |
431 |
0 |
return null; |
432 |
|
} |
433 |
0 |
return prlist.toArray(new DbSourceProxy[0]); |
434 |
|
} |
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
@param |
441 |
|
@return |
442 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
443 |
0 |
public FeatureSettingsModelI getFeatureColourScheme(String source)... |
444 |
|
{ |
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
0 |
for (DbSourceProxy proxy : getSourceProxy(source)) |
450 |
|
{ |
451 |
0 |
FeatureSettingsModelI preferredColours = proxy |
452 |
|
.getFeatureColourScheme(); |
453 |
0 |
if (preferredColours != null) |
454 |
|
{ |
455 |
0 |
return preferredColours; |
456 |
|
} |
457 |
|
} |
458 |
0 |
return null; |
459 |
|
} |
460 |
|
} |