diff options
author | Christoph Helma <helma@in-silico.ch> | 2011-02-24 12:51:15 +0100 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2011-02-24 12:51:15 +0100 |
commit | 825388a6f2fe5e4e9529f3a0b43805097455c2b1 (patch) | |
tree | f2f1f8ba9ca00b474cb7d22732d6a906e6cf58e0 | |
parent | 63ba9ff4305e71ac9f91299624f1acbee26ff327 (diff) | |
parent | 9bc9d1c5c11aa64d410200cc21d07acc39cc3019 (diff) |
Merge remote branch 'mguetlein/development' into development
-rw-r--r-- | lib/authorization.rb | 6 | ||||
-rw-r--r-- | lib/dataset.rb | 10 | ||||
-rw-r--r-- | lib/feature.rb | 10 | ||||
-rw-r--r-- | lib/helper.rb | 8 | ||||
-rw-r--r-- | lib/model.rb | 22 | ||||
-rw-r--r-- | lib/overwrite.rb | 6 | ||||
-rw-r--r-- | lib/parser.rb | 47 | ||||
-rw-r--r-- | lib/rest_client_wrapper.rb | 3 | ||||
-rwxr-xr-x | lib/to-html.rb | 99 |
9 files changed, 137 insertions, 74 deletions
diff --git a/lib/authorization.rb b/lib/authorization.rb index 1942e95..eab20df 100644 --- a/lib/authorization.rb +++ b/lib/authorization.rb @@ -334,15 +334,15 @@ module OpenTox true elsif CONFIG[:authorization][:authenticate_request].include?(request_method) ret = OpenTox::Authorization.is_token_valid(subjectid) - #LOGGER.debug "authorized? >>#{ret}<< (token is in/valid), method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" + LOGGER.debug "authorized? >>#{ret}<< (token is in/valid), method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" unless ret ret elsif OpenTox::Authorization.authorize_exception?(uri, request_method) ret = OpenTox::Authorization.is_token_valid(subjectid) - #LOGGER.debug "authorized? >>#{ret}<< (uris is authorize exception, token is in/valid), method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" + LOGGER.debug "authorized? >>#{ret}<< (uris is authorize exception, token is in/valid), method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" unless ret ret elsif CONFIG[:authorization][:authorize_request].include?(request_method) ret = OpenTox::Authorization.authorize(uri, request_method, subjectid) - LOGGER.debug "authorized? >>#{ret}<< (uri (not) authorized), method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" + LOGGER.debug "authorized? >>#{ret}<< (uri (not) authorized), method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" unless ret ret else LOGGER.error "invalid request/uri method: #{request_method}, URI: #{uri}, subjectid: #{subjectid}" diff --git a/lib/dataset.rb b/lib/dataset.rb index a843cea..2c47502 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -237,7 +237,7 @@ module OpenTox @features[feature] = {} unless @features[feature] @data_entries[compound] = {} unless @data_entries[compound] @data_entries[compound][feature] = [] unless @data_entries[compound][feature] - @data_entries[compound][feature] << value + @data_entries[compound][feature] << value if value!=nil end # Add/modify metadata, existing entries will be overwritten @@ -283,8 +283,12 @@ module OpenTox else compounds.each do |c| features.each do |f| - @data_entries[c][f].each do |v| - dataset.add(c,f,v) + unless @data_entries[c][f] + dataset.add(c,f,nil) + else + @data_entries[c][f].each do |v| + dataset.add(c,f,v) + end end end end diff --git a/lib/feature.rb b/lib/feature.rb index be063dd..c0729a7 100644 --- a/lib/feature.rb +++ b/lib/feature.rb @@ -16,8 +16,14 @@ module OpenTox # provides domain (possible target values) of classification feature # @return [Array] list with possible target values def domain - #TODO derieve from metadata / ontology - return [true, false] + if metadata[OT.acceptValue] + raise "accept value found, remove hack and implement correctly" + else + if @uri=~/feature\/26221/ || @uri=~/feature\/221726/ + return ["mutagen" , "nonmutagen"] + end + return [true, false] + end end # provides feature type, possible types are "regression" or "classification" diff --git a/lib/helper.rb b/lib/helper.rb index 191b932..b30908c 100644 --- a/lib/helper.rb +++ b/lib/helper.rb @@ -19,8 +19,6 @@ helpers do #Check Authorization for URI with method and subjectid. def authorized?(subjectid) - # hack for reports, address problem as soon as subjectid is not longer allowed as param - return true if request.env['REQUEST_URI'] =~ /validation\/report\/.*svg$/ request_method = request.env['REQUEST_METHOD'] uri = clean_uri("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}") request_method = "GET" if request_method == "POST" && uri =~ /\/model\/\d+\/?$/ @@ -34,8 +32,9 @@ helpers do uri = uri[0,uri.index("InChI=")] if uri.index("InChI=") out = URI.parse(uri) - out.path = out.path[0, out.path.length - (out.path.reverse.rindex(/\/{1}\d+\/{1}/))] if out.path.index(/\/{1}\d+\/{1}/) #cuts after /id/ for a&a - "#{out.scheme}:" + (out.port != 80 ? out.port : "") + "//#{out.host}#{out.path.chomp('/')}" + out.path = out.path[0, out.path.length - (out.path.reverse.rindex(/\/{1}\d+\/{1}/))] if out.path.index(/\/{1}\d+\/{1}/) #cuts after /id/ for a&a + port = (out.scheme=="http" && out.port==80)||(out.scheme=="https" && out.port==443) ? "" : ":#{out.port.to_s}" + "#{out.scheme}://#{out.host}#{port}#{out.path.chomp("/")}" #" end #unprotected uri for login @@ -52,6 +51,7 @@ before do subjectid = session[:subjectid] if session[:subjectid] subjectid = params[:subjectid] if params[:subjectid] and !subjectid subjectid = request.env['HTTP_SUBJECTID'] if request.env['HTTP_SUBJECTID'] and !subjectid + subjectid = request.cookies["subjectid"] unless subjectid # see http://rack.rubyforge.org/doc/SPEC.html subjectid = CGI.unescape(subjectid) if subjectid.include?("%23") @subjectid = subjectid diff --git a/lib/model.rb b/lib/model.rb index 9622d65..74408d8 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -38,24 +38,28 @@ module OpenTox # provides feature type, possible types are "regression" or "classification" # @return [String] feature type, "unknown" if type could not be estimated def feature_type(subjectid=nil) + return @feature_type if @feature_type + # dynamically perform restcalls if necessary load_metadata(subjectid) if @metadata==nil or @metadata.size==0 or (@metadata.size==1 && @metadata.values[0]==@uri) - - @algorithm = OpenTox::Algorithm::Generic.find(@metadata[OT.algorithm], subjectid) unless @algorithm - algorithm_title = @algorithm ? @algorithm.metadata[DC.title] : nil - algorithm_type = @algorithm ? @algorithm.metadata[OT.isA] : nil - @dependentVariable = OpenTox::Feature.find( @metadata[OT.dependentVariables],subjectid ) unless @dependentVariable - type_indicators = [@dependentVariable.feature_type, @metadata[OT.isA], @metadata[DC.title], + algorithm = OpenTox::Algorithm::Generic.find(@metadata[OT.algorithm], subjectid) + algorithm_title = algorithm ? algorithm.metadata[DC.title] : nil + algorithm_type = algorithm ? algorithm.metadata[OT.isA] : nil + dependent_variable = OpenTox::Feature.find( @metadata[OT.dependentVariables],subjectid ) + dependent_variable_type = dependent_variable ? dependent_variable.feature_type : nil + type_indicators = [dependent_variable_type, @metadata[OT.isA], @metadata[DC.title], @uri, algorithm_type, algorithm_title] type_indicators.each do |type| case type when /(?i)classification/ - return "classification" + @feature_type = "classification" + break when /(?i)regression/ - return "regression" + @feature_type = "regression" end end - raise "unknown model "+type_indicators.inspect + raise "unknown model "+type_indicators.inspect unless @feature_type + @feature_type end end diff --git a/lib/overwrite.rb b/lib/overwrite.rb index 7b53122..b5c3942 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -29,7 +29,7 @@ error Exception do halt error.http_code,rep.to_rdfxml when /html/ content_type 'text/html' - halt error.http_code,(OpenTox.text_to_html rep.to_yaml) + halt error.http_code,(OpenTox.text_to_html rep.to_yaml, @subjectid) else content_type 'application/x-yaml' halt error.http_code,rep.to_yaml @@ -45,11 +45,11 @@ class Sinatra::Base response['Content-Type'] = "application/rdf+xml" halt code,task.to_rdfxml when /yaml/ - response['Content-Type'] = "application/rdf+xml" + response['Content-Type'] = "application/x-yaml" halt code,task.to_yaml # PENDING differs from task-webservice when /html/ response['Content-Type'] = "text/html" - halt code,OpenTox.text_to_html(task.to_yaml) + halt code,OpenTox.text_to_html(task.to_yaml, @subjectid) else # default /uri-list/ response['Content-Type'] = "text/uri-list" halt code,task.uri+"\n" diff --git a/lib/parser.rb b/lib/parser.rb index d2beeac..f33017d 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -39,7 +39,14 @@ module OpenTox file = File.new(@uri) else file = Tempfile.new("ot-rdfxml") - uri = @dataset ? File.join(@uri,"metadata") : @uri + if @dataset + # do not concat /metadata to uri string, this would not work for dataset/R401577?max=3 + uri = URI::parse(@uri) + uri.path = File.join(uri.path,"metadata") + uri = uri.to_s + else + uri = @uri + end file.puts OpenTox::RestClientWrapper.get uri,{:subjectid => subjectid,:accept => "application/rdf+xml"},nil,false file.close to_delete = file.path @@ -163,26 +170,35 @@ module OpenTox data[triple[0]] = {:compound => "", :values => []} unless data[triple[0]] data[triple[0]][:compound] = triple[2] when /#{OT.feature}/i - feature[triple[0]] = triple[2] + feature[triple[0]] = triple[2] + when /#{RDF.type}/i + if triple[2]=~/#{OT.Compound}/i and !data[triple[0]] + data[triple[0]] = {:compound => triple[0], :values => []} + end else end end File.delete(to_delete) if to_delete data.each do |id,entry| - entry[:values].each do |value_id| - split = feature_values[value_id].split(/\^\^/) - case split[-1] - when XSD.double, XSD.float - value = split.first.to_f - when XSD.boolean - value = split.first=~/(?i)true/ ? true : false - else - value = split.first + if entry[:values].size==0 + # no feature values add plain compounds + @dataset.add_compound(entry[:compound]) + else + entry[:values].each do |value_id| + split = feature_values[value_id].split(/\^\^/) + case split[-1] + when XSD.double, XSD.float + value = split.first.to_f + when XSD.boolean + value = split.first=~/(?i)true/ ? true : false + else + value = split.first + end + @dataset.add entry[:compound],feature[value_id],value end - @dataset.add entry[:compound],feature[value_id],value end end - load_features + load_features subjectid @dataset.metadata = load_metadata(subjectid) @dataset end @@ -194,7 +210,10 @@ module OpenTox file = File.new(@uri) else file = Tempfile.new("ot-rdfxml") - uri = File.join(@uri,"features") + # do not concat /features to uri string, this would not work for dataset/R401577?max=3 + uri = URI::parse(@uri) + uri.path = File.join(uri.path,"features") + uri = uri.to_s file.puts OpenTox::RestClientWrapper.get uri,{:subjectid => subjectid,:accept => "application/rdf+xml"},nil,false file.close to_delete = file.path diff --git a/lib/rest_client_wrapper.rb b/lib/rest_client_wrapper.rb index d3136c7..dac24dc 100644 --- a/lib/rest_client_wrapper.rb +++ b/lib/rest_client_wrapper.rb @@ -76,7 +76,8 @@ module OpenTox else result = resource.send(rest_call, headers) end - + #LOGGER.debug "result body size: #{result.body.size}" + # PENDING NTUA does return errors with 200 raise RestClient::ExceptionWithResponse.new(result) if uri=~/ntua/ and result.body =~ /about.*http:\/\/anonymous.org\/error/ diff --git a/lib/to-html.rb b/lib/to-html.rb index 4de5ee6..1f30ca1 100755 --- a/lib/to-html.rb +++ b/lib/to-html.rb @@ -1,7 +1,6 @@ OT_LOGO = "http://opentox.informatik.uni-freiburg.de/ot-logo.png" - class String # encloses URI in text with with link tag @@ -25,29 +24,30 @@ module OpenTox # @param [optional,String] description general info # @param [optional,Array] post_params, array of arrays containing info on POST operation, see example # @return [String] html page - def self.text_to_html( text, related_links=nil, description=nil, post_params=nil ) + def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_params=nil ) # TODO add title as parameter title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra - - html = <<EOF -<html> -EOF - html.chomp! + html = "<html>" html += "<title>"+title+"</title>" if title - html += <<EOF -<img src=" -EOF - html.chomp! - html += OT_LOGO - html += <<EOF -"> -<body> -EOF - html.chomp! - html += "<h3>Description</h3><pre><p>"+description.link_urls+"</p></pre>" if description - html += "<h3>Related links</h3><pre><p>"+related_links.link_urls+"</p></pre>" if related_links - if post_params + html += "<img src="+OT_LOGO+"><body>" + + if AA_SERVER + user = OpenTox::Authorization.get_user(subjectid) if subjectid + html += "<pre><p align=\"right\">" + unless user + html += "You are currently not logged in to "+$url_provider.url_for("",:full)+ + ", <a href="+$url_provider.url_for("/login",:full)+">login</a>" + else + html += "You are logged in as '#{user}' to "+$url_provider.url_for("",:full)+ + ", <a href="+$url_provider.url_for("/logout",:full)+">logout</a>" + end + html += " </p></pre>" + end + + html += "<h3>Description</h3><pre><p>"+description.link_urls+"</p></pre>" if description + html += "<h3>Related links</h3><pre><p>"+related_links.link_urls+"</p></pre>" if related_links + if post_params html += "<h3>POST parameters</h3>" count = 0 post_params.each do |p| @@ -59,23 +59,52 @@ EOF html += "</table></p></pre>" count += 1 end - end - html += "<h3>Content</h3>" if description || related_links - html += <<EOF -<pre> -<p style="padding:15px; border:10px solid #5D308A"> -EOF - html.chomp! - html += text.link_urls - html += <<EOF -</p> -</pre> -</body> -<html> -EOF + end + html += "<h3>Content</h3>" if description || related_links + html += "<pre><p style=\"padding:15px; border:10px solid \#5D308A\">" + html += text.link_urls + html += "</p></pre></body><html>" html end + def self.login( msg=nil ) + html = "<html><title>Login</title><img src="+OT_LOGO+"><body>" + html += "<form method='POST' action='"+$url_provider.url_for("/login",:full)+"'>" + html += "<pre><p style=\"padding:15px; border:10px solid \#5D308A\">" + html += msg+"\n\n" if msg + html += "Please login to "+$url_provider.url_for("",:full)+"\n\n" + html += "<table border=0>" + html += "<tr><td>user:</td><td><input type='text' name='user' size='15' /></td></tr>"+ + "<tr><td>password:</td><td><input type='password' name='password' size='15' /></td></tr>"+ + #"<input type=hidden name=back_to value="+back_to.to_s+">"+ + "<tr><td><input type='submit' value='Login' /></td></tr>" + html += "</table></p></pre></form></body><html>" + html + end +end + +get '/logout/?' do + response.set_cookie("subjectid",{:value=>nil}) + content_type "text/html" + content = "Sucessfully logged out from "+$url_provider.url_for("",:full) + OpenTox.text_to_html(content) +end + +get '/login/?' do + content_type "text/html" + OpenTox.login +end + +post '/login/?' do + subjectid = OpenTox::Authorization.authenticate(params[:user], params[:password]) + if (subjectid) + response.set_cookie("subjectid",{:value=>subjectid}) + content_type "text/html" + content = "Sucessfully logged in as '"+params[:user]+"' to "+$url_provider.url_for("",:full) + OpenTox.text_to_html(content,subjectid) + else + content_type "text/html" + OpenTox.login("Login failed, please try again") + end end -#puts OpenTox.text_to_html("bla")
\ No newline at end of file |