Class: BELParser::Script::NanopubMapper

Inherits:
Object
  • Object
show all
Includes:
Parsers
Defined in:
lib/bel_parser/script/nanopub_mapper.rb

Overview

NanopubMapper maps BEL Script AST nodes and state to aggregated nanopub hash objects.

Constant Summary collapse

STATEMENT_TYPES =
[
  :simple_statement,
  :nested_statement,
  :observed_term
]
DEFINITIONS =
[:annotation_definitions, :namespace_definitions]

Instance Method Summary collapse

Methods included from Parsers

#serialize

Constructor Details

#initialize(ast_enum, omit_on_error = false, omit_on_warning = false) ⇒ NanopubMapper

Returns a new instance of NanopubMapper.



18
19
20
21
22
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 18

def initialize(ast_enum, omit_on_error = false, omit_on_warning = false)
  @ast_enum        = ast_enum
  @omit_on_error   = omit_on_error
  @omit_on_warning = omit_on_warning
end

Instance Method Details

#citation(citation) ⇒ Object



88
89
90
91
92
93
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 88

def citation(citation)
  return nil unless citation
  citation.each do |field, value|
    citation[field] = value
  end
end

#domain_value(type, domain) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 127

def domain_value(type, domain)
  case type
  when :url, :uri
    domain.identifier
  when :list
    domain
  else
    domain.to_s
  end
end

#eachObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 24

def each
  if block_given?
    @ast_enum.each do |(num, line, ast_node, state)|
      next unless STATEMENT_TYPES.include?(ast_node.type)

      errors   = errors(ast_node)
      warnings = warnings(ast_node)

      if (@omit_on_error && !errors.empty?) ||
        (@omit_on_warning && !warnings.empty?)

        report(num, line, errors, warnings)
        next
      end

      yield [num, line, ast_node, nanopub(ast_node, state)]
    end
  else
    enum_for(:each)
  end
end

#errors(ast_node) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 71

def errors(ast_node)
  ast_node
    .syntax_errors
    .select do |err|
      err.is_a?(::BELParser::Language::Syntax::SyntaxError)
    end.each(&:to_s)
end

#experiment_context(annotations) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 99

def experiment_context(annotations)
  (annotations || []).map do |name, value|
    {
      name:  name,
      value: value
    }
  end
end

#nanopub(ast_node, state) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 46

def nanopub(ast_node, state)
  {
    bel_statement:      serialize(ast_node),
    citation:           citation(state[:citation]),
    support:            support(state[:support]),
    experiment_context: experiment_context(state[:annotations]),
    references:         references(*state.values_at(*DEFINITIONS)),
    metadata:           {
      bel_version:     state[:specification].version,
      document_header: state[:document_properties] || nil
    }
  }
end

#references(anno_defs, ns_defs) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 108

def references(anno_defs, ns_defs)
  {
    annotations: (anno_defs || []).map do |keyword, (type, domain)|
      {
        keyword: keyword,
        type:    type,
        domain:  domain_value(type, domain)
      }
    end,
    namespaces: (ns_defs || []).map do |keyword, namespace|
      {
        keyword: keyword,
        type:    namespace.uri? ? :uri : :url,
        domain:  namespace.uri? ? namespace.uri : namespace.url
      }
    end
  }
end

#report(num, line, errors, warnings) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 60

def report(num, line, errors, warnings)
  warn "Line #{num}: #{line}"
  errors.each do |err|
    warn "  #{err}"
  end
  warnings.each do |warn|
    warn "  #{warn}"
  end
  warn
end

#support(support) ⇒ Object



95
96
97
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 95

def support(support)
  support
end

#warnings(ast_node) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/bel_parser/script/nanopub_mapper.rb', line 79

def warnings(ast_node)
  ast_node
    .syntax_errors
    .select do |warn|
      warn.is_a?(::BELParser::Language::Syntax::SyntaxError) ||
        warn.is_a?(::BELParser::Language::Semantics::SemanticsWarning)
    end.each(&:to_s)
end