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.AlignmentAnnotation; |
26 |
|
import jalview.datamodel.AlignmentI; |
27 |
|
import jalview.datamodel.SequenceI; |
28 |
|
|
29 |
|
import java.util.Arrays; |
30 |
|
import java.util.Comparator; |
31 |
|
import java.util.HashMap; |
32 |
|
import java.util.Map; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@author |
39 |
|
|
40 |
|
|
|
|
| 74.4% |
Uncovered Elements: 52 (203) |
Complexity: 69 |
Complexity Density: 0.64 |
|
41 |
|
public class AnnotationSorter |
42 |
|
{ |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@author |
50 |
|
|
51 |
|
|
|
|
| 18.2% |
Uncovered Elements: 9 (11) |
Complexity: 4 |
Complexity Density: 0.67 |
|
52 |
|
public enum SequenceAnnotationOrder |
53 |
|
{ |
54 |
|
|
55 |
|
SEQUENCE_AND_LABEL("Sequence"), LABEL_AND_SEQUENCE("Label"), |
56 |
|
NONE("No sort"); |
57 |
|
|
58 |
|
private String description; |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
60 |
6 |
private SequenceAnnotationOrder(String s)... |
61 |
|
{ |
62 |
6 |
description = s; |
63 |
|
} |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
0 |
@Override... |
66 |
|
public String toString() |
67 |
|
{ |
68 |
0 |
return description; |
69 |
|
} |
70 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
71 |
0 |
public static SequenceAnnotationOrder forDescription(String d)... |
72 |
|
{ |
73 |
0 |
for (SequenceAnnotationOrder order : values()) |
74 |
|
{ |
75 |
0 |
if (order.toString().equals(d)) |
76 |
|
{ |
77 |
0 |
return order; |
78 |
|
} |
79 |
|
} |
80 |
0 |
return null; |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
private final AlignmentI alignment; |
86 |
|
|
87 |
|
|
88 |
|
private boolean showAutocalcAbove; |
89 |
|
|
90 |
|
|
91 |
|
private final Map<SequenceI, Integer> sequenceIndices = new HashMap<SequenceI, Integer>(); |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
@param |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
100 |
1999 |
public AnnotationSorter(AlignmentI alignmentI,... |
101 |
|
boolean showAutocalculatedAbove) |
102 |
|
{ |
103 |
1999 |
this.alignment = alignmentI; |
104 |
1999 |
this.showAutocalcAbove = showAutocalculatedAbove; |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
private final Comparator<? super AlignmentAnnotation> bySequenceAndLabel = new Comparator<AlignmentAnnotation>() |
119 |
|
{ |
|
|
| 82.5% |
Uncovered Elements: 7 (40) |
Complexity: 13 |
Complexity Density: 0.65 |
|
120 |
170279 |
@Override... |
121 |
|
public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2) |
122 |
|
{ |
123 |
170279 |
if (o1 == null && o2 == null) |
124 |
|
{ |
125 |
0 |
return 0; |
126 |
|
} |
127 |
170279 |
if (o1 == null) |
128 |
|
{ |
129 |
0 |
return -1; |
130 |
|
} |
131 |
170279 |
if (o2 == null) |
132 |
|
{ |
133 |
0 |
return 1; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
170279 |
boolean o1auto = o1.autoCalculated && o1.sequenceRef == null; |
138 |
170279 |
boolean o2auto = o2.autoCalculated && o2.sequenceRef == null; |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
170279 |
if (o1auto && o2auto) |
144 |
|
{ |
145 |
2 |
return 0; |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
170277 |
if (o1auto) |
152 |
|
{ |
153 |
7 |
return showAutocalcAbove ? -1 : 1; |
154 |
|
} |
155 |
170270 |
if (o2auto) |
156 |
|
{ |
157 |
1 |
return showAutocalcAbove ? 1 : -1; |
158 |
|
} |
159 |
170269 |
int computedOrder = compareSequences(o1, o2); |
160 |
170269 |
if (computedOrder == 0) |
161 |
|
{ |
162 |
14010 |
computedOrder = compareLabels(o1, o2); |
163 |
|
} |
164 |
170269 |
if (computedOrder == 0) |
165 |
|
{ |
166 |
927 |
computedOrder = compareDescriptions(o1, o2); |
167 |
|
} |
168 |
170269 |
return computedOrder; |
169 |
|
} |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0 |
@Override... |
172 |
|
public String toString() |
173 |
|
{ |
174 |
0 |
return "Sort by sequence and label"; |
175 |
|
} |
176 |
|
}; |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
private final Comparator<? super AlignmentAnnotation> byLabelAndSequence = new Comparator<AlignmentAnnotation>() |
189 |
|
{ |
|
|
| 79.4% |
Uncovered Elements: 7 (34) |
Complexity: 12 |
Complexity Density: 0.75 |
|
190 |
172066 |
@Override... |
191 |
|
public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2) |
192 |
|
{ |
193 |
172066 |
if (o1 == null && o2 == null) |
194 |
|
{ |
195 |
0 |
return 0; |
196 |
|
} |
197 |
172066 |
if (o1 == null) |
198 |
|
{ |
199 |
0 |
return -1; |
200 |
|
} |
201 |
172066 |
if (o2 == null) |
202 |
|
{ |
203 |
0 |
return 1; |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
172066 |
boolean o1auto = o1.autoCalculated && o1.sequenceRef == null; |
208 |
172066 |
boolean o2auto = o2.autoCalculated && o2.sequenceRef == null; |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
172066 |
if (o1auto && o2auto) |
214 |
|
{ |
215 |
2 |
return 0; |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
172064 |
if (o1auto) |
222 |
|
{ |
223 |
7 |
return showAutocalcAbove ? -1 : 1; |
224 |
|
} |
225 |
172057 |
if (o2auto) |
226 |
|
{ |
227 |
1 |
return showAutocalcAbove ? 1 : -1; |
228 |
|
} |
229 |
172056 |
int labelOrder = compareLabels(o1, o2); |
230 |
172056 |
return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder; |
231 |
|
} |
232 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
233 |
0 |
@Override... |
234 |
|
public String toString() |
235 |
|
{ |
236 |
0 |
return "Sort by label and sequence"; |
237 |
|
} |
238 |
|
}; |
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>() |
247 |
|
{ |
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 9 |
Complexity Density: 1.12 |
|
248 |
5165 |
@Override... |
249 |
|
public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2) |
250 |
|
{ |
251 |
|
|
252 |
5165 |
boolean o1auto = o1.autoCalculated && o1.sequenceRef == null; |
253 |
5165 |
boolean o2auto = o2.autoCalculated && o2.sequenceRef == null; |
254 |
|
|
255 |
|
|
256 |
5165 |
if (o1 != null && o2 != null) |
257 |
|
{ |
258 |
5165 |
if (o1auto && !o2auto) |
259 |
|
{ |
260 |
96 |
return showAutocalcAbove ? -1 : 1; |
261 |
|
} |
262 |
5069 |
if (!o1auto && o2auto) |
263 |
|
{ |
264 |
74 |
return showAutocalcAbove ? 1 : -1; |
265 |
|
} |
266 |
|
} |
267 |
4995 |
return 0; |
268 |
|
} |
269 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
270 |
0 |
@Override... |
271 |
|
public String toString() |
272 |
|
{ |
273 |
0 |
return "No sort"; |
274 |
|
} |
275 |
|
}; |
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
@param |
281 |
|
@param |
282 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
283 |
2005 |
public void sort(AlignmentAnnotation[] alignmentAnnotations,... |
284 |
|
SequenceAnnotationOrder order) |
285 |
|
{ |
286 |
2005 |
if (alignmentAnnotations == null) |
287 |
|
{ |
288 |
0 |
return; |
289 |
|
} |
290 |
|
|
291 |
2005 |
saveSequenceIndices(alignmentAnnotations); |
292 |
|
|
293 |
2005 |
Comparator<? super AlignmentAnnotation> comparator = getComparator( |
294 |
|
order); |
295 |
|
|
296 |
2005 |
if (alignmentAnnotations != null) |
297 |
|
{ |
298 |
2005 |
synchronized (alignmentAnnotations) |
299 |
|
{ |
300 |
2005 |
Arrays.sort(alignmentAnnotations, comparator); |
301 |
|
} |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
@param |
311 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
312 |
2005 |
private void saveSequenceIndices(... |
313 |
|
AlignmentAnnotation[] alignmentAnnotations) |
314 |
|
{ |
315 |
2005 |
sequenceIndices.clear(); |
316 |
2005 |
for (AlignmentAnnotation ann : alignmentAnnotations) |
317 |
|
{ |
318 |
62641 |
SequenceI seq = ann.sequenceRef; |
319 |
62641 |
if (seq != null) |
320 |
|
{ |
321 |
56066 |
int index = AlignmentUtils.getSequenceIndex(alignment, seq); |
322 |
56066 |
sequenceIndices.put(seq, index); |
323 |
|
} |
324 |
|
} |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
@param |
331 |
|
@return |
332 |
|
|
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 5 |
Complexity Density: 0.45 |
|
333 |
2005 |
private Comparator<? super AlignmentAnnotation> getComparator(... |
334 |
|
SequenceAnnotationOrder order) |
335 |
|
{ |
336 |
2005 |
if (order == null) |
337 |
|
{ |
338 |
0 |
return noSort; |
339 |
|
} |
340 |
2005 |
switch (order) |
341 |
|
{ |
342 |
1986 |
case NONE: |
343 |
1986 |
return this.noSort; |
344 |
8 |
case SEQUENCE_AND_LABEL: |
345 |
8 |
return this.bySequenceAndLabel; |
346 |
11 |
case LABEL_AND_SEQUENCE: |
347 |
11 |
return this.byLabelAndSequence; |
348 |
0 |
default: |
349 |
0 |
throw new UnsupportedOperationException(order.toString()); |
350 |
|
} |
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
@param |
358 |
|
@param |
359 |
|
@return |
360 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
361 |
186066 |
private int compareLabels(AlignmentAnnotation o1, AlignmentAnnotation o2)... |
362 |
|
{ |
363 |
186066 |
if (o1 == null || o2 == null) |
364 |
|
{ |
365 |
0 |
return 0; |
366 |
|
} |
367 |
186066 |
String label1 = o1.label; |
368 |
186066 |
String label2 = o2.label; |
369 |
186066 |
return compareString(label1, label2); |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
@param |
377 |
|
@param |
378 |
|
@return |
379 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
380 |
927 |
private int compareDescriptions(AlignmentAnnotation o1,... |
381 |
|
AlignmentAnnotation o2) |
382 |
|
{ |
383 |
927 |
if (o1 == null || o2 == null) |
384 |
|
{ |
385 |
0 |
return 0; |
386 |
|
} |
387 |
927 |
String label1 = o1.description; |
388 |
927 |
String label2 = o2.description; |
389 |
927 |
return compareString(label1, label2); |
390 |
|
} |
391 |
|
|
|
|
| 53.8% |
Uncovered Elements: 6 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
392 |
186993 |
private int compareString(String label1, String label2)... |
393 |
|
{ |
394 |
186993 |
if (label1 == null && label2 == null) |
395 |
|
{ |
396 |
0 |
return 0; |
397 |
|
} |
398 |
186993 |
if (label1 == null) |
399 |
|
{ |
400 |
0 |
return -1; |
401 |
|
} |
402 |
186993 |
if (label2 == null) |
403 |
|
{ |
404 |
0 |
return 1; |
405 |
|
} |
406 |
186993 |
return label1.toUpperCase(Locale.ROOT) |
407 |
|
.compareTo(label2.toUpperCase(Locale.ROOT)); |
408 |
|
} |
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
@param |
415 |
|
@param |
416 |
|
@return |
417 |
|
|
|
|
| 57.6% |
Uncovered Elements: 14 (33) |
Complexity: 10 |
Complexity Density: 0.59 |
|
418 |
237416 |
private int compareSequences(AlignmentAnnotation o1,... |
419 |
|
AlignmentAnnotation o2) |
420 |
|
{ |
421 |
237416 |
SequenceI seq1 = o1.sequenceRef; |
422 |
237416 |
SequenceI seq2 = o2.sequenceRef; |
423 |
237416 |
if (seq1 == null && seq2 == null) |
424 |
|
{ |
425 |
0 |
return 0; |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
|
|
430 |
237416 |
if (seq1 == null) |
431 |
|
{ |
432 |
0 |
return showAutocalcAbove ? -1 : 1; |
433 |
|
} |
434 |
237416 |
if (seq2 == null) |
435 |
|
{ |
436 |
0 |
return showAutocalcAbove ? 1 : -1; |
437 |
|
} |
438 |
|
|
439 |
237416 |
int index1 = sequenceIndices.get(seq1); |
440 |
237416 |
int index2 = sequenceIndices.get(seq2); |
441 |
237416 |
if (index1 == index2) |
442 |
|
{ |
443 |
15864 |
return 0; |
444 |
|
} |
445 |
221552 |
if (index1 == -1) |
446 |
|
{ |
447 |
0 |
return -1; |
448 |
|
} |
449 |
221552 |
if (index2 == -1) |
450 |
|
{ |
451 |
0 |
return 1; |
452 |
|
} |
453 |
221552 |
return Integer.compare(index1, index2); |
454 |
|
} |
455 |
|
} |