diff options
author | Christoph Helma <helma@in-silico.ch> | 2011-08-25 17:36:11 +0000 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2011-08-25 17:36:11 +0000 |
commit | 446edfce4bce7b461b0b231f13e53203345becfd (patch) | |
tree | b27ae165833e1aadab5b0d67dd0d29ef0e27c1bb | |
parent | 64ab23142ffb068c8b901c73ec227ca484d9efb6 (diff) | |
parent | b4e4ac6a6eee4d05144de7447091e27f624325d4 (diff) |
Merge branch 'feature/json' into development
-rw-r--r-- | lib/dataset.rb | 35 | ||||
-rw-r--r-- | lib/model.rb | 45 |
2 files changed, 70 insertions, 10 deletions
diff --git a/lib/dataset.rb b/lib/dataset.rb index 4102e18..8061153 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -46,6 +46,12 @@ module OpenTox dataset.save(subjectid) dataset end + + def self.from_json(json, subjectid=nil) + dataset = Dataset.new(nil,subjectid) + dataset.copy_hash Yajl::Parser.parse(json) + dataset + end # Find a dataset and load all data. This can be time consuming, use Dataset.new together with one of the load_* methods for a fine grained control over data loading. # @param [String] uri Dataset URI @@ -84,6 +90,10 @@ module OpenTox copy YAML.load(yaml) end + def load_json(json) + copy_hash Yajl::Parser.parse(json) + end + def load_rdfxml(rdfxml, subjectid=nil) raise "rdfxml data is empty" if rdfxml.to_s.size==0 file = Tempfile.new("ot-rdfxml") @@ -146,7 +156,7 @@ module OpenTox # Load all data (metadata, data_entries, compounds and features) from URI def load_all(subjectid=nil) if (CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)) - copy YAML.load(RestClientWrapper.get(@uri, {:accept => "application/x-yaml", :subjectid => subjectid})) + copy_hash Yajl::Parser.parse(RestClientWrapper.get(@uri, {:accept => "application/json", :subjectid => subjectid})) else parser = Parser::Owl::Dataset.new(@uri, subjectid) copy parser.load_uri(subjectid) @@ -170,7 +180,7 @@ module OpenTox # @return [Hash] Features of the dataset def load_features(subjectid=nil) if (CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)) - @features = YAML.load(RestClientWrapper.get(File.join(@uri,"features"), {:accept => "application/x-yaml", :subjectid => subjectid})) + @features = Yajl::Parser.parse(RestClientWrapper.get(File.join(@uri,"features"), {:accept => "application/json", :subjectid => subjectid})) else parser = Parser::Owl::Dataset.new(@uri, subjectid) @features = parser.load_features(subjectid) @@ -203,6 +213,10 @@ module OpenTox =begin =end + def to_json + Yajl::Encoder.encode({:uri => @uri, :metadata => @metadata, :data_entries => @data_entries, :compounds => @compounds, :features => @features}) + end + # Get Spreadsheet representation # @return [Spreadsheet::Workbook] Workbook which can be written with the spreadsheet gem (data_entries only, metadata will will be discarded)) def to_spreadsheet @@ -359,11 +373,11 @@ module OpenTox @compounds.uniq! if @uri if (CONFIG[:yaml_hosts].include?(URI.parse(@uri).host)) - RestClientWrapper.post(@uri,self.to_yaml,{:content_type => "application/x-yaml", :subjectid => subjectid}) + #LOGGER.debug self.to_json + RestClientWrapper.post(@uri,self.to_json,{:content_type => "application/json", :subjectid => subjectid}) 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" , :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', :subjectid => subjectid}) end @@ -379,6 +393,19 @@ module OpenTox RestClientWrapper.delete(@uri, :subjectid => subjectid) end + # Copy a hash (eg. from JSON) into a dataset (rewrites URI) + def copy_hash(hash) + @metadata = hash["metadata"] + @data_entries = hash["data_entries"] + @compounds = hash["compounds"] + @features = hash["features"] + if @uri + self.uri = @uri + else + @uri = hash["metadata"][XSD.anyURI] + end + end + private # Copy a dataset (rewrites URI) def copy(dataset) diff --git a/lib/model.rb b/lib/model.rb index e9e0b09..42e5b92 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -11,7 +11,7 @@ module OpenTox def run( params, accept_header=nil, waiting_task=nil ) unless accept_header if CONFIG[:yaml_hosts].include?(URI.parse(@uri).host) - accept_header = 'application/x-yaml' + accept_header = 'application/json' else accept_header = 'application/rdf+xml' end @@ -144,7 +144,7 @@ module OpenTox # @param [String] uri Model URI # @return [OpenTox::Model::Lazar] lazar model def self.find(uri, subjectid=nil) - YAML.load RestClientWrapper.get(uri,{:accept => 'application/x-yaml', :subjectid => subjectid}) + OpenTox::Model::Lazar.from_json RestClientWrapper.get(uri,{:accept => 'application/json', :subjectid => subjectid}) end # Create a new lazar model @@ -157,10 +157,42 @@ module OpenTox OpenTox::Model::Lazar.find(model_uri, subjectid) end + def self.from_json(json) + hash = Yajl::Parser.parse(json) + #LOGGER.debug hash.to_yaml + lazar = OpenTox::Model::Lazar.new + #hash.each { |k,v| eval("lazar.#{k} = #{v}") } + lazar.uri = hash["uri"] if hash["uri"] + lazar.metadata = hash["metadata"] if hash["metadata"] + lazar.compound = hash["compound"] if hash["compound"] + lazar.prediction_dataset = hash["prediction_dataset"] if hash["prediction_dataset"] + lazar.features = hash["features"] if hash["features"] + lazar.effects = hash["effects"] if hash["effects"] + lazar.activities = hash["activities"] if hash["activities"] + lazar.p_values = hash["p_values"] if hash["p_values"] + lazar.fingerprints = hash["fingerprints"] if hash["fingerprints"] + lazar.feature_calculation_algorithm = hash["feature_calculation_algorithm"] if hash["feature_calculation_algorithm"] + lazar.similarity_algorithm = hash["similarity_algorithm"] if hash["similarity_algorithm"] + lazar.prediction_algorithm = hash["prediction_algorithm"] if hash["prediction_algorithm"] + lazar.min_sim = hash["min_sim"] if hash["min_sim"] + lazar.subjectid = hash["subjectid"] if hash["subjectid"] + lazar.prop_kernel = hash["prop_kernel"] if hash["prop_kernel"] + lazar.value_map = hash["value_map"] if hash["value_map"] + lazar.nr_hits = hash["nr_hits"] if hash["nr_hits"] + lazar.transform = hash["transform"] if hash["transform"] + lazar.conf_stdev = hash["conf_stdev"] if hash["conf_stdev"] + lazar.prediction_min_max = hash["prediction_min_max"] if hash["prediction_min_max"] + lazar + end + + def to_json + Yajl::Encoder.encode({:uri => @uri,:metadata => @metadata, :compound => @compound, :prediction_dataset => @prediction_dataset, :features => @features, :effects => @effects, :activities => @activities, :p_values => @p_values, :fingerprints => @fingerprints, :feature_calculation_algorithm => @feature_calculation_algorithm, :similarity_algorithm => @similarity_algorithm, :prediction_algorithm => @prediction_algorithm, :min_sim => @min_sim, :subjectid => @subjectid, :prop_kernel => @prop_kernel, :value_map => @value_map, :nr_hits => @nr_hits, :transform => @transform, :conf_stdev => @conf_stdev, :prediction_min_max => @prediction_min_max}) + end + def run( params, accept_header=nil, waiting_task=nil ) unless accept_header if CONFIG[:yaml_hosts].include?(URI.parse(@uri).host) - accept_header = 'application/x-yaml' + accept_header = 'application/json' else accept_header = 'application/rdf+xml' end @@ -215,6 +247,7 @@ module OpenTox @compound = Compound.new compound_uri features = {} + #LOGGER.debug self.to_yaml unless @prediction_dataset @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid) @prediction_dataset.add_metadata( { @@ -255,7 +288,7 @@ module OpenTox @prediction_dataset.metadata[OT.predictedVariables] = [value_feature_uri, confidence_feature_uri] unless @prediction_dataset.metadata[OT.predictedVariables] if OpenTox::Feature.find(metadata[OT.dependentVariables], subjectid).feature_type == "classification" - @prediction_dataset.add @compound.uri, value_feature_uri, @value_map[prediction[:prediction]] + @prediction_dataset.add @compound.uri, value_feature_uri, @value_map[prediction[:prediction].to_s] else @prediction_dataset.add @compound.uri, value_feature_uri, prediction[:prediction] end @@ -365,7 +398,7 @@ module OpenTox def database_activity(subjectid) if @activities[@compound.uri] if OpenTox::Feature.find(metadata[OT.dependentVariables], subjectid).feature_type == "classification" - @activities[@compound.uri].each { |act| @prediction_dataset.add @compound.uri, @metadata[OT.dependentVariables], @value_map[act] } + @activities[@compound.uri].each { |act| @prediction_dataset.add @compound.uri, @metadata[OT.dependentVariables], @value_map[act.to_s] } else @activities[@compound.uri].each { |act| @prediction_dataset.add @compound.uri, @metadata[OT.dependentVariables], act } end @@ -408,7 +441,7 @@ module OpenTox # Save model at model service def save(subjectid) - self.uri = RestClientWrapper.post(@uri,self.to_yaml,{:content_type => "application/x-yaml", :subjectid => subjectid}) + self.uri = RestClientWrapper.post(@uri,self.to_json,{:content_type => "application/json", :subjectid => subjectid}) end # Delete model at model service |