summaryrefslogtreecommitdiff
path: root/application.rb
blob: 6518ea862145d3a1728340674df7a154d1af93c4 (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
require 'rubygems'
gem "opentox-ruby", "~> 0"
require 'opentox-ruby'

class Dataset

  include DataMapper::Resource
  property :id, Serial
  property :uri, String, :length => 255
  property :yaml, Text, :length => 2**32-1 
  property :created_at, DateTime

  def load(params,request)

    data = request.env["rack.input"].read
    content_type = request.content_type
    content_type = "application/rdf+xml" if content_type.nil?
    dataset = OpenTox::Dataset.new

    case content_type

    when /yaml/
      dataset.load_yaml(data)

    when "application/rdf+xml"
      dataset.load_rdfxml(data)

    when /multipart\/form-data/ # file uploads

      case params[:file][:type]

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

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

      when "text/csv"
        dataset = OpenTox::Dataset.new @uri
        dataset.load_csv(params[:file][:tempfile].read)
        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)
        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]}
    update(:yaml => dataset.to_yaml)
  end

=begin
  def create_representations
    dataset = YAML.load yaml
    ["rdfxml","xls"].each do |extension|
      file = "public/#{@id}.#{extension}"
      FileUtils.rm Dir["public/#{file}"] if File.exists? file
      File.open(file,"w+") { |f| f.puts eval("dataset.to_#{extension}") }
    end
  end
=end

end

DataMapper.auto_upgrade!

before do
  @accept = request.env['HTTP_ACCEPT']
  @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil?
end

## REST API

# Get a list of available datasets
# @return [text/uri-list] List of available datasets
get '/?' do
  response['Content-Type'] = 'text/uri-list'
  Dataset.all(params).collect{|d| d.uri}.join("\n") + "\n"
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

  extension = File.extname(params[:id]).sub(/\./,'')
  unless extension.empty?
    params[:id].sub!(/\.#{extension}$/,'')
    case extension
    when "yaml"
      @accept = 'application/x-yaml'
    when "csv"
      @accept = 'text/csv'
    when "rdfxml"
      @accept = 'application/rdf+xml'
    when "xls"
      @accept = 'application/ms-excel'
    else
      halt 404, "File format #{extension} not supported."
    end
  end

  dataset = OpenTox::Dataset.new
  dataset.load_yaml(Dataset.get(params[:id]).yaml)
  halt 404, "Dataset #{params[:id]} empty." if dataset.nil? # not sure how an empty dataset can be returned, but if this happens stale processes keep runing at 100% cpu
  
  case @accept

  when /rdf/ # redland sends text/rdf instead of application/rdf+xml
    file = "public/#{params[:id]}.rdfxml"
    File.open(file,"w+") { |f| f.puts dataset.to_rdfxml } unless File.exists? file # lazy rdfxml generation
    response['Content-Type'] = 'application/rdf+xml'
    File.open(file).read

  when /yaml/
    response['Content-Type'] = 'application/x-yaml'
    dataset.to_yaml
 
  when "text/csv"
    response['Content-Type'] = 'text/csv'
    dataset.to_csv

  when /ms-excel/
    file = "public/#{params[:id]}.xls"
    dataset.to_xls.write(file) unless File.exists? file # lazy xls generation
    response['Content-Type'] = 'application/ms-excel'
    File.open(file).read

  else
    halt 404, "Content-type #{@accept} not supported."
  end
end

# Get metadata of the dataset
# @return [application/rdf+xml] Metadata OWL-DL
get '/:id/metadata' do

  metadata = YAML.load(Dataset.get(params[:id]).yaml).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 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|
#get '/:id/feature/:feature_name/?' do 

  #feature_uri = url_for("/#{params[:id]}/feature/#{URI.encode(params[:feature_name])}",:full) # work around  racks internal uri decoding 
  #dataset = YAML.load(Dataset.get(params[:id]).yaml)
  feature_uri = url_for("/#{id}/feature/#{URI.encode(feature)}",:full) # work around  racks internal uri decoding 
  dataset = YAML.load(Dataset.get(id).yaml)
  metadata = 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
  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 = YAML.load(Dataset.get(params[:id]).yaml).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 "text/uri-list"
    response['Content-Type'] = 'text/uri-list'
    YAML.load(Dataset.get(params[:id]).yaml).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'
  YAML.load(Dataset.get(params[:id]).yaml).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 
  @dataset = Dataset.create
  response['Content-Type'] = 'text/uri-list'
  @dataset.update(:uri => url_for("/#{@dataset.id}", :full))
  if params.empty? and request.env["rack.input"].read.empty?
    @dataset.update(:yaml => OpenTox::Dataset.new(@dataset.uri).to_yaml)
    @dataset.uri
  else
    task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do 
      @dataset.load params, request 
      @dataset.uri
    end
    halt 503,task.uri+"\n" if task.status == "Cancelled"
    halt 202,task.uri+"\n"
  end
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 
  @dataset = Dataset.get(params[:id])
  halt 404, "Dataset #{params[:id]} not found." unless @dataset
  response['Content-Type'] = 'text/uri-list'
  task = OpenTox::Task.create("Converting and saving dataset ", @dataset.uri) do 
    @dataset.load params, request 
    FileUtils.rm Dir["public/#{params[:id]}.*"]
    @dataset.uri
  end
  halt 503,task.uri+"\n" if task.status == "Cancelled"
  halt 202,task.uri.to_s+"\n"
end

# Delete a dataset
# @return [text/plain] Status message
delete '/:id' do
  begin
    dataset = Dataset.get(params[:id])
    FileUtils.rm Dir["public/#{params[:id]}.*"]
    dataset.destroy!
    response['Content-Type'] = 'text/plain'
    "Dataset #{params[:id]} deleted."
  rescue
    halt 404, "Dataset #{params[:id]} does not exist."
  end
end

# Delete all datasets
# @return [text/plain] Status message
delete '/?' do
  FileUtils.rm Dir["public/*.rdfxml"]
  FileUtils.rm Dir["public/*.xls"]
  Dataset.auto_migrate!
  response['Content-Type'] = 'text/plain'
  "All datasets deleted."
end