Module: BEL::BELRDF::Reader::NanopubYielder

Includes:
Nanopub, Quoting
Included in:
BufferedNanopubYielder, UnbufferedNanopubYielder
Defined in:
lib/bel/translator/plugins/rdf2/reader.rb

Constant Summary

Constants included from Quoting

Quoting::KeywordMatcher, Quoting::Keywords, Quoting::LenientQuotedMatcher, Quoting::NonWordMatcher, Quoting::QuoteNotEscapedMatcher, Quoting::StrictQuotedMatcher

Instance Method Summary collapse

Methods included from Quoting

#always_quote, #ensure_quotes, #identifier_value?, #quote, #quote_if_needed, #quoted?, #quotes_required?, #remove_quotes, #string_value?, #unquote, #unquoted?

Methods included from Nanopub

union_annotation_references, union_by_keyword, union_namespace_references

Instance Method Details

#describe(resource, graph) ⇒ Hash

Describes an RDF resource contained within graph. Describing an RDF resource will retrieve the neighborhood of RDF statements with resource in the subject position.



31
32
33
34
35
36
# File 'lib/bel/translator/plugins/rdf2/reader.rb', line 31

def describe(resource, graph)
  graph.query([resource, nil, nil]).reduce({}) { |hash, statement|
    hash[statement.predicate] = statement.object
    hash
  }
end

#make_nanopub(nanopub_hash, graph) ⇒ Nanopub

Create an Nanopub::Nanopub object from RDF statements found in the graph.



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

def make_nanopub(nanopub_hash, graph)
  statement     = describe(nanopub_hash[BELV.hasStatement], graph)

  # values
  bel_statement = statement[::RDF::RDFS.label].value
  support_text  = nanopub_hash[BELV.hasSupport]
  citation      = nanopub_hash[BELV.hasCitation]

  # model
  nanopub               = Nanopub.new
  nanopub.bel_statement = ::BEL::Script.parse(bel_statement)
                             .find { |obj|
                               obj.is_a? Statement
                             }
  nanopub.support       = Support.new(support_text.value) if support_text

  if citation.respond_to?(:value)
    nanopub.citation =
      case citation.value
      when /pubmed:(\d+)$/
        pubmed_id = $1.to_i
        Citation.new({
          :type => 'PubMed',
          :id   => pubmed_id,
          :name => "PubMed Citation - #{pubmed_id}"
        })
      else
        nil
      end
  end

  nanopub
end

#nanopub_yielder(graph) {|::BEL::Nanopub::Nanopub| ... } ⇒ Object

Iterate the BELV.Nanopub predicated statements, from the graph, and yield those correspdonding Nanopub::Nanopub objects.

Yields:



44
45
46
47
48
49
# File 'lib/bel/translator/plugins/rdf2/reader.rb', line 44

def nanopub_yielder(graph)
  resources_of_type(BELV.Nanopub, graph).each do |nanopub|

    yield make_nanopub(nanopub, graph)
  end
end

#resources_of_type(type, graph) ⇒ Enumerator

Find described resources by type in graph.



15
16
17
18
19
20
21
# File 'lib/bel/translator/plugins/rdf2/reader.rb', line 15

def resources_of_type(type, graph)
  graph.query([nil, ::RDF.type, type])
    .lazy
    .map { |rdf_statement|
      describe(rdf_statement.subject, graph)
    }
end