class BELParser::Script::NanopubMapper

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

Constants

DEFINITIONS
STATEMENT_TYPES

Public Class Methods

new(ast_enum, omit_on_error = false, omit_on_warning = false) click to toggle source
# 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

Public Instance Methods

citation(citation) click to toggle source
# 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) click to toggle source
# 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
each() { |num, line, ast_node, nanopub(ast_node, state)| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bel_parser/script/nanopub_mapper.rb, line 95
def support(support)
  support
end
warnings(ast_node) click to toggle source
# 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