rbbt-rest 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,68 +34,6 @@ module KnowledgeBaseRESTHelpers
34
34
  end
35
35
  end
36
36
 
37
- #def get_kb(name)
38
- # @@knowledge_bases ||= IndiferentHash.setup({})
39
- # case name
40
- # when :genomics, "genomics"
41
- # @@knowledge_bases[name] ||= Genomics.knowledge_base
42
- # else
43
- # @@knowledge_bases[name] ||= KnowledgeBase.new File.join(KnowledgeBaseRESTHelpers.knowledge_base_dir, name)
44
- # end
45
- #end
46
-
47
- #def user_kb(user)
48
- # user = "guest" if user.nil?
49
- # @@user_knowledge_bases ||= IndiferentHash.setup({})
50
- # @@user_knowledge_bases[user] ||= begin
51
- # kb = KnowledgeBase.new File.join(KnowledgeBaseRESTHelpers.knowledge_base_dir, File.join('user', user)), Organism.default_code("Hsa")
52
- # kb.format["Gene"] = "Ensembl Gene ID"
53
-
54
- # kb.syndicate Genomics.knowledge_base, :genomics
55
-
56
- # if defined? user_studies
57
- # TSV.traverse user_studies[user], :cpus => 10 do |study|
58
- # Study.setup(study)
59
- # study.sample_genes if study.has_genotypes?
60
- # end
61
-
62
- # user_studies[user].each do |study|
63
- # Study.setup(study)
64
- # kb.syndicate study.knowledge_base, study
65
- # end
66
- # end
67
-
68
- # kb
69
- # end
70
- #end
71
-
72
- #def get_knowledge_base(name, namespace = nil)
73
- # @@knowledge_bases ||= IndiferentHash.setup({})
74
- # @@knowledge_bases[name] ||= begin
75
- # begin
76
- # mod = Kernel.const_get name
77
- # return mod.knowledge_base if mod.respond_to? :knowledge_base
78
- # rescue Exception
79
- # end
80
- # kb = case
81
- # when [:genomics, "genomics"].include?(name)
82
- # Genomics.knowledge_base
83
- # when (Misc.path_relative_to(settings.cache_dir, name) and File.exists?(name))
84
- # KnowledgeBase.new name
85
- # when KnowledgeBase.registry.include?(name)
86
- # KnowledgeBase.registry[name]
87
- # when (defined? Study and Study.studies.include?(name))
88
- # Study.setup(name).knowledge_base
89
- # when name.to_s == "user"
90
- # user_kb(user)
91
- # else
92
- # KnowledgeBase.new File.join(KnowledgeBaseRESTHelpers.knowledge_base_dir, name)
93
- # end
94
-
95
- # namespace ? kb.version(namespace) : kb
96
- # end
97
- #end
98
-
99
37
  def user_kb(user)
100
38
  @@user_kbs ||= {}
101
39
  @@user_kbs[user] ||= begin
