-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo2obo.py
executable file
·116 lines (99 loc) · 4.61 KB
/
obo2obo.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
#!/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
from sys import stdout
def stanza_type_weight(stanza):
t = stanza.__class__
if t == obo.Term:
return 1
if t == obo.Typedef:
return 2
if t == obo.Instance:
return 3
return 0
def _get_value(v):
if hasattr(v, 'value'):
return v.value
return v
def stanza_comparator(attr):
def result(a, b):
twa = stanza_type_weight(a)
twb = stanza_type_weight(b)
if attr is None or twa != twb:
return twa - twb
aa = _get_value(getattr(a, attr))
ab = _get_value(getattr(b, attr))
if aa == ab:
return 0
if aa < ab:
return -1
return 1
return result
def stanza_sort_key(attr):
if attr is None:
return stanza_type_weight
return lambda x: (stanza_type_weight(x), getattr(x, attr, x))
class OBO2OBO(OptionParser):
def __init__(self):
OptionParser.__init__(self, usage='usage: %prog [options]')
self.add_option('--include-obsolete', action='store_true', dest='include_obsolete', default=False, help='include obsolete objects')
self.add_option('--sort-by-id', action='store_const', dest='sort_by', const='id', default=None, help='sort stanzas by id')
self.add_option('--sort-by-name', action='store_const', dest='sort_by', const='name', default=None, help='sort stanzas by name')
self.add_option('--keep-synonyms-from', action='append', type='int', dest='synonyms_from', default=[], help='')
self.add_option('--keep-isa-from', action='append', type='int', dest='isa_from', default=[], help='')
self.add_option('--keep-named-from', action='append', type='int', dest='name_from', default=[], help='')
def run(self):
options, args = self.parse_args()
onto = obo.Ontology()
onto.load_files(obo.UnhandledTagFail(), obo.DeprecatedTagWarn(), obo.InvalidXRefWarn(), *args)
onto.check_required()
if options.include_obsolete:
obsolete_reference_option = obo.DanglingReferenceWarn()
else:
obsolete_reference_option = obo.DanglingReferenceFail()
onto.resolve_references(obo.DanglingReferenceFail(), obsolete_reference_option)
onto.write_obo(stdout)
stanzas = list(onto.stanzas.values())
stanzas.sort(key=stanza_sort_key(options.sort_by))
# stanzas.sort(cmp=stanza_comparator(options.sort_by))
synonym_sources = set(args[i] for i in options.synonyms_from)
isa_sources = set(args[i] for i in options.isa_from)
name_sources = set(args[i] for i in options.name_from)
for stanza in stanzas:
if stanza.is_obsolete and not options.include_obsolete:
continue
if isinstance(stanza, obo.BuiltinStanza):
continue
if stanza.source == '<<builtin>>':
continue
if isinstance(stanza, obo.Term) and name_sources and stanza.name.source not in name_sources:
stanza.is_obsolete = True
if synonym_sources:
stanza.synonyms[:] = [s for s in stanza.synonyms if (s.source in synonym_sources)]
if isa_sources and 'is_a' in stanza.references:
stanza.references['is_a'][:] = [r for r in stanza.references['is_a'] if (r.source in isa_sources)]
stanza.write_obo(stdout)
stdout.write('\n')
if __name__ == '__main__':
OBO2OBO().run()