Module: BEL

Extended by:
Translate::ClassMethods
Defined in:
lib/bel.rb,
lib/bel/dsl.rb,
lib/bel/gen.rb,
lib/bel/util.rb,
lib/bel/libbel.rb,
lib/bel/script.rb,
lib/bel/quoting.rb,
lib/bel/version.rb,
lib/bel/gen/term.rb,
lib/bel/language.rb,
lib/bel/resource.rb,
lib/bel/namespace.rb,
lib/bel/translate.rb,
lib/bel/annotation.rb,
lib/bel/completion.rb,
lib/bel/translator.rb,
lib/bel/gen/nanopub.rb,
lib/bel/gen/citation.rb,
lib/bel/nanopub/util.rb,
lib/bel/gen/namespace.rb,
lib/bel/gen/parameter.rb,
lib/bel/gen/statement.rb,
lib/bel/gen/annotation.rb,
lib/bel/rdf_repository.rb,
lib/bel/completion_rule.rb,
lib/bel/nanopub/nanopub.rb,
lib/bel/nanopub/support.rb,
lib/bel/resource/search.rb,
lib/bel/libbel/bel_token.rb,
lib/bel/libbel/node_test.rb,
lib/bel/nanopub/citation.rb,
lib/bel/nanopub/metadata.rb,
lib/bel/resource/concept.rb,
lib/bel/nanopub/references.rb,
lib/bel/resource/namespace.rb,
lib/bel/gen/document_header.rb,
lib/bel/resource/annotation.rb,
lib/bel/resource/namespaces.rb,
lib/bel/gen/sample_resources.rb,
lib/bel/resource/annotations.rb,
lib/bel/libbel/bel_token_list.rb,
lib/bel/libbel/node_traversal.rb,
lib/bel/libbel/bel_ast_structs.rb,
lib/bel/nanopub/map_references.rb,
lib/bel/libbel/library_resolver.rb,
lib/bel/resource/concept_scheme.rb,
lib/bel/resource/namespace_value.rb,
lib/bel/libbel/library_load_error.rb,
lib/bel/resource/annotation_value.rb,
lib/bel/libbel/node_transformation.rb,
lib/bel/nanopub/experiment_context.rb,
lib/bel/nanopub/hash_map_references.rb,
lib/bel/translator/plugins/rdf2/uuid.rb,
lib/bel/libbel/platform_support_error.rb,
lib/bel/resource/search/search_result.rb,
lib/bel/translator/plugins/rdf2/reader.rb,
lib/bel/nanopub/map_references_combiner.rb,
lib/bel/translator/plugins/rdf2/belv2_0.rb,
lib/bel/translator/plugins/rdf2/converter.rb,
lib/bel/nanopub/buffering_nanopub_combiner.rb,
lib/bel/nanopub/streaming_nanopub_combiner.rb,
lib/bel/translator/plugins/rdf2/rdf_writer.rb,
lib/bel/translator/plugins/rdf2/translator.rb,
lib/bel/translator/plugins/rdf2/rdf_converter.rb,
lib/bel/translator/plugins/rdf2/term_converter.rb,
lib/bel/translator/plugins/rdf2/nanopub_converter.rb,
lib/bel/translator/plugins/rdf2/namespace_converter.rb,
lib/bel/translator/plugins/rdf2/parameter_converter.rb,
lib/bel/translator/plugins/rdf2/statement_converter.rb,
lib/bel/translator/plugins/rdf2/relationship_converter.rb

Defined Under Namespace

Modules: Annotation, BELRDF, Completion, DSL, Gen, JSON, Language, LibBEL, Namespace, Nanopub, Quoting, RdfRepository, Resource, Script, Translate, Translator, Version

Constant Summary collapse

DEFAULT_RESOURCE_VALUE_DELIMITER =

The default delimiter to use when parsing values from BEL Resource files.

'|'

Class Method Summary collapse

Methods included from Translate::ClassMethods

nanopub, process_input, translate, translator

Class Method Details

.keys_to_symbols(obj) ⇒ Object



84
85
86
# File 'lib/bel/util.rb', line 84

def self.keys_to_symbols(obj)
  self.object_convert(:key, obj) { |key| key.to_sym }
end

.object_convert(type, source, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bel/util.rb', line 88

def self.object_convert(type, source, &block)
  case source
  when Array
    source.inject([]) { |new, v|
      new << object_convert(type, v, &block)
      new
    }
  when Hash
    source.inject({}) { |new, (k,v)|
      if type == :key || k.is_a?(type)
        k = block.call(k)
      end

      new[k] = object_convert(type, v, &block)
      new
    }
  else
    if type != :key && source.is_a?(type)
      block.call(source)
    else
      source
    end
  end
end

.read_all(reference, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bel/util.rb', line 51

def self.read_all(reference, options = {})
  return [] if reference.to_s.strip.empty?

  if cached? reference
    read_cached reference do |cf|
      return cf.read
    end
  end

  multi_open(reference, options) do |f|
    content = f.read
    write_cached reference, content
    return content
  end
end

.read_lines(reference, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bel/util.rb', line 67

def self.read_lines(reference, options = {})
  return [] if reference.to_s.strip.empty?

  if cached? reference
    read_cached reference do |cf|
      return cf.readlines
    end
  end

  multi_open(reference, options) do |f|
    content = f.read
    write_cached reference, content
    f.rewind
    return f.readlines
  end
end

.read_resource(url) ⇒ Hash

Retrieves values from a BEL resource file (e.g. .belns, .belanno).

The file parser respects the DelimiterString value specified in the file but will default to #DEFAULT_RESOURCE_VALUE_DELIMITER if not present.

argument is the value encoding for a BEL namespace file (e.g. lung disease|O) and a database value identifier for a BEL annotation file (e.g. lung|UBERON_0002048).

Parameters:

  • url (String)

    the resource file URL to fetch

Returns:

  • (Hash)

    hash of value symbol to extra argument symbol; the extra



21
22
23
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
# File 'lib/bel/util.rb', line 21

def self.read_resource(url)
  @@url_content_cache ||= {}
  @@url_content_cache.fetch(url) {
    resource_lines = BEL::read_lines(url)

    # Drop until the delimiter line and extract the delimiter, e.g.
    # DelimiterString=|
    delimiter_line = resource_lines.take(100).find { |l| l.start_with?("DelimiterString") }
    delimiter =
      if delimiter_line
        delimiter_line.strip.split('=')[1]
      else
        DEFAULT_RESOURCE_VALUE_DELIMITER
      end

    # Extract resource values based on the delimiter.
    resource_values = Hash[
      resource_lines.
      drop_while { |l| !l.start_with?("[Values]") }.
      drop(1).
      map { |s|
        val_enc = s.strip!.split(delimiter).map(&:to_sym)
        val_enc[0..1]
      }
    ]
    @@url_content_cache[url] = resource_values
    resource_values
  }
end