@@ -0,0 +1,442 @@
1
+ require 'rbbt/util/misc'
2
+ require 'rbbt/knowledge_base'
3
+
4
+ class KnowledgeBaseRESTQuery < Sinatra::Application
5
+
6
+ get '/knowledge_base/:name/:database/children/:entity' do
7
+ name = consume_parameter :name
8
+ database = consume_parameter :database
9
+ entity = consume_parameter :entity
10
+
11
+ kb = get_knowledge_base name
12
+ matches = kb.children(database, entity)
13
+ case @format
14
+ when :tsv
15
+ content_type "text/tab-separated-values"
16
+ halt 200, matches.tsv.to_s
17
+ when :html
18
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Children: #{ [name, database, entity] }")
19
+ when :json
20
+ content_type :json
21
+ halt 200, matches.target.to_json
22
+ else
23
+ content_type :text
24
+ halt 200, matches.target * "\n"
25
+ end
26
+ end
27
+
28
+ get '/knowledge_base/:name/:database/parents/:entity' do
29
+ name = consume_parameter :name
30
+ database = consume_parameter :database
31
+ entity = consume_parameter :entity
32
+
33
+ kb = get_knowledge_base name
34
+ matches = kb.parents(database, entity)
35
+ case @format
36
+ when :tsv
37
+ content_type "text/tab-separated-values"
38
+ halt 200, matches.tsv.to_s
39
+ when :html
40
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Parents: #{ [name, database, entity] }")
41
+ when :json
42
+ content_type :json
43
+ halt 200, matches.source.to_json
44
+ else
45
+ content_type :text
46
+ halt 200, matches.source * "\n"
47
+ end
48
+ end
49
+
50
+ get '/knowledge_base/:name/:database/neighbours/:entity' do
51
+ name = consume_parameter :name
52
+ database = consume_parameter :database
53
+ entity = consume_parameter :entity
54
+
55
+ kb = get_knowledge_base name
56
+ neighbours = kb.neighbours(database, entity)
57
+ case @format
58
+ when :tsv
59
+ content_type "text/tab-separated-values"
60
+ halt 200, neighbours.values.collect{|m| m.tsv.to_s } * "\n\n"
61
+ when :html
62
+ template_render('knowledge_base_partials/matches', {:matches => neighbours}, "Neighbours: #{ [name, database, entity] }")
63
+ when :json
64
+ content_type :json
65
+ neighs = {}
66
+ neighs[:parents] = neighbours[:parents].source if neighbours[:parents]
67
+ neighs[:children] = neighbours[:children].target
68
+ halt 200, neighs.to_json
69
+ else
70
+ content_type :text
71
+ neighs = []
72
+ neighs.concat neighbours[:parents].source if neighbours[:parents]
73
+ neighs.concat neighbours[:children].target
74
+ halt 200, neighs * "\n"
75
+ end
76
+ end
77
+
78
+ get '/knowledge_base/:name/:database/subset' do
79
+ name = consume_parameter :name
80
+ database = consume_parameter :database
81
+ source = consume_parameter :source
82
+ target = consume_parameter :target
83
+
84
+ source = source == "all" ? :all : source.split(@array_separator) if source
85
+ target = target == "all" ? :all : target.split(@array_separator) if target
86
+ entities = { :source => source, :target => target }
87
+
88
+ kb = get_knowledge_base name
89
+ subset = kb.subset(database, entities)
90
+ case @format
91
+ when :tsv
92
+ content_type "text/tab-separated-values"
93
+ halt 200, subset.tsv.to_s
94
+ when :html
95
+ template_render('knowledge_base_partials/subset', {:subset => subset}, "Subset: #{ [name, database] }")
96
+ when :json
97
+ content_type :json
98
+ halt 200, subset.source.to_json
99
+ else
100
+ content_type :text
101
+ halt 200, subset.source * "\n"
102
+ end
103
+ end
104
+
105
+
106
+ #{{{ Collection
107
+
108
+ post '/knowledge_base/:name/:database/collection_children' do
109
+ name = consume_parameter :name
110
+ database = consume_parameter :database
111
+ collection = consume_parameter :collection
112
+ raise ParameterException, "No collection specified" if collection.nil?
113
+ collection = JSON.parse(collection)
114
+
115
+ kb = get_knowledge_base name
116
+ matches = collection.keys.inject({}){|acc,type|
117
+ entities = collection[type]
118
+ entities.each do |entity|
119
+ _matches = kb.children(database, entity)
120
+ acc.merge!({ _matches.target_type => _matches}) if _matches and _matches.any?
121
+ end
122
+ acc
123
+ }
124
+ case @format
125
+ when :tsv
126
+ content_type "text/tab-separated-values"
127
+ matches = matches.sort_by{|k,list| list.length }.last.last
128
+ halt 200, matches.tsv.to_s
129
+ when :html
130
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Children: #{ [name, database] }")
131
+ when :json
132
+ content_type :json
133
+ _matches = {}
134
+ matches.each{|type,list|
135
+ _matches[type] = list.target
136
+ }
137
+ halt 200, _matches.to_json
138
+ else
139
+ content_type :text
140
+ matches = matches.sort_by{|k,list| list.length }.last.last
141
+ halt 200, matches.target * "\n"
142
+ end
143
+ end
144
+
145
+ post '/knowledge_base/:name/:database/collection_parents' do
146
+ name = consume_parameter :name
147
+ database = consume_parameter :database
148
+ collection = consume_parameter :collection
149
+ raise ParameterException, "No collection specified" if collection.nil?
150
+ collection = JSON.parse(collection)
151
+
152
+ kb = get_knowledge_base name
153
+ matches = collection.keys.inject({}){|acc,type|
154
+ entities = collection[type]
155
+ entities.each do |entity|
156
+ _matches = kb.parents(database, entity)
157
+ acc.merge!({ _matches.target_type => _matches}) if _matches and _matches.any?
158
+ end
159
+ acc
160
+ }
161
+ case @format
162
+ when :tsv
163
+ content_type "text/tab-separated-values"
164
+ matches = matches.sort_by{|k,list| list.length }.last.last
165
+ halt 200, matches.tsv.to_s
166
+ when :html
167
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Parents: #{ [name, database] }")
168
+ when :json
169
+ content_type :json
170
+ _matches = {}
171
+ matches.each{|type,list|
172
+ _matches[type] = list.target
173
+ }
174
+ halt 200, _matches.to_json
175
+ else
176
+ content_type :text
177
+ matches = matches.sort_by{|k,list| list.length }.last.last
178
+ halt 200, matches.target * "\n"
179
+ end
180
+ end
181
+
182
+ post '/knowledge_base/:name/:database/collection_neighbours' do
183
+ name = consume_parameter :name
184
+ database = consume_parameter :database
185
+ collection = consume_parameter :collection
186
+ raise ParameterException, "No collection specified" if collection.nil?
187
+ collection = JSON.parse(collection)
188
+
189
+ kb = get_knowledge_base name
190
+ matches = collection.keys.inject({}){|acc,type|
191
+ entities = collection[type]
192
+ entities.each do |entity|
193
+ _matches_h = kb.neighbours(database, entity)
194
+ _matches_h.each do |key, _matches|
195
+ target_type = case key
196
+ when :children
197
+ _matches.target_entity_type
198
+ when :parents
199
+ _matches.source_entity_type
200
+ end
201
+ _matches = acc[target_type].concat _matches if acc[target_type] and acc[target_type].any?
202
+ acc.merge!({ target_type => _matches }) if _matches and _matches.any?
203
+ end
204
+ end
205
+ acc
206
+ }
207
+
208
+ case @format
209
+ when :tsv
210
+ content_type "text/tab-separated-values"
211
+ matches = matches.sort_by{|k,list| list.length }.last.last
212
+ halt 200, matches.tsv.to_s
213
+ when :html
214
+ template_render('knowledge_base_partials/matches', {:matches => matches}, "Collection Parents: #{ [name, database] }")
215
+ when :json
216
+ content_type :json
217
+ _matches = {}
218
+ matches.each{|type,list|
219
+ _matches[type] = list.target.uniq.sort
220
+ }
221
+ halt 200, _matches.to_json
222
+ else
223
+ content_type :text
224
+ matches = matches.sort_by{|k,list| list.length }.last.last
225
+ halt 200, matches.target * "\n"
226
+ end
227
+ end
228
+
229
+ get '/knowledge_base/:name/:database/subset' do
230
+ name = consume_parameter :name
231
+ database = consume_parameter :database
232
+ source = consume_parameter :source
233
+ target = consume_parameter :target
234
+
235
+ source = source == "all" ? :all : source.split(@array_separator) if source
236
+ target = target == "all" ? :all : target.split(@array_separator) if target
237
+ entities = { :source => source, :target => target }
238
+
239
+ kb = get_knowledge_base name
240
+ subset = kb.subset(database, entities)
241
+ case @format
242
+ when :tsv
243
+ content_type "text/tab-separated-values"
244
+ halt 200, subset.tsv.to_s
245
+ when :html
246
+ template_render('knowledge_base_partials/subset', {:subset => subset}, "Subset: #{ [name, database] }")
247
+ when :json
248
+ content_type :json
249
+ halt 200, subset.source.to_json
250
+ else
251
+ content_type :text
252
+ halt 200, subset.source * "\n"
253
+ end
254
+ end
255
+
256
+ #{{{ Info
257
+
258
+ get '/knowledge_base/info/:name/:database/:pair' do
259
+ name = consume_parameter :name
260
+ database = consume_parameter :database
261
+ pair = consume_parameter :pair
262
+
263
+ kb = get_knowledge_base name
264
+ index = kb.get_index(database)
265
+
266
+ AssociationItem.setup(pair, kb, database, false)
267
+ template_render('knowledge_base_partials/association', {:pair => pair, :kb => kb, :index => index, :database => database}, "Association: #{ pair }")
268
+ end
269
+
270
+ #{{{ Children
271
+
272
+ get '/knowledge_base/:name/:database/entity_children/:entity' do
273
+ name = consume_parameter :name
274
+ database = consume_parameter :database
275
+ entity = consume_parameter :entity
276
+
277
+ kb = get_knowledge_base name
278
+ found = kb.identify database, entity
279
+ raise ParameterException, "Entity #{entity} was not found" unless found
280
+
281
+ list = kb.children(database, found).target_entity
282
+
283
+ case @format
284
+ when :json
285
+ content_type "application/json"
286
+ halt 200, prepare_entities_for_json(list).to_json
287
+ when :html
288
+ end
289
+ end
290
+
291
+ # List children
292
+ post '/knowledge_base/:name/:database/entity_list_children/' do
293
+ name = consume_parameter :name
294
+ database = consume_parameter :database
295
+ entities = consume_parameter :entities
296
+
297
+ raise ParameterException, "No 'entities' provided" if entities.nil?
298
+
299
+ entities = entities.split("|")
300
+
301
+ kb = get_knowledge_base name
302
+
303
+ children = {}
304
+ entities.each do |entity|
305
+ found = kb.identify database, entity
306
+ next if found.nil?
307
+ children[entity] = kb.children(database, found).target_entity
308
+ end
309
+ case @format
310
+ when :json
311
+ content_type "application/json"
312
+ halt 200, prepare_entities_for_json(children).to_json
313
+ when :html
314
+ end
315
+ end
316
+
317
+ # Collection children
318
+ post '/knowledge_base/:name/:database/entity_collection_children' do
319
+ name = consume_parameter :name
320
+ database = consume_parameter :database
321
+ entities = consume_parameter :entities
322
+
323
+ raise ParameterException, "No 'entities' provided" if entities.nil?
324
+
325
+ entities = JSON.parse(entities)
326
+
327
+ kb = get_knowledge_base name
328
+
329
+ entities.each do |type,list|
330
+ list.each do |entity|
331
+ found = kb.identify database, entity
332
+ next if found.nil?
333
+ targets = kb.children(database, found).target_entity
334
+ next if targets.nil? or targets.empty?
335
+ target_type = kb.target database
336
+ children[target_type] ||= []
337
+ children[target_type].concat targets
338
+ end
339
+ end
340
+
341
+ case @format
342
+ when :json
343
+ content_type "application/json"
344
+ halt 200, prepare_entities_for_json(children).to_json
345
+ when :html
346
+ end
347
+ end
348
+
349
+ #{{{ Neighbours
350
+
351
+ get '/knowledge_base/:name/:database/entity_neighbours/:entity' do
352
+ name = consume_parameter :name
353
+ database = consume_parameter :database
354
+ entity = consume_parameter :entity
355
+
356
+ kb = get_knowledge_base name
357
+ found = kb.identify database, entity
358
+ raise ParameterException, "Entity #{entity} was not found" unless found
359
+
360
+ list = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
361
+ list = list.target_entity if list.respond_to? :target_entity
362
+ list ||= []
363
+
364
+ case @format
365
+ when :json
366
+ content_type "application/json"
367
+ halt 200, prepare_entities_for_json(list).to_json
368
+ when :html
369
+ end
370
+ end
371
+
372
+ post '/knowledge_base/:name/:database/entity_list_neighbours/' do
373
+ name = consume_parameter :name
374
+ database = consume_parameter :database
375
+ entities = consume_parameter :entities
376
+
377
+ raise ParameterException, "No 'entities' provided" if entities.nil?
378
+
379
+ entities = entities.split("|")
380
+
381
+ kb = get_knowledge_base name
382
+
383
+ children = {}
384
+ entities.each do |entity|
385
+ found = kb.identify database, entity
386
+ next if found.nil?
387
+ matches = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
388
+ next if matches.nil? or matches.empty?
389
+ children[entity] = matches.target_entity
390
+ end
391
+ case @format
392
+ when :json
393
+ content_type "application/json"
394
+ halt 200, prepare_entities_for_json(children).to_json
395
+ when :html
396
+ end
397
+ end
398
+
399
+ post '/knowledge_base/:name/:database/entity_collection_neighbours' do
400
+ name = consume_parameter :name
401
+ database = consume_parameter :database
402
+ entities = consume_parameter :entities
403
+
404
+ raise ParameterException, "No 'entities' provided" if entities.nil?
405
+
406
+ entities = JSON.parse(entities)
407
+
408
+ kb = get_knowledge_base name
409
+
410
+ neighbours = {}
411
+ entities.each do |type,list|
412
+ list.each do |entity|
413
+
414
+ found = kb.identify_source database, entity
415
+ if found.nil?
416
+ reverse = true
417
+ found = kb.identify_target database, entity
418
+ else
419
+ reverse = false
420
+ end
421
+ next if found.nil?
422
+
423
+ matches = kb.neighbours(database, found)[reverse ? :parents : :children]
424
+ next if matches.nil? or matches.empty?
425
+ targets = matches.target
426
+
427
+ entity_type = reverse ? kb.source_type(database) : kb.target_type(database)
428
+ neighbours[entity_type] ||= []
429
+ neighbours[entity_type].concat targets
430
+ end
431
+ end
432
+
433
+ neighbours.each{|type, list| list.uniq!}
434
+
435
+ case @format
436
+ when :json
437
+ content_type "application/json"
438
+ halt 200, prepare_entities_for_json(neighbours).to_json
439
+ when :html
440
+ end
441
+ end
442
+ end