-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo2dict.py
executable file
·158 lines (129 loc) · 6.1 KB
/
obo2dict.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
#!/usr/bin/env python
# MIT License
#
# Copyright (c) 2017-2023 Institut national de recherche pour l'agriculture, l'alimentation et l'environnement (Inrae)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from optparse import OptionParser
import obo
import codecs
class ValueMap(object):
def __init__(self):
self.item = None
self.stanza = None
def set(self, value):
(self.item, self.stanza) = value
def __getitem__(self, key):
if not isinstance(key, str):
raise TypeError()
method = 'key_' + key.replace('-', '_')
if not hasattr(self, method):
raise KeyError(key)
return getattr(self, method)()
def key_name(self):
return self.stanza.name.value
def key_id(self):
return self.stanza.id.value
def key_parent_id(self):
if isinstance(self.item, obo.Stanza):
return self.item.id.value
for parent in self.stanza.parents():
return parent.id.value
def key_synonym(self):
if isinstance(self.item, obo.Synonym):
return self.item.text
if isinstance(self.item, obo.Stanza):
return self.item.name.value
raise Exception()
def key_xref(self):
if isinstance(self.item, obo.XRef):
return self.item.reference
return '\t'.join(x.reference for x in self.stanza.xref)
def key_subset(self):
for ancestor in self.stanza.ancestors(include_self=True):
if isinstance(ancestor, obo.TermOrType):
for subset in ancestor.subsets:
return subset
return ''
def key_id_path(self):
if isinstance(self.item, list):
return '/' + '/'.join(term.id.value for term in self.item)
if isinstance(self.item, obo.Term):
paths = list(self.item.paths(include_self=True))
return '/' + '/'.join(term.id.value for term in paths[0])
if isinstance(self.item, obo.Synonym):
paths = list(self.item.stanza.paths(include_self=True))
return '/' + '/'.join(term.id.value for term in paths[0])
if isinstance(self.item, obo.XRef):
paths = list(self.item.term.paths(include_self=True))
return '/' + '/'.join(term.id.value for term in paths[0])
raise Exception('expected list, got ' + str(self.item))
def key_name_path(self):
if isinstance(self.item, list):
return '/' + '/'.join(term.name.value for term in self.item)
if isinstance(self.item, obo.Term):
paths = list(self.item.paths(include_self=True))
return '/' + '/'.join(term.name.value for term in paths[0])
raise Exception('expected list, got ' + str(self.item))
def iter_terms(onto):
return ((term, term) for term in onto.stanzas.values() if isinstance(term, obo.Term))
def iter_term_synonyms(onto):
for term in onto.stanzas.values():
if isinstance(term, obo.Term):
yield term, term
for syn in term.synonyms:
yield syn, term
def iter_term_parents(onto):
for term in onto.stanzas.values():
if isinstance(term, obo.Term):
for parent in term.parents():
yield parent, term
def iter_term_paths(onto):
for term in onto.stanzas.values():
if isinstance(term, obo.Term):
for path in term.paths(include_self=True):
yield path, term
def iter_term_xrefs(onto):
for term in onto.stanzas.values():
if isinstance(term, obo.Term):
for xref in term.xref:
yield xref, term
class OBO2Dict(OptionParser):
def __init__(self):
OptionParser.__init__(self, usage='usage: %prog [options]')
self.set_defaults(iter=iter_term_synonyms, pattern='%(synonym)s\\t%(id)s\\t%(name)s')
self.add_option('--term-paths', action='store_const', dest='iter', const=iter_term_paths, help='iterates over term paths')
self.add_option('--term-synonyms', action='store_const', dest='iter', const=iter_term_synonyms, help='iterates over term synonyms')
self.add_option('--term-xrefs', action='store_const', dest='iter', const=iter_term_xrefs, help='iterates over term cross references')
self.add_option('--term-parents', action='store_const', dest='iter', const=iter_term_parents, help='iterates over term parents')
self.add_option('--terms', action='store_const', dest='iter', const=iter_terms, help='iterates over terms')
self.add_option('--pattern', action='store', type='string', dest='pattern', metavar='PATTERN', help='item output pattern (default: %default)')
def run(self):
options, args = self.parse_args()
onto = obo.Ontology()
onto.load_files(obo.UnhandledTagFail(), obo.DeprecatedTagWarn(), obo.InvalidXRefWarn(), *args)
onto.check_required()
onto.resolve_references(obo.DanglingReferenceFail(), obo.DanglingReferenceWarn())
map = ValueMap()
pattern = options.pattern.replace('\\t', '\t')
for value in options.iter(onto):
map.set(value)
print(pattern % map)
if __name__ == '__main__':
OBO2Dict().run()