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 7
/
jgf.rb
217 lines (186 loc) · 5.67 KB
/
jgf.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
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
require 'bel'
module BEL::Extension::Format
class FormatJGF
include Formatter
ID = :jgf
MEDIA_TYPES = %i(application/vnd.jgf+json)
EXTENSIONS = %i(jgf.json)
ResourceIndex = ::BEL::Namespace::ResourceIndex
def initialize
json_module = load_implementation_module!
@json_reader = json_module::JSONReader
@json_writer = json_module::JSONWriter
end
def id
ID
end
def media_types
MEDIA_TYPES
end
def file_extensions
EXTENSIONS
end
def deserialize(data, options = {}, &block)
default_resource_index = options.fetch(:default_resource_index) {
ResourceIndex.openbel_published_index('20131211')
}
@json_reader.new(data).each.lazy.select { |obj|
obj.include?(:nodes) && obj.include?(:edges)
}.flat_map { |graph|
unwrap(graph, default_resource_index)
}
end
def serialize(objects, writer = StringIO.new, options = {})
graph = {
:type => 'BEL-V1.0',
:nodes => [],
:edges => []
}
objects.each do |evidence|
stmt = evidence.bel_statement
subject = stmt.subject.to_bel
graph[:nodes] << {
:id => subject,
:label => subject
}
if stmt.object
object = stmt.object.to_bel
graph[:nodes] << {
:id => object,
:label => object
}
graph[:edges] << {
:source => subject,
:relation => stmt.relationship,
:target => object
}
end
end
graph[:nodes].uniq!
json_writer = @json_writer.new
writer << json_writer.write_json_object(
{
:graph => graph
}
)
writer
end
private
def unwrap(graph, default_resource_index)
# index nodes
id_nodes = Hash[
graph[:nodes].map { |node|
[node[:id], (node[:label] || node[:id])]
}
]
ids = id_nodes.keys.to_set
# map edges to statements
bel_statements = graph[:edges].map { |edge|
src, rel, tgt = edge.values_at(:source, :relation, :target)
source_node = id_nodes[src]
target_node = id_nodes[tgt]
if !source_node || !target_node
nil
else
ids.delete(source_node)
ids.delete(target_node)
# semantic default
rel = 'association' unless rel
bel_statement = ::BEL::Script.parse(
"#{source_node} #{rel} #{target_node}\n").select { |obj| obj.is_a? ::BEL::Model::Statement }.first
end
}.compact
# map island nodes to bel statements
if !ids.empty?
bel_statements.concat(
ids.map { |id|
::BEL::Script.parse(
"#{id_nodes[id]}\n"
).select { |obj|
obj.is_a? ::BEL::Model::Statement
}.first
}
)
end
# map statements to evidence objects
bel_statements.map { |bel_statement|
graph_name = graph[:label] || graph[:id] || 'BEL Graph'
metadata = ::BEL::Model::Metadata.new
# establish document header
metadata.document_header[:Name] = graph_name
metadata.document_header[:Description] = graph_name
metadata.document_header[:Version] = '1.0'
# establish annotation definitions
annotations = graph.fetch(:metadata, {}).
fetch(:annotation_definitions, nil)
if !annotations && default_resource_index
annotations = Hash[
default_resource_index.annotations.sort_by { |anno|
anno.prefix
}.map { |anno|
[
anno.prefix,
{
:type => anno.type,
:domain => anno.value
}
]
}
]
end
metadata.annotation_definitions = annotations if annotations
# establish namespace definitions
namespaces = graph.fetch(:metadata, {}).
fetch(:namespace_definitions, nil)
if !namespaces && default_resource_index
namespaces = Hash[
default_resource_index.namespaces.sort_by { |ns|
ns.prefix
}.map { |ns|
[
ns.prefix,
ns.url
]
}
]
end
metadata.namespace_definitions = namespaces if namespaces
::BEL::Model::Evidence.create(
:bel_statement => bel_statement,
:metadata => metadata
)
}
end
# Load the most suitable JSON implementation available within ruby.
# The load order attempted is:
# - oj (provides stream parsing utilizing event callbacks)
# - TODO jrjackson (stream parsing support for JRuby)
# - multi_json (simple buffering abstraction over multiple ruby libraries)
# - json (stock ruby implementation)
def load_implementation_module!
impl_modules = [
'json/oj',
'json/jrjackson',
'json/multi_json',
'json/ruby_json'
]
load_success = impl_modules.any? { |impl_module|
begin
require_relative impl_module
true
rescue LoadError
# Could not load +impl_module+; try the next one
false
end
}
if load_success
BEL::Extension::Format::JSONImplementation
else
mod_s = impl_modules.join(', ')
msg = "Could not load any JSON implementation (tried: #{mod_s})."
raise LoadError.new(msg)
end
end
end
register_formatter(FormatJGF.new)
end