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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
# File 'lib/rbbt/workflow/rest.rb', line 25
def self.setup
configure do
Compass.add_project_configuration(Rbbt.share.views.compass["compass.config"].find.dup.to_s)
end
Sinatra.module_eval do
helpers Sinatra::SinatraRbbt
use Rack::Deflater
enable :static
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:expire_after => 2592000,
:secret => 'rbbt_default_secret. Please CHANGE! ' + WorkflowREST.workflows.collect{|w| w.to_s} * ", "
set :root, ENV["PWD"]
set :cache_dir, "var/cache" unless settings.respond_to? :cache_dir and settings.cache_dir
set :persistence_dir, "var/cache/persistence" unless settings.respond_to? :persistence_dir and settings.persistence_dir
set :file_dir, "var/files" unless settings.respond_to? :file_dir and settings.file_dir
set :note_dir, "var/notes" unless settings.respond_to? :note_dir and settings.note_dir
set :user_action_defaults, "var/user_defaults" unless settings.respond_to? :user_action_defaults and settings.user_action_defaults
set :protection, :except => :json_csrf
before do
@file_cache = {}
@result_cache = {}
@js_files = []
@css_files = []
"AJAX-URL" => request.env["REQUEST_URI"]
params.delete "_"
params.delete :_
cache_control :public, :must_revalidate, :max_age => 60
'Access-Control-Allow-Origin' => request.env['HTTP_ORIGIN'],
'Access-Control-Allow-Methods' => %w(GET POST OPTIONS),
'Access-Control-Allow-Credentials' => "true",
'Access-Control-Max-Age' => "1728000"
end
after do
@file_cache.clear if @file_cache
@result_cache.clear if @result_cache
end
helpers do
def authorization_realm
"Workflow Explorer"
end
def check_user(user)
login_required
request.env['REMOTE_USER'] == user || throw(:halt, [ 401, 'Authorization Required' ])
end
end
end
set :public_folder, RbbtHTMLHelpers.locate_file(nil, nil, 'public/').find
begin
set :users, RbbtHTMLHelpers.locate_file(nil, nil, 'config/users').yaml
rescue
if Rbbt.etc.web_users.exists?
set :users, Rbbt.etc.web_users.yaml
else
set :users, {}
end
end
followed_users = {}
all_users = settings.users.keys
all_users.each do |user|
followed_users[user] = all_users - [user]
end
set :followed_users, followed_users
error do
error = request.env['sinatra.error']
workflow_render('error', :message => error.message)
end
get '/files/:filename' do
cache_control :public, :max_age => 36000 if production?
file = File.join(settings.file_dir, params[:filename])
send_file file
end
get '/stylesheets/entity/:type/:entity/:name.css' do
entity_type, format = params["type"].split ":"
entity = setup_entity(entity_type, format, params["entity"])
file = locate_file(WorkflowREST.workflows.last, nil, File.join("compass", params[:name] + '.sass'), entity)
content_type 'text/css', :charset => 'utf-8'
cache_control :public, :max_age => 36000 if production?
cache_type = production? ? :sync : :none
cache('css', params.merge(:cache_type => cache_type)) do
renderer = Sass::Engine.new(file.read, :filename => file.find)
css_text = renderer.render
YUI::CssCompressor.new.compress(css_text)
end
end
get '/stylesheets/:name.css' do
file = locate_file(WorkflowREST.workflows.last, nil, File.join("compass", params[:name] + '.sass'))
content_type 'text/css', :charset => 'utf-8'
cache_control :public, :max_age => 36000 if production?
cache_type = production? ? :sync : :none
cache('css', params.merge(:cache_type => cache_type)) do
renderer = Sass::Engine.new(file.read, :filename => file.find)
css_text = renderer.render
YUI::CssCompressor.new.compress(css_text)
end
end
get '/js/entity/:type/:entity/:name.js' do
content_type 'text/css', :charset => 'utf-8'
cache_control :public, :max_age => 36000 if production?
entity_type, format = params["type"].split ":"
entity = setup_entity(entity_type, format, params["entity"])
send_file locate_file(WorkflowREST.workflows.last, nil, File.join("js", params[:name] + '.js'), entity)
end
get '/js/:name.js' do
content_type 'text/css', :charset => 'utf-8'
cache_control :public, :max_age => 36000 if production?
send_file locate_file(WorkflowREST.workflows.last, nil, File.join("js", params[:name] + '.js')).find
end
get '/entity/:entity_type/:entity' do
params.delete "captures"
params.delete "splat"
entity_type = params[:entity_type]
visualization_parameters = get_visualization_parameters(params)
cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
update = params.delete(:_update) || params.delete("_update") || nil
cache("Entity [#{ params[:entity] }]:", :update => update, :cache_type => cache_type, :params => params, :visualization_params => visualization_parameters, :user => user) do
entity_render(entity_type, params.merge(visualization_parameters))
end
end
get '/entity_list/:user' do
params.delete "captures"
params.delete "splat"
list_user = params.delete(:user) || params.delete("user")
list_user = user if list_user.to_s == "user"
if list_user != "public"
raise "Not allowed to view entity lists for: #{ user }" unless authorized? and user.to_s == list_user
end
lists = Entity::REST.list_files(list_user)
workflow_render('user_lists', params.merge(:lists => lists))
end
get '/entity_list/:entity_type/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
visualization_parameters = get_visualization_parameters(params)
list_id = params[:id] = CGI.unescape(params[:id])
cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
update = params.delete(:_update) || params.delete("_update") || nil
check = Entity::REST.list_file(entity_type, list_id, user)
check = Entity::REST.list_file(entity_type, list_id, :public) if check.nil? or not File.exists? check
check = Entity::REST.list_file(entity_type, list_id) if check.nil? or not File.exists? check
raise "List of type #{ entity_type } not found for #{list_id}: #{ check.inspect }" if check.nil? or not File.exists?(check)
content_type "text/plain" if visualization_parameters[:_format] == 'literal' or visualization_parameters[:_format] == 'tsv' or visualization_parameters[:_format] == 'bed'
content_type "application/json" if visualization_parameters[:_format] == 'json'
cache("Entity List:", :check => [check], :update => update, :cache_type => cache_type, :params => params, :entity_type => entity_type, :visualization_params => visualization_parameters, :user => user) do
case (visualization_parameters[:_format] || 'html').to_s
when 'json'
list = Entity::REST.load_list(entity_type, list_id, user)
list.collect{|elem| [elem.clean_annotations, elem.info.merge(:id => elem.id)]}.to_json
when 'bed'
list = Entity::REST.load_list(entity_type, list_id, user)
type = entity_type.split(":").first
case type
when "GenomicMutation"
list.collect{|elem| ['chr' + elem.chromosome, elem.position, elem.position+1] * "\t"} * "\n"
else
raise "Don't know how to produce bed file for entity type: #{ entity_type }"
end
when 'tsv'
list = Entity::REST.load_list(entity_type, list_id, user)
Annotated.tsv(list, :all).to_s
when 'literal'
list = Entity::REST.load_list(entity_type, list_id, user)
list * "\n"
else
entity_list_render(entity_type, params.merge(visualization_parameters))
end
end
end
post '/entity_list/:entity_type/create/:id' do
params.delete "captures"
params.delete "splat"
list_id = params.delete("id")
list_id = CGI.unescape(list_id)
entity_type = params.delete "entity_type"
entities = params.delete("entities")
raise "No entities" unless (entities and not entities.empty?)
entities = entities.split(/[,;|]/)
options = IndiferentHash.setup(params.dup)
entities = Misc.prepare_entity(entities, entity_type, options)
Entity::REST.save_list(entity_type, list_id, entities, user)
redirect "/entity_list/#{ entity_type }/#{list_id}"
end
post '/entity_list/:entity_type/copy/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
list_id = params[:id] = CGI.unescape(params[:id])
list = Entity::REST.load_list(entity_type, list_id, user)
Entity::REST.save_list(entity_type, params[:new_id], list, user)
redirect "/entity_list/#{ entity_type }/#{params[:new_id]}"
end
post '/entity_list/:entity_type/add/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
list_id = params[:id] = CGI.unescape(params[:id])
list = Entity::REST.load_list(entity_type, list_id, user)
other_list = Entity::REST.load_list(entity_type, params[:other_list], user)
list.concat other_list
list.uniq!
Entity::REST.save_list(entity_type, list_id, list, user)
redirect back
end
post '/entity_list/:entity_type/remove/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
list_id = params[:id] = CGI.unescape(params[:id])
list = Entity::REST.load_list(entity_type, list_id, user)
other_list = Entity::REST.load_list(entity_type, params[:other_list], user)
Entity::REST.save_list(entity_type, list_id, list.remove(other_list), user)
redirect back
end
post '/entity_list/:entity_type/subset/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
list_id = params[:id] = CGI.unescape(params[:id])
list = Entity::REST.load_list(entity_type, list_id, user)
other_list = Entity::REST.load_list(entity_type, params[:other_list], user)
Entity::REST.save_list(entity_type, list_id, list.subset(other_list), user)
redirect back
end
get '/entity_list/:entity_type/edit/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params[:entity_type]
visualization_parameters = get_visualization_parameters(params)
workflow = WorkflowREST.workflows.last
list_id = params[:id] = CGI.unescape(params[:id])
id = list_id
list = Entity::REST.load_list(entity_type, id, user)
workflow_render('edit_list', workflow, params.merge(:list => list))
end
delete '/entity_list/:entity_type/:id' do
params.delete "captures"
params.delete "splat"
list_id = params[:id] = CGI.unescape(params[:id])
Entity::REST.delete_list(params[:entity_type], list_id, user)
redirect '/'
end
delete '/entity_list/:entity_type/:entity/:id' do
params.delete "captures"
params.delete "splat"
id = params[:id] = CGI.unescape(params[:id])
Log.debug "Deleting #{params[:entity] } from #{id}"
list = Entity::REST.load_list(params[:entity_type], id, user)
list.delete params[:entity]
Entity::REST.save_list(params[:entity_type], id, list, user)
redirect back
end
post '/entity_list/:entity_type/:entity/:list_id' do
params.delete "captures"
params.delete "splat"
list_id = params.delete(:list_id) || params.delete("list_id")
entity = params.delete(:entity) || params.delete("entity")
entity_type = params.delete(:entity_type) || params.delete("entity_type")
entity_type, entity_format = entity_type.split(":")
info = {:annotation_types => entity_type, :format => entity_format}
params.each{|k,v| info[k] = begin JSON.parse(v) rescue v end}
entity = Annotated.load_tsv_values(entity, [[info.to_json]], "JSON")
entity.id = params[:entity_id] if params.include? :entity_id
list =
begin
Entity::REST.load_list(entity_type, list_id, user) || []
rescue
[]
end
different = Annotated === list ? entity.info.select{|k,v| not v.nil? and v != list.info[k]}.any? : true
if different
new = list.collect{|e| e}
else
new = list
end
new = new.reject{|e| e.id == entity.id}
new << entity
new.first.annotate new if new.collect{|e| e.info}.uniq.length == 1
Entity::REST.save_list(entity_type, list_id, new, user)
redirect back unless request.xhr?
end
get '/entity_action/:entity_type/:action/:entity' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
action = params.delete "action"
visualization_parameters = get_visualization_parameters(params)
cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
update = params.delete(:_update) || params.delete("_update") || nil
add_user_defaults(user, entity_type, action, params) if authorized?
html = cache("Entity Action [#{action}][#{ params[:entity] }]", :update => update, :cache_type => cache_type, :params => params, :entity_type => entity_type, :action => action, :visualization_params => visualization_parameters, :user => user) do
entity_action_render(entity_type, action, params.merge(visualization_parameters))
end
save_user_defaults(user, entity_type, action, params) if authorized? and response.status == 200
clear_user_defaults(user, entity_type, action) if authorized? and response.status == 500
html
end
get '/entity_list_action/:entity_type/:action/:id' do
params.delete "captures"
params.delete "splat"
entity_type = params.delete "entity_type"
action = params.delete "action"
visualization_parameters = get_visualization_parameters(params)
cache_type = params.delete(:_cache_type) || params.delete("_cache_type") || :async
update = params.delete(:_update) || params.delete("_update") || nil
add_user_defaults(user, entity_type, action, params) if authorized?
list_id = params[:id] = params[:id] = CGI.unescape(params[:id])
html = cache("Entity List Action [#{action}] [#{ list_id}]", :update => update, :cache_type => cache_type, :params => params, :entity_type => entity_type, :action => action, :visualization_params => visualization_parameters, :user => user) do
entity_list_action_render(entity_type, action, params.merge(visualization_parameters))
end
save_user_defaults(user, entity_type, action, params) if authorized? and response.status == 200
clear_user_defaults(user, entity_type, action) if authorized? and response.status == 500
html
end
get '/notes/edit/*' do
params.delete "captures"
path = params.delete("splat").last
markup = get_notes_markup(path, params, user)
workflow_render('notes', StudyExplorer, nil, :markup => markup)
end
post '/notes/edit/*' do
params.delete "captures"
path = params.delete("splat").last
path = path.sub("/notes/edit", "/notes")
markup = params.delete('markup')
save_notes(path, params, markup, user)
redirect request.env["REQUEST_URI"].sub('/notes/edit', '/notes')
end
post '/notes/share/*' do
params.delete "captures"
path = params.delete("splat").last
path = path.sub("/notes/edit", "/notes")
markup = params.delete('markup')
params.delete("_method")
share_notes(path, params, markup, user)
redirect back unless request.xhr?
end
post '/notes/unshare/*' do
params.delete "captures"
path = params.delete("splat").last
path = path.sub("/notes/edit", "/notes")
markup = params.delete('markup')
params.delete("_method")
unshare_notes(path, params, markup, user)
redirect back unless request.xhr?
end
delete '/notes/*' do
params.delete "captures"
path = params.delete("splat").last
params.delete("_method")
markup = delete_notes_markup(path, params, user)
redirect File.join("/", path)
end
get '/notes/shared/:user/*' do
params.delete "captures"
path = params.delete("splat").last
sharing_user = params.delete('user')
render_notes(path, params, sharing_user, true)
end
get '/notes/*' do
params.delete "captures"
path = params.delete("splat").last
render_notes(path, params, user)
end
end
|