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