summaryrefslogtreecommitdiff
path: root/compounds.rb
blob: 7ec4c857487b10b96cce6312f673e7f8dfed7ca4 (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
require 'rubygems'
require 'sinatra'
require 'rest_client'
require 'sinatra/url_for'

mime :smiles, "chemical/x-daylight-smiles"
mime :inchi, "chemical/x-inchi"
mime :sdf, "chemical/x-mdl-sdfile"
mime :image, "image/gif"
mime :names, "text/plain"

CACTUS_URI="http://cactus.nci.nih.gov/chemical/structure/"

get '/' do 
	status 404
	"No index available for this component."
end

get '/*.*' do
	begin
		identifier = params[:splat][0]
		format = params[:splat][1]
		case format 
		when 'smiles'
			RestClient.get "#{CACTUS_URI}#{identifier}/smiles"
		when 'inchikey'
			RestClient.get "#{CACTUS_URI}#{identifier}/stdinchikey"
		when 'inchi'
			RestClient.get "#{CACTUS_URI}#{identifier}/stdinchi"
		when 'sdf'
			RestClient.get "#{CACTUS_URI}#{identifier}/sdf"
		when 'names'
			RestClient.get "#{CACTUS_URI}#{identifier}/names"
		when 'image'
			RestClient.get "#{CACTUS_URI}#{identifier}/image"
		else
			status 400
			"Cannot provide #{format}."
		end
	rescue
		status 404
		"Cannot find #{identifier}."
	end
end

# TODO: select format by media type

# default format is smiles
get '/:identifier' do
	begin
		RestClient.get "#{CACTUS_URI}#{params[:identifier]}/smiles"
	rescue
		status 404
		"Cannot find #{params[:identifier]}."
	end
end

# return canonical uri
post '/' do
	begin
		inchikey = RestClient.get "#{CACTUS_URI}#{params[:name]}/stdinchikey"
		inchikey.chomp!
		if inchikey.match(/\n/)
			status 400
			"More than one structure found for #{params[:identifier]}."
		else
			url_for("/", :full) + inchikey
		end
	rescue
		status 500
		"Cannot find an InChI Key for #{params[:name]}."
	end
end

delete '/*' do
	status 404
	"Cannot delete - compounds are not stored permanently"
end