From 4c2470353a1e3b69b4260d0052c9c48137ef76d3 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 14 Dec 2010 12:30:20 +0100 Subject: remove token_id from tables in database --- lib/dataset.rb | 16 ++++++---------- lib/helper.rb | 2 +- lib/model.rb | 10 +++++----- lib/task.rb | 1 + lib/templates/config.yaml | 2 +- 5 files changed, 14 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/dataset.rb b/lib/dataset.rb index bbd8b8b..b7feeec 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -6,7 +6,6 @@ module OpenTox include OpenTox attr_reader :features, :compounds, :data_entries, :metadata - attr_accessor :token_id # Create dataset with optional URI. Does not load data into the dataset - you will need to execute one of the load_* methods to pull data from a service or to insert it from other representations. # @example Create an empty dataset @@ -15,9 +14,8 @@ module OpenTox # dataset = OpenTox::Dataset.new("http:://webservices.in-silico/ch/dataset/1") # @param [optional, String] uri Dataset URI # @return [OpenTox::Dataset] Dataset object - def initialize(uri=nil,token_id=nil) + def initialize(uri=nil) super uri - @token_id = token_id @features = {} @compounds = [] @data_entries = {} @@ -30,8 +28,7 @@ module OpenTox # @return [OpenTox::Dataset] Dataset object def self.create(uri=CONFIG[:services]["opentox-dataset"], token_id=nil) dataset = Dataset.new - dataset.token_id = token_id if token_id - dataset.save + dataset.save(token_id) dataset end @@ -250,22 +247,22 @@ module OpenTox # - creates a new dataset if uri is not set # - overwrites dataset if uri exists # @return [String] Dataset URI - def save + def save(token_id=nil) # TODO: rewrite feature URI's ?? @compounds.uniq! if @uri if (CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)) - RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :token_id => @token_id},self.to_yaml) + RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :token_id => token_id},self.to_yaml) else File.open("ot-post-file.rdf","w+") { |f| f.write(self.to_rdfxml); @path = f.path } - task_uri = RestClient.post(@uri, {:file => File.new(@path)},{:accept => "text/uri-list"}).to_s.chomp + task_uri = RestClient.post(@uri, {:file => File.new(@path)},{:accept => "text/uri-list" , :token_id => token_id}).to_s.chomp #task_uri = `curl -X POST -H "Accept:text/uri-list" -F "file=@#{@path};type=application/rdf+xml" http://apps.ideaconsult.net:8080/ambit2/dataset` Task.find(task_uri).wait_for_completion self.uri = RestClientWrapper.get(task_uri,:accept => 'text/uri-list') end else # create dataset if uri is empty - self.uri = RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:token_id => @token_id}).to_s.chomp + self.uri = RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:token_id => token_id}).to_s.chomp end @uri end @@ -282,7 +279,6 @@ module OpenTox @data_entries = dataset.data_entries @compounds = dataset.compounds @features = dataset.features - @token_id = dataset.token_id if @uri self.uri = @uri else diff --git a/lib/helper.rb b/lib/helper.rb index 6247460..42c35e8 100644 --- a/lib/helper.rb +++ b/lib/helper.rb @@ -32,7 +32,7 @@ helpers do def unprotected_requests case env['REQUEST_URI'] - when /\/login$|\/logout$|\/predict$|\/upload$/ + when /\/login$|\/logout$|\/predict$|\/toxcreate\/models$/ return true when /\/compound|\/feature|\/task|\/toxcreate/ #to fix: read from config | validation should be protected return true diff --git a/lib/model.rb b/lib/model.rb index 5dc4d4a..9c2fb97 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -111,7 +111,7 @@ module OpenTox # @param [String] compound_uri Compound URI # @param [optinal,Boolean] verbose Verbose prediction (output includes neighbors and features) # @return [OpenTox::Dataset] Dataset with prediction - def predict(compound_uri,verbose=false) + def predict(compound_uri,verbose=false,token_id=nil) @compound = Compound.new compound_uri features = {} @@ -119,7 +119,7 @@ module OpenTox unless @prediction_dataset #@prediction_dataset = cached_prediction #return @prediction_dataset if cached_prediction - @prediction_dataset = Dataset.create + @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], token_id) @prediction_dataset.add_metadata( { OT.hasSource => @uri, DC.creator => @uri, @@ -217,7 +217,7 @@ module OpenTox end end - @prediction_dataset.save + @prediction_dataset.save(token_id) @prediction_dataset end @@ -257,8 +257,8 @@ module OpenTox end # Save model at model service - def save - self.uri = RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :token_id => @token_id},self.to_yaml) + def save(token_id) + self.uri = RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :token_id => token_id},self.to_yaml) end # Delete model at model service diff --git a/lib/task.rb b/lib/task.rb index 17f95e6..18fba6e 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -49,6 +49,7 @@ module OpenTox cpu_load = `cat /proc/loadavg`.split(/\s+/)[0..2].collect{|c| c.to_f} nr_cpu_cores = `cat /proc/cpuinfo |grep "cpu cores"|cut -d ":" -f2|tr -d " "`.split("\n").collect{|c| c.to_i}.inject{|sum,n| sum+n} + nr_cpu_cores = 1 if !nr_cpu_cores if cpu_load[0] > nr_cpu_cores and cpu_load[0] > cpu_load[1] and cpu_load[1] > cpu_load[2] # average CPU load of the last minute is high and CPU load is increasing LOGGER.warn "Cannot start task - CPU load too high (#{cpu_load.join(", ")})" task.cancel diff --git a/lib/templates/config.yaml b/lib/templates/config.yaml index db11006..116f462 100644 --- a/lib/templates/config.yaml +++ b/lib/templates/config.yaml @@ -41,7 +41,7 @@ # :logger: debug # OpenSSO Authorization -# set ":server: nil" to disable A&A +# set ":server: " to disable A&A :authorization: :server: "https://opensso.in-silico.ch" :free_request: #not controlled by A&A -- cgit v1.2.3 From 2fb2f4cd34f499f8c9def5e4091cb5998794c595 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 14 Dec 2010 16:39:01 +0100 Subject: rename token_id to subjectid --- lib/authorization.rb | 132 +++++++++++++++++++++++++-------------------------- lib/dataset.rb | 12 ++--- lib/helper.rb | 40 ++++++++-------- lib/model.rb | 12 ++--- 4 files changed, 98 insertions(+), 98 deletions(-) (limited to 'lib') diff --git a/lib/authorization.rb b/lib/authorization.rb index 0cba96a..f9499e6 100644 --- a/lib/authorization.rb +++ b/lib/authorization.rb @@ -16,13 +16,13 @@ module OpenTox # OpenTox::Authorization.create_policy(xml,tok) class AA - attr_accessor :user, :token_id, :policy + attr_accessor :user, :subjectid, :policy - #Generates AA object - requires token_id - # @param [String] token_id - def initialize(token_id) - @user = Authorization.get_user(token_id) - @token_id = token_id + #Generates AA object - requires subjectid + # @param [String] subjectid + def initialize(subjectid) + @user = Authorization.get_user(subjectid) + @subjectid = subjectid @policy = Policies.new() end @@ -40,8 +40,8 @@ module OpenTox def send(uri) xml = get_xml(uri) ret = false - ret = Authorization.create_policy(xml, @token_id) - LOGGER.debug "Policy send with token_id: #{@token_id}" + ret = Authorization.create_policy(xml, @subjectid) + LOGGER.debug "Policy send with subjectid: #{@subjectid}" LOGGER.warn "Not created Policy is: #{xml}" if !ret ret end @@ -56,7 +56,7 @@ module OpenTox #Authentication against OpenSSO. Returns token. Requires Username and Password. # @param [String, String]Username,Password - # @return [String, nil] gives token_id or nil + # @return [String, nil] gives subjectid or nil def self.authenticate(user, pw) return true if !AA_SERVER begin @@ -69,12 +69,12 @@ module OpenTox end #Logout on opensso. Make token invalid. Requires token - # @param [String]token_id the token_id + # @param [String]subjectid the subjectid # @return [Boolean] true if logout is OK - def self.logout(token_id) + def self.logout(subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/auth/logout") - resource.post(:subjectid => token_id) + resource.post(:subjectid => subjectid) return true rescue return false @@ -82,38 +82,38 @@ module OpenTox end #Authorization against OpenSSO for a URI with request-method (action) [GET/POST/PUT/DELETE] - # @param [String,String,String]uri,action,token_id + # @param [String,String,String]uri,action,subjectid # @return [Boolean, nil] returns true, false or nil (if authorization-request fails). - def self.authorize(uri, action, token_id) + def self.authorize(uri, action, subjectid) return true if !AA_SERVER begin resource = RestClient::Resource.new("#{AA_SERVER}/auth/authorize") - return true if resource.post(:uri => uri, :action => action, :subjectid => token_id) == "boolean=true\n" + return true if resource.post(:uri => uri, :action => action, :subjectid => subjectid) == "boolean=true\n" rescue return nil end end #Checks if a token is a valid token - # @param [String]token_id token_id from openSSO session - # @return [Boolean] token_id is valid or not. - def self.is_token_valid(token_id) + # @param [String]subjectid subjectid from openSSO session + # @return [Boolean] subjectid is valid or not. + def self.is_token_valid(subjectid) return true if !AA_SERVER begin resource = RestClient::Resource.new("#{AA_SERVER}/auth/isTokenValid") - return true if resource.post(:tokenid => token_id) == "boolean=true\n" + return true if resource.post(:tokenid => subjectid) == "boolean=true\n" rescue return false end end #Returns array with all policies of the token owner - # @param [String]token_id requires token_id + # @param [String]subjectid requires subjectid # @return [Array, nil] returns an Array of policy names or nil if request fails - def self.list_policies(token_id) + def self.list_policies(subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/pol") - out = resource.get(:subjectid => token_id) + out = resource.get(:subjectid => subjectid) return out.split("\n") rescue return nil @@ -121,45 +121,45 @@ module OpenTox end #Returns a policy in xml-format - # @param [String, String]policy,token_id + # @param [String, String]policy,subjectid # @return [String] XML of the policy - def self.list_policy(policy, token_id) + def self.list_policy(policy, subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/pol") - return resource.get(:subjectid => token_id,:id => policy) + return resource.get(:subjectid => subjectid,:id => policy) rescue return nil end end #Returns the owner (who created the first policy) of an URI - # @param [String, String]uri,token_id + # @param [String, String]uri,subjectid # return [String, nil]owner,nil returns owner of the URI - def self.get_uri_owner(uri, token_id) + def self.get_uri_owner(uri, subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/pol") - return resource.get(:uri => uri, :subjectid => token_id).sub("\n","") + return resource.get(:uri => uri, :subjectid => subjectid).sub("\n","") rescue return nil end end #Checks if a policy exists to a URI. Requires URI and token. - # @param [String, String]uri,token_id + # @param [String, String]uri,subjectid # return [Boolean] - def self.uri_has_policy(uri, token_id) - owner = get_uri_owner(uri, token_id) + def self.uri_has_policy(uri, subjectid) + owner = get_uri_owner(uri, subjectid) return true if owner and owner != "null" false end #List all policynames for a URI. Requires URI and token. - # @param [String, String]uri,token_id + # @param [String, String]uri,subjectid # return [Array, nil] returns an Array of policy names or nil if request fails - def self.list_uri_policies(uri, token_id) + def self.list_uri_policies(uri, subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/pol") - out = resource.get(:uri => uri, :polnames => true, :subjectid => token_id) + out = resource.get(:uri => uri, :polnames => true, :subjectid => subjectid) policies = []; notfirstline = false out.split("\n").each do |line| policies << line if notfirstline @@ -172,39 +172,39 @@ module OpenTox end #Sends a policy in xml-format to opensso server. Requires policy-xml and token. - # @param [String, String]policyxml,token_id + # @param [String, String]policyxml,subjectid # return [Boolean] returns true if policy is created - def self.create_policy(policy, token_id) + def self.create_policy(policy, subjectid) begin # resource = RestClient::Resource.new("#{AA_SERVER}/Pol/opensso-pol") - LOGGER.debug "OpenTox::Authorization.create_policy policy: #{policy[168,43]} with token:" + token_id.to_s + " length: " + token_id.length.to_s -# return true if resource.post(policy, :subjectid => token_id, :content_type => "application/xml") - return true if RestClientWrapper.post("#{AA_SERVER}/pol", {:subjectid => token_id, :content_type => "application/xml"}, policy) + LOGGER.debug "OpenTox::Authorization.create_policy policy: #{policy[168,43]} with token:" + subjectid.to_s + " length: " + subjectid.length.to_s +# return true if resource.post(policy, :subjectid => subjectid, :content_type => "application/xml") + return true if RestClientWrapper.post("#{AA_SERVER}/pol", {:subjectid => subjectid, :content_type => "application/xml"}, policy) rescue return false end end #Deletes a policy - # @param [String, String]policyname,token_id + # @param [String, String]policyname,subjectid # @return [Boolean,nil] - def self.delete_policy(policy, token_id) + def self.delete_policy(policy, subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/pol") - LOGGER.debug "OpenTox::Authorization.delete_policy policy: #{policy} with token: #{token_id}" - return true if resource.delete(:subjectid => token_id, :id => policy) + LOGGER.debug "OpenTox::Authorization.delete_policy policy: #{policy} with token: #{subjectid}" + return true if resource.delete(:subjectid => subjectid, :id => policy) rescue return nil end end #Returns array of all possible LDAP-Groups - # @param [String]token_id + # @param [String]subjectid # @return [Array] - def self.list_groups(token_id) + def self.list_groups(subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/opensso/identity/search") - grps = resource.post(:admin => token_id, :attributes_names => "objecttype", :attributes_values_objecttype => "group") + grps = resource.post(:admin => subjectid, :attributes_names => "objecttype", :attributes_values_objecttype => "group") grps.split("\n").collect{|x| x.sub("string=","")} rescue [] @@ -212,12 +212,12 @@ module OpenTox end #Returns array of the LDAP-Groups of an user - # @param [String]token_id + # @param [String]subjectid # @return [Array] gives array of LDAP groups of a user - def self.list_user_groups(user, token_id) + def self.list_user_groups(user, subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/opensso/identity/read") - out = resource.post(:name => user, :admin => token_id, :attributes_names => "group") + out = resource.post(:name => user, :admin => subjectid, :attributes_names => "group") grps = [] out.split("\n").each do |line| grps << line.sub("identitydetails.group=","") if line.include?("identitydetails.group=") @@ -229,12 +229,12 @@ module OpenTox end #Returns the owner (user id) of a token - # @param [String]token_id + # @param [String]subjectid # @return [String]user - def self.get_user(token_id) + def self.get_user(subjectid) begin resource = RestClient::Resource.new("#{AA_SERVER}/opensso/identity/attributes") - out = resource.post(:subjectid => token_id, :attributes_names => "uid") + out = resource.post(:subjectid => subjectid, :attributes_names => "uid") user = ""; check = false out.split("\n").each do |line| if check @@ -250,34 +250,34 @@ module OpenTox end #Send default policy with Authorization::AA class - # @param [String, String]URI,token_id - def self.send_policy(uri, token_id) + # @param [String, String]URI,subjectid + def self.send_policy(uri, subjectid) return true if !AA_SERVER - aa = Authorization::AA.new(token_id) + aa = Authorization::AA.new(subjectid) ret = aa.send(uri) - LOGGER.debug "OpenTox::Authorization send policy for URI: #{uri} | token_id: #{token_id} - policy created: #{ret}" + LOGGER.debug "OpenTox::Authorization send policy for URI: #{uri} | subjectid: #{subjectid} - policy created: #{ret}" ret end #Deletes all policies of an URI - # @param [String, String]URI,token_id + # @param [String, String]URI,subjectid # @return [Boolean] - def self.delete_policies_from_uri(uri, token_id) - policies = list_uri_policies(uri, token_id) + def self.delete_policies_from_uri(uri, subjectid) + policies = list_uri_policies(uri, subjectid) policies.each do |policy| - ret = delete_policy(policy, token_id) + ret = delete_policy(policy, subjectid) LOGGER.debug "OpenTox::Authorization delete policy: #{policy} - with result: #{ret}" end return true end - #Checks (if token_id is valid) if a policy exist and create default policy if not - def self.check_policy(uri, token_id) - token_valid = OpenTox::Authorization.is_token_valid(token_id) - LOGGER.debug "OpenTox::Authorization.check_policy with uri: #{uri}, token_id: #{token_id} is valid: #{token_valid}" + #Checks (if subjectid is valid) if a policy exist and create default policy if not + def self.check_policy(uri, subjectid) + token_valid = OpenTox::Authorization.is_token_valid(subjectid) + LOGGER.debug "OpenTox::Authorization.check_policy with uri: #{uri}, subjectid: #{subjectid} is valid: #{token_valid}" if uri and token_valid - if !uri_has_policy(uri, token_id) - return send_policy(uri, token_id) + if !uri_has_policy(uri, subjectid) + return send_policy(uri, subjectid) else LOGGER.debug "OpenTox::Authorization.check_policy URI: #{uri} has already a Policy." end diff --git a/lib/dataset.rb b/lib/dataset.rb index b7feeec..7c70c9d 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -26,9 +26,9 @@ module OpenTox # dataset = OpenTox::Dataset.create # @param [optional, String] uri Dataset URI # @return [OpenTox::Dataset] Dataset object - def self.create(uri=CONFIG[:services]["opentox-dataset"], token_id=nil) + def self.create(uri=CONFIG[:services]["opentox-dataset"], subjectid=nil) dataset = Dataset.new - dataset.save(token_id) + dataset.save(subjectid) dataset end @@ -247,22 +247,22 @@ module OpenTox # - creates a new dataset if uri is not set # - overwrites dataset if uri exists # @return [String] Dataset URI - def save(token_id=nil) + def save(subjectid=nil) # TODO: rewrite feature URI's ?? @compounds.uniq! if @uri if (CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)) - RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :token_id => token_id},self.to_yaml) + RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :subjectid => subjectid},self.to_yaml) else File.open("ot-post-file.rdf","w+") { |f| f.write(self.to_rdfxml); @path = f.path } - task_uri = RestClient.post(@uri, {:file => File.new(@path)},{:accept => "text/uri-list" , :token_id => token_id}).to_s.chomp + task_uri = RestClient.post(@uri, {:file => File.new(@path)},{:accept => "text/uri-list" , :subjectid => subjectid}).to_s.chomp #task_uri = `curl -X POST -H "Accept:text/uri-list" -F "file=@#{@path};type=application/rdf+xml" http://apps.ideaconsult.net:8080/ambit2/dataset` Task.find(task_uri).wait_for_completion self.uri = RestClientWrapper.get(task_uri,:accept => 'text/uri-list') end else # create dataset if uri is empty - self.uri = RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:token_id => token_id}).to_s.chomp + self.uri = RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:subjectid => subjectid}).to_s.chomp end @uri end diff --git a/lib/helper.rb b/lib/helper.rb index 42c35e8..6b616bc 100644 --- a/lib/helper.rb +++ b/lib/helper.rb @@ -1,32 +1,32 @@ helpers do # Authentification - def protected!(token_id) + def protected!(subjectid) if env["session"] flash[:notice] = "You don't have access to this section: " and \ redirect back and \ - return unless authorized?(token_id) - elsif !env["session"] && token_id + return unless authorized?(subjectid) + elsif !env["session"] && subjectid throw(:halt, [401, "Not authorized.\n"]) and \ redirect back and \ - return unless authorized?(token_id) + return unless authorized?(subjectid) end throw(:halt, [401, "Not authorized.\n"]) and \ - return unless authorized?(token_id) + return unless authorized?(subjectid) end - def authorized?(token_id) + def authorized?(subjectid) if CONFIG[:authorization][:authorize_request].include?(request.env['REQUEST_METHOD']) - ret = OpenTox::Authorization.authorize("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}", request.env['REQUEST_METHOD'], token_id) - LOGGER.debug "OpenTox helpers OpenTox::Authorization authorized? method: #{request.env['REQUEST_METHOD']} , URI: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}, token_id: #{token_id} with return #{ret}." + ret = OpenTox::Authorization.authorize("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}", request.env['REQUEST_METHOD'], subjectid) + LOGGER.debug "OpenTox helpers OpenTox::Authorization authorized? method: #{request.env['REQUEST_METHOD']} , URI: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}, subjectid: #{subjectid} with return #{ret}." return ret end if CONFIG[:authorization][:authenticate_request].include?(env['REQUEST_METHOD']) - if OpenTox::Authorization.is_token_valid(token_id) + if OpenTox::Authorization.is_token_valid(subjectid) return true end end - LOGGER.debug "Not authorized for: #{request.env['rack.url_scheme']}://#{request['REQUEST_URI']} with Method: #{request.env['REQUEST_METHOD']} with Token #{token_id}" + LOGGER.debug "Not authorized for: #{request.env['rack.url_scheme']}://#{request['REQUEST_URI']} with Method: #{request.env['REQUEST_METHOD']} with Token #{subjectid}" return false end @@ -41,9 +41,9 @@ helpers do end end - def check_token_id(token_id) - return false if !token_id - return true if token_id.size > 62 + def check_subjectid(subjectid) + return false if !subjectid + return true if subjectid.size > 62 false end end @@ -51,16 +51,16 @@ end before do unless unprotected_requests or CONFIG[:authorization][:free_request].include?(env['REQUEST_METHOD']) begin - token_id = session[:token_id] if session[:token_id] - token_id = params[:token_id] if params[:token_id] and !check_token_id(token_id) - token_id = request.env['HTTP_TOKEN_ID'] if request.env['HTTP_TOKEN_ID'] and !check_token_id(token_id) + subjectid = session[:subjectid] if session[:subjectid] + subjectid = params[:subjectid] if params[:subjectid] and !check_subjectid(subjectid) + subjectid = request.env['HTTP_SUBJECTID'] if request.env['HTTP_SUBJECTID'] and !check_subjectid(subjectid) # see http://rack.rubyforge.org/doc/SPEC.html - token_id = CGI.unescape(token_id) if token_id.include?("%23") + subjectid = CGI.unescape(subjectid) if subjectid.include?("%23") rescue - LOGGER.debug "OpenTox ruby api wrapper: helper before filter: NO token_id." - token_id = "" + LOGGER.debug "OpenTox ruby api wrapper: helper before filter: NO subjectid." + subjectid = "" end - protected!(token_id) if AA_SERVER + protected!(subjectid) if AA_SERVER end end diff --git a/lib/model.rb b/lib/model.rb index 9c2fb97..953bb6c 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -32,7 +32,7 @@ module OpenTox include Model include Algorithm - attr_accessor :compound, :prediction_dataset, :features, :effects, :activities, :p_values, :fingerprints, :feature_calculation_algorithm, :similarity_algorithm, :prediction_algorithm, :min_sim, :token_id + attr_accessor :compound, :prediction_dataset, :features, :effects, :activities, :p_values, :fingerprints, :feature_calculation_algorithm, :similarity_algorithm, :prediction_algorithm, :min_sim, :subjectid def initialize(uri=nil) @@ -111,7 +111,7 @@ module OpenTox # @param [String] compound_uri Compound URI # @param [optinal,Boolean] verbose Verbose prediction (output includes neighbors and features) # @return [OpenTox::Dataset] Dataset with prediction - def predict(compound_uri,verbose=false,token_id=nil) + def predict(compound_uri,verbose=false,subjectid=nil) @compound = Compound.new compound_uri features = {} @@ -119,7 +119,7 @@ module OpenTox unless @prediction_dataset #@prediction_dataset = cached_prediction #return @prediction_dataset if cached_prediction - @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], token_id) + @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid) @prediction_dataset.add_metadata( { OT.hasSource => @uri, DC.creator => @uri, @@ -217,7 +217,7 @@ module OpenTox end end - @prediction_dataset.save(token_id) + @prediction_dataset.save(subjectid) @prediction_dataset end @@ -257,8 +257,8 @@ module OpenTox end # Save model at model service - def save(token_id) - self.uri = RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :token_id => token_id},self.to_yaml) + def save(subjectid) + self.uri = RestClientWrapper.post(@uri,{:content_type => "application/x-yaml", :subjectid => subjectid},self.to_yaml) end # Delete model at model service -- cgit v1.2.3 From b22110ae0d8e902d700e0a3dc629ebfde1edfe10 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 16 Dec 2010 10:59:46 +0100 Subject: A&A --- lib/dataset.rb | 18 +++++++++--------- lib/helper.rb | 2 +- lib/model.rb | 18 +++++++++--------- 3 files changed, 19 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/dataset.rb b/lib/dataset.rb index 7c70c9d..aba7754 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -38,12 +38,12 @@ module OpenTox # - you will have to set remaining metadata manually # @param [String] file CSV file path # @return [OpenTox::Dataset] Dataset object with CSV data - def self.create_from_csv_file(file) - dataset = Dataset.create + def self.create_from_csv_file(file, subjectid=nil) + dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid) parser = Parser::Spreadsheets.new parser.dataset = dataset parser.load_csv(File.open(file).read) - dataset.save + dataset.save(subjectid) dataset end @@ -89,8 +89,8 @@ module OpenTox # - you will have to set remaining metadata manually # @param [String] csv CSV representation of the dataset # @return [OpenTox::Dataset] Dataset object with CSV data - def load_csv(csv) - save unless @uri # get a uri for creating features + def load_csv(csv, subjectid=nil) + save(subjectid) unless @uri # get a uri for creating features parser = Parser::Spreadsheets.new parser.dataset = self parser.load_csv(csv) @@ -102,8 +102,8 @@ module OpenTox # - you will have to set remaining metadata manually # @param [Excel] book Excel workbook object (created with roo gem) # @return [OpenTox::Dataset] Dataset object with Excel data - def load_spreadsheet(book) - save unless @uri # get a uri for creating features + def load_spreadsheet(book, subjectid=nil) + save(subjectid) unless @uri # get a uri for creating features parser = Parser::Spreadsheets.new parser.dataset = self parser.load_spreadsheet(book) @@ -268,8 +268,8 @@ module OpenTox end # Delete dataset at the dataset service - def delete - RestClientWrapper.delete @uri + def delete(subjectid=nil) + RestClientWrapper.delete(@uri, :subjectid => subjectid) end private diff --git a/lib/helper.rb b/lib/helper.rb index 6b616bc..965b4ad 100644 --- a/lib/helper.rb +++ b/lib/helper.rb @@ -57,7 +57,7 @@ before do # see http://rack.rubyforge.org/doc/SPEC.html subjectid = CGI.unescape(subjectid) if subjectid.include?("%23") rescue - LOGGER.debug "OpenTox ruby api wrapper: helper before filter: NO subjectid." + LOGGER.debug "OpenTox ruby api wrapper: helper before filter: NO subjectid for URI: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}" subjectid = "" end protected!(subjectid) if AA_SERVER diff --git a/lib/model.rb b/lib/model.rb index 953bb6c..c645bdc 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -90,8 +90,8 @@ module OpenTox # Predict a dataset # @param [String] dataset_uri Dataset URI # @return [OpenTox::Dataset] Dataset with predictions - def predict_dataset(dataset_uri) - @prediction_dataset = Dataset.create + def predict_dataset(dataset_uri, subjectid=nil) + @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid) @prediction_dataset.add_metadata({ OT.hasSource => @uri, DC.creator => @uri, @@ -101,9 +101,9 @@ module OpenTox d = Dataset.new(dataset_uri) d.load_compounds d.compounds.each do |compound_uri| - predict(compound_uri,false) + predict(compound_uri,false,subjectid) end - @prediction_dataset.save + @prediction_dataset.save(subjectid) @prediction_dataset end @@ -129,7 +129,7 @@ module OpenTox } ) end - return @prediction_dataset if database_activity + return @prediction_dataset if database_activity(subjectid) neighbors prediction = eval("#{@prediction_algorithm}(@neighbors,{:similarity_algorithm => @similarity_algorithm, :p_values => @p_values})") @@ -245,11 +245,11 @@ module OpenTox # Find database activities and store them in @prediction_dataset # @return [Boolean] true if compound has databasse activities, false if not - def database_activity + def database_activity(subjectid) if @activities[@compound.uri] @activities[@compound.uri].each { |act| @prediction_dataset.add @compound.uri, @metadata[OT.dependentVariables], act } @prediction_dataset.add_metadata(OT.hasSource => @metadata[OT.trainingDataset]) - @prediction_dataset.save + @prediction_dataset.save(subjectid) true else false @@ -262,8 +262,8 @@ module OpenTox end # Delete model at model service - def delete - RestClientWrapper.delete @uri unless @uri == CONFIG[:services]["opentox-model"] + def delete(subjectid) + RestClientWrapper.delete(@uri, :subjectid => subjectid) unless @uri == CONFIG[:services]["opentox-model"] end end -- cgit v1.2.3