bel 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/README.md +4 -2
  4. data/bin/bel +60 -3
  5. data/lib/bel/completion_rule.rb +2 -2
  6. data/lib/bel/evidence_model.rb +5 -0
  7. data/lib/bel/evidence_model/bel_parameter.rb +1 -6
  8. data/lib/bel/evidence_model/buffering_evidence_combiner.rb +182 -0
  9. data/lib/bel/evidence_model/evidence.rb +21 -0
  10. data/lib/bel/evidence_model/hash_map_references.rb +33 -0
  11. data/lib/bel/evidence_model/map_references.rb +30 -0
  12. data/lib/bel/evidence_model/map_references_combiner.rb +30 -0
  13. data/lib/bel/evidence_model/streaming_evidence_combiner.rb +37 -0
  14. data/lib/bel/evidence_model/util.rb +84 -0
  15. data/lib/bel/gen.rb +7 -3
  16. data/lib/bel/gen/annotation.rb +5 -2
  17. data/lib/bel/gen/evidence.rb +10 -4
  18. data/lib/bel/gen/namespace.rb +37 -0
  19. data/lib/bel/gen/parameter.rb +71 -0
  20. data/lib/bel/gen/sample_resources.rb +52 -0
  21. data/lib/bel/gen/statement.rb +33 -0
  22. data/lib/bel/gen/term.rb +33 -0
  23. data/lib/bel/language.rb +4 -4
  24. data/lib/bel/libbel/library_resolver.rb +1 -1
  25. data/lib/bel/namespace.rb +3 -3
  26. data/lib/bel/quoting.rb +218 -14
  27. data/lib/bel/translator.rb +11 -3
  28. data/lib/bel/translator/plugins/bel_script/bel_citation_serialization.rb +2 -2
  29. data/lib/bel/translator/plugins/bel_script/bel_discrete_serialization.rb +1 -1
  30. data/lib/bel/translator/plugins/bel_script/bel_yielder.rb +32 -21
  31. data/lib/bel/translator/plugins/bel_script/evidence_serialization.rb +11 -10
  32. data/lib/bel/translator/plugins/bel_script/evidence_yielder.rb +2 -2
  33. data/lib/bel/translator/plugins/bel_script/translator.rb +1 -1
  34. data/lib/bel/translator/plugins/json_evidence/translator.rb +5 -1
  35. data/lib/bel/translator/plugins/rdf/monkey_patch.rb +4 -0
  36. data/lib/bel/translator/plugins/xbel/evidence_handler.rb +23 -12
  37. data/lib/bel/translator/plugins/xbel/evidence_yielder.rb +1 -1
  38. data/lib/bel/translator/plugins/xbel/translator.rb +4 -4
  39. data/lib/bel/translator/plugins/xbel/xbel_yielder.rb +24 -7
  40. data/lib/bel/util.rb +55 -21
  41. data/lib/bel/version.rb +1 -1
  42. metadata +13 -3
  43. data/lib/bel/gen/bel_expression.rb +0 -128
