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 |
|
|
|
|
| 92.8% |
Uncovered Elements: 11 (152) |
Complexity: 37 |
Complexity Density: 0.37 |
|
44 |
|
public abstract class GffHelperBase implements GffHelperI |
45 |
|
{ |
46 |
|
private static final String NOTE = "Note"; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
protected static final int SEQID_COL = 0; |
52 |
|
|
53 |
|
protected static final int SOURCE_COL = 1; |
54 |
|
|
55 |
|
protected static final int TYPE_COL = 2; |
56 |
|
|
57 |
|
protected static final int START_COL = 3; |
58 |
|
|
59 |
|
protected static final int END_COL = 4; |
60 |
|
|
61 |
|
protected static final int SCORE_COL = 5; |
62 |
|
|
63 |
|
protected static final int STRAND_COL = 6; |
64 |
|
|
65 |
|
protected static final int PHASE_COL = 7; |
66 |
|
|
67 |
|
protected static final int ATTRIBUTES_COL = 8; |
68 |
|
|
69 |
|
private AlignmentI lastmatchedAl = null; |
70 |
|
|
71 |
|
private SequenceIdMatcher matcher = null; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@param |
78 |
|
@param |
79 |
|
@param |
80 |
|
@param |
81 |
|
|
82 |
|
@return |
83 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
84 |
19 |
protected MapList constructMappingFromAlign(int fromStart, int fromEnd,... |
85 |
|
int toStart, int toEnd, MappingType mappingType) |
86 |
|
{ |
87 |
19 |
int[] from = new int[] { fromStart, fromEnd }; |
88 |
19 |
int[] to = new int[] { toStart, toEnd }; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
19 |
if (mappingType == MappingType.PeptideToNucleotide) |
95 |
|
{ |
96 |
15 |
int[] temp = from; |
97 |
15 |
from = to; |
98 |
15 |
to = temp; |
99 |
15 |
mappingType = mappingType.getInverse(); |
100 |
|
} |
101 |
|
|
102 |
19 |
int fromRatio = mappingType.getFromRatio(); |
103 |
19 |
int toRatio = mappingType.getToRatio(); |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
19 |
if (!trimMapping(from, to, fromRatio, toRatio)) |
110 |
|
{ |
111 |
0 |
System.err.println("Ignoring mapping from " + Arrays.toString(from) |
112 |
|
+ " to " + Arrays.toString(to) + " as counts don't match!"); |
113 |
0 |
return null; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
19 |
return new MapList(from, to, fromRatio, toRatio); |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@param |
132 |
|
@param |
133 |
|
@param |
134 |
|
@param |
135 |
|
@return |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 8 |
Complexity Density: 0.38 |
|
137 |
36 |
protected static boolean trimMapping(int[] from, int[] to, int fromRatio,... |
138 |
|
int toRatio) |
139 |
|
{ |
140 |
36 |
int fromLength = Math.abs(from[1] - from[0]) + 1; |
141 |
36 |
int toLength = Math.abs(to[1] - to[0]) + 1; |
142 |
36 |
int fromOverlap = fromLength * toRatio - toLength * fromRatio; |
143 |
36 |
if (fromOverlap == 0) |
144 |
|
{ |
145 |
24 |
return true; |
146 |
|
} |
147 |
12 |
if (fromOverlap > 0 && fromOverlap % toRatio == 0) |
148 |
|
{ |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
6 |
System.err.print( |
154 |
|
"Truncating mapping from " + Arrays.toString(from) + " to "); |
155 |
6 |
if (from[1] > from[0]) |
156 |
|
{ |
157 |
3 |
from[1] -= fromOverlap / toRatio; |
158 |
|
} |
159 |
|
else |
160 |
|
{ |
161 |
3 |
from[1] += fromOverlap / toRatio; |
162 |
|
} |
163 |
6 |
System.err.println(Arrays.toString(from)); |
164 |
6 |
return true; |
165 |
|
} |
166 |
6 |
else if (fromOverlap < 0 && fromOverlap % fromRatio == 0) |
167 |
|
{ |
168 |
5 |
fromOverlap = -fromOverlap; |
169 |
|
|
170 |
|
|
171 |
|
|
172 |
5 |
System.err.print( |
173 |
|
"Truncating mapping to " + Arrays.toString(to) + " to "); |
174 |
5 |
if (to[1] > to[0]) |
175 |
|
{ |
176 |
2 |
to[1] -= fromOverlap / fromRatio; |
177 |
|
} |
178 |
|
else |
179 |
|
{ |
180 |
3 |
to[1] += fromOverlap / fromRatio; |
181 |
|
} |
182 |
5 |
System.err.println(Arrays.toString(to)); |
183 |
5 |
return true; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
1 |
return false; |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
@param |
207 |
|
@param |
208 |
|
@param |
209 |
|
|
210 |
|
@return |
211 |
|
|
|
|
| 89.2% |
Uncovered Elements: 4 (37) |
Complexity: 11 |
Complexity Density: 0.52 |
|
212 |
16 |
protected SequenceI findSequence(String seqId, AlignmentI align,... |
213 |
|
List<SequenceI> newseqs, boolean relaxedIdMatching) |
214 |
|
{ |
215 |
16 |
if (seqId == null) |
216 |
|
{ |
217 |
0 |
return null; |
218 |
|
} |
219 |
16 |
SequenceI match = null; |
220 |
16 |
if (relaxedIdMatching) |
221 |
|
{ |
222 |
3 |
if (lastmatchedAl != align) |
223 |
|
{ |
224 |
3 |
lastmatchedAl = align; |
225 |
3 |
matcher = new SequenceIdMatcher(align.getSequencesArray()); |
226 |
3 |
if (newseqs != null) |
227 |
|
{ |
228 |
3 |
matcher.addAll(newseqs); |
229 |
|
} |
230 |
|
} |
231 |
3 |
match = matcher.findIdMatch(seqId); |
232 |
|
} |
233 |
|
else |
234 |
|
{ |
235 |
13 |
match = align.findName(seqId, true); |
236 |
13 |
if (match == null && newseqs != null) |
237 |
|
{ |
238 |
12 |
for (SequenceI m : newseqs) |
239 |
|
{ |
240 |
3 |
if (seqId.equals(m.getName())) |
241 |
|
{ |
242 |
1 |
return m; |
243 |
|
} |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
} |
248 |
15 |
if (match == null && newseqs != null) |
249 |
|
{ |
250 |
14 |
match = new SequenceDummy(seqId); |
251 |
14 |
if (relaxedIdMatching) |
252 |
|
{ |
253 |
3 |
matcher.addAll(Arrays.asList(new SequenceI[] { match })); |
254 |
|
} |
255 |
|
|
256 |
14 |
newseqs.add(match); |
257 |
|
} |
258 |
15 |
return match; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
@param |
277 |
|
@param |
278 |
|
|
279 |
|
@param |
280 |
|
|
281 |
|
@param |
282 |
|
|
283 |
|
@return |
284 |
|
|
|
|
| 93.3% |
Uncovered Elements: 2 (30) |
Complexity: 7 |
Complexity Density: 0.35 |
|
285 |
35 |
public static Map<String, List<String>> parseNameValuePairs(String text,... |
286 |
|
String namesDelimiter, char nameValueSeparator, |
287 |
|
String valuesDelimiter) |
288 |
|
{ |
289 |
35 |
Map<String, List<String>> map = new HashMap<String, List<String>>(); |
290 |
35 |
if (text == null || text.trim().length() == 0) |
291 |
|
{ |
292 |
2 |
return map; |
293 |
|
} |
294 |
|
|
295 |
33 |
for (String pair : text.trim().split(namesDelimiter)) |
296 |
|
{ |
297 |
89 |
pair = pair.trim(); |
298 |
89 |
if (pair.length() == 0) |
299 |
|
{ |
300 |
0 |
continue; |
301 |
|
} |
302 |
|
|
303 |
89 |
int sepPos = pair.indexOf(nameValueSeparator); |
304 |
89 |
if (sepPos == -1) |
305 |
|
{ |
306 |
|
|
307 |
2 |
continue; |
308 |
|
} |
309 |
|
|
310 |
87 |
String key = pair.substring(0, sepPos).trim(); |
311 |
87 |
String values = pair.substring(sepPos + 1).trim(); |
312 |
87 |
if (values.length() > 0) |
313 |
|
{ |
314 |
86 |
List<String> vals = map.get(key); |
315 |
86 |
if (vals == null) |
316 |
|
{ |
317 |
79 |
vals = new ArrayList<String>(); |
318 |
79 |
map.put(key, vals); |
319 |
|
} |
320 |
86 |
for (String val : values.split(valuesDelimiter)) |
321 |
|
{ |
322 |
87 |
vals.add(val); |
323 |
|
} |
324 |
|
} |
325 |
|
} |
326 |
33 |
return map; |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
@param |
335 |
|
@param |
336 |
|
@return |
337 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
338 |
23 |
protected SequenceFeature buildSequenceFeature(String[] gff,... |
339 |
|
Map<String, List<String>> attributes) |
340 |
|
{ |
341 |
23 |
return buildSequenceFeature(gff, TYPE_COL, gff[SOURCE_COL], attributes); |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
@param |
346 |
|
@param |
347 |
|
@param |
348 |
|
@param |
349 |
|
@return |
350 |
|
|
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 5 |
Complexity Density: 0.26 |
|
351 |
24 |
protected SequenceFeature buildSequenceFeature(String[] gff,... |
352 |
|
int typeColumn, String group, Map<String, List<String>> attributes) |
353 |
|
{ |
354 |
24 |
try |
355 |
|
{ |
356 |
24 |
int start = Integer.parseInt(gff[START_COL]); |
357 |
24 |
int end = Integer.parseInt(gff[END_COL]); |
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
24 |
float score = 0f; |
364 |
24 |
try |
365 |
|
{ |
366 |
24 |
score = Float.parseFloat(gff[SCORE_COL]); |
367 |
|
} catch (NumberFormatException nfe) |
368 |
|
{ |
369 |
|
|
370 |
|
} |
371 |
|
|
372 |
24 |
SequenceFeature sf = new SequenceFeature(gff[typeColumn], |
373 |
|
gff[SOURCE_COL], start, end, score, group); |
374 |
|
|
375 |
24 |
sf.setStrand(gff[STRAND_COL]); |
376 |
|
|
377 |
24 |
sf.setPhase(gff[PHASE_COL]); |
378 |
|
|
379 |
24 |
if (attributes != null) |
380 |
|
{ |
381 |
|
|
382 |
|
|
383 |
|
|
384 |
18 |
sf.setAttributes(gff[ATTRIBUTES_COL]); |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
|
390 |
18 |
for (Entry<String, List<String>> attr : attributes.entrySet()) |
391 |
|
{ |
392 |
41 |
String values = StringUtils.listToDelimitedString(attr.getValue(), |
393 |
|
","); |
394 |
41 |
sf.setValue(attr.getKey(), values); |
395 |
41 |
if (NOTE.equals(attr.getKey())) |
396 |
|
{ |
397 |
2 |
sf.setDescription(values); |
398 |
|
} |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
24 |
return sf; |
403 |
|
} catch (NumberFormatException nfe) |
404 |
|
{ |
405 |
0 |
System.err.println("Invalid number in gff: " + nfe.getMessage()); |
406 |
0 |
return null; |
407 |
|
} |
408 |
|
} |
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
@return |
415 |
|
|
416 |
|
protected abstract char getNameValueSeparator(); |
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
@param |
425 |
|
@param |
426 |
|
@param |
427 |
|
@return |
428 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
429 |
16 |
protected AlignedCodonFrame getMapping(AlignmentI align,... |
430 |
|
SequenceI fromSeq, SequenceI toSeq) |
431 |
|
{ |
432 |
16 |
AlignedCodonFrame acf = align.getMapping(fromSeq, toSeq); |
433 |
16 |
if (acf == null) |
434 |
|
{ |
435 |
15 |
acf = new AlignedCodonFrame(); |
436 |
|
} |
437 |
16 |
return acf; |
438 |
|
} |
439 |
|
|
440 |
|
} |