Class: BEL::Extension::Format::FormatJGF

Inherits:
Object
  • Object
show all
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

Methods included from Formatter

#evidence_hash

Constructor Details

#initializeFormatJGF

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, 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

#file_extensionsObject



28
29
30
# File 'lib/bel/extensions/jgf.rb', line 28

def file_extensions
  EXTENSIONS
end

#idObject



20
21
22
# File 'lib/bel/extensions/jgf.rb', line 20

def id
  ID
end

#media_typesObject



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, 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