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