rbbt-util 5.19.10 → 5.19.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/etc/app.d/knowledge_bases.rb +13 -0
- data/lib/rbbt/association/open.rb +2 -1
- data/lib/rbbt/knowledge_base/traverse.rb +105 -25
- data/lib/rbbt/util/misc.rb +45 -0
- data/lib/rbbt/util/misc/inspect.rb +1 -1
- data/share/config.ru +3 -0
- data/test/rbbt/knowledge_base/test_traverse.rb +9 -5
- data/test/rbbt/util/test_misc.rb +14 -0
- data/test/rbbt/workflow/test_doc.rb +2 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d033f774277a3c6644e82ccb1d904d394dd68fe8
|
4
|
+
data.tar.gz: 654e1dabdcabf1bd94d5ad4bc48f1284ffc26007
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9595ff8109bcdaac80a1e2f04eb4516814586de65605a4721ece5f9754d8dcdec31176db1933bf397c2c16d6b99bd66c3c4d638a1c722f45a07b55ea412d2ef5
|
7
|
+
data.tar.gz: 7c7d186eedf2bf54debecde2057523fb433c7c9c6fcd12fc46a15f14298c614cc497b569b7d40870f3ecda8081b01a1b2948b62e56f59370b921964df48812c5
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#{{{ Require files
|
2
|
+
Rbbt.etc.knowledge_bases.read.split("\n").each do |workflow|
|
3
|
+
next if workflow.empty?
|
4
|
+
Log.debug("syndicating knowledgebase from workflow #{ workflow }")
|
5
|
+
begin
|
6
|
+
wf = Kernel.const_get workflow
|
7
|
+
begin require "rbbt/knowledge_base/#{ workflow }" rescue Exception end
|
8
|
+
KnowledgeBaseRESTHelpers.add_syndication Misc.snake_case(workflow), wf.knowledge_base
|
9
|
+
rescue Exception
|
10
|
+
Log.warn "Exception loading knowledgebase from #{ workflow }"
|
11
|
+
Log.exception $!
|
12
|
+
end
|
13
|
+
end if Rbbt.etc.knowledge_bases.exists?
|
@@ -16,9 +16,10 @@ module Association
|
|
16
16
|
persist = persist_options[:persist]
|
17
17
|
|
18
18
|
file = version_file(file, options[:namespace]) if options[:namespace] and String === file
|
19
|
-
file = file.call if Proc === file
|
20
19
|
|
21
20
|
data = Persist.persist_tsv(file, "Association Database", options, persist_options) do |data|
|
21
|
+
file = file.call if Proc === file
|
22
|
+
|
22
23
|
options = options.dup
|
23
24
|
tsv = Association.database(file, options.merge(:persist => persist, :unnamed => true))
|
24
25
|
tsv = tsv.to_double unless tsv.type == :double
|
@@ -24,10 +24,10 @@ class KnowledgeBase
|
|
24
24
|
|
25
25
|
def identify(db, source, target)
|
26
26
|
source_entities = if is_wildcard? source
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
assignments[source] || :all
|
28
|
+
else
|
29
|
+
kb.identify_source db, source
|
30
|
+
end
|
31
31
|
|
32
32
|
target_entities = if is_wildcard? target
|
33
33
|
assignments[target] || :all
|
@@ -42,8 +42,8 @@ class KnowledgeBase
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def reassign(matches, source, target)
|
45
|
-
assignments[source] = (matches.any? ? matches.
|
46
|
-
assignments[target] = (matches.any? ? matches.
|
45
|
+
assignments[source] = (matches.any? ? matches.collect{|m|m.partition("~").first}.uniq : nil) if is_wildcard? source
|
46
|
+
assignments[target] = (matches.any? ? matches.collect{|m|m.partition("~").last}.uniq : nil) if is_wildcard? target
|
47
47
|
end
|
48
48
|
|
49
49
|
def clean_matches(rules, all_matches, assignments)
|
@@ -53,13 +53,13 @@ class KnowledgeBase
|
|
53
53
|
source, db, target = rule.split /\s+/
|
54
54
|
|
55
55
|
if is_wildcard? source
|
56
|
-
assigned = assignments[source]
|
57
|
-
matches = matches.select{|m| assigned.include? m.
|
56
|
+
assigned = assignments[source] || []
|
57
|
+
matches = matches.select{|m| assigned.include? m.partition("~").first }
|
58
58
|
end
|
59
59
|
|
60
60
|
if is_wildcard? target
|
61
|
-
assigned = assignments[target]
|
62
|
-
matches = matches.select{|m| assigned.include? m.
|
61
|
+
assigned = assignments[target] || []
|
62
|
+
matches = matches.select{|m| assigned.include? m.partition("~").last }
|
63
63
|
end
|
64
64
|
|
65
65
|
paths[rule] = matches
|
@@ -127,9 +127,29 @@ class KnowledgeBase
|
|
127
127
|
return [] unless path_hash
|
128
128
|
_ep(path_hash).collect do |path|
|
129
129
|
path.zip(clean_matches.values_at(*rules)).collect do |item, matches|
|
130
|
-
matches.
|
130
|
+
matches.select{|m| m == item}.first
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def traverse_db(db, source, target, conditions)
|
136
|
+
source_entities, target_entities = identify db, source, target
|
137
|
+
|
138
|
+
options = {:source => source_entities, :target => target_entities}
|
139
|
+
matches = kb.subset(db, options)
|
140
|
+
|
141
|
+
if conditions
|
142
|
+
Misc.tokenize(conditions).each do |condition|
|
143
|
+
if condition.index "="
|
144
|
+
key, value = conditions.split("=")
|
145
|
+
matches = matches.select{|m| Misc.match_value(m.info[key.strip], value)}
|
146
|
+
else
|
147
|
+
matches = matches.select{|m| m.info[condition.strip].to_s =~ /\btrue\b/}
|
148
|
+
end
|
131
149
|
end
|
132
150
|
end
|
151
|
+
|
152
|
+
matches
|
133
153
|
end
|
134
154
|
|
135
155
|
|
@@ -139,32 +159,92 @@ class KnowledgeBase
|
|
139
159
|
rules.each do |rule|
|
140
160
|
rule = rule.strip
|
141
161
|
next if rule.empty?
|
142
|
-
source, db, target, conditions = rule.match(/([^\s]+)\s+([^\s]+)\s+([^\s]+)(?:\s+-\s+([^\s]+))?/).captures
|
143
162
|
|
144
|
-
|
163
|
+
if m = rule.match(/([^\s]+)\s+([^\s]+)\s+([^\s]+)(?:\s+-\s+([^\s]+))?/)
|
164
|
+
|
165
|
+
source, db, target, conditions = m.captures
|
166
|
+
if db.include? '?'
|
167
|
+
all_dbs = kb.registry.keys
|
168
|
+
_name, _sep, _kb = db.partition("@")
|
169
|
+
case
|
170
|
+
when _kb[0] == '?'
|
171
|
+
dbs = all_dbs.select{|_db| _db.partition("@").first == _name}
|
172
|
+
when _name[0] == '?'
|
173
|
+
dbs = all_dbs.select{|_db| _db.include?("@") ? db.partition("@").last == _kb : true}
|
174
|
+
end
|
175
|
+
else
|
176
|
+
dbs = [db]
|
177
|
+
end
|
145
178
|
|
146
|
-
|
179
|
+
rule_matches = []
|
180
|
+
dbs.each do |_db|
|
181
|
+
matches = traverse_db(_db, source, target, conditions)
|
182
|
+
|
183
|
+
next if matches.nil? or matches.empty?
|
184
|
+
|
185
|
+
if db.include? '?'
|
186
|
+
_name, _sep, _kb = db.partition("@")
|
187
|
+
case
|
188
|
+
when _kb[0] == '?'
|
189
|
+
assignments[_kb] ||= []
|
190
|
+
assignments[_kb] << _db.partition("@").reject{|p| p.empty?}.last
|
191
|
+
when _name[0] == '?'
|
192
|
+
assignments[_name] ||= []
|
193
|
+
assignments[_name] << _db.partition("@").first
|
194
|
+
end
|
195
|
+
end
|
147
196
|
|
148
|
-
|
149
|
-
|
150
|
-
if condition.index "="
|
151
|
-
key, value = conditions.split("=")
|
152
|
-
matches = matches.select{|m| m.info[key.strip].to_s =~ /\b#{value.strip}\b/}
|
153
|
-
else
|
154
|
-
matches = matches.select{|m| m.info[condition.strip].to_s =~ /\btrue\b/}
|
197
|
+
matches.each do |m|
|
198
|
+
rule_matches << m
|
155
199
|
end
|
156
200
|
end
|
157
|
-
end
|
158
201
|
|
159
|
-
|
202
|
+
reassign rule_matches, source, target
|
160
203
|
|
161
|
-
|
204
|
+
all_matches << rule_matches
|
205
|
+
else
|
206
|
+
raise "Rule not understood: #{rule}"
|
207
|
+
end
|
162
208
|
end
|
163
209
|
|
164
210
|
paths = find_paths rules, all_matches, assignments
|
165
|
-
|
211
|
+
|
166
212
|
[assignments, paths]
|
167
213
|
end
|
214
|
+
|
215
|
+
#def traverse
|
216
|
+
# all_matches = []
|
217
|
+
|
218
|
+
# rules.each do |rule|
|
219
|
+
# rule = rule.strip
|
220
|
+
# next if rule.empty?
|
221
|
+
# source, db, target, conditions = rule.match(/([^\s]+)\s+([^\s]+)\s+([^\s]+)(?:\s+-\s+([^\s]+))?/).captures
|
222
|
+
|
223
|
+
# source_entities, target_entities = identify db, source, target
|
224
|
+
|
225
|
+
# matches = kb.subset(db, :source => source_entities, :target => target_entities)
|
226
|
+
|
227
|
+
# if conditions
|
228
|
+
# conditions.split(/\s+/).each do |condition|
|
229
|
+
# if condition.index "="
|
230
|
+
# key, value = conditions.split("=")
|
231
|
+
# matches = matches.select{|m| m.info[key.strip].to_s =~ /\b#{value.strip}\b/}
|
232
|
+
# else
|
233
|
+
# matches = matches.select{|m| m.info[condition.strip].to_s =~ /\btrue\b/}
|
234
|
+
# end
|
235
|
+
# end
|
236
|
+
# end
|
237
|
+
|
238
|
+
# reassign matches, source, target
|
239
|
+
|
240
|
+
# all_matches << matches
|
241
|
+
# end
|
242
|
+
|
243
|
+
# paths = find_paths rules, all_matches, assignments
|
244
|
+
|
245
|
+
# [assignments, paths]
|
246
|
+
#end
|
247
|
+
|
168
248
|
end
|
169
249
|
|
170
250
|
def traverse(rules)
|
data/lib/rbbt/util/misc.rb
CHANGED
@@ -20,9 +20,53 @@ require 'rbbt/util/misc/system'
|
|
20
20
|
require 'rbbt/util/misc/objects'
|
21
21
|
require 'rbbt/util/misc/manipulation'
|
22
22
|
|
23
|
+
require 'to_regexp'
|
24
|
+
|
23
25
|
module MultipleResult; end
|
24
26
|
|
25
27
|
module Misc
|
28
|
+
def self._convert_match_condition(condition)
|
29
|
+
return true if condition == 'true'
|
30
|
+
return false if condition == 'false'
|
31
|
+
return condition.to_regexp if condition[0] == "/"
|
32
|
+
return [:cmp, $1, $2.to_f] if condition =~ /^([<>]=?)(.*)/
|
33
|
+
return [:invert, _convert_match_condition(condition[1..-1].strip)] if condition[0] == "!"
|
34
|
+
#return {$1 => $2.to_f} if condition =~ /^([<>]=?)(.*)/
|
35
|
+
#return {false => _convert_match_condition(condition[1..-1].strip)} if condition[0] == "!"
|
36
|
+
return condition
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.match_value(value, condition)
|
40
|
+
condition = _convert_match_condition(condition.strip) if String === condition
|
41
|
+
|
42
|
+
case condition
|
43
|
+
when Regexp
|
44
|
+
!! value.match(condition)
|
45
|
+
when NilClass, TrueClass
|
46
|
+
value === TrueClass or (String === value and value.downcase == 'true')
|
47
|
+
when FalseClass
|
48
|
+
value === FalseClass or (String === value and value.downcase == 'false')
|
49
|
+
when String
|
50
|
+
Fixnum === value ? value.to_f == condition.to_f : value == condition
|
51
|
+
when Fixnum
|
52
|
+
value.to_f == condition.to_f
|
53
|
+
when Array
|
54
|
+
case condition.first
|
55
|
+
when :cmp
|
56
|
+
value.to_f.send(condition[1], condition[2])
|
57
|
+
when :invert
|
58
|
+
! match_value(value, condition[1] )
|
59
|
+
else
|
60
|
+
condition.inject(false){|acc,e| acc = acc ? true : match_value(value, e) }
|
61
|
+
end
|
62
|
+
else
|
63
|
+
raise "Condition not understood: #{Misc.fingerprint condition}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.tokenize(str)
|
68
|
+
str.scan(/"[^"]*"|'[^']*'|[^"'\s]+/)
|
69
|
+
end
|
26
70
|
end
|
27
71
|
|
28
72
|
module PDF2Text
|
@@ -36,4 +80,5 @@ module PDF2Text
|
|
36
80
|
CMD.cmd("pdftotext #{pdf_file} -", :pipe => false, :stderr => true)
|
37
81
|
end
|
38
82
|
end
|
83
|
+
|
39
84
|
end
|
data/share/config.ru
CHANGED
@@ -40,6 +40,9 @@ app_eval app, Rbbt.etc['app.d/base.rb'].find
|
|
40
40
|
#{{{ RESOURCES
|
41
41
|
load_file Rbbt.etc['app.d/resources.rb'].find
|
42
42
|
|
43
|
+
#{{{ KNOWLEDGEBASE
|
44
|
+
load_file Rbbt.etc['app.d/knowledge_bases.rb'].find
|
45
|
+
|
43
46
|
#{{{ ENTITIES
|
44
47
|
load_file Rbbt.etc['app.d/entities.rb'].find
|
45
48
|
|
@@ -8,24 +8,22 @@ class TestKnowledgeBaseTraverse < Test::Unit::TestCase
|
|
8
8
|
Genomics.knowledge_base
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
11
|
+
def test_traverse
|
12
12
|
rules = []
|
13
13
|
rules << "?1 pina SF3B1 - Method=MI:0006"
|
14
14
|
rules << "TP53 pina ?2"
|
15
15
|
rules << "?2 pina ?1"
|
16
16
|
res = kb.traverse rules
|
17
|
-
iii res
|
18
17
|
assert res.first.include? "?1"
|
19
18
|
end
|
20
19
|
|
21
|
-
def
|
20
|
+
def test_path
|
22
21
|
rules = []
|
23
22
|
rules << "?1 pina ARPC2"
|
24
23
|
rules << "ARPC3 pina ?2"
|
25
24
|
rules << "?2 pina ?1"
|
26
25
|
res = kb.traverse rules
|
27
26
|
assert res.first.include? "?1"
|
28
|
-
iii res.last.first
|
29
27
|
end
|
30
28
|
|
31
29
|
def test_path2
|
@@ -35,7 +33,13 @@ class TestKnowledgeBaseTraverse < Test::Unit::TestCase
|
|
35
33
|
rules << "?1 pina ?2"
|
36
34
|
res = kb.traverse rules
|
37
35
|
assert res.first.include? "?1"
|
38
|
-
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_wildcard_db
|
39
|
+
rules = []
|
40
|
+
rules << "?1 ?db SF3B1 - Method=MI:0006"
|
41
|
+
res = kb.traverse rules
|
42
|
+
assert res.first.include? "?1"
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
data/test/rbbt/util/test_misc.rb
CHANGED
@@ -512,4 +512,18 @@ eum fugiat quo voluptas nulla pariatur?"
|
|
512
512
|
max = 1000
|
513
513
|
assert_equal Misc.sample_large_obj(str, max).length, max+29
|
514
514
|
end
|
515
|
+
|
516
|
+
def test_match_value
|
517
|
+
assert Misc.match_value("10", "10")
|
518
|
+
assert Misc.match_value("Hi", /hi/i)
|
519
|
+
assert Misc.match_value("Hi", /Hi/)
|
520
|
+
assert Misc.match_value("Hi", "/Hi/")
|
521
|
+
assert Misc.match_value("Hi", "/hi/i")
|
522
|
+
assert Misc.match_value("15", "<=20")
|
523
|
+
assert Misc.match_value("15", ">14")
|
524
|
+
assert Misc.match_value("15", "! >15")
|
525
|
+
assert Misc.match_value("15", "! >15")
|
526
|
+
assert Misc.match_value("15", [14, 15, 25])
|
527
|
+
assert ! Misc.match_value("15", [14, 25])
|
528
|
+
end
|
515
529
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbbt-util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.19.
|
4
|
+
version: 5.19.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miguel Vazquez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: to_regexp
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
125
139
|
description: Utilities for handling tsv files, caches, etc
|
126
140
|
email: miguel.vazquez@cnio.es
|
127
141
|
executables:
|
@@ -148,6 +162,7 @@ files:
|
|
148
162
|
- etc/app.d/foundation.rb
|
149
163
|
- etc/app.d/grid_system.rb
|
150
164
|
- etc/app.d/init.rb
|
165
|
+
- etc/app.d/knowledge_bases.rb
|
151
166
|
- etc/app.d/resources.rb
|
152
167
|
- etc/app.d/workflows.rb
|
153
168
|
- lib/rbbt-util.rb
|