1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.analysis; |
22 |
|
|
23 |
|
import java.util.Locale; |
24 |
|
|
25 |
|
import jalview.datamodel.DBRefEntry; |
26 |
|
import jalview.datamodel.SequenceI; |
27 |
|
|
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.Arrays; |
30 |
|
import java.util.HashMap; |
31 |
|
import java.util.List; |
32 |
|
import java.util.Vector; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
|
|
| 0% |
Uncovered Elements: 114 (114) |
Complexity: 33 |
Complexity Density: 0.47 |
|
40 |
|
public class SequenceIdMatcher |
41 |
|
{ |
42 |
|
private HashMap<SeqIdName, SequenceI> names; |
43 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
44 |
0 |
public SequenceIdMatcher(List<SequenceI> seqs)... |
45 |
|
{ |
46 |
0 |
names = new HashMap<SeqIdName, SequenceI>(); |
47 |
0 |
addAll(seqs); |
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@param |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
55 |
0 |
public void addAll(List<SequenceI> seqs)... |
56 |
|
{ |
57 |
0 |
for (SequenceI seq : seqs) |
58 |
|
{ |
59 |
0 |
add(seq); |
60 |
|
} |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@param |
67 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
68 |
0 |
public void add(SequenceI seq)... |
69 |
|
{ |
70 |
|
|
71 |
|
|
72 |
0 |
names.put(new SeqIdName(seq.getDisplayId(true)), seq); |
73 |
0 |
SequenceI dbseq = seq; |
74 |
0 |
while (dbseq.getDatasetSequence() != null) |
75 |
|
{ |
76 |
0 |
dbseq = dbseq.getDatasetSequence(); |
77 |
|
} |
78 |
|
|
79 |
0 |
List<DBRefEntry> dbr = dbseq.getDBRefs(); |
80 |
0 |
if (dbr != null) |
81 |
|
{ |
82 |
0 |
SeqIdName sid = null; |
83 |
0 |
for (int r = 0, nr = dbr.size(); r < nr; r++) |
84 |
|
{ |
85 |
0 |
sid = new SeqIdName(dbr.get(r).getAccessionId()); |
86 |
0 |
if (!names.containsKey(sid)) |
87 |
|
{ |
88 |
0 |
names.put(sid, seq); |
89 |
|
} |
90 |
|
} |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
0 |
public SequenceIdMatcher(SequenceI[] sequences)... |
100 |
|
{ |
101 |
0 |
this(Arrays.asList(sequences)); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@param |
109 |
|
|
110 |
|
@param |
111 |
|
|
112 |
|
@return |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
114 |
0 |
private SequenceI pickbestMatch(SeqIdName candName,... |
115 |
|
List<SequenceI> matches) |
116 |
|
{ |
117 |
0 |
List<SequenceI> st = pickbestMatches(candName, matches); |
118 |
0 |
return st == null || st.size() == 0 ? null : st.get(0); |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@param |
126 |
|
|
127 |
|
@param |
128 |
|
|
129 |
|
@return |
130 |
|
|
131 |
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 10 |
Complexity Density: 0.43 |
|
132 |
0 |
private List<SequenceI> pickbestMatches(SeqIdName candName,... |
133 |
|
List<SequenceI> matches) |
134 |
|
{ |
135 |
0 |
ArrayList<SequenceI> best = new ArrayList<SequenceI>(); |
136 |
0 |
if (candName == null || matches == null || matches.size() == 0) |
137 |
|
{ |
138 |
0 |
return null; |
139 |
|
} |
140 |
0 |
SequenceI match = matches.remove(0); |
141 |
0 |
best.add(match); |
142 |
0 |
names.put(new SeqIdName(match.getName()), match); |
143 |
0 |
int matchlen = match.getName().length(); |
144 |
0 |
int namlen = candName.id.length(); |
145 |
0 |
while (matches.size() > 0) |
146 |
|
{ |
147 |
|
|
148 |
0 |
SequenceI cand = matches.remove(0); |
149 |
0 |
names.put(new SeqIdName(cand.getName()), cand); |
150 |
0 |
int q, w, candlen = cand.getName().length(); |
151 |
|
|
152 |
0 |
if ((q = Math.abs(matchlen - namlen)) > (w = Math |
153 |
|
.abs(candlen - namlen)) && candlen > matchlen) |
154 |
|
{ |
155 |
0 |
best.clear(); |
156 |
0 |
match = cand; |
157 |
0 |
matchlen = candlen; |
158 |
0 |
best.add(match); |
159 |
|
} |
160 |
0 |
if (q == w && candlen == matchlen) |
161 |
|
{ |
162 |
|
|
163 |
0 |
best.add(cand); |
164 |
|
} |
165 |
|
} |
166 |
0 |
if (best.size() == 0) |
167 |
|
{ |
168 |
0 |
return null; |
169 |
|
} |
170 |
0 |
; |
171 |
0 |
return best; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
@param |
178 |
|
|
179 |
|
@return |
180 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
181 |
0 |
public SequenceI findIdMatch(SequenceI seq)... |
182 |
|
{ |
183 |
0 |
SeqIdName nam = new SeqIdName(seq.getName()); |
184 |
0 |
return findIdMatch(nam); |
185 |
|
} |
186 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
187 |
0 |
public SequenceI findIdMatch(String seqnam)... |
188 |
|
{ |
189 |
0 |
SeqIdName nam = new SeqIdName(seqnam); |
190 |
0 |
return findIdMatch(nam); |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
@param |
197 |
|
|
198 |
|
@return |
199 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
200 |
0 |
public SequenceI[] findAllIdMatches(String seqnam)... |
201 |
|
{ |
202 |
|
|
203 |
0 |
SeqIdName nam = new SeqIdName(seqnam); |
204 |
0 |
List<SequenceI> m = findAllIdMatches(nam); |
205 |
0 |
if (m != null) |
206 |
|
{ |
207 |
0 |
return m.toArray(new SequenceI[m.size()]); |
208 |
|
} |
209 |
0 |
return null; |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
|
220 |
|
@return |
221 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
222 |
0 |
public SequenceI[] findIdMatch(SequenceI[] seqs)... |
223 |
|
{ |
224 |
0 |
SequenceI[] namedseqs = null; |
225 |
0 |
int i = 0; |
226 |
0 |
SeqIdName nam; |
227 |
|
|
228 |
0 |
if (seqs.length > 0) |
229 |
|
{ |
230 |
0 |
namedseqs = new SequenceI[seqs.length]; |
231 |
0 |
do |
232 |
|
{ |
233 |
0 |
nam = new SeqIdName(seqs[i].getName()); |
234 |
|
|
235 |
0 |
if (names.containsKey(nam)) |
236 |
|
{ |
237 |
0 |
namedseqs[i] = findIdMatch(nam); |
238 |
|
} |
239 |
|
else |
240 |
|
{ |
241 |
0 |
namedseqs[i] = null; |
242 |
|
} |
243 |
0 |
} while (++i < seqs.length); |
244 |
|
} |
245 |
|
|
246 |
0 |
return namedseqs; |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
@param |
253 |
|
|
254 |
|
@return |
255 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
256 |
0 |
private SequenceI findIdMatch(... |
257 |
|
jalview.analysis.SequenceIdMatcher.SeqIdName nam) |
258 |
|
{ |
259 |
0 |
Vector matches = new Vector(); |
260 |
0 |
while (names.containsKey(nam)) |
261 |
|
{ |
262 |
0 |
matches.addElement(names.remove(nam)); |
263 |
|
} |
264 |
0 |
return pickbestMatch(nam, matches); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
@param |
271 |
|
|
272 |
|
@return |
273 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
274 |
0 |
private List<SequenceI> findAllIdMatches(... |
275 |
|
jalview.analysis.SequenceIdMatcher.SeqIdName nam) |
276 |
|
{ |
277 |
0 |
ArrayList<SequenceI> matches = new ArrayList<SequenceI>(); |
278 |
0 |
while (names.containsKey(nam)) |
279 |
|
{ |
280 |
0 |
matches.add(names.remove(nam)); |
281 |
|
} |
282 |
0 |
List<SequenceI> r = pickbestMatches(nam, matches); |
283 |
0 |
return r; |
284 |
|
} |
285 |
|
|
|
|
| 0% |
Uncovered Elements: 38 (38) |
Complexity: 14 |
Complexity Density: 0.93 |
|
286 |
|
class SeqIdName |
287 |
|
{ |
288 |
|
String id; |
289 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
290 |
0 |
SeqIdName(String s)... |
291 |
|
{ |
292 |
0 |
if (s != null) |
293 |
|
{ |
294 |
0 |
id = s.toLowerCase(Locale.ROOT); |
295 |
|
} |
296 |
|
else |
297 |
|
{ |
298 |
0 |
id = ""; |
299 |
|
} |
300 |
|
} |
301 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
302 |
0 |
@Override... |
303 |
|
public int hashCode() |
304 |
|
{ |
305 |
0 |
return ((id.length() >= 4) ? id.substring(0, 4).hashCode() |
306 |
|
: id.hashCode()); |
307 |
|
} |
308 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
309 |
0 |
@Override... |
310 |
|
public boolean equals(Object s) |
311 |
|
{ |
312 |
0 |
if (s == null) |
313 |
|
{ |
314 |
0 |
return false; |
315 |
|
} |
316 |
0 |
if (s instanceof SeqIdName) |
317 |
|
{ |
318 |
0 |
return this.stringequals(((SeqIdName) s).id); |
319 |
|
} |
320 |
|
else |
321 |
|
{ |
322 |
0 |
if (s instanceof String) |
323 |
|
{ |
324 |
0 |
return this.stringequals(((String) s).toLowerCase(Locale.ROOT)); |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
0 |
return false; |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4) |
338 |
|
+ "$%^*)}[@',?_"; |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
@param |
346 |
|
@return |
347 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 5 |
Complexity Density: 1.67 |
|
348 |
0 |
private boolean stringequals(String s)... |
349 |
|
{ |
350 |
0 |
if (id.length() > s.length()) |
351 |
|
{ |
352 |
0 |
return id.startsWith(s) |
353 |
|
? (WORD_SEP.indexOf(id.charAt(s.length())) > -1) |
354 |
|
: false; |
355 |
|
} |
356 |
|
else |
357 |
|
{ |
358 |
0 |
return s.startsWith(id) |
359 |
0 |
? (s.equals(id) ? true |
360 |
|
: (WORD_SEP.indexOf(s.charAt(id.length())) > -1)) |
361 |
|
: false; |
362 |
|
} |
363 |
|
} |
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
369 |
0 |
@Override... |
370 |
|
public String toString() |
371 |
|
{ |
372 |
0 |
return id; |
373 |
|
} |
374 |
|
} |
375 |
|
} |