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