bel 0.3.3 → 0.4.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -8
- data/bel.gemspec +10 -15
- data/bin/bel +63 -4
- data/bin/bel2rdf.rb +1 -1
- data/bin/bel_compare.rb +1 -1
- data/bin/bel_parse.rb +1 -1
- data/bin/bel_rdfschema.rb +1 -1
- data/bin/bel_summarize.rb +1 -1
- data/bin/bel_upgrade.rb +1 -1
- data/lib/bel.rb +8 -10
- data/lib/bel/completion.rb +3 -2
- data/lib/bel/completion/value_match_rule.rb +10 -0
- data/lib/bel/evidence_model/citation.rb +101 -43
- data/lib/bel/evidence_model/evidence.rb +2 -2
- data/lib/bel/evidence_model/experiment_context.rb +1 -0
- data/lib/bel/evidence_model/metadata.rb +15 -2
- data/lib/bel/evidence_model/references.rb +10 -10
- data/lib/bel/json.rb +63 -0
- data/lib/bel/json/adapter/multi_json.rb +36 -0
- data/lib/bel/json/adapter/oj.rb +65 -0
- data/lib/bel/json/adapter/ruby_json.rb +28 -0
- data/lib/bel/json/reader.rb +9 -0
- data/lib/bel/json/writer.rb +9 -0
- data/lib/bel/libbel.rb +1 -4
- data/lib/bel/parser.rb +2 -2
- data/lib/bel/rdf_repository.rb +18 -0
- data/lib/bel/rdf_repository/plugins/memory.rb +28 -0
- data/lib/bel/rdf_repository/plugins/mongo.rb +28 -0
- data/lib/bel/resource.rb +24 -0
- data/lib/bel/resource/namespace.rb +122 -0
- data/lib/bel/resource/namespace_value.rb +69 -0
- data/lib/bel/resource/namespaces.rb +83 -0
- data/lib/bel/resource/search.rb +26 -0
- data/lib/bel/resource/search/api.rb +36 -0
- data/lib/bel/resource/search/search_result.rb +32 -0
- data/lib/bel/translate.rb +108 -0
- data/lib/bel/translator.rb +69 -0
- data/lib/bel/translator/plugins/bel_script.rb +36 -0
- data/lib/bel/translator/plugins/bel_script/bel_yielder.rb +144 -0
- data/lib/bel/translator/plugins/bel_script/evidence_yielder.rb +95 -0
- data/lib/bel/translator/plugins/bel_script/translator.rb +24 -0
- data/lib/bel/translator/plugins/jgf.rb +37 -0
- data/lib/bel/translator/plugins/jgf/translator.rb +160 -0
- data/lib/bel/translator/plugins/json_evidence.rb +38 -0
- data/lib/bel/translator/plugins/json_evidence/translator.rb +90 -0
- data/lib/bel/translator/plugins/rdf.rb +48 -0
- data/lib/bel/translator/plugins/rdf/bel_schema.rb +339 -0
- data/lib/bel/translator/plugins/rdf/monkey_patch.rb +310 -0
- data/lib/bel/translator/plugins/rdf/reader.rb +173 -0
- data/lib/bel/translator/plugins/rdf/translator.rb +40 -0
- data/lib/bel/translator/plugins/rdf/writer.rb +45 -0
- data/lib/bel/translator/plugins/xbel.rb +36 -0
- data/lib/bel/translator/plugins/xbel/evidence_handler.rb +468 -0
- data/lib/bel/translator/plugins/xbel/evidence_yielder.rb +24 -0
- data/lib/bel/translator/plugins/xbel/translator.rb +24 -0
- data/lib/bel/translator/plugins/xbel/xbel_yielder.rb +414 -0
- data/lib/bel/vendor/little-plugger.rb +323 -0
- data/lib/bel/version.rb +1 -1
- metadata +44 -158
- data/lib/bel/extension.rb +0 -37
- data/lib/bel/extension_format.rb +0 -207
- data/lib/bel/extensions/bel.rb +0 -258
- data/lib/bel/extensions/jgf.rb +0 -219
- data/lib/bel/extensions/json/jrjackson.rb +0 -31
- data/lib/bel/extensions/json/json.rb +0 -133
- data/lib/bel/extensions/json/multi_json.rb +0 -29
- data/lib/bel/extensions/json/oj.rb +0 -68
- data/lib/bel/extensions/json/ruby_json.rb +0 -29
- data/lib/bel/extensions/rdf/bel_rdf.rb +0 -338
- data/lib/bel/extensions/rdf/rdf.rb +0 -584
- data/lib/bel/extensions/xbel.rb +0 -923
- data/lib/bel/format.rb +0 -58
data/lib/bel/extensions/jgf.rb
DELETED
@@ -1,219 +0,0 @@
|
|
1
|
-
require 'bel'
|
2
|
-
|
3
|
-
module BEL::Extension::Format
|
4
|
-
|
5
|
-
class FormatJGF
|
6
|
-
|
7
|
-
include Formatter
|
8
|
-
|
9
|
-
ID = :jgf
|
10
|
-
MEDIA_TYPES = %i(application/vnd.jgf+json)
|
11
|
-
EXTENSIONS = %i(jgf.json)
|
12
|
-
ResourceIndex = ::BEL::Namespace::ResourceIndex
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
json_module = load_implementation_module!
|
16
|
-
@json_reader = json_module::JSONReader
|
17
|
-
@json_writer = json_module::JSONWriter
|
18
|
-
end
|
19
|
-
|
20
|
-
def id
|
21
|
-
ID
|
22
|
-
end
|
23
|
-
|
24
|
-
def media_types
|
25
|
-
MEDIA_TYPES
|
26
|
-
end
|
27
|
-
|
28
|
-
def file_extensions
|
29
|
-
EXTENSIONS
|
30
|
-
end
|
31
|
-
|
32
|
-
def deserialize(data, options = {}, &block)
|
33
|
-
default_resource_index = options.fetch(:default_resource_index) {
|
34
|
-
ResourceIndex.openbel_published_index('20131211')
|
35
|
-
}
|
36
|
-
|
37
|
-
@json_reader.new(data).each.lazy.select { |obj|
|
38
|
-
obj.include?(:nodes) && obj.include?(:edges)
|
39
|
-
}.flat_map { |graph|
|
40
|
-
unwrap(graph, default_resource_index)
|
41
|
-
}
|
42
|
-
end
|
43
|
-
|
44
|
-
def serialize(objects, writer = StringIO.new, options = {})
|
45
|
-
graph = {
|
46
|
-
:type => 'BEL-V1.0',
|
47
|
-
:nodes => [],
|
48
|
-
:edges => []
|
49
|
-
}
|
50
|
-
|
51
|
-
objects.each do |evidence|
|
52
|
-
stmt = evidence.bel_statement
|
53
|
-
subject = stmt.subject.to_bel
|
54
|
-
|
55
|
-
graph[:nodes] << {
|
56
|
-
:id => subject,
|
57
|
-
:label => subject
|
58
|
-
}
|
59
|
-
|
60
|
-
if stmt.object
|
61
|
-
object = stmt.object.to_bel
|
62
|
-
graph[:nodes] << {
|
63
|
-
:id => object,
|
64
|
-
:label => object
|
65
|
-
}
|
66
|
-
graph[:edges] << {
|
67
|
-
:source => subject,
|
68
|
-
:relation => stmt.relationship,
|
69
|
-
:target => object
|
70
|
-
}
|
71
|
-
end
|
72
|
-
end
|
73
|
-
graph[:nodes].uniq!
|
74
|
-
|
75
|
-
json_writer = @json_writer.new
|
76
|
-
writer << json_writer.write_json_object(
|
77
|
-
{
|
78
|
-
:graph => graph
|
79
|
-
}
|
80
|
-
)
|
81
|
-
writer
|
82
|
-
end
|
83
|
-
|
84
|
-
private
|
85
|
-
|
86
|
-
def unwrap(graph, default_resource_index)
|
87
|
-
# index nodes
|
88
|
-
id_nodes = Hash[
|
89
|
-
graph[:nodes].map { |node|
|
90
|
-
[node[:id], (node[:label] || node[:id])]
|
91
|
-
}
|
92
|
-
]
|
93
|
-
ids = id_nodes.keys.to_set
|
94
|
-
|
95
|
-
# map edges to statements
|
96
|
-
bel_statements = graph[:edges].map { |edge|
|
97
|
-
src, rel, tgt = edge.values_at(:source, :relation, :target)
|
98
|
-
source_node = id_nodes[src]
|
99
|
-
target_node = id_nodes[tgt]
|
100
|
-
|
101
|
-
if !source_node || !target_node
|
102
|
-
nil
|
103
|
-
else
|
104
|
-
ids.delete(source_node)
|
105
|
-
ids.delete(target_node)
|
106
|
-
|
107
|
-
# semantic default
|
108
|
-
rel = 'association' unless rel
|
109
|
-
|
110
|
-
bel_statement = ::BEL::Script.parse(
|
111
|
-
"#{source_node} #{rel} #{target_node}\n").select { |obj| obj.is_a? ::BEL::Model::Statement }.first
|
112
|
-
end
|
113
|
-
}.compact
|
114
|
-
|
115
|
-
# map island nodes to bel statements
|
116
|
-
if !ids.empty?
|
117
|
-
bel_statements.concat(
|
118
|
-
ids.map { |id|
|
119
|
-
::BEL::Script.parse(
|
120
|
-
"#{id_nodes[id]}\n"
|
121
|
-
).select { |obj|
|
122
|
-
obj.is_a? ::BEL::Model::Statement
|
123
|
-
}.first
|
124
|
-
}
|
125
|
-
)
|
126
|
-
end
|
127
|
-
|
128
|
-
# map statements to evidence objects
|
129
|
-
bel_statements.map { |bel_statement|
|
130
|
-
graph_name = graph[:label] || graph[:id] || 'BEL Graph'
|
131
|
-
metadata = ::BEL::Model::Metadata.new
|
132
|
-
references = ::BEL::Model::References.new
|
133
|
-
|
134
|
-
# establish document header
|
135
|
-
metadata.document_header[:Name] = graph_name
|
136
|
-
metadata.document_header[:Description] = graph_name
|
137
|
-
metadata.document_header[:Version] = '1.0'
|
138
|
-
|
139
|
-
# establish annotation definitions
|
140
|
-
annotations = graph.fetch(:metadata, {}).
|
141
|
-
fetch(:annotation_definitions, nil)
|
142
|
-
if !annotations && default_resource_index
|
143
|
-
annotations = Hash[
|
144
|
-
default_resource_index.annotations.sort_by { |anno|
|
145
|
-
anno.prefix
|
146
|
-
}.map { |anno|
|
147
|
-
[
|
148
|
-
anno.prefix,
|
149
|
-
{
|
150
|
-
:type => anno.type,
|
151
|
-
:domain => anno.value
|
152
|
-
}
|
153
|
-
]
|
154
|
-
}
|
155
|
-
]
|
156
|
-
end
|
157
|
-
references.annotation_definitions = annotations if annotations
|
158
|
-
|
159
|
-
# establish namespace definitions
|
160
|
-
namespaces = graph.fetch(:metadata, {}).
|
161
|
-
fetch(:namespace_definitions, nil)
|
162
|
-
if !namespaces && default_resource_index
|
163
|
-
namespaces = Hash[
|
164
|
-
default_resource_index.namespaces.sort_by { |ns|
|
165
|
-
ns.prefix
|
166
|
-
}.map { |ns|
|
167
|
-
[
|
168
|
-
ns.prefix,
|
169
|
-
ns.url
|
170
|
-
]
|
171
|
-
}
|
172
|
-
]
|
173
|
-
end
|
174
|
-
references.namespace_definitions = namespaces if namespaces
|
175
|
-
|
176
|
-
::BEL::Model::Evidence.create(
|
177
|
-
:bel_statement => bel_statement,
|
178
|
-
:metadata => metadata,
|
179
|
-
:references => references
|
180
|
-
)
|
181
|
-
}
|
182
|
-
end
|
183
|
-
|
184
|
-
# Load the most suitable JSON implementation available within ruby.
|
185
|
-
# The load order attempted is:
|
186
|
-
# - oj (provides stream parsing utilizing event callbacks)
|
187
|
-
# - TODO jrjackson (stream parsing support for JRuby)
|
188
|
-
# - multi_json (simple buffering abstraction over multiple ruby libraries)
|
189
|
-
# - json (stock ruby implementation)
|
190
|
-
def load_implementation_module!
|
191
|
-
impl_modules = [
|
192
|
-
'json/oj',
|
193
|
-
'json/jrjackson',
|
194
|
-
'json/multi_json',
|
195
|
-
'json/ruby_json'
|
196
|
-
]
|
197
|
-
|
198
|
-
load_success = impl_modules.any? { |impl_module|
|
199
|
-
begin
|
200
|
-
require_relative impl_module
|
201
|
-
true
|
202
|
-
rescue LoadError
|
203
|
-
# Could not load +impl_module+; try the next one
|
204
|
-
false
|
205
|
-
end
|
206
|
-
}
|
207
|
-
|
208
|
-
if load_success
|
209
|
-
BEL::Extension::Format::JSONImplementation
|
210
|
-
else
|
211
|
-
mod_s = impl_modules.join(', ')
|
212
|
-
msg = "Could not load any JSON implementation (tried: #{mod_s})."
|
213
|
-
raise LoadError.new(msg)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
register_formatter(FormatJGF.new)
|
219
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'jrjackson'
|
2
|
-
# XXX Fake impl
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module BEL::Extension::Format
|
6
|
-
module JSONImplementation
|
7
|
-
|
8
|
-
class JSONReader
|
9
|
-
def initialize(data)
|
10
|
-
@data = data
|
11
|
-
end
|
12
|
-
|
13
|
-
def each(&block)
|
14
|
-
if block_given?
|
15
|
-
JSON.load(@data).each do |obj|
|
16
|
-
yield obj
|
17
|
-
end
|
18
|
-
else
|
19
|
-
to_enum(:each)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class JSONWriter
|
25
|
-
|
26
|
-
def write_json_object(json_object)
|
27
|
-
JSON.dump(json_object)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,133 +0,0 @@
|
|
1
|
-
require 'bel/evidence_model'
|
2
|
-
require 'bel/namespace'
|
3
|
-
require 'bel/script'
|
4
|
-
|
5
|
-
module BEL::Extension::Format
|
6
|
-
|
7
|
-
class FormatJSON
|
8
|
-
|
9
|
-
include Formatter
|
10
|
-
ID = :json
|
11
|
-
MEDIA_TYPES = %i(application/json)
|
12
|
-
EXTENSIONS = %i(json)
|
13
|
-
EVIDENCE_ROOT = :evidence
|
14
|
-
|
15
|
-
def initialize
|
16
|
-
json_module = load_implementation_module!
|
17
|
-
@json_reader = json_module::JSONReader
|
18
|
-
@json_writer = json_module::JSONWriter
|
19
|
-
end
|
20
|
-
|
21
|
-
def id
|
22
|
-
ID
|
23
|
-
end
|
24
|
-
|
25
|
-
def media_types
|
26
|
-
MEDIA_TYPES
|
27
|
-
end
|
28
|
-
|
29
|
-
def file_extensions
|
30
|
-
EXTENSIONS
|
31
|
-
end
|
32
|
-
|
33
|
-
def deserialize(data)
|
34
|
-
@json_reader.new(data).each.lazy.select { |obj|
|
35
|
-
obj.include?(:evidence)
|
36
|
-
}.map { |hash|
|
37
|
-
unwrap(hash)
|
38
|
-
}
|
39
|
-
end
|
40
|
-
|
41
|
-
def serialize(objects, writer = StringIO.new)
|
42
|
-
json_writer = @json_writer.new
|
43
|
-
# json array start
|
44
|
-
writer << "["
|
45
|
-
|
46
|
-
# json objects
|
47
|
-
begin
|
48
|
-
evidence_enum = objects.each
|
49
|
-
|
50
|
-
# write first evidence
|
51
|
-
evidence = evidence_enum.next
|
52
|
-
writer << json_writer.write_json_object(wrap(evidence))
|
53
|
-
|
54
|
-
# each successive evidence starts with a comma
|
55
|
-
while true
|
56
|
-
evidence = evidence_enum.next
|
57
|
-
writer << ","
|
58
|
-
writer << json_writer.write_json_object(wrap(evidence))
|
59
|
-
end
|
60
|
-
rescue StopIteration
|
61
|
-
# end of evidence hashes
|
62
|
-
end
|
63
|
-
|
64
|
-
# json array end
|
65
|
-
writer << "]"
|
66
|
-
writer
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
# Load the most suitable JSON implementation available within ruby.
|
72
|
-
# The load order attempted is:
|
73
|
-
# - oj (provides stream parsing utilizing event callbacks)
|
74
|
-
# - TODO jrjackson (stream parsing support for JRuby)
|
75
|
-
# - multi_json (simple buffering abstraction over multiple ruby libraries)
|
76
|
-
# - json (stock ruby implementation)
|
77
|
-
def load_implementation_module!
|
78
|
-
impl_modules = [ 'oj', 'jrjackson', 'multi_json', 'ruby_json' ]
|
79
|
-
|
80
|
-
load_success = impl_modules.any? { |impl_module|
|
81
|
-
begin
|
82
|
-
require_relative impl_module
|
83
|
-
true
|
84
|
-
rescue LoadError
|
85
|
-
# Could not load +impl_module+; try the next one
|
86
|
-
false
|
87
|
-
end
|
88
|
-
}
|
89
|
-
|
90
|
-
if load_success
|
91
|
-
BEL::Extension::Format::JSONImplementation
|
92
|
-
else
|
93
|
-
mod_s = impl_modules.join(', ')
|
94
|
-
msg = "Could not load any JSON implementation (tried: #{mod_s})."
|
95
|
-
raise LoadError.new(msg)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def wrap(evidence)
|
100
|
-
hash = evidence.to_h
|
101
|
-
{
|
102
|
-
EVIDENCE_ROOT => {
|
103
|
-
:bel_statement => hash[:bel_statement].to_s,
|
104
|
-
:citation => hash[:citation],
|
105
|
-
:summary_text => hash[:summary_text],
|
106
|
-
:experiment_context => hash[:experiment_context],
|
107
|
-
:references => hash[:references],
|
108
|
-
:metadata => hash[:metadata]
|
109
|
-
}
|
110
|
-
}
|
111
|
-
end
|
112
|
-
|
113
|
-
def unwrap(hash)
|
114
|
-
evidence_hash = hash[EVIDENCE_ROOT]
|
115
|
-
evidence = ::BEL::Model::Evidence.create(evidence_hash)
|
116
|
-
namespace_definitions = evidence.references.namespace_definitions
|
117
|
-
evidence.bel_statement = ::BEL::Script.parse(
|
118
|
-
"#{evidence.bel_statement}\n",
|
119
|
-
Hash[
|
120
|
-
namespace_definitions.map { |k, v|
|
121
|
-
[k, BEL::Namespace::NamespaceDefinition.new(k, v)]
|
122
|
-
}
|
123
|
-
]
|
124
|
-
).select { |obj|
|
125
|
-
obj.is_a? ::BEL::Model::Statement
|
126
|
-
}.first
|
127
|
-
|
128
|
-
evidence
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
register_formatter(FormatJSON.new)
|
133
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'multi_json'
|
2
|
-
|
3
|
-
module BEL::Extension::Format
|
4
|
-
module JSONImplementation
|
5
|
-
|
6
|
-
class JSONReader
|
7
|
-
def initialize(data)
|
8
|
-
@data = data
|
9
|
-
end
|
10
|
-
|
11
|
-
def each(&block)
|
12
|
-
if block_given?
|
13
|
-
MultiJson.load(@data, :symbolize_keys => true).each do |obj|
|
14
|
-
yield obj
|
15
|
-
end
|
16
|
-
else
|
17
|
-
to_enum(:each)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class JSONWriter
|
23
|
-
|
24
|
-
def write_json_object(json_object)
|
25
|
-
MultiJson.dump(json_object)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'oj'
|
2
|
-
|
3
|
-
module BEL::Extension::Format
|
4
|
-
module JSONImplementation
|
5
|
-
|
6
|
-
class JSONReader
|
7
|
-
def initialize(data)
|
8
|
-
@data = data
|
9
|
-
end
|
10
|
-
|
11
|
-
def each(&block)
|
12
|
-
if block_given?
|
13
|
-
Oj.sc_parse(EvidenceHandler.new(block), @data, :symbol_keys => true)
|
14
|
-
else
|
15
|
-
to_enum(:each)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class JSONWriter
|
21
|
-
|
22
|
-
def write_json_object(json_object)
|
23
|
-
Oj.dump(json_object, :mode => :compat)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
class EvidenceHandler < Oj::ScHandler
|
30
|
-
|
31
|
-
def initialize(callable)
|
32
|
-
@callable = callable
|
33
|
-
end
|
34
|
-
|
35
|
-
def hash_start
|
36
|
-
{}
|
37
|
-
end
|
38
|
-
|
39
|
-
def hash_end
|
40
|
-
@callable.call @hash
|
41
|
-
end
|
42
|
-
|
43
|
-
def hash_set(hash, key, value)
|
44
|
-
hash[key] = value
|
45
|
-
@hash = hash
|
46
|
-
end
|
47
|
-
|
48
|
-
def array_start
|
49
|
-
@array = []
|
50
|
-
@array
|
51
|
-
end
|
52
|
-
|
53
|
-
def array_append(array, value)
|
54
|
-
array << value
|
55
|
-
@array = array
|
56
|
-
end
|
57
|
-
|
58
|
-
def array_end()
|
59
|
-
@callable.call @array
|
60
|
-
end
|
61
|
-
|
62
|
-
def error(message, line, column)
|
63
|
-
msg = "Parse error at line #{line}, column #{column}: #{message}"
|
64
|
-
raise Oj::ParseError.new(msg)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|