summaryrefslogtreecommitdiff
path: root/application.rb
blob: a82411d9e2eb308ec6400aeb9e56896de3057fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
require 'rubygems'
gem "opentox-ruby", "~> 3"
require 'opentox-ruby'
require 'profiler'
require 'rjb'

set :lock, true

@@datadir = "data"

@@idfile_path = @@datadir+"/id" 
unless File.exist?(@@idfile_path)
  id = Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.last
  id = 0 if id.nil?
  open(@@idfile_path,"w") do |f|
    f.puts(id)
  end
end

helpers do
  def next_id
    open(@@idfile_path, "r+") do |f|
      f.flock(File::LOCK_EX)
      @id = f.gets.to_i + 1
      f.rewind
      f.print @id
    end
    return @id
  end

  def uri(id)
    url_for "/#{id}", :full
  end

  # subjectid ist stored as memeber variable, not in params
  def load_dataset(id, params,content_type,input_data)

    @uri = uri id
    raise "store subject-id in dataset-object, not in params" if params.has_key?(:subjectid) and @subjectid==nil

    content_type = "application/rdf+xml" if content_type.nil?
    dataset = OpenTox::Dataset.new(@uri, @subjectid) 

    case content_type

    when /yaml/
      dataset.load_yaml(input_data)

    when /json/
      dataset.load_json(input_data)

    when "text/csv"
      dataset.load_csv(input_data, @subjectid)

    when /application\/rdf\+xml/
      dataset.load_rdfxml(input_data, @subjectid)
         
    when "chemical/x-mdl-sdfile"
      dataset.load_sdf(input_data, @subjectid)

    when /multipart\/form-data/ , "application/x-www-form-urlencoded" # file uploads

      case params[:file][:type]

      when "chemical/x-mdl-sdfile"
        dataset.load_sdf(input_data, @subjectid)

      when /json/
        dataset.load_json(params[:file][:tempfile].read)

      when /yaml/
        dataset.load_yaml(params[:file][:tempfile].read)

      when "application/rdf+xml"
        dataset.load_rdfxml_file(params[:file][:tempfile], @subjectid)

      when "text/csv"
        dataset.load_csv(params[:file][:tempfile].read, @subjectid)
        dataset.add_metadata({
        DC.title => File.basename(params[:file][:filename],".csv"),
        OT.hasSource => File.basename(params[:file][:filename])
      })

      when /ms-excel/
        extension =  File.extname(params[:file][:filename])
        case extension
        when ".xls"
          xls = params[:file][:tempfile].path + ".xls"
          File.rename params[:file][:tempfile].path, xls # roo needs these endings
          book = Excel.new xls
        when ".xlsx"
          xlsx = params[:file][:tempfile].path + ".xlsx"
          File.rename params[:file][:tempfile].path, xlsx # roo needs these endings
          book = Excel.new xlsx
        else
          raise "#{params[:file][:filename]} is not a valid Excel input file."
        end
        dataset.load_spreadsheet(book, @subjectid)
        dataset.add_metadata({
          DC.title => File.basename(params[:file][:filename],extension),
          OT.hasSource => File.basename(params[:file][:filename])
        })

      else
        raise "MIME type \"#{params[:file][:type]}\" not supported."
      end

    else
      raise "MIME type \"#{content_type}\" not supported."
    end

    dataset.uri = @uri # update uri (also in metdata)
    dataset.features.keys.each { |f| dataset.features[f][OT.hasSource] = dataset.metadata[OT.hasSource] unless dataset.features[f][OT.hasSource]}
    zip_dataset( dataset )
  end
  
  def json_file()
    "#{@@datadir}/#{@id}.json"
  end
  
  def json_zip_file()
    "#{@@datadir}/#{@id}.json.zip"
  end
  
  def zip_dataset( dataset )
    raise unless @id
    File.open(json_file(),"w+"){|f| f.puts dataset.to_json}
    ZipUtil.zip(json_zip_file(),json_file())
    #File.delete(json_file())
  end

  def unzip_dataset()
    OpenTox::Dataset.from_json(unzip_json())
  end
  
  def unzip_json()
    if !File.exist?(json_file())
      ZipUtil.unzip(json_zip_file(),@@datadir)
      raise "could not unzip file" unless File.exist?(json_file())
    end
    content =  File.read(json_file())
    #File.delete(json_file())
    content
  end

  def arff_file()
    "#{@@datadir}/#{@id}.arff"
  end
  
  def arff_zip_file()
    "#{@@datadir}/#{@id}.arff.zip"
  end
  
  def zip_arff( arff )
    File.open(arff_file,"w+") { |f| f.puts arff }
    ZipUtil.zip(arff_zip_file(),arff_file())
    #File.delete(arff_file())
  end
  
  def unzip_arff()
    if !File.exist?(arff_file())
      ZipUtil.unzip(arff_zip_file(),@@datadir)
      raise "could not unzip file" unless File.exist?(arff_file())
    end
    content = File.read(arff_file())
    #File.delete(arff_file())
    content
  end
    
  
  def to_arff(dataset, subjectid=nil, features=nil )
          
    LOGGER.debug "convert dataset to arff #{dataset.uri}"
          
    # count duplicates
    num_compounds = {}
    dataset.features.keys.each do |f|
      dataset.compounds.each do |c|
        if dataset.data_entries[c]
          val = dataset.data_entries[c][f]
          size = val==nil ? 1 : val.size
          num_compounds[c] = num_compounds[c]==nil ? size : [num_compounds[c],size].max
        else
          num_compounds[c] = 1
        end
      end
    end  
    
    #puts "found duplicates"
    
    # use either all, or the provided features, sorting is important as col-index := features
    if features
      features.sort!
    else
      features = dataset.features.keys.sort
    end
    compounds = []
    compound_names = []
    dataset.compounds.each do |c|
      count = 0
      num_compounds[c].times do |i|
        compounds << c
        compound_names << "#{c}$#{count}"
        count+=1
      end
    end
    
    missing = {}
    
    arff = "@RELATION #{dataset.uri}\n\n"
    features.each do |f|
      numeric = dataset.features[f][RDF.type].to_a.flatten.include?(OT.NumericFeature)
      #feat = OpenTox::Feature.find(f,subjectid)
      #numeric = feat.metadata[RDF.type].to_a.flatten.include?(OT.NumericFeature)
      if numeric
        arff << "@ATTRIBUTE '#{f}' NUMERIC\n"
      else
        # HACK for binary 0,1 features without accept values (as returned by fminer):
        # set missing value to 0 instead ?
        # set accept value to [0,1]
        accept_values = dataset.accept_values(f)
        missing[f] = "0" if accept_values==nil
        arff << "@ATTRIBUTE '#{f}' {#{(accept_values==nil ? [0,1] : accept_values).join(",")}}\n"
      end
    end
    
    #puts "found feature types"

    arff << "\n@DATA\n"
    
    dataset.compounds.each do |c|
      num_compounds[c].times do |i|
        c_values = []
        features.each do |f|
          accept_values = 
          if dataset.data_entries[c]
            val = dataset.data_entries[c][f]
            v = val==nil ? "" : val[i].to_s
          else
            raise "wtf" if i>0
            v = ""
          end
          v = (missing[f]==nil ? "?" : missing[f]) if v.size()==0
          c_values << v
        end
        arff << "#{c_values.join(",")}\n"
      end
    end
    arff
  end
      
