-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_local_json.py
260 lines (215 loc) · 8.05 KB
/
test_local_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from ontobio.ontol_factory import OntologyFactory
import logging
PART_OF = 'BFO:0000050'
QUALITY = 'PATO:0000001'
PLOIDY = 'PATO:0001374'
EUPLOID = 'PATO:0001393'
SHAPE = 'PATO:0000052'
Y_SHAPED = 'PATO:0001201'
def test_local_json_parse():
"""
Load ontology from JSON
"""
factory = OntologyFactory()
print("Creating ont")
ont = factory.create('tests/resources/pato.json')
ploidy = ont.node(PLOIDY)
print("PLOIDY: {}".format(ploidy))
assert ont.label(PLOIDY) == 'ploidy'
# exact match
search_results = ont.search('shape')
print("SEARCH (exact): {}".format(search_results))
assert [SHAPE] == search_results
# implicit regexp
search_results = ont.search('%shape%')
print("SEARCH (re, implicit): {}".format(search_results))
assert SHAPE in search_results
assert len(search_results)>10
# explicit regexp
search_results = ont.search('.*shape.*', is_regex=True)
print("SEARCH (re, explicit): {}".format(search_results))
assert SHAPE in search_results
assert len(search_results)>10
num_nodes = 0
for n in ont.nodes():
num_nodes = num_nodes+1
assert num_nodes > 100
NUCLEUS='GO:0005634'
INTRACELLULAR='GO:0005622'
INTRACELLULAR_PART='GO:0044424'
IMBO = 'GO:0043231'
CELL = 'GO:0005623'
CELLULAR_COMPONENT = 'GO:0005575'
WIKIPEDIA_CELL = 'Wikipedia:Cell_(biology)'
NIF_CELL = 'NIF_Subcellular:sao1813327414'
CELL_PART = 'GO:0044464'
def test_graph():
"""
Load ontology from JSON
"""
factory = OntologyFactory()
print("Creating ont")
ont = factory.create('tests/resources/nucleus.json')
assert ont.id == 'http://purl.obolibrary.org/obo/go-test.owl'
icp = ont.node(INTRACELLULAR_PART)
print("ICP: {}".format(icp))
assert ont.label(INTRACELLULAR_PART) == 'intracellular part'
assert ont.node_type(INTRACELLULAR_PART) == 'CLASS'
graph = ont.get_graph()
print("GRAPH: {}".format(graph.nodes))
ancs = ont.ancestors(NUCLEUS)
print("ANCS nucleus (all): {}".format(ancs))
assert CELL in ancs
assert CELLULAR_COMPONENT in ancs
assert INTRACELLULAR in ancs
assert NUCLEUS not in ancs
ancs = ont.ancestors(INTRACELLULAR_PART)
print("ANCS intracellular part(all): {}".format(ancs))
assert CELL in ancs
assert CELLULAR_COMPONENT in ancs
assert NUCLEUS not in ancs
ancs = ont.ancestors(INTRACELLULAR_PART, relations=['subClassOf'])
print("ANCS intracellular part(subclass): {}".format(ancs))
assert CELLULAR_COMPONENT in ancs
assert CELL not in ancs
assert NUCLEUS not in ancs
assert INTRACELLULAR_PART not in ancs
ancs = ont.ancestors(INTRACELLULAR_PART, relations=['subClassOf'], reflexive=True)
print("ANCS intracellular part(subclass): {}".format(ancs))
assert CELLULAR_COMPONENT in ancs
assert CELL not in ancs
assert NUCLEUS not in ancs
assert INTRACELLULAR_PART in ancs
# note: queries over *only* part_of are a non-use case, as we
# typically always include subClassOf, due to how these chain
# together according to OWL semantics
ancs = ont.ancestors(INTRACELLULAR_PART, relations=[PART_OF])
print("ANCS intracellular part(part_of): {}".format(ancs))
assert INTRACELLULAR in ancs
assert CELL not in ancs
assert NUCLEUS not in ancs
ancs = ont.parents(INTRACELLULAR_PART)
print("PARENTS intracellular (all): {}".format(ancs))
assert INTRACELLULAR in ancs
assert CELL_PART in ancs
assert CELLULAR_COMPONENT not in ancs
assert NUCLEUS not in ancs
ancs = ont.parents(INTRACELLULAR_PART, relations=[PART_OF])
print("PARENTS intracellular (part_of): {}".format(ancs))
assert INTRACELLULAR in ancs
assert CELL_PART not in ancs
assert CELLULAR_COMPONENT not in ancs
assert NUCLEUS not in ancs
decs = ont.descendants(INTRACELLULAR_PART)
print("DECS: {}".format(decs))
assert NUCLEUS in decs
assert CELL not in decs
decs = ont.descendants(INTRACELLULAR, relations=[PART_OF])
print("DECS: {}".format(decs))
assert INTRACELLULAR_PART in decs
assert NUCLEUS not in decs
assert CELL not in decs
decs = ont.children(INTRACELLULAR)
print("CHILDREN (all): {}".format(decs))
assert [INTRACELLULAR_PART] == decs
decs = ont.children(CELL_PART)
print("CHILDREN (all): {}".format(decs))
assert INTRACELLULAR_PART in decs
assert INTRACELLULAR in decs
decs = ont.children(INTRACELLULAR, relations=[PART_OF])
print("CHILDREN (po): {}".format(decs))
assert INTRACELLULAR_PART in decs
assert NUCLEUS not in decs
assert CELL not in decs
xrefs = ont.xrefs(CELL)
print("XREFS (from GO): {}".format(xrefs))
assert WIKIPEDIA_CELL in xrefs
assert NIF_CELL in xrefs
assert len(xrefs) == 2
# xrefs are bidirectional
xrefs = ont.xrefs(WIKIPEDIA_CELL, bidirectional=True)
print("XREFS (from WP, bidi): {}".format(xrefs))
assert CELL in xrefs
assert len(xrefs) == 1
# xrefs queries unidirectional by default
xrefs = ont.xrefs(WIKIPEDIA_CELL)
print("XREFS (from WP): {}".format(xrefs))
assert len(xrefs) == 0
tdef = ont.text_definition(NUCLEUS)
print("TDEF: {}".format(tdef))
assert tdef.xrefs == [ "GOC:go_curators" ]
assert tdef.val.startswith("A membrane-bounded organelle of eukaryotic cells in which")
[ldef] = ont.logical_definitions(INTRACELLULAR_PART)
print("LDEF: {}".format(ldef))
assert ldef.genus_ids == [CELLULAR_COMPONENT]
assert ldef.restrictions == [(PART_OF, INTRACELLULAR)]
syns = ont.synonyms(CELL_PART, include_label=True)
print("SYNS: {}".format(syns))
[s1] = [x for x in syns if x.val == 'protoplast']
assert s1.pred == 'hasRelatedSynonym'
assert s1.xrefs == ['GOC:mah']
GOSLIM = 'goslim_generic'
subsets = ont.subsets(NUCLEUS)
print("SUBSETS: {}".format(subsets))
assert GOSLIM in subsets
assert len(subsets) > 0
in_slim = ont.extract_subset(GOSLIM)
print("IN SLIM: {}".format(in_slim))
assert len(in_slim) > 0
assert NUCLEUS in in_slim
#logging.basicConfig(level=logging.DEBUG)
assert [] == ont.search('protoplast', synonyms=False)
assert {CELL_PART,INTRACELLULAR} == set(ont.search('protoplast', synonyms=True))
assert ont.has_node(CELL_PART)
assert not ont.has_node('FOO:123')
# relations
assert ont.label(PART_OF) == 'part of'
assert ont.node_type(PART_OF) == 'PROPERTY'
# ensure subontology retains properties
decs = ont.descendants(CELL, reflexive=True)
subont = ont.subontology(nodes=decs)
syns = subont.synonyms(CELL_PART, include_label=True)
print("SYNS: {}".format(syns))
[s1] = [x for x in syns if x.val == 'protoplast']
assert s1.pred == 'hasRelatedSynonym'
assert s1.xrefs == ['GOC:mah']
assert subont.parents(NUCLEUS) == [IMBO]
from ontobio import GraphRenderer
w = GraphRenderer.create('obo')
w.write(subont, query_ids=[CELL, CELL_PART, NUCLEUS])
def test_subontology():
"""
Load extracting subontology
"""
factory = OntologyFactory()
print("Creating ont")
ont = factory.create('tests/resources/go-truncated-pombase.json')
print("ONT NODES: {}".format(ont.nodes()))
subont = ont.subontology(relations=['subClassOf'])
PERM = 'GO:1990578'
print("NODES: {}".format(subont.nodes()))
ancs = subont.ancestors(PERM)
print(str(ancs))
assert len(ancs) > 0
def test_obsolete():
"""
Test obsoletion metadata
"""
factory = OntologyFactory()
print("Creating ont")
ont = factory.create('tests/resources/obsolete.json')
print("ONT NODES: {}".format(ont.nodes()))
n_obs = 0
for nid in ont.nodes():
is_obs = ont.is_obsolete(nid)
if is_obs:
print("OBS: {} {}".format(nid, ont.label(nid)))
n_obs += 1
rb = ont.replaced_by(nid)
if rb is not None:
print("REPLACED BY: {} {}".format(rb, ont.label(rb)))
assert ont.replaced_by('GO:1') == ['GO:2']
assert ont.replaced_by('GO:4') == ['GO:3']
assert ont.replaced_by('GO:0005913') == ['GO:0005912']
assert ont.replaced_by('GO:0006758') == ['GO:0006754']
assert n_obs == 5