From 13ffab048630cc30a4af9e9113c1843ae5af0a09 Mon Sep 17 00:00:00 2001 From: gebele Date: Wed, 25 Sep 2013 00:12:04 +0200 Subject: several major changes in code, several minor changes in style; prepared for OT meeting --- application.rb | 146 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 124 insertions(+), 22 deletions(-) (limited to 'application.rb') diff --git a/application.rb b/application.rb index 925c208..5cd5093 100644 --- a/application.rb +++ b/application.rb @@ -8,10 +8,11 @@ require_relative 'helper.rb' require File.join(ENV["HOME"],".opentox","config","lazar-gui.rb") # until added to ot-tools helpers do - # fetch models from uri-list + # get prediction models from text file, ignore validation models + # model uris must be manually added @@models = [] - uris = `curl -Lk -X GET -H accept:text/uri-list #{$model[:uri]} `.chomp.split("\n") - uris.each{|u| m = OpenTox::Model::Lazar.find u; @@models << m} + CSV.foreach("./prediction_models.csv"){|uri| m = OpenTox::Model::Lazar.find uri[0]; @@models << m} + #$logger.debug "model uris from csv file:\t#{@@models}\n" end get '/?' do @@ -27,36 +28,126 @@ get '/jme_help/?' do File.read(File.join('views','jme_help.html')) end -# best way to get individual compound uri for details +# get individual compound details get '/prediction/:neighbor/details/?' do - @compound_uri = OpenTox::Compound.new params[:neighbor] - @smiles = @compound_uri.smiles + @compound = OpenTox::Compound.new params[:neighbor] + @smiles = @compound.smiles + task = OpenTox::Task.run("look for names.") do - names = @compound_uri.names + names = @compound.names end task.wait + case task[RDF::OT.hasStatus] when "Error" @names = "No names for this compound available." when "Completed" - @names = @compound_uri.names.join(",") + @names = @compound.names end - @inchi = @compound_uri.inchi.gsub("InChI=", "") + @inchi = @compound.inchi.gsub("InChI=", "") + haml :details, :layout => false end +# fingerprints for compound in predictions +get '/prediction/:model_uri/:type/:compound_uri/fingerprints/?' do + @type = params[:type] + model = OpenTox::Model::Lazar.find params[:model_uri] + feature_dataset = OpenTox::Dataset.find model[RDF::OT.featureDataset] + @compound = OpenTox::Compound.new params[:compound_uri] + + if @type =~ /classification/i + # collect all feature values with fingerprint + fingerprints = OpenTox::Algorithm::Descriptor.send("smarts_match", [@compound], feature_dataset.features.collect{ |f| f[RDF::DC.title]})[@compound.uri] + #$logger.debug "fingerprints:\t#{fingerprints}\n" + + # collect fingerprints with value 1 + @fingerprint_values = [] + fingerprints.each{|smarts, value| @fingerprint_values << [smarts, value] if value > 0} + + # collect all features from feature_dataset + @features = feature_dataset.features.collect{|f| f } + + # search for each fingerprint in all features and collect feature values(smarts, pValue, effect) + @significant_fragments = [] + @fingerprint_values.each{ |fi, v| @features.each{ |f| @significant_fragments << [f[RDF::OT.effect].to_i, f[RDF::OT.smarts], f[RDF::OT.pValue]] if fi == f[RDF::OT.smarts] } } + else #regression + @significant_fragments = [] + end + + haml :significant_fragments, :layout => false +end + +get '/prediction/:model_uri/:type/:neighbor/significant_fragments/?' do + @type = params[:type] + #$logger.debug "sf type:\t#{@type}" + @compound = OpenTox::Compound.new params[:neighbor] + #$logger.debug "neighbor compound uri:\t#{@compound.uri}\n" + + model = OpenTox::Model::Lazar.find params[:model_uri] + #$logger.debug "model for significant fragments:\t#{model.uri}" + + feature_dataset = OpenTox::Dataset.find model[RDF::OT.featureDataset] + $logger.debug "fd :\t#{feature_dataset.uri}" + + # load all compounds + feature_dataset.compounds + + # load all features + @features = [] + feature_dataset.features.each{|f| @features << f} + #$logger.debug "all features in fd:\t#{@features}\n" + + # find all features and values for a neighbor compound + @significant_fragments = [] + # check type first + if @type =~ /classification/i + @feat = [] + # get compound index in feature dataset + c_idx = feature_dataset.compound_indices @compound.uri + #$logger.debug "compound idx:\t#{c_idx}\n" + + # collect feature uris with value + @features.each{|f| @feat << [feature_dataset.data_entry_value(c_idx[0], f.uri), f.uri]} + #$logger.debug "collected features:\t#{@feat}\n" + + # pass feature uris if value > 0 + @feat.each do |f| + if f[0] > 0 + f = OpenTox::Feature.find f[1] + @significant_fragments << [f[RDF::OT.effect].to_i, f[RDF::OT.smarts], f[RDF::OT.pValue].to_f.round(3)] + end + end + else # regression + # find a value in feature dataset by compound and feature + @values = [] + @features.each{|f| @values << feature_dataset.values(@compound, f)} + #$logger.debug "values in fd:\t#{@values}" + + count = 0 + @features.each{|f| @significant_fragments << [f.description, @values[count]]; count +=1} + end + #$logger.debug "significant fragments:\t#{@significant_fragments}\n" + + haml :significant_fragments, :layout => false +end + post '/predict/?' do # validate identifier input task = OpenTox::Task.run("Validate SMILES string.") do # transfered input @identifier = params[:identifier] + # get compound from SMILES @compound = OpenTox::Compound.from_smiles @identifier.to_s + # validate SMILES by converting to INCHI inchi = @compound.inchi end + # necessary to wait for task task.wait + # case task fails return message smiles invalid # case task completed go ahead case task[RDF::OT.hasStatus] @@ -66,30 +157,41 @@ post '/predict/?' do when "Completed" @identifier = params[:identifier] @compound = OpenTox::Compound.from_smiles @identifier.to_s - # init - @@prediction_models = [] - @@predictions = [] + # init arrays + @prediction_models = [] + @predictions = [] # init lazar algorithm lazar = OpenTox::Algorithm::Fminer.new File.join($algorithm[:uri],"lazar") - # gather models from service and compare if selected - #TODO compare selected by uri + + # get selected models + #TODO compare if model is selected by uri not title params[:selection].each do |model| - @mselected = model[0] - @mall = @@models - @mall.each do |m| - @@prediction_models << m if m.title =~ /#{@mselected}/ + # selected model = model[0] + # compare selected with all models + @@models.each do |m| + @prediction_models << m if m.title =~ /#{model[0]}/ end end # predict with selected models - # results in prediction variable - # store prediction in array for better handling - @@prediction_models.each do |m| + # one prediction in 'pa' array = OpenTox::Dataset + # all collected predictions in '@predictions' array + # init model_type array + @model_type = [] + @prediction_models.each do |m| + # define type (classification|regression) + m.type.join =~ /classification/i ? (@model_type << "classification") : (@model_type << "regression") + + #TODO each prediction get a task; load predictions page if first task finished and load results individually + + # predict against compound @prediction_uri = m.run :compound_uri => "#{@compound.uri}" + $logger.debug "prediction dataset:\t#{@prediction_uri}\n" + prediction = OpenTox::Dataset.new @prediction_uri pa = [] pa << prediction - @@predictions << pa + @predictions << pa end haml :prediction -- cgit v1.2.3