summaryrefslogtreecommitdiff
path: root/application.rb
blob: 11115ffe1156d0db7e8220dddce6fe6933dff011 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
['rubygems', "haml", "sass"].each do |lib|
	require lib
end
require 'rack-flash'
#require 'benchmark'
gem 'opentox-ruby-api-wrapper', '= 1.2.6'
require 'opentox-ruby-api-wrapper'
gem 'sinatra-static-assets'
require 'sinatra/static_assets'

use Rack::Flash
set :sessions, true

get '/?' do
	redirect url_for('/create')
end

get '/predict/?' do 
	@models = OpenTox::Model::Lazar.find_all
	haml :predict
end

get '/create' do
	haml :create
end

get '/about' do
	haml :about
end

get '/csv_format' do
	haml :csv_format
end

get '/tasks' do
	@tasks = OpenTox::Task.all
	haml :tasks
end

get '/task' do
	@task = OpenTox::Task.find(session[:task_uri])
	haml :task
end

post '/upload' do # create a new model
	dataset = OpenTox::Dataset.new
	title = params[:endpoint].sub(/\s+/,'_')
	dataset.title = title
	feature_uri = url_for("/feature#"+title, :full)
	feature = dataset.find_or_create_feature(feature_uri)
	params[:file][:tempfile].each_line do |line|
		items = line.chomp.split(/\s*,\s*/)
		smiles = items[0]
		compound_uri = OpenTox::Compound.new(:smiles => smiles).uri
		compound = dataset.find_or_create_compound(compound_uri)
		case items[1].to_s
		when '1'
			dataset.add(compound,feature,true)
		when '0'
			dataset.add(compound,feature,false)
		else
			flash[:notice] = "Irregular activity '#{items[1]}' for SMILES #{smiles}. Please use 1 for active and 0 for inactive compounds"
		end
	end
	dataset_uri = dataset.save
	task_uri = OpenTox::Algorithm::Lazar.create_model(:dataset_uri => dataset_uri, :feature_uri => feature_uri)
	flash[:notice] = "Model creation started - this may take some time. You can view and manage the status of current tasks at the #{link_to("Tasks page", "/tasks")}. #{link_to("Reload this page", "/predict")} to use the new model."
	session[:task_uri] = task_uri
	redirect url_for('/predict')
end

post '/predict/?' do # post chemical name to model
	@identifier = params[:identifier]
	begin
		@compound = OpenTox::Compound.new(:name => params[:identifier])
	rescue
		flash[:notice] = "Could not find a structure for #{@identifier}. Please try again."
		redirect '/predict'
	end
	@predictions = []
	params[:selection].keys.each do |uri|
		prediction = nil
		confidence = nil
		title = nil
		prediction = RestClient.post uri, :compound_uri => @compound.uri, :accept => "application/x-yaml"
		model = Redland::Model.new Redland::MemoryStore.new
		parser = Redland::Parser.new
		parser.parse_string_into_model(model,prediction,'/')
		f = model.subject(RDF['type'],OT['Feature']) # this can be dangerous if OWL is not properly sorted
		title = model.object(f,DC['title']).to_s
		model.subjects(RDF['type'], OT['FeatureValue']).each do |v|
			feature = model.object(v,OT['feature'])
			feature_name = model.object(feature,DC['title']).to_s
			prediction = model.object(v,OT['value']).to_s if feature_name.match(/classification/)
			confidence = model.object(v,OT['value']).to_s if feature_name.match(/confidence/)
		end
		case prediction.to_s
		when "true"
			prediction = "active"
		when "false"
			prediction = "inactive"
		else
			prediction = "not available"
		end
		@predictions << {:title => title, :prediction => prediction, :confidence => confidence}
	end

	haml :prediction
	#@predictions.to_yaml 
end

post '/task/cancel' do
	puts params[:task_uri]
	task = OpenTox::Task.find(params[:task_uri])
	task.cancel
	redirect url_for('/tasks')
end

delete '/:id' do
	#begin
		OpenTox::Model::LazarClassification.delete(params[:id])
		haml :index
	#rescue
		#"Deletion of model with ID #{params[:id]} failed. Please check your username and password."
	#end
end

# SASS stylesheet
get '/stylesheets/style.css' do
  headers 'Content-Type' => 'text/css; charset=utf-8'
  sass :style
end