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 |
|
|
|
|
| 0% |
Uncovered Elements: 160 (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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
84 |
0 |
public SequenceOntology()... |
85 |
|
{ |
86 |
0 |
termsFound = new ArrayList<String>(); |
87 |
0 |
termsNotFound = new ArrayList<String>(); |
88 |
0 |
termsByDescription = new HashMap<String, Term>(); |
89 |
0 |
termIsA = new HashMap<Term, List<Term>>(); |
90 |
|
|
91 |
0 |
loadOntologyZipFile("so-xp-simple.obo"); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
99 |
0 |
protected void loadOntologyZipFile(String ontologyFile)... |
100 |
|
{ |
101 |
0 |
long now = System.currentTimeMillis(); |
102 |
0 |
ZipInputStream zipStream = null; |
103 |
0 |
try |
104 |
|
{ |
105 |
0 |
String zipFile = ontologyFile + ".zip"; |
106 |
0 |
InputStream inStream = this.getClass() |
107 |
|
.getResourceAsStream("/" + zipFile); |
108 |
0 |
zipStream = new ZipInputStream(new BufferedInputStream(inStream)); |
109 |
0 |
ZipEntry entry; |
110 |
0 |
while ((entry = zipStream.getNextEntry()) != null) |
111 |
|
{ |
112 |
0 |
if (entry.getName().equals(ontologyFile)) |
113 |
|
{ |
114 |
0 |
loadOboFile(zipStream); |
115 |
|
} |
116 |
|
} |
117 |
0 |
long elapsed = System.currentTimeMillis() - now; |
118 |
0 |
Console.info("Loaded Sequence Ontology from " + zipFile + " (" |
119 |
|
+ elapsed + "ms)"); |
120 |
|
} catch (Exception e) |
121 |
|
{ |
122 |
0 |
e.printStackTrace(); |
123 |
|
} finally |
124 |
|
{ |
125 |
0 |
closeStream(zipStream); |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
@param |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
134 |
0 |
protected void closeStream(InputStream is)... |
135 |
|
{ |
136 |
0 |
if (is != null) |
137 |
|
{ |
138 |
0 |
try |
139 |
|
{ |
140 |
0 |
is.close(); |
141 |
|
} catch (IOException e) |
142 |
|
{ |
143 |
|
|
144 |
|
} |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
@throws |
153 |
|
@throws |
154 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
155 |
0 |
protected void loadOboFile(InputStream is)... |
156 |
|
throws ParseException, IOException |
157 |
|
{ |
158 |
0 |
BufferedReader oboFile = new BufferedReader(new InputStreamReader(is)); |
159 |
0 |
OboParser parser = new OboParser(); |
160 |
0 |
ontology = parser.parseOBO(oboFile, "SO", "the SO ontology"); |
161 |
0 |
isA = ontology.getTerm("is_a"); |
162 |
0 |
storeTermNames(); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 8 |
Complexity Density: 0.53 |
|
171 |
0 |
protected void storeTermNames()... |
172 |
|
{ |
173 |
0 |
for (Term term : ontology.getTerms()) |
174 |
|
{ |
175 |
0 |
if (term instanceof Impl) |
176 |
|
{ |
177 |
0 |
String description = term.getDescription(); |
178 |
0 |
if (description != null) |
179 |
|
{ |
180 |
0 |
Term replaced = termsByDescription.get(description); |
181 |
0 |
if (replaced != null) |
182 |
|
{ |
183 |
0 |
boolean newTermIsObsolete = isObsolete(term); |
184 |
0 |
boolean oldTermIsObsolete = isObsolete(replaced); |
185 |
0 |
if (newTermIsObsolete && !oldTermIsObsolete) |
186 |
|
{ |
187 |
0 |
Console.debug("Ignoring " + term.getName() |
188 |
|
+ " as obsolete and duplicated by " |
189 |
|
+ replaced.getName()); |
190 |
0 |
term = replaced; |
191 |
|
} |
192 |
0 |
else if (!newTermIsObsolete && oldTermIsObsolete) |
193 |
|
{ |
194 |
0 |
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 |
0 |
termsByDescription.put(description, term); |
205 |
|
} |
206 |
|
} |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@return |
216 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
217 |
0 |
public static boolean isObsolete(Term term)... |
218 |
|
{ |
219 |
0 |
Annotation ann = term.getAnnotation(); |
220 |
0 |
if (ann != null) |
221 |
|
{ |
222 |
0 |
try |
223 |
|
{ |
224 |
0 |
if (Boolean.TRUE.equals(ann.getProperty("is_obsolete"))) |
225 |
|
{ |
226 |
0 |
return true; |
227 |
|
} |
228 |
|
} catch (NoSuchElementException e) |
229 |
|
{ |
230 |
|
|
231 |
|
} |
232 |
|
} |
233 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
283 |
0 |
@Override... |
284 |
|
public boolean isA(String child, String parent) |
285 |
|
{ |
286 |
0 |
if (child == null || parent == null) |
287 |
|
{ |
288 |
0 |
return false; |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
0 |
if (child.equals(parent)) |
294 |
|
{ |
295 |
0 |
termFound(child); |
296 |
0 |
return true; |
297 |
|
} |
298 |
|
|
299 |
0 |
Term childTerm = getTerm(child); |
300 |
0 |
if (childTerm != null) |
301 |
|
{ |
302 |
0 |
termFound(child); |
303 |
|
} |
304 |
|
else |
305 |
|
{ |
306 |
0 |
termNotFound(child); |
307 |
|
} |
308 |
0 |
Term parentTerm = getTerm(parent); |
309 |
|
|
310 |
0 |
return termIsA(childTerm, parentTerm); |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
318 |
0 |
private void termFound(String term)... |
319 |
|
{ |
320 |
0 |
synchronized (termsFound) |
321 |
|
{ |
322 |
0 |
if (!termsFound.contains(term)) |
323 |
|
{ |
324 |
0 |
termsFound.add(term); |
325 |
|
} |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
@param |
333 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
334 |
0 |
private void termNotFound(String term)... |
335 |
|
{ |
336 |
0 |
synchronized (termsNotFound) |
337 |
|
{ |
338 |
0 |
if (!termsNotFound.contains(term)) |
339 |
|
{ |
340 |
0 |
Console.error("SO term " + term + " invalid"); |
341 |
0 |
termsNotFound.add(term); |
342 |
|
} |
343 |
|
} |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
@param |
350 |
|
@param |
351 |
|
@return |
352 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 7 |
Complexity Density: 0.54 |
|
353 |
0 |
protected synchronized boolean termIsA(Term childTerm, Term parentTerm)... |
354 |
|
{ |
355 |
|
|
356 |
|
|
357 |
|
|
358 |
0 |
if (childTerm == null || parentTerm == null) |
359 |
|
{ |
360 |
0 |
return false; |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
0 |
if (childTerm == parentTerm) |
367 |
|
{ |
368 |
0 |
return true; |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
0 |
if (!termIsA.containsKey(childTerm)) |
376 |
|
{ |
377 |
0 |
findParents(childTerm); |
378 |
|
} |
379 |
|
|
380 |
0 |
List<Term> parents = termIsA.get(childTerm); |
381 |
0 |
for (Term parent : parents) |
382 |
|
{ |
383 |
0 |
if (termIsA(parent, parentTerm)) |
384 |
|
{ |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
0 |
if (!parents.contains(parentTerm)) |
390 |
|
{ |
391 |
0 |
parents.add(parentTerm); |
392 |
|
} |
393 |
0 |
return true; |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
0 |
return false; |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
@param |
405 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
406 |
0 |
protected synchronized void findParents(Term childTerm)... |
407 |
|
{ |
408 |
0 |
List<Term> result = new ArrayList<Term>(); |
409 |
0 |
for (Triple triple : ontology.getTriples(childTerm, null, isA)) |
410 |
|
{ |
411 |
0 |
Term parent = triple.getObject(); |
412 |
0 |
result.add(parent); |
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
0 |
findParents(parent); |
418 |
|
} |
419 |
0 |
termIsA.put(childTerm, result); |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
@param |
427 |
|
@return |
428 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
429 |
0 |
protected Term getTerm(String nameOrDescription)... |
430 |
|
{ |
431 |
0 |
Term t = termsByDescription.get(nameOrDescription); |
432 |
0 |
if (t == null) |
433 |
|
{ |
434 |
0 |
try |
435 |
|
{ |
436 |
0 |
t = ontology.getTerm(nameOrDescription); |
437 |
|
} catch (NoSuchElementException e) |
438 |
|
{ |
439 |
|
|
440 |
|
} |
441 |
|
} |
442 |
0 |
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 |
|
} |