1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ext.so; |
22 |
|
|
23 |
|
import java.io.BufferedInputStream; |
24 |
|
import java.io.BufferedReader; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.io.InputStreamReader; |
28 |
|
import java.text.ParseException; |
29 |
|
import java.util.ArrayList; |
30 |
|
import java.util.Collections; |
31 |
|
import java.util.HashMap; |
32 |
|
import java.util.List; |
33 |
|
import java.util.Map; |
34 |
|
import java.util.NoSuchElementException; |
35 |
|
import java.util.zip.ZipEntry; |
36 |
|
import java.util.zip.ZipInputStream; |
37 |
|
|
38 |
|
import org.biojava.nbio.ontology.Ontology; |
39 |
|
import org.biojava.nbio.ontology.Term; |
40 |
|
import org.biojava.nbio.ontology.Term.Impl; |
41 |
|
import org.biojava.nbio.ontology.Triple; |
42 |
|
import org.biojava.nbio.ontology.io.OboParser; |
43 |
|
import org.biojava.nbio.ontology.utils.Annotation; |
44 |
|
|
45 |
|
import jalview.bin.Console; |
46 |
|
import jalview.io.gff.SequenceOntologyI; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
|
|
| 85% |
Uncovered Elements: 24 (160) |
Complexity: 47 |
Complexity Density: 0.47 |
|
52 |
|
public class SequenceOntology implements SequenceOntologyI |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private Ontology ontology; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private Term isA; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private Map<String, Term> termsByDescription; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private Map<Term, List<Term>> termIsA; |
75 |
|
|
76 |
|
private List<String> termsFound; |
77 |
|
|
78 |
|
private List<String> termsNotFound; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
84 |
131 |
public SequenceOntology()... |
85 |
|
{ |
86 |
131 |
termsFound = new ArrayList<String>(); |
87 |
131 |
termsNotFound = new ArrayList<String>(); |
88 |
131 |
termsByDescription = new HashMap<String, Term>(); |
89 |
131 |
termIsA = new HashMap<Term, List<Term>>(); |
90 |
|
|
91 |
131 |
loadOntologyZipFile("so-xp-simple.obo"); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
|
|
|
| 83.3% |
Uncovered Elements: 3 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
99 |
131 |
protected void loadOntologyZipFile(String ontologyFile)... |
100 |
|
{ |
101 |
131 |
long now = System.currentTimeMillis(); |
102 |
131 |
ZipInputStream zipStream = null; |
103 |
131 |
try |
104 |
|
{ |
105 |
131 |
String zipFile = ontologyFile + ".zip"; |
106 |
131 |
InputStream inStream = this.getClass() |
107 |
|
.getResourceAsStream("/" + zipFile); |
108 |
131 |
zipStream = new ZipInputStream(new BufferedInputStream(inStream)); |
109 |
131 |
ZipEntry entry; |
110 |
? |
while ((entry = zipStream.getNextEntry()) != null) |
111 |
|
{ |
112 |
393 |
if (entry.getName().equals(ontologyFile)) |
113 |
|
{ |
114 |
131 |
loadOboFile(zipStream); |
115 |
|
} |
116 |
|
} |
117 |
131 |
long elapsed = System.currentTimeMillis() - now; |
118 |
131 |
Console.info("Loaded Sequence Ontology from " + zipFile + " (" |
119 |
|
+ elapsed + "ms)"); |
120 |
|
} catch (Exception e) |
121 |
|
{ |
122 |
0 |
e.printStackTrace(); |
123 |
|
} finally |
124 |
|
{ |
125 |
131 |
closeStream(zipStream); |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
@param |
133 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
134 |
131 |
protected void closeStream(InputStream is)... |
135 |
|
{ |
136 |
131 |
if (is != null) |
137 |
|
{ |
138 |
131 |
try |
139 |
|
{ |
140 |
131 |
is.close(); |
141 |
|
} catch (IOException e) |
142 |
|
{ |
143 |
|
|
144 |
|
} |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
@throws |
153 |
|
@throws |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
155 |
131 |
protected void loadOboFile(InputStream is)... |
156 |
|
throws ParseException, IOException |
157 |
|
{ |
158 |
131 |
BufferedReader oboFile = new BufferedReader(new InputStreamReader(is)); |
159 |
131 |
OboParser parser = new OboParser(); |
160 |
131 |
ontology = parser.parseOBO(oboFile, "SO", "the SO ontology"); |
161 |
131 |
isA = ontology.getTerm("is_a"); |
162 |
131 |
storeTermNames(); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
|
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 8 |
Complexity Density: 0.53 |
|
171 |
131 |
protected void storeTermNames()... |
172 |
|
{ |
173 |
131 |
for (Term term : ontology.getTerms()) |
174 |
|
{ |
175 |
753512 |
if (term instanceof Impl) |
176 |
|
{ |
177 |
341517 |
String description = term.getDescription(); |
178 |
341517 |
if (description != null) |
179 |
|
{ |
180 |
299073 |
Term replaced = termsByDescription.get(description); |
181 |
299073 |
if (replaced != null) |
182 |
|
{ |
183 |
1048 |
boolean newTermIsObsolete = isObsolete(term); |
184 |
1048 |
boolean oldTermIsObsolete = isObsolete(replaced); |
185 |
1048 |
if (newTermIsObsolete && !oldTermIsObsolete) |
186 |
|
{ |
187 |
262 |
Console.debug("Ignoring " + term.getName() |
188 |
|
+ " as obsolete and duplicated by " |
189 |
|
+ replaced.getName()); |
190 |
262 |
term = replaced; |
191 |
|
} |
192 |
786 |
else if (!newTermIsObsolete && oldTermIsObsolete) |
193 |
|
{ |
194 |
786 |
Console.debug("Ignoring " + replaced.getName() |
195 |
|
+ " as obsolete and duplicated by " + term.getName()); |
196 |
|
} |
197 |
|
else |
198 |
|
{ |
199 |
0 |
Console.debug("Warning: " + term.getName() + " has replaced " |
200 |
|
+ replaced.getName() + " for lookup of '" |
201 |
|
+ description + "'"); |
202 |
|
} |
203 |
|
} |
204 |
299073 |
termsByDescription.put(description, term); |
205 |
|
} |
206 |
|
} |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@return |
216 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
217 |
2096 |
public static boolean isObsolete(Term term)... |
218 |
|
{ |
219 |
2096 |
Annotation ann = term.getAnnotation(); |
220 |
2096 |
if (ann != null) |
221 |
|
{ |
222 |
2096 |
try |
223 |
|
{ |
224 |
1048 |
if (Boolean.TRUE.equals(ann.getProperty("is_obsolete"))) |
225 |
|
{ |
226 |
1048 |
return true; |
227 |
|
} |
228 |
|
} catch (NoSuchElementException e) |
229 |
|
{ |
230 |
|
|
231 |
|
} |
232 |
|
} |
233 |
1048 |
return false; |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
@param |
241 |
|
|
242 |
|
@return |
243 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
0 |
public boolean isNucleotideMatch(String soTerm)... |
245 |
|
{ |
246 |
0 |
return isA(soTerm, NUCLEOTIDE_MATCH); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
|
255 |
|
@return |
256 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
257 |
0 |
public boolean isProteinMatch(String soTerm)... |
258 |
|
{ |
259 |
0 |
return isA(soTerm, PROTEIN_MATCH); |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
@param |
267 |
|
|
268 |
|
@return |
269 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
270 |
0 |
public boolean isPolypeptide(String soTerm)... |
271 |
|
{ |
272 |
0 |
return isA(soTerm, POLYPEPTIDE); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
@param |
280 |
|
@param |
281 |
|
@return |
282 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
283 |
218 |
@Override... |
284 |
|
public boolean isA(String child, String parent) |
285 |
|
{ |
286 |
218 |
if (child == null || parent == null) |
287 |
|
{ |
288 |
3 |
return false; |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
215 |
if (child.equals(parent)) |
294 |
|
{ |
295 |
29 |
termFound(child); |
296 |
29 |
return true; |
297 |
|
} |
298 |
|
|
299 |
186 |
Term childTerm = getTerm(child); |
300 |
186 |
if (childTerm != null) |
301 |
|
{ |
302 |
143 |
termFound(child); |
303 |
|
} |
304 |
|
else |
305 |
|
{ |
306 |
43 |
termNotFound(child); |
307 |
|
} |
308 |
186 |
Term parentTerm = getTerm(parent); |
309 |
|
|
310 |
186 |
return termIsA(childTerm, parentTerm); |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
318 |
172 |
private void termFound(String term)... |
319 |
|
{ |
320 |
172 |
synchronized (termsFound) |
321 |
|
{ |
322 |
172 |
if (!termsFound.contains(term)) |
323 |
|
{ |
324 |
48 |
termsFound.add(term); |
325 |
|
} |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
@param |
333 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
334 |
43 |
private void termNotFound(String term)... |
335 |
|
{ |
336 |
43 |
synchronized (termsNotFound) |
337 |
|
{ |
338 |
43 |
if (!termsNotFound.contains(term)) |
339 |
|
{ |
340 |
13 |
Console.error("SO term " + term + " invalid"); |
341 |
13 |
termsNotFound.add(term); |
342 |
|
} |
343 |
|
} |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
@param |
350 |
|
@param |
351 |
|
@return |
352 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 7 |
Complexity Density: 0.54 |
|
353 |
1120 |
protected synchronized boolean termIsA(Term childTerm, Term parentTerm)... |
354 |
|
{ |
355 |
|
|
356 |
|
|
357 |
|
|
358 |
1120 |
if (childTerm == null || parentTerm == null) |
359 |
|
{ |
360 |
44 |
return false; |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
1076 |
if (childTerm == parentTerm) |
367 |
|
{ |
368 |
64 |
return true; |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
1012 |
if (!termIsA.containsKey(childTerm)) |
376 |
|
{ |
377 |
40 |
findParents(childTerm); |
378 |
|
} |
379 |
|
|
380 |
1012 |
List<Term> parents = termIsA.get(childTerm); |
381 |
1012 |
for (Term parent : parents) |
382 |
|
{ |
383 |
934 |
if (termIsA(parent, parentTerm)) |
384 |
|
{ |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
256 |
if (!parents.contains(parentTerm)) |
390 |
|
{ |
391 |
95 |
parents.add(parentTerm); |
392 |
|
} |
393 |
256 |
return true; |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
756 |
return false; |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
@param |
405 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
406 |
371 |
protected synchronized void findParents(Term childTerm)... |
407 |
|
{ |
408 |
371 |
List<Term> result = new ArrayList<Term>(); |
409 |
371 |
for (Triple triple : ontology.getTriples(childTerm, null, isA)) |
410 |
|
{ |
411 |
331 |
Term parent = triple.getObject(); |
412 |
331 |
result.add(parent); |
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
331 |
findParents(parent); |
418 |
|
} |
419 |
371 |
termIsA.put(childTerm, result); |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
@param |
427 |
|
@return |
428 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
429 |
372 |
protected Term getTerm(String nameOrDescription)... |
430 |
|
{ |
431 |
372 |
Term t = termsByDescription.get(nameOrDescription); |
432 |
372 |
if (t == null) |
433 |
|
{ |
434 |
61 |
try |
435 |
|
{ |
436 |
61 |
t = ontology.getTerm(nameOrDescription); |
437 |
|
} catch (NoSuchElementException e) |
438 |
|
{ |
439 |
|
|
440 |
|
} |
441 |
|
} |
442 |
372 |
return t; |
443 |
|
} |
444 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
445 |
0 |
public boolean isSequenceVariant(String soTerm)... |
446 |
|
{ |
447 |
0 |
return isA(soTerm, SEQUENCE_VARIANT); |
448 |
|
} |
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
453 |
0 |
@Override... |
454 |
|
public List<String> termsFound() |
455 |
|
{ |
456 |
0 |
synchronized (termsFound) |
457 |
|
{ |
458 |
0 |
Collections.sort(termsFound, String.CASE_INSENSITIVE_ORDER); |
459 |
0 |
return termsFound; |
460 |
|
} |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
466 |
0 |
@Override... |
467 |
|
public List<String> termsNotFound() |
468 |
|
{ |
469 |
0 |
synchronized (termsNotFound) |
470 |
|
{ |
471 |
0 |
Collections.sort(termsNotFound, String.CASE_INSENSITIVE_ORDER); |
472 |
0 |
return termsNotFound; |
473 |
|
} |
474 |
|
} |
475 |
|
} |