end

before do

  @accept = request.env['HTTP_ACCEPT']
  @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil?
  @id = request.path_info.match(/^\/\d+/)
  unless @id.nil?
    @id = @id.to_s.sub(/\//,'').to_i

    @uri = uri @id
    @json_zip_file = "#{@@datadir}/#{@id}.json.zip"
    raise OpenTox::NotFoundError.new "Dataset #{@id} not found." unless File.exists? @json_zip_file

    extension = File.extname(request.path_info)
    unless extension.empty?
     case extension
     when ".html"
       @accept = 'text/html'
     when ".json"
       @accept = 'application/json'
     when ".yaml"
       @accept = 'application/x-yaml'
     when ".csv"
       @accept = 'text/csv'
     when ".rdfxml"
       @accept = 'application/rdf+xml'
     when ".xls"
       @accept = 'application/ms-excel'
     when ".sdf"
       @accept = 'chemical/x-mdl-sdfile'
     else
       raise OpenTox::NotFoundError.new "File format #{extension} not supported."
     end
    end
  end
  
  # make sure subjectid is not included in params, subjectid is set as member variable
  params.delete(:subjectid) 
end

## REST API

# Get a list of available datasets
# @return [text/uri-list] List of available datasets
get '/?' do
  uri_list = Dir["./#{@@datadir}/*json.zip"].collect{|f| File.basename(f.sub(/.json.zip/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" 
  case @accept
  when /html/
    response['Content-Type'] = 'text/html'
    OpenTox.text_to_html uri_list
  else
    response['Content-Type'] = 'text/uri-list'
    uri_list
  end
end

#post '/:id/rdf' do 
#  response['Content-Type'] = 'text/uri-list'
#  task = OpenTox::Task.create("Converting dataset to rdf ", @uri) do 
#    file = "#{@@datadir}/#{params[:id]}.rdfxml"
#    unless File.exists? file # lazy rdfxml generation
#      dataset = OpenTox::Dataset.from_json File.read(@json_file)
#      File.open(file,"w+") { |f| f.puts dataset.to_rdfxml }
#    end
#    @uri
#  end
#  return_task task
#end

# Get a dataset representation
# @param [Header] Accept one of `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` (default application/rdf+xml)
# @return [application/rdf+xml, application-x-yaml, text/csv, application/ms-excel] Dataset representation
get '/:id' do
  case @accept

  when /rdf/ # redland sends text/rdf instead of application/rdf+xml
    file = "#{@@datadir}/#{params[:id]}.rdfxml"
    unless File.exists? file # lazy rdfxml generation
      #dataset = OpenTox::Dataset.from_json File.read(@json_file)
      dataset = unzip_dataset()
      File.open(file,"w+") { |f| f.puts dataset.to_rdfxml }
    end
    send_file file, :type => 'application/rdf+xml'

  when /json/
    #send_file @json_file, :type => 'application/x-yaml' 
    response['Content-Type'] = 'application/x-json'
    unzip_json()

  when /arff/
    response['Content-Type'] = 'application/x-arff'
    unless File.exists? "#{@@datadir}/#{params[:id]}.arff.zip"
      arff_content = to_arff(unzip_dataset())
      zip_arff(arff_content)
      arff_content
    else
      unzip_arff()
    end
    
  when /yaml/
    file = "#{@@datadir}/#{params[:id]}.yaml"
    unless File.exists? file # lazy yaml generation
      #dataset = OpenTox::Dataset.from_json File.read(@json_file)
      dataset = unzip_dataset()
      File.open(file,"w+") { |f| f.puts dataset.to_yaml }
    end
    send_file file, :type => 'application/x-yaml' 

  when /html/
    response['Content-Type'] = 'text/html'
    OpenTox.text_to_html JSON.pretty_generate(JSON.parse(unzip_json())) 

  when "text/csv"
    response['Content-Type'] = 'text/csv'
    #OpenTox::Dataset.from_json(File.read(@json_file)).to_csv
    unzip_dataset().to_csv

  when /ms-excel/
    file = "#{@@datadir}/#{params[:id]}.xls"
    #OpenTox::Dataset.from_json(File.read(@json_file)).to_xls.write(file) unless File.exists? file # lazy xls generation
    unzip_dataset().to_xls.write(file) unless File.exists? file # lazy xls generation
    send_file file, :type => 'application/ms-excel'

  when /sdfile/
    response['Content-Type'] = 'chemical/x-mdl-sdfile'
    #OpenTox::Dataset.from_json(File.read(@json_file)).to_sdf
    unzip_dataset().to_sdf

#  when /uri-list/
#    response['Content-Type'] = 'text/uri-list'
#    Yajl::Parser.parse(File.read(@json_file)).to_urilist

  else
    raise OpenTox::NotFoundError.new "Content-type #{@accept} not supported."
  end
end

# Get metadata of the dataset
# @return [application/rdf+xml] Metadata OWL-DL
get '/:id/metadata' do
  metadata = unzip_dataset().metadata
  
  case @accept
  when /rdf/ # redland sends text/rdf instead of application/rdf+xml
    response['Content-Type'] = 'application/rdf+xml'
    serializer = OpenTox::Serializer::Owl.new
    serializer.add_metadata url_for("/#{params[:id]}",:full), metadata
    serializer.to_rdfxml
  when /yaml/
    response['Content-Type'] = 'application/x-yaml'
    metadata.to_yaml
  end

end

  get '/:id/info' do
    dataset = unzip_dataset()
    
    array = []
    array << @uri
    array << "#{dataset.compounds.size} compounds"
    array << "#{dataset.features.size} features"
    array << ["metadata", dataset.metadata]
    array << ["features-uris", dataset.features.keys.sort]  
    array << ["compounds", dataset.compounds]
    array << ["features", dataset.features]
    
    case @accept
    when /html/ # redland sends text/rdf instead of application/rdf+xml
      response['Content-Type'] = 'text/html'
      OpenTox.text_to_html array.to_yaml
    else
      response['Content-Type'] = 'application/x-yaml'
      metadata.to_yaml
    end
  end
  
  get '/:id/info_values' do
      dataset = unzip_dataset()
      
      array = []
      array << @uri
      array << "#{dataset.compounds.size} compounds"
      array << "#{dataset.features.size} features"
      array << ["metadata", dataset.metadata]
      array << ["compounds", dataset.compounds]
      array << ["features", dataset.features]
      array << ["values", dataset.data_entries]
            
      case @accept
      when /html/ # redland sends text/rdf instead of application/rdf+xml
        response['Content-Type'] = 'text/html'
        OpenTox.text_to_html array.to_yaml
      else
        response['Content-Type'] = 'application/x-yaml'
        metadata.to_yaml
      end
    end  

# Get a dataset feature
# @param [Header] Accept one of `application/rdf+xml or application-x-yaml` (default application/rdf+xml)
# @return [application/rdf+xml,application/x-yaml] Feature metadata 
get %r{/(\d+)/feature/(.*)$} do |id,feature|

  @id = id
  @uri = uri @id
  #@json_file = "#{@@datadir}/#{@id}.json"
  @json_zip_file = "#{@@datadir}/#{@id}.json.zip"
  feature_uri = url_for("/#{@id}/feature/#{URI.encode(feature)}",:full) # work around  racks internal uri decoding
  metadata = unzip_dataset().features[feature_uri]
  
  case @accept
  when /rdf/ # redland sends text/rdf instead of application/rdf+xml
    response['Content-Type'] = 'application/rdf+xml'
    serializer = OpenTox::Serializer::Owl.new
    serializer.add_feature feature_uri, metadata
    serializer.to_rdfxml
  when /yaml/
    response['Content-Type'] = 'application/x-yaml'
    metadata.to_yaml
  when /json/
    response['Content-Type'] = 'application/json'
    Yajl::Encoder.encode(metadata)
  end

end

# Get a list of all features
# @param [Header] Accept one of `application/rdf+xml, application-x-yaml, text/uri-list` (default application/rdf+xml)
# @return [application/rdf+xml, application-x-yaml, text/uri-list] Feature list 
get '/:id/features' do

  features = unzip_dataset().features

  case @accept
  when /rdf/ # redland sends text/rdf instead of application/rdf+xml
    response['Content-Type'] = 'application/rdf+xml'
    serializer = OpenTox::Serializer::Owl.new
    features.each { |feature,metadata| serializer.add_feature feature, metadata }
    serializer.to_rdfxml
  when /yaml/
    response['Content-Type'] = 'application/x-yaml'
    features.to_yaml
  when /json/
    response['Content-Type'] = 'application/json'
    Yajl::Encoder.encode(features)
  when "text/uri-list"
    response['Content-Type'] = 'text/uri-list'
    features.keys.join("\n") + "\n"
  end
end

# Get a list of all compounds
# @return [text/uri-list] Feature list 
get '/:id/compounds' do
  response['Content-Type'] = 'text/uri-list'
  unzip_dataset().compounds.join("\n") + "\n"
end

# Create a new dataset.
#
# Posting without parameters creates and saves an empty dataset (with assigned URI).
# Posting with parameters creates and saves a new dataset.
# Data can be submitted either
# - in the message body with the appropriate Content-type header or
# - as file uploads with Content-type:multipart/form-data and a specified file type
# @example
#   curl -X POST -F "file=@training.csv;type=text/csv" http://webservices.in-silico.ch/dataset
# @param [Header] Content-type one of `application/x-yaml, application/rdf+xml, multipart/form-data/`
# @param [BODY] - string with data in selected Content-type
# @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` 
# @return [text/uri-list] Task URI or Dataset URI (empty datasets)
post '/?' do 

  response['Content-Type'] = 'text/uri-list'
  
  # it could be that the read function works only once!, store in varible
  input_data = request.env["rack.input"].read
  @id = next_id
  @uri = uri @id
  #@json_file = "#{@@datadir}/#{@id}.json"
  @json_zip_file = "#{@@datadir}/#{@id}.json.zip"
  if params.size == 0 and input_data.size==0
    #File.open(@json_file,"w+"){|f| f.puts OpenTox::Dataset.new(@uri).to_json}
    zip_dataset(OpenTox::Dataset.new(@uri))      
    #OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_file
    OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_zip_file
    @uri
  else
    task = OpenTox::Task.create("Converting and saving dataset ", @uri) do 
      load_dataset @id, params, request.content_type, input_data 
      #OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_file
      OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_zip_file
      @uri
    end
    raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled"
    halt 202,task.uri+"\n"
  end
end

post '/merge' do
  LOGGER.info "merging datasets #{params.inspect}"
  raise "dataset1 or dataset2 is missing" unless params["dataset1"] and params["dataset2"]
  task = OpenTox::Task.create("Merging datasets", @uri) do 
    dataset1 = OpenTox::Dataset.find(params["dataset1"])
    dataset2 = OpenTox::Dataset.find(params["dataset2"])
    features1 = params.has_key?("features1") ? params["features1"].split(",") : nil
    features2 = params.has_key?("features2") ? params["features2"].split(",") : nil
    compounds1 = params.has_key?("compounds1") ? params["compounds1"].split(",") : nil
    compounds2 = params.has_key?("compounds2") ? params["compounds2"].split(",") : nil
    OpenTox::Dataset.merge(dataset1,dataset2,{},@subjectid,features1,features2,compounds1,compounds2).uri
  end
  return_task task
end

post '/filter' do
  LOGGER.info "filtering dataset #{params.inspect}"
  raise "dataset_uri is missing" unless params["dataset_uri"]
  task = OpenTox::Task.create("Filtering dataset", @uri) do 
    dataset = OpenTox::Dataset.find(params["dataset_uri"])
    dataset.filter( params.has_key?("features") ? params["features"].split(",") : nil ).uri
  end
  return_task task
end


# Save a dataset, will overwrite all existing data
#
# Data can be submitted either
# - in the message body with the appropriate Content-type header or
# - as file uploads with Content-type:multipart/form-data and a specified file type
# @example
#   curl -X POST -F "file=@training.csv;type=text/csv" http://webservices.in-silico.ch/dataset/1
# @param [Header] Content-type one of `application/x-yaml, application/rdf+xml, multipart/form-data/`
# @param [BODY] - string with data in selected Content-type
# @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` 
# @return [text/uri-list] Task ID 
post '/:id' do 
  response['Content-Type'] = 'text/uri-list'
  task = OpenTox::Task.create("Converting and saving dataset ", @uri) do 
    FileUtils.rm Dir["#{@@datadir}/#{@id}.*"]
    load_dataset @id, params, request.content_type, request.env["rack.input"].read 
    @uri
  end
  raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled"
  halt 202,task.uri.to_s+"\n"
end



# Deletes datasets that have been created by a crossvalidatoin that does not exist anymore
# (This can happen if a crossvalidation fails unexpectedly)
delete '/cleanup' do
  Dir["./#{@@datadir}/*json"].each do |file|
    dataset = OpenTox::Dataset.from_json File.read(file)
    if dataset.metadata[DC.creator] && dataset.metadata[DC.creator] =~ /crossvalidation\/[0-9]/
      begin
        cv = OpenTox::Crossvalidation.find(dataset.metadata[DC.creator],@subjectid)
        raise unless cv
      rescue
        LOGGER.debug "deleting #{dataset.uri}, crossvalidation missing: #{dataset.metadata[DC.creator]}"
        begin
          dataset.delete @subjectid
        rescue
        end
      end
    end 
  end
  "cleanup done"   
end

# Delete a dataset
# @return [text/plain] Status message
delete '/:id' do
  LOGGER.debug "deleting dataset with id #{@id}"
  begin
    FileUtils.rm Dir["#{@@datadir}/#{@id}.*"]
    #if @subjectid and !File.exists? @json_file and @uri
    if @subjectid and !File.exists? @json_zip_file and @uri
      begin
        res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid)
        LOGGER.debug "Policy deleted for Dataset URI: #{@uri} with result: #{res}"
      rescue
        LOGGER.warn "Policy delete error for Dataset URI: #{@uri}"
      end
    end
    response['Content-Type'] = 'text/plain'
    "Dataset #{@id} deleted."
  rescue
    raise OpenTox::NotFoundError.new "Dataset #{@id} does not exist."
  end
end

# Delete all datasets
# @return [text/plain] Status message
delete '/?' do
  FileUtils.rm Dir["#{@@datadir}/*.rdfxml"]
  FileUtils.rm Dir["#{@@datadir}/*.xls"]
  FileUtils.rm Dir["#{@@datadir}/*.yaml"]
  FileUtils.rm Dir["#{@@datadir}/*.json"]
  response['Content-Type'] = 'text/plain'
  "All datasets deleted."
end