Class: BEL::Translator::Plugins::Jgf::JgfTranslator

Inherits:
Object
  • Object
show all
Includes:
BEL::Translator
Defined in:
lib/bel/translator/plugins/jgf/translator.rb

Instance Method Summary collapse

Instance Method Details

#read(data, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bel/translator/plugins/jgf/translator.rb', line 12

def read(data, options = {})
  default_resource_index = options.fetch(:default_resource_index) {
    ResourceIndex.openbel_published_index('20131211')
  }

  ::BEL::JSON.read(data, options).lazy.select { |obj|
    obj.include?(:nodes) && obj.include?(:edges)
  }.flat_map { |graph|
    unwrap(graph, default_resource_index)
  }
end

#write(objects, writer = StringIO.new, options = {}) ⇒ Object



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
# File 'lib/bel/translator/plugins/jgf/translator.rb', line 24

def write(objects, writer = StringIO.new, options = {})
  graph = {
    :type  => 'BEL-V1.0',
    :nodes => [],
    :edges => []
  }

  objects.each do |nanopub|
    unless nanopub.bel_statement.is_a?(::BEL::Nanopub::Statement)
      nanopub.bel_statement = ::BEL::Nanopub::Nanopub.parse_statement(nanopub)
    end

    stmt    = nanopub.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!

  ::BEL::JSON.write({:graph => graph}, writer, options)
  writer
end