1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.io.gff; |
22 |
|
|
23 |
|
import jalview.analysis.SequenceIdMatcher; |
24 |
|
import jalview.datamodel.AlignedCodonFrame; |
25 |
|
import jalview.datamodel.AlignmentI; |
26 |
|
import jalview.datamodel.MappingType; |
27 |
|
import jalview.datamodel.SequenceDummy; |
28 |
|
import jalview.datamodel.SequenceFeature; |
29 |
|
import jalview.datamodel.SequenceI; |
30 |
|
import jalview.util.MapList; |
31 |
|
import jalview.util.StringUtils; |
32 |
|
|
33 |
|
import java.util.ArrayList; |
34 |
|
import java.util.Arrays; |
35 |
|
import java.util.HashMap; |
36 |
|
import java.util.List; |
37 |
|
import java.util.Map; |
38 |
|
import java.util.Map.Entry; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
|
|
| 94.8% |
Uncovered Elements: 11 (211) |
Complexity: 53 |
Complexity Density: 0.39 |
|
44 |
|
public abstract class GffHelperBase implements GffHelperI |
45 |
|
{ |
46 |
|
private static final String INVALID_GFF_ATTRIBUTE_FORMAT = "Invalid GFF attribute format: "; |
47 |
|
|
48 |
|
protected static final String COMMA = ","; |
49 |
|
|
50 |
|
protected static final String EQUALS = "="; |
51 |
|
|
52 |
|
protected static final String NOTE = "Note"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
protected static final int SEQID_COL = 0; |
58 |
|
|
59 |
|
protected static final int SOURCE_COL = 1; |
60 |
|
|
61 |
|
protected static final int TYPE_COL = 2; |
62 |
|
|
63 |
|
protected static final int START_COL = 3; |
64 |
|
|
65 |
|
protected static final int END_COL = 4; |
66 |
|
|
67 |
|
protected static final int SCORE_COL = 5; |
68 |
|
|
69 |
|
protected static final int STRAND_COL = 6; |
70 |
|
|
71 |
|
protected static final int PHASE_COL = 7; |
72 |
|
|
73 |
|
protected static final int ATTRIBUTES_COL = 8; |
74 |
|
|
75 |
|
private AlignmentI lastmatchedAl = null; |
76 |
|
|
77 |
|
private SequenceIdMatcher matcher = null; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@param |
84 |
|
@param |
85 |
|
@param |
86 |
|
@param |
87 |
|
|
88 |
|
@return |
89 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
90 |
19 |
protected MapList constructMappingFromAlign(int fromStart, int fromEnd,... |
91 |
|
int toStart, int toEnd, MappingType mappingType) |
92 |
|
{ |
93 |
19 |
int[] from = new int[] { fromStart, fromEnd }; |
94 |
19 |
int[] to = new int[] { toStart, toEnd }; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
19 |
if (mappingType == MappingType.PeptideToNucleotide) |
101 |
|
{ |
102 |
15 |
int[] temp = from; |
103 |
15 |
from = to; |
104 |
15 |
to = temp; |
105 |
15 |
mappingType = mappingType.getInverse(); |
106 |
|
} |
107 |
|
|
108 |
19 |
int fromRatio = mappingType.getFromRatio(); |
109 |
19 |
int toRatio = mappingType.getToRatio(); |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
19 |
if (!trimMapping(from, to, fromRatio, toRatio)) |
116 |
|
{ |
117 |
0 |
System.err.println("Ignoring mapping from " + Arrays.toString(from) |
118 |
|
+ " to " + Arrays.toString(to) + " as counts don't match!"); |
119 |
0 |
return null; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
19 |
return new MapList(from, to, fromRatio, toRatio); |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
@param |
138 |
|
@param |
139 |
|
@param |
140 |
|
@param |
141 |
|
@return |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 8 |
Complexity Density: 0.38 |
|
143 |
36 |
protected static boolean trimMapping(int[] from, int[] to, int fromRatio,... |
144 |
|
int toRatio) |
145 |
|
{ |
146 |
36 |
int fromLength = Math.abs(from[1] - from[0]) + 1; |
147 |
36 |
int toLength = Math.abs(to[1] - to[0]) + 1; |
148 |
36 |
int fromOverlap = fromLength * toRatio - toLength * fromRatio; |
149 |
36 |
if (fromOverlap == 0) |
150 |
|
{ |
151 |
24 |
return true; |
152 |
|
} |
153 |
12 |
if (fromOverlap > 0 && fromOverlap % toRatio == 0) |
154 |
|
{ |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
6 |
System.err.print( |
160 |
|
"Truncating mapping from " + Arrays.toString(from) + " to "); |
161 |
6 |
if (from[1] > from[0]) |
162 |
|
{ |
163 |
3 |
from[1] -= fromOverlap / toRatio; |
164 |
|
} |
165 |
|
else |
166 |
|
{ |
167 |
3 |
from[1] += fromOverlap / toRatio; |
168 |
|
} |
169 |
6 |
System.err.println(Arrays.toString(from)); |
170 |
6 |
return true; |
171 |
|
} |
172 |
6 |
else if (fromOverlap < 0 && fromOverlap % fromRatio == 0) |
173 |
|
{ |
174 |
5 |
fromOverlap = -fromOverlap; |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
5 |
System.err.print( |
179 |
|
"Truncating mapping to " + Arrays.toString(to) + " to "); |
180 |
5 |
if (to[1] > to[0]) |
181 |
|
{ |
182 |
2 |
to[1] -= fromOverlap / fromRatio; |
183 |
|
} |
184 |
|
else |
185 |
|
{ |
186 |
3 |
to[1] += fromOverlap / fromRatio; |
187 |
|
} |
188 |
5 |
System.err.println(Arrays.toString(to)); |
189 |
5 |
return true; |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
1 |
return false; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
@param |
212 |
|
@param |
213 |
|
@param |
214 |
|
@param |
215 |
|
|
216 |
|
@return |
217 |
|
|
|
|
| 89.2% |
Uncovered Elements: 4 (37) |
Complexity: 11 |
Complexity Density: 0.52 |
|
218 |
16 |
protected SequenceI findSequence(String seqId, AlignmentI align,... |
219 |
|
List<SequenceI> newseqs, boolean relaxedIdMatching) |
220 |
|
{ |
221 |
16 |
if (seqId == null) |
222 |
|
{ |
223 |
0 |
return null; |
224 |
|
} |
225 |
16 |
SequenceI match = null; |
226 |
16 |
if (relaxedIdMatching) |
227 |
|
{ |
228 |
3 |
if (lastmatchedAl != align) |
229 |
|
{ |
230 |
3 |
lastmatchedAl = align; |
231 |
3 |
matcher = new SequenceIdMatcher(align.getSequencesArray()); |
232 |
3 |
if (newseqs != null) |
233 |
|
{ |
234 |
3 |
matcher.addAll(newseqs); |
235 |
|
} |
236 |
|
} |
237 |
3 |
match = matcher.findIdMatch(seqId); |
238 |
|
} |
239 |
|
else |
240 |
|
{ |
241 |
13 |
match = align.findName(seqId, true); |
242 |
13 |
if (match == null && newseqs != null) |
243 |
|
{ |
244 |
12 |
for (SequenceI m : newseqs) |
245 |
|
{ |
246 |
3 |
if (seqId.equals(m.getName())) |
247 |
|
{ |
248 |
1 |
return m; |
249 |
|
} |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
|
} |
254 |
15 |
if (match == null && newseqs != null) |
255 |
|
{ |
256 |
14 |
match = new SequenceDummy(seqId); |
257 |
14 |
if (relaxedIdMatching) |
258 |
|
{ |
259 |
3 |
matcher.addAll(Arrays.asList(new SequenceI[] { match })); |
260 |
|
} |
261 |
|
|
262 |
14 |
newseqs.add(match); |
263 |
|
} |
264 |
15 |
return match; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
@param |
288 |
|
@param |
289 |
|
|
290 |
|
@param |
291 |
|
|
292 |
|
@param |
293 |
|
|
294 |
|
@return |
295 |
|
|
|
|
| 94.3% |
Uncovered Elements: 2 (35) |
Complexity: 8 |
Complexity Density: 0.35 |
|
296 |
36 |
public static Map<String, List<String>> parseNameValuePairs(String text,... |
297 |
|
String namesDelimiter, char nameValueSeparator, |
298 |
|
String valuesDelimiter) |
299 |
|
{ |
300 |
36 |
Map<String, List<String>> map = new HashMap<>(); |
301 |
36 |
if (text == null || text.trim().length() == 0) |
302 |
|
{ |
303 |
2 |
return map; |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
34 |
for (String nameValuePair : text.trim().split(namesDelimiter)) |
310 |
|
{ |
311 |
92 |
nameValuePair = nameValuePair.trim(); |
312 |
92 |
if (nameValuePair.length() == 0) |
313 |
|
{ |
314 |
0 |
continue; |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
92 |
int sepPos = nameValuePair.indexOf(nameValueSeparator); |
321 |
92 |
if (sepPos == -1) |
322 |
|
{ |
323 |
|
|
324 |
2 |
continue; |
325 |
|
} |
326 |
|
|
327 |
90 |
String name = nameValuePair.substring(0, sepPos).trim(); |
328 |
90 |
String values = nameValuePair.substring(sepPos + 1).trim(); |
329 |
90 |
if (values.isEmpty()) |
330 |
|
{ |
331 |
1 |
continue; |
332 |
|
} |
333 |
|
|
334 |
89 |
List<String> vals = map.get(name); |
335 |
89 |
if (vals == null) |
336 |
|
{ |
337 |
82 |
vals = new ArrayList<>(); |
338 |
82 |
map.put(name, vals); |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
89 |
if (values.indexOf(nameValueSeparator) != -1) |
346 |
|
{ |
347 |
17 |
vals.add(values); |
348 |
|
} |
349 |
|
else |
350 |
|
{ |
351 |
72 |
for (String val : values.split(valuesDelimiter)) |
352 |
|
{ |
353 |
74 |
vals.add(val); |
354 |
|
} |
355 |
|
} |
356 |
|
} |
357 |
|
|
358 |
34 |
return map; |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
@param |
367 |
|
@param |
368 |
|
@return |
369 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
370 |
23 |
protected SequenceFeature buildSequenceFeature(String[] gff,... |
371 |
|
Map<String, List<String>> attributes) |
372 |
|
{ |
373 |
23 |
return buildSequenceFeature(gff, TYPE_COL, gff[SOURCE_COL], attributes); |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
@param |
378 |
|
@param |
379 |
|
@param |
380 |
|
@param |
381 |
|
@return |
382 |
|
|
|
|
| 93.3% |
Uncovered Elements: 2 (30) |
Complexity: 7 |
Complexity Density: 0.29 |
|
383 |
24 |
protected SequenceFeature buildSequenceFeature(String[] gff,... |
384 |
|
int typeColumn, String group, Map<String, List<String>> attributes) |
385 |
|
{ |
386 |
24 |
try |
387 |
|
{ |
388 |
24 |
int start = Integer.parseInt(gff[START_COL]); |
389 |
24 |
int end = Integer.parseInt(gff[END_COL]); |
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
|
394 |
24 |
float score = 0f; |
395 |
24 |
try |
396 |
|
{ |
397 |
24 |
score = Float.parseFloat(gff[SCORE_COL]); |
398 |
|
} catch (NumberFormatException nfe) |
399 |
|
{ |
400 |
|
|
401 |
|
} |
402 |
|
|
403 |
24 |
SequenceFeature sf = new SequenceFeature(gff[typeColumn], |
404 |
|
gff[SOURCE_COL], start, end, score, group); |
405 |
|
|
406 |
24 |
sf.setStrand(gff[STRAND_COL]); |
407 |
|
|
408 |
24 |
sf.setPhase(gff[PHASE_COL]); |
409 |
|
|
410 |
24 |
if (attributes != null) |
411 |
|
{ |
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
18 |
for (Entry<String, List<String>> attr : attributes.entrySet()) |
418 |
|
{ |
419 |
42 |
String key = attr.getKey(); |
420 |
42 |
List<String> values = attr.getValue(); |
421 |
42 |
if (values.size() == 1 && values.get(0).contains(EQUALS)) |
422 |
|
{ |
423 |
|
|
424 |
|
|
425 |
|
|
426 |
1 |
Map<String, String> valueMap = parseAttributeMap(values.get(0)); |
427 |
1 |
sf.setValue(key, valueMap); |
428 |
|
} |
429 |
|
else |
430 |
|
{ |
431 |
41 |
String csvValues = StringUtils.listToDelimitedString(values, |
432 |
|
COMMA); |
433 |
41 |
csvValues = StringUtils.urlDecode(csvValues, GFF_ENCODABLE); |
434 |
41 |
sf.setValue(key, csvValues); |
435 |
41 |
if (NOTE.equals(key)) |
436 |
|
{ |
437 |
2 |
sf.setDescription(csvValues); |
438 |
|
} |
439 |
|
} |
440 |
|
} |
441 |
|
} |
442 |
|
|
443 |
24 |
return sf; |
444 |
|
} catch (NumberFormatException nfe) |
445 |
|
{ |
446 |
0 |
System.err.println("Invalid number in gff: " + nfe.getMessage()); |
447 |
0 |
return null; |
448 |
|
} |
449 |
|
} |
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
|
458 |
|
|
459 |
|
|
460 |
|
|
461 |
|
|
462 |
|
|
463 |
|
@param |
464 |
|
|
465 |
|
@return |
466 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (46) |
Complexity: 13 |
Complexity Density: 0.46 |
|
467 |
17 |
protected static Map<String, String> parseAttributeMap(String s)... |
468 |
|
{ |
469 |
17 |
Map<String, String> map = new HashMap<>(); |
470 |
17 |
String[] fields = s.split(EQUALS); |
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
|
475 |
16 |
boolean valid = true; |
476 |
16 |
if (fields.length < 2) |
477 |
|
{ |
478 |
|
|
479 |
|
|
480 |
|
|
481 |
6 |
valid = false; |
482 |
|
} |
483 |
10 |
else if (fields[0].isEmpty() || fields[0].contains(COMMA)) |
484 |
|
{ |
485 |
|
|
486 |
|
|
487 |
|
|
488 |
3 |
valid = false; |
489 |
|
} |
490 |
|
else |
491 |
|
{ |
492 |
13 |
for (int i = 1; i < fields.length - 1; i++) |
493 |
|
{ |
494 |
6 |
if (fields[i].isEmpty() || !fields[i].contains(COMMA)) |
495 |
|
{ |
496 |
|
|
497 |
|
|
498 |
|
|
499 |
2 |
valid = false; |
500 |
|
} |
501 |
|
} |
502 |
|
} |
503 |
|
|
504 |
16 |
if (!valid) |
505 |
|
{ |
506 |
11 |
System.err.println(INVALID_GFF_ATTRIBUTE_FORMAT + s); |
507 |
11 |
return map; |
508 |
|
} |
509 |
|
|
510 |
5 |
int i = 0; |
511 |
13 |
while (i < fields.length - 1) |
512 |
|
{ |
513 |
9 |
boolean lastPair = i == fields.length - 2; |
514 |
9 |
String before = fields[i]; |
515 |
9 |
String after = fields[i + 1]; |
516 |
|
|
517 |
|
|
518 |
|
|
519 |
|
|
520 |
|
|
521 |
9 |
String theKey = before.contains(COMMA) |
522 |
|
? before.substring(before.lastIndexOf(COMMA) + 1) |
523 |
|
: before; |
524 |
|
|
525 |
9 |
theKey = theKey.trim(); |
526 |
9 |
if (theKey.isEmpty()) |
527 |
|
{ |
528 |
1 |
System.err.println(INVALID_GFF_ATTRIBUTE_FORMAT + s); |
529 |
1 |
map.clear(); |
530 |
1 |
return map; |
531 |
|
} |
532 |
|
|
533 |
|
|
534 |
|
|
535 |
|
|
536 |
|
|
537 |
|
|
538 |
8 |
String theValue = after.contains(COMMA) && !lastPair |
539 |
|
? after.substring(0, after.lastIndexOf(COMMA)) |
540 |
|
: after; |
541 |
8 |
map.put(StringUtils.urlDecode(theKey, GFF_ENCODABLE), |
542 |
|
StringUtils.urlDecode(theValue, GFF_ENCODABLE)); |
543 |
8 |
i += 1; |
544 |
|
} |
545 |
|
|
546 |
4 |
return map; |
547 |
|
} |
548 |
|
|
549 |
|
|
550 |
|
|
551 |
|
|
552 |
|
|
553 |
|
|
554 |
|
|
555 |
|
@param |
556 |
|
@param |
557 |
|
@param |
558 |
|
@return |
559 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
560 |
16 |
protected AlignedCodonFrame getMapping(AlignmentI align,... |
561 |
|
SequenceI fromSeq, SequenceI toSeq) |
562 |
|
{ |
563 |
16 |
AlignedCodonFrame acf = align.getMapping(fromSeq, toSeq); |
564 |
16 |
if (acf == null) |
565 |
|
{ |
566 |
15 |
acf = new AlignedCodonFrame(); |
567 |
|
} |
568 |
16 |
return acf; |
569 |
|
} |
570 |
|
|
571 |
|
} |