summaryrefslogtreecommitdiff
path: root/application.rb
blob: 3ffc080b624ea986657ef251b954d6afb6f75197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'rdiscount'
$ambit_search = "http://data.enanomapper.net/substance?type=name&search="

configure :development do
  $logger = Logger.new(STDOUT)
end

before do
  @version = File.read("VERSION").chomp
end

get '/?' do
  redirect to('/predict') 
end

get '/predict/?' do
  @prediction_models = []
  prediction_models = OpenTox::Model::NanoPrediction.all
  prediction_models.each{|m| m.model[:feature_selection_algorithm_parameters]["category"] == "P-CHEM" ? @prediction_models[0] = m : @prediction_models[1] = m}
  @prediction_models.each_with_index{|m,idx| idx == 0 ? m[:pc_model] = true : m[:pcp_model] = true}
  nanoparticles = OpenTox::Nanoparticle.all.select{|n| n.core["name"] == "Au" || n.core["name"] == "Ag"}
  example = nanoparticles.collect{|n| n if n.physchem_descriptors.size > 16}.compact
  pcp = example.sample
  @example_pcp = pcp
  pc = example.sample
  pc.physchem_descriptors.delete_if{|k,v| feature = OpenTox::Feature.find_by(:id => k); feature.category != "P-CHEM"}
  @example_pc = pc

  haml :predict
end

get '/license' do
  @license = RDiscount.new(File.read("LICENSE.md")).to_html
  haml :license, :layout => false
end

post '/predict/?' do
  prediction_model = OpenTox::Model::NanoPrediction.find(params[:prediction_model])
  size = params[:size].to_i
  @type = params[:type]

  example_core = eval(params[:example_core])
  example_coating = eval(params[:example_coating])
  example_pc = eval(params[:example_pc])

  in_core = eval(params[:in_core])
  in_core["name"] = params[:input_core]
  input_core = in_core

  in_coating = eval(params[:in_coating])
  in_coating[0]["name"] = params[:input_coating]
  input_coating = in_coating

  input_pc = {}
  (1..size).each{|i| input_pc["#{params["input_key_#{i}"]}"] = [params["input_value_#{i}"].to_f]}
  if input_pc == example_pc && input_core == example_core && input_coating == example_coating
    # unchanged input = database hit
    nanoparticle = OpenTox::Nanoparticle.find_by(:id => params[:example_id])
    nanoparticle.physchem_descriptors = input_pc
    @match = true,@nanoparticle = nanoparticle,@name = nanoparticle.name
  else
    # changed input = create nanoparticle to predict
    nanoparticle = OpenTox::Nanoparticle.new
    nanoparticle.core = input_core
    nanoparticle.coating = input_coating
    nanoparticle.physchem_descriptors = input_pc
    @match = false
  end
  # output
  @input = input_pc
  @prediction = prediction_model.model.predict_substance nanoparticle

  haml :prediction
end