-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathowl2atomese.py
69 lines (61 loc) · 2.78 KB
/
owl2atomese.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
import sys
from pronto import Ontology
def to_atomese(owlfile):
onto = Ontology(owlfile)
with open(owlfile.split('.')[0] +'.scm', 'w') as output:
import_terms(onto, output)
output.write(import_meta(onto, owlfile.split('.')[0]))
def import_meta(onto, name):
meta = "(ListLink \n"
for k in onto.meta.keys():
if str(','.join(onto.meta[k])):
meta = meta + '(ConceptNode "'+ k +': ' + str(','.join(onto.meta[k])) +'")\n'
return eva('has_metadata', '(ConceptNode "'+ name +'")', meta + ')\n')
def import_terms(onto, output):
try:
for term in onto.terms:
output.write(eva("has_name", add_term_type(onto[term].id), add_term_type(onto[term].name)))
output.write(eva("has_description", add_term_type(onto[term].id), add_term_type(str(onto[term].desc))))
for r in onto[term].relations.keys():
if r.direction is 'bottomup':
output.write(''.join([inherit(add_term_type(onto[term].id), add_term_type(i.id)) for i in onto[term].relations[r]]))
elif r.direction is 'topdown':
output.write(''.join([inherit(add_term_type(i.id), add_term_type(onto[term].id)) for i in onto[term].relations[r]]))
for k in onto[term].other.keys():
output.write(''.join([eva(k, add_term_type(onto[term].id), add_term_type(i)) for i in onto[term].other[k]]))
for s in onto[term].synonyms:
output.write(eva("has_synonyms", add_term_type(onto[term].id), add_term_type(str(s))))
except AttributeError:
print(term)
def add_term_type(term):
if 'http' in term:
term = remove_hyperlink(term)
if '"' in term:
term = term.replace('"', '')
if term:
if 'CHEBI' in term:
return '(MoleculeNode "'+ str(term) +'")'
else:
return '(ConceptNode "'+ str(term) +'")'
else:
return str(term)
def eva(predicate, node1, node2):
if node1 and node2:
if predicate == 'is_a' or predicate == 'is-a':
return inherit(node1, node2)
elif predicate == 'id':
return ""
else:
return '(EvaluationLink \n (PredicateNode "' + predicate + '")\n' + '(ListLink \n' + node1 + '\n' + node2 + '))\n\n'
else:
return ""
def inherit(parent, child):
if parent and child:
return '(InheritanceLink \n' + child + '\n' + parent + ')\n\n'
else:
return ""
def remove_hyperlink(term):
term = term.split('/')[-1]
return term.replace('_', ':')
if __name__ == "__main__":
to_atomese(sys.argv[1])