From 0f632c60773985d3fb8e713e1378daed73d21c3d Mon Sep 17 00:00:00 2001 From: ist Date: Fri, 24 Aug 2012 15:52:42 +0200 Subject: Added transformation for measured acts. --- application.rb | 67 +++++++++++++++++++++++++++------------------------ helper.rb | 34 ++++++++++++++++++++++++++ views/prediction.haml | 2 ++ 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/application.rb b/application.rb index 34a11e3..57503db 100644 --- a/application.rb +++ b/application.rb @@ -527,12 +527,35 @@ post '/predict/?' do # post chemical name to model else @value_map = nil end + prediction_dataset = OpenTox::LazarPrediction.find(prediction_dataset_uri, @subjectid) if prediction_dataset.metadata[OT.hasSource].match(/dataset/) - @predictions << { - :title => model.name, - :measured_activities => prediction_dataset.measured_activities(@compound) - } + if model.name.downcase.include? "ptd50" + mw = calc_mw(@compound.uri) + td50 = ptd50_to_td50(prediction_dataset.measured_activities(@compound).first.to_f, mw) + prediction_trans = "TD50: #{td50}" + elsif model.name.downcase.include? "loael" + if model.name.downcase.include? "mol" + mw = calc_mw(@compound.uri) + mg = logmmol_to_mg(prediction_dataset.measured_activities(@compound).first.to_f, mw) + prediction_trans = "mg/kg bw/day: #{mg}" + elsif model.name.downcase.include? "mg" + mg = logmg_to_mg(prediction_dataset.measured_activities(@compound).first.to_f) + prediction_trans = "mg/kg bw/day: #{mg}" + end + end + if prediction_trans.nil? + @predictions << { + :title => model.name, + :measured_activities => prediction_dataset.measured_activities(@compound) + } + else + @predictions << { + :title => model.name, + :measured_activities => prediction_dataset.measured_activities(@compound), + :prediction_transformed => prediction_trans + } + end else predicted_feature = prediction_dataset.metadata[OT.dependentVariables] prediction = OpenTox::Feature.find(predicted_feature, @subjectid) @@ -540,42 +563,24 @@ post '/predict/?' do # post chemical name to model @predictions << { :title => model.name, :error => prediction.metadata[OT.error] - } + } elsif prediction_dataset.value(@compound).nil? @predictions << { :title => model.name, :error => "Not enough similar compounds in training dataset." - } + } else if model.name.downcase.include? "ptd50" - ds = OpenTox::Dataset.new() - ds.save(@subjectid) - ds.add_compound(@compound.uri) - ds.save(@subjectid) - mw_algorithm_uri = File.join(CONFIG[:services]["opentox-algorithm"],"pc/MW") - mw_uri = OpenTox::RestClientWrapper.post(mw_algorithm_uri, {:dataset_uri=>ds.uri}) - ds.delete(@subjectid) - mw_ds = OpenTox::Dataset.find(mw_uri, @subjectid) - mw = mw_ds.data_entries[@compound.uri][mw_uri.to_s + "/feature/MW"].first - mw_ds.delete(@subjectid) - td50 = (((10**(-1.0*prediction_dataset.value(@compound)))*(mw.to_f*1000))*1000).round / 1000.0 + mw = calc_mw(@compound.uri) + td50 = ptd50_to_td50(prediction_dataset.value(@compound), mw) prediction_trans = "TD50: #{td50}" elsif model.name.downcase.include? "loael" if model.name.downcase.include? "mol" - ds = OpenTox::Dataset.new() - ds.save(@subjectid) - ds.add_compound(@compound.uri) - ds.save(@subjectid) - mw_algorithm_uri = File.join(CONFIG[:services]["opentox-algorithm"],"pc/MW") - mw_uri = OpenTox::RestClientWrapper.post(mw_algorithm_uri, {:dataset_uri=>ds.uri}) - ds.delete(@subjectid) - mw_ds = OpenTox::Dataset.find(mw_uri, @subjectid) - mw = mw_ds.data_entries[@compound.uri][mw_uri.to_s + "/feature/MW"].first - mw_ds.delete(@subjectid) - mg = (((10**(-1.0*prediction_dataset.value(@compound)))*(mw.to_f*1000))*1000).round / 1000.0 + mw = calc_mw(@compound.uri) + mg = logmmol_to_mg(prediction_dataset.value(@compound), mw) prediction_trans = "mg/kg bw/day: #{mg}" elsif model.name.downcase.include? "mg" - mg = ((10**prediction_dataset.value(@compound))*1000).round / 1000.0 + mg = logmg_to_mg(prediction_dataset.value(@compound)) prediction_trans = "mg/kg bw/day: #{mg}" end end @@ -585,7 +590,7 @@ post '/predict/?' do # post chemical name to model :model_uri => model.uri, :prediction => prediction_dataset.value(@compound), :confidence => prediction_dataset.confidence(@compound) - } + } else @predictions << { :title => model.name, @@ -593,7 +598,7 @@ post '/predict/?' do # post chemical name to model :prediction => prediction_dataset.value(@compound), :confidence => prediction_dataset.confidence(@compound), :prediction_transformed => prediction_trans - } + } end end end diff --git a/helper.rb b/helper.rb index c38d2f4..b7943a1 100644 --- a/helper.rb +++ b/helper.rb @@ -158,4 +158,38 @@ helpers do return out end + def logmmol_to_mg(value ,mw) + mg = round_to((10**(-1.0*round_to(value.to_f, 2))*(mw.to_f*1000)),4) + return mg + end + + def logmg_to_mg(value) + mg = round_to(10**round_to(value.to_f, 2),4) + return mg + end + + def ptd50_to_td50(value ,mw) + td50 = round_to((10**(-1.0*round_to(value.to_f, 2))*(mw.to_f*1000)),4) + return td50 + end + + def round_to(value, deci) + rounded = (value.to_f*(10**deci)).round / (10**deci).to_f + return rounded + end + + def calc_mw(compound_uri) + ds = OpenTox::Dataset.new() + ds.save(@subjectid) + ds.add_compound(compound_uri) + ds.save(@subjectid) + mw_algorithm_uri = File.join(CONFIG[:services]["opentox-algorithm"],"pc/MW") + mw_uri = OpenTox::RestClientWrapper.post(mw_algorithm_uri, {:dataset_uri=>ds.uri}) + ds.delete(@subjectid) + mw_ds = OpenTox::Dataset.find(mw_uri, @subjectid) + mw = mw_ds.data_entries[compound_uri][mw_uri.to_s + "/feature/MW"].first.to_f + mw_ds.delete(@subjectid) + return mw + end + end diff --git a/views/prediction.haml b/views/prediction.haml index efa5a84..f44eec1 100644 --- a/views/prediction.haml +++ b/views/prediction.haml @@ -17,6 +17,8 @@ %br - p[:measured_activities].each do |a| = activity_markup(a, @value_map) + - if p[:prediction_transformed] + = p[:prediction_transformed] %br ( %a{:href => "#", :id => "linkTrainingData#{p.object_id}"} Measured activity -- cgit v1.2.3