This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade-namespaces.rb
executable file
·132 lines (117 loc) · 3.72 KB
/
upgrade-namespaces.rb
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
#!/usr/bin/env ruby
# vim: ts=2 sw=2:
# upgrade-namespaces: Upgrade namespaces for BEL file given a change log.
#
# From BEL file
# usage: ruby upgrade-namespaces.rb -b file.bel -c file.json
# From standard in
# usage: echo "<BEL DOCUMENT STRING>" | ruby upgrade-namespaces.rb -c file.json
if __FILE__ == $0
require 'optparse'
# setup and parse options
options = {}
op = OptionParser.new do |opts|
opts.banner = "Usage: upgrade-namespaces.rb [options] [.bel file]"
opts.on('-b', '--bel BEL_FILE', 'BEL file to upgrade. STDIN (standard in) can also be used for BEL content.') do |bel|
options['bel'] = bel
end
opts.on("-c", "--change-log JSON_FILE", "Change log file") do |change_log|
options['change_log'] = change_log
end
end.parse!
# option guards
unless options['change_log']
$stderr.puts "Missing --change-log option. Use -h / --help for details."
exit
end
unless options['bel'] or not STDIN.tty?
$stderr.puts "No bel content provided. Either use --bel option or STDIN (standard in). Use -h / --help for details."
exit
end
if not File.exists? options['change_log']
$stderr.puts "No file for change_log, #{options['change_log']}"
exit
end
if options['bel'] and not File.exists? options['bel']
$stderr.puts "No file for bel, #{options['bel']}"
exit
end
# read bel content
content = (STDIN.tty?) ? File.open(options['bel']).read : $stdin.read
require 'bel'
require 'json'
require 'set'
class Main
EvidenceMatcher = Regexp.compile(/SET Evidence = ([0-9a-zA-Z]+)/)
attr_reader :ttl
def initialize(content, change_log)
begin
changelog_file = File.open(change_log)
@clog = JSON.parse(changelog_file.read)
ensure
changelog_file.close
end
@keywords_seen = Set.new
parser = BEL::Script::Parser.new
parser.add_observer self
parser.parse(content)
end
def update(obj)
# redefine namespace based on change log's `redefine` block
if obj.is_a? BEL::Script::NamespaceDefinition
if @clog.has_key? 'redefine'
redefine = @clog['redefine']
if redefine.has_key? obj.prefix
entry = redefine[obj.prefix]
obj.prefix = entry['new_keyword']
obj.value = entry['new_url']
end
end
# deduplicate namespaces for output purposes
if @keywords_seen.include? obj.prefix
return
end
@keywords_seen.add(obj.prefix)
end
if obj.is_a? BEL::Script::Annotation
if obj.name == 'Evidence'
ev = obj.to_s
ev.gsub!(EvidenceMatcher, 'SET Evidence = "\1"')
puts ev
return
end
end
if obj.is_a? BEL::Script::Parameter
if @clog.has_key? obj.ns
clog_ns = @clog[obj.ns]
if clog_ns.has_key? obj.value
replacement = clog_ns[obj.value]
if replacement == 'unresolved' or replacement == 'withdrawn'
$stderr.puts "no replacement value for #{obj.ns} '#{obj.value}' - value '#{replacement}'"
return
end
obj.value = replacement
end
end
# redefine param namespace based on change log's `redefine` block
if @clog.has_key? 'redefine'
redefine = @clog['redefine']
if redefine.has_key? obj.ns
obj.ns = redefine[obj.ns]['new_keyword']
end
end
return
end
if obj.is_a? BEL::Script::Term
return # do not print (part of statement)
end
puts obj.to_s
end
private
def error_file(file_name)
$stderr.puts "#{file_name} is not readable"
exit 1
end
end
prog = Main.new(content, options['change_log'])
end