@@ -0,0 +1,33 @@
1
+ require_relative 'map_references'
2
+
3
+ module BEL
4
+ module Model
5
+ class HashMapReferences
6
+ include MapReferences
7
+
8
+ def initialize(annotation_reference_map, namespace_reference_map)
9
+ @annotation_reference_map = annotation_reference_map
10
+ @namespace_reference_map = namespace_reference_map
11
+
12
+ @annotation_references = annotation_reference_map.values.uniq
13
+ @namespace_references = namespace_reference_map.values.uniq
14
+ end
15
+
16
+ def annotation_references
17
+ @annotation_references
18
+ end
19
+
20
+ def namespace_references
21
+ @namespace_references
22
+ end
23
+
24
+ def map_annotation_reference(reference)
25
+ @annotation_reference_map[reference]
26
+ end
27
+
28
+ def map_namespace_reference(reference)
29
+ @namespace_reference_map[reference]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ module BEL
2
+ module Model
3
+ module MapReferences
4
+
5
+ def annotation_references
6
+ raise NotImplementedError.new(
7
+ "#{__method__} is not implemented for class #{self.class}"
8
+ )
9
+ end
10
+
11
+ def namespace_references
12
+ raise NotImplementedError.new(
13
+ "#{__method__} is not implemented for class #{self.class}"
14
+ )
15
+ end
16
+
17
+ def map_annotation_reference(reference)
18
+ raise NotImplementedError.new(
19
+ "#{__method__} is not implemented for class #{self.class}"
20
+ )
21
+ end
22
+
23
+ def map_namespace_reference(reference)
24
+ raise NotImplementedError.new(
25
+ "#{__method__} is not implemented for class #{self.class}"
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module BEL
2
+ module Model
3
+
4
+ class MapReferencesCombiner < BufferingEvidenceCombiner
5
+
6
+ def initialize(evidence_collection, map_references)
7
+ @evidence_collection = evidence_collection
8
+ @map_references = map_references
9
+ end
10
+
11
+ def annotation_references
12
+ @map_references.annotation_references
13
+ end
14
+
15
+ def namespace_references
16
+ @map_references.namespace_references
17
+ end
18
+
19
+ def each
20
+ if block_given?
21
+ @evidence_collection.each do |evidence|
22
+ yield rewrite_evidence!(evidence, @map_references)
23
+ end
24
+ else
25
+ to_enum(:each)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ module BEL
2
+ module Model
3
+
4
+ class StreamingEvidenceCombiner
5
+
6
+ attr_reader :annotation_references, :namespace_references
7
+
8
+ def initialize(evidence_collection)
9
+ @evidence_collection = evidence_collection
10
+ end
11
+
12
+ def each
13
+ if block_given?
14
+ @evidence_collection.each do |evidence|
15
+ once {
16
+ @annotation_references = evidence.references.annotations
17
+ @namespace_references = evidence.references.namespaces
18
+ }
19
+
20
+ yield evidence
21
+ end
22
+ else
23
+ to_enum(:each)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def once(*block_args, &block)
30
+ unless @_once_switch
31
+ block.call(*block_args)
32
+ @_once_switch = 1
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,84 @@
1
+ module BEL
2
+ module Model
3
+
4
+ def self.union_namespace_references(destination, source, suffix = 'incr')
5
+ BEL::Model.union_by_keyword(
6
+ destination,
7
+ source,
8
+ suffix
9
+ )
10
+ end
11
+
12
+ def self.union_annotation_references(destination, source, suffix = 'incr')
13
+ BEL::Model.union_by_keyword(
14
+ destination,
15
+ source,
16
+ suffix
17
+ )
18
+ end
19
+
20
+ # Combines annotation/namespace references together by disambiguating
21
+ # the keywords. The references in @source@ are combined into
22
+ # @destination@.
23
+ #
24
+ # @param [Array<Hash>] destination existing references to merge with; may
25
+ # be an empty @[]@ array
26
+ # @param [Array<Hash>] source references to union with @destination@
27
+ # @param [String] suffix the suffix to apply to reference keywords
28
+ # in @source@ for disambiguation
29
+ # @return [Array<Array<Hash>,Hash>] array where the first item is and
30
+ # Array of combined references and the second item is a Hash that
31
+ # remaps references from source
32
+ def self.union_by_keyword(destination, source, suffix = 'incr')
33
+ # find new
34
+ new = source - destination
35
+ suffix_pattern = /#{Regexp.escape(suffix)}([0-9]+)$/
36
+ remap_result = {}
37
+
38
+ combined = destination + new.map { |new_obj|
39
+ new_key = new_obj[:keyword].to_s
40
+
41
+ # find a match where the keyword differs
42
+ new_value = new_obj.reject { |key, value| key == :keyword }
43
+ match_by_value = destination.find { |dest|
44
+ new_value == dest.reject { |key, value| key == :keyword }
45
+ }
46
+
47
+ rewrite_key =
48
+ if match_by_value
49
+ match_by_value[:keyword]
50
+ else
51
+ # find max suffix match
52
+ max_suffix = destination.map { |dest|
53
+ key, suffix_number = dest[:keyword].to_s.split(suffix_pattern)
54
+ if new_key == key
55
+ suffix_number
56
+ else
57
+ nil
58
+ end
59
+ }.compact.max { |suffix_number|
60
+ suffix_number.to_i
61
+ }
62
+
63
+ if max_suffix
64
+ new_key + suffix + max_suffix.next
65
+ else
66
+ if destination.any? { |dest| dest[:keyword] == new_key }
67
+ "#{new_key}#{suffix}1"
68
+ else
69
+ new_key
70
+ end
71
+ end
72
+ end
73
+
74
+ rewrite_obj = new_obj.merge({:keyword => rewrite_key})
75
+ remap_result[new_obj] = rewrite_obj
76
+ rewrite_obj
77
+ }
78
+ combined.uniq!
79
+
80
+ [combined, remap_result]
81
+ end
82
+
83
+ end
84
+ end
@@ -38,11 +38,15 @@ module BEL
38
38
  end
39
39
 
40
40
  # Load generators after {BEL::Gen#soft_require} is available.
41
- require_relative 'gen/evidence'
41
+ require_relative 'gen/document_header'
42
42
  require_relative 'gen/annotation'
43
43
  require_relative 'gen/citation'
44
- require_relative 'gen/bel_expression'
45
- require_relative 'gen/document_header'
44
+ require_relative 'gen/namespace'
45
+ require_relative 'gen/parameter'
46
+ require_relative 'gen/term'
47
+ require_relative 'gen/statement'
48
+ require_relative 'gen/evidence'
49
+ require_relative 'gen/sample_resources'
46
50
 
47
51
  # vim: ts=2 sw=2:
48
52
  # encoding: utf-8
@@ -7,7 +7,6 @@ module BEL
7
7
  # The {Annotation} module defines methods that generate random annotations
8
8
  # to be used in an evidence's {BEL::Model::ExperimentContext}.
9
9
  module Annotation
10
- include BEL::Quoting
11
10
 
12
11
  # Array of the latest OpenBEL {BEL::Annotation::AnnotationDefinition}.
13
12
  ANNOTATIONS =
@@ -38,7 +37,11 @@ module BEL
38
37
 
39
38
  {
40
39
  :name => anno.keyword,
41
- :value => anno.values.keys.sample
40
+ :value => Rantly {
41
+ sized(range(5, 20)) {
42
+ string(/[[:alnum:]]|[[:blank:]]|[[:punct:]]/)
43
+ }
44
+ }
42
45
  }
43
46
  end
44
47
  end
@@ -1,8 +1,11 @@
1
1
  require 'bel'
2
+ require_relative 'document_header'
2
3
  require_relative 'annotation'
3
4
  require_relative 'citation'
4
- require_relative 'bel_expression'
5
- require_relative 'document_header'
5
+ require_relative 'namespace'
6
+ require_relative 'parameter'
7
+ require_relative 'term'
8
+ require_relative 'statement'
6
9
 
7
10
  require_relative '../gen'
8
11
  BEL::Gen.soft_require('rantly')
@@ -15,10 +18,13 @@ module BEL
15
18
  module Evidence
16
19
 
17
20
  # Include other generators needed to create {BEL::Model::Evidence}.
21
+ include BEL::Gen::DocumentHeader
18
22
  include BEL::Gen::Annotation
19
23
  include BEL::Gen::Citation
20
- include BEL::Gen::DocumentHeader
21
- include BEL::Gen::Expression
24
+ include BEL::Gen::Namespace
25
+ include BEL::Gen::Parameter
26
+ include BEL::Gen::Statement
27
+ include BEL::Gen::Term
22
28
 
23
29
  # Returns a random {BEL::Model::Evidence}.
24
30
  #
@@ -0,0 +1,37 @@
1
+ require 'bel'
2
+ require_relative '../gen'
3
+ BEL::Gen.soft_require('rantly')
4
+
5
+ module BEL
6
+ module Gen
7
+
8
+ # The {Namespace} module defines methods that generate random
9
+ # {BEL::Namespace::NamespaceDefinition}. The generated namespace are saved
10
+ # and can be accessed from {Namespace#referenced_namespaces}.
11
+ module Namespace
12
+
13
+ # Array of the latest OpenBEL {BEL::Namespace::NamespaceDefinition}.
14
+ NAMESPACES = BEL::Namespace::NAMESPACE_LATEST.map { |prefix, (url, rdf_uri)|
15
+ BEL::Namespace::NamespaceDefinition.new(prefix, url, rdf_uri)
16
+ }
17
+
18
+ # Retrieve the namespaces chosen during use of {#namespace}.
19
+ # @return [Hash] hash of namespace prefix => {BEL::Namespace::NamespaceDefinition}
20
+ def referenced_namespaces
21
+ @referenced_namespaces ||= Hash[
22
+ NAMESPACES.map { |ns| [ns.prefix, ns] }
23
+ ]
24
+ end
25
+
26
+ # Returns a randomly chosen namespace.
27
+ # @return [BEL::Namespace::NamespaceDefinition] a random namespace
28
+ def namespace
29
+ ns = Rantly {
30
+ choose(*NAMESPACES)
31
+ }
32
+ referenced_namespaces[ns.prefix] = ns
33
+ ns
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,71 @@
1
+ require 'bel'
2
+ require_relative '../gen'
3
+ BEL::Gen.soft_require('rantly')
4
+
5
+ module BEL
6
+ module Gen
7
+
8
+ # The {Parameter} module defines methods that generate random BEL
9
+ # {BEL::Model::Parameter parameters}.
10
+ module Parameter
11
+ include BEL::Gen::Namespace
12
+
13
+ # Returns a BEL parameter from a random namespace. The value will be
14
+ # a random string and not necessarily part of the namespace.
15
+ #
16
+ # @return [BEL::Model::Parameter] a randomly generated parameter with
17
+ # a namespace
18
+ def bel_parameter_with_namespace
19
+ ns = namespace
20
+ value = Rantly.value {
21
+ sized(range(3,10)) {
22
+ string(/[[:alnum:]]|[[:blank:]]|[[:punct:]]/)
23
+ }
24
+ }
25
+ BEL::Model::Parameter.new(
26
+ ns,
27
+ value,
28
+ :A
29
+ )
30
+ end
31
+
32
+ # Returns a BEL parameter without a namespace. The value will be
33
+ # a random string.
34
+ #
35
+ # @return [BEL::Model::Parameter] a randomly generated parameter
36
+ # without a namespace
37
+ def bel_parameter_without_namespace
38
+ value = Rantly.value {
39
+ sized(range(3,10)) {
40
+ string(/[[:alnum:]]|[[:blank:]]|[[:punct:]]/)
41
+ }
42
+ }
43
+ BEL::Model::Parameter.new(
44
+ nil,
45
+ value,
46
+ :A
47
+ )
48
+ end
49
+
50
+ # Returns a BEL parameter that may or may not have a namespace.
51
+ #
52
+ # Note: This method has a better chance of selecting a BEL parameter
53
+ # with a namespace.
54
+ #
55
+ # @see #bel_parameter_with_namespace
56
+ # @see #bel_parameter_without_namespace
57
+ # @return [BEL::Model::Parameter] a parameter that may or may not have
58
+ # a namespace
59
+ def bel_parameter
60
+ with_namespace = bel_parameter_with_namespace
61
+ without_namespace = bel_parameter_without_namespace
62
+ Rantly {
63
+ freq(
64
+ [5, :literal, with_namespace],
65
+ [1, :literal, without_namespace],
66
+ )
67
+ }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,52 @@
1
+ require_relative '../gen'
2
+ BEL::Gen.soft_require('rantly')
3
+
4
+ module BEL
5
+ module Gen
6
+
7
+ # The {SampleResources} module defines methods that sample data from actual
8
+ # resources.
9
+ #
10
+ # {BEL::Model::Parameter BEL parameters} are sampled from a
11
+ # {BEL::Namespace::NamespaceDefinition}.
12
+ #
13
+ # Annotations are sampled from a {BEL::Annotation::AnnotationDefinition}.
14
+ module SampleResources
15
+ include BEL::Gen::Annotation
16
+
17
+ # Returns a sampled annotation as a hash of +:name+ and +:value+. These
18
+ # can be added directly to an evidence's {BEL::Model::ExperimentContext}.
19
+ #
20
+ # @return [Hash] sampled annotation; hash of +:name+ and +:value+
21
+ def annotation
22
+ # pick annotation definition
23
+ anno = Rantly {
24
+ choose(*ANNOTATIONS)
25
+ }
26
+
27
+ # track annotation definitions
28
+ referenced_annotations[anno.keyword] = anno
29
+
30
+ {
31
+ :name => anno.keyword,
32
+ :value => anno.values.keys.sample
33
+ }
34
+ end
35
+
36
+ # Returns a BEL parameter with a value sampled from a random namespace.
37
+ #
38
+ # @return [BEL::Model::Parameter] a parameter with value sampled from a
39
+ # random namespace
40
+ def bel_parameter_with_namespace
41
+ ns = namespace
42
+ value = ns.values.keys.sample
43
+ enc = ns.values[value]
44
+ BEL::Model::Parameter.new(
45
+ ns,
46
+ value,
47
+ enc
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end