diff options
author | Christoph Helma <helma@in-silico.de> | 2009-07-20 10:14:54 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.de> | 2009-07-20 10:14:54 +0200 |
commit | 11418075d5ff21cd68e2188af7693d4d65b92b86 (patch) | |
tree | 65d4832c5217f4e4c81ea885741da82d1e8a1b4e |
Initial import of opentox-compounds-cactus
-rw-r--r-- | README | 13 | ||||
-rw-r--r-- | compounds.rb | 77 | ||||
-rw-r--r-- | test.rb | 28 |
3 files changed, 118 insertions, 0 deletions
@@ -0,0 +1,13 @@ +OpenTox Compounds +================= + +* A thin wrapper around the CACTUS REST webservice (http://cactus.nci.nih.gov/chemical/structure). + +* Implements a subset of the OpenTox compounds API (http://opentox.org/wiki/opentox/Structure). + +* Requirements: + + - Sinatra (http://www.sinatrarb.com/), + - sinatra-url-for (http://github.com/emk/sinatra-url-for/) + - RestClient (http://rest-client.heroku.com/) + - Rack::Test (http://github.com/brynary/rack-test/) for the tests diff --git a/compounds.rb b/compounds.rb new file mode 100644 index 0000000..a775442 --- /dev/null +++ b/compounds.rb @@ -0,0 +1,77 @@ +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 + "Cannot find an InChI Key for #{params[:name]}." + end +end + +delete '/*' do + status 404 + "Cannot delete - compounds are not stored permanently" +end @@ -0,0 +1,28 @@ +require 'compounds' +require 'test/unit' +require 'rack/test' + + +class CompoundsTest < Test::Unit::TestCase + include Rack::Test::Methods + + def app + Sinatra::Application + end + + def test_uri_generation + post '/', :name => 'c1ccccc1' + assert last_response.body.include?('InChIKey=UHOVQNZJYSORNB-UHFFFAOYSA-N') + end + + def test_smiles + get '/InChIKey=UHOVQNZJYSORNB-UHFFFAOYSA-N' + assert last_response.body.include?('c1ccccc1') + end + + def test_inchi + get '/Benzene.inchi' + assert last_response.body.include?('InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H') + end + +end |