Class: BEL::Extension::Format::FormatJGF
- Inherits:
-
Object
- Object
- BEL::Extension::Format::FormatJGF
- Includes:
- Formatter
- Defined in:
- lib/bel/extensions/jgf.rb
Constant Summary collapse
- ID =
:jgf
- MEDIA_TYPES =
%i(application/vnd.jgf+json)
- EXTENSIONS =
%i(jgf.json)
- ResourceIndex =
::BEL::Namespace::ResourceIndex
Instance Method Summary collapse
- #deserialize(data, options = {}, &block) ⇒ Object
- #file_extensions ⇒ Object
- #id ⇒ Object
-
#initialize ⇒ FormatJGF
constructor
A new instance of FormatJGF.
- #media_types ⇒ Object
- #serialize(objects, writer = StringIO.new, options = {}) ⇒ Object
Methods included from Formatter
Constructor Details
#initialize ⇒ FormatJGF
Returns a new instance of FormatJGF.
14 15 16 17 18 |
# File 'lib/bel/extensions/jgf.rb', line 14 def initialize json_module = load_implementation_module! @json_reader = json_module::JSONReader @json_writer = json_module::JSONWriter end |
Instance Method Details
#deserialize(data, options = {}, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bel/extensions/jgf.rb', line 32 def deserialize(data, = {}, &block) default_resource_index = .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 |
#file_extensions ⇒ Object
28 29 30 |
# File 'lib/bel/extensions/jgf.rb', line 28 def file_extensions EXTENSIONS end |
#id ⇒ Object
20 21 22 |
# File 'lib/bel/extensions/jgf.rb', line 20 def id ID end |
#media_types ⇒ Object
24 25 26 |
# File 'lib/bel/extensions/jgf.rb', line 24 def media_types MEDIA_TYPES end |
#serialize(objects, writer = StringIO.new, options = {}) ⇒ Object
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 |
# File 'lib/bel/extensions/jgf.rb', line 44 def serialize(objects, writer = StringIO.new, = {}) 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 |