-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.rb
286 lines (249 loc) · 6.96 KB
/
routes.rb
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
get "/css/:filename.css" do
scss :"sass/#{params[:filename]}"
end
get "/" do
@news = NewsPost.visible_news
haml :home
end
get "/login" do
haml :login
end
put "/default_ortholog" do
session[:scientific_name] = params[:scientific_name]
end
post "/login" do
eperson = password_authorization(email: params[:email],
password: params[:password])
session[:current_user_id] = eperson.id if eperson
redirect session[:previous_location] || "/"
end
get "/logout" do
session[:current_user_id] = nil
redirect "/", info: "You logged out"
end
get "/news" do
authentication_required
authorized_for_roles(["admin"])
haml :news
end
post "/news_posts" do
NewsPost.create(user: current_user,
subject: params[:subject],
body: params[:body])
redirect "/news"
end
get "/news/:id" do
authentication_required
authorized_for_roles(["admin"])
@news_post = NewsPost.find(params[:id])
haml :news_edit
end
put "/news" do
@news_post = NewsPost.find(params[:id])
@news_post.update(deleted: params[:deleted],
subject: params[:subject],
body: params[:body])
haml :news_edit
end
delete "/news" do
@news_post = NewsPost.find(params[:id])
@news_post.delete
haml :news
end
get "/blast" do
haml :blast
end
post "/blast" do
path = File.join(settings.root, "blast", "blast.cgi")
blast = Seabase::BlastCgi.new(env, path)
@cgi_blast = blast.run
haml :blast_result
end
get "/search.?:format?" do
opts = {
format: params[:format],
scientific_name: params[:scientific_name],
term: params[:term],
limit: (params[:batch_size] || 100),
exact_search: (params[:exact_search] == "true"),
callback: params[:callback]
}
@external_names = perform_search(opts)
format_search_results(opts)
end
get "/external_names/:id" do
@en = ExternalName.find(params[:id])
@table_data = @en.table_items.unshift(Replicate.all_stages)
@chart_title = "Transcripts homologous to #{@en.gene_name}"
haml :external_name
end
get "/transcript/?:id?" do
@tr = Transcript.find_by_name(params[:name]) if params[:name]
@tr = Transcript.find(params[:id]) if params[:id]
if params[:external_name_id]
@en = ExternalName.find(params[:external_name_id])
end
@chart_title = "Transcript #{@tr.name}"
@table_data = @tr.table_items.unshift(Replicate.all_stages)
@name = "Transcript #{@tr.name}"
@en = get_homolog(@tr)
haml :transcript
end
get "/export" do
haml :export
end
get "/import" do
haml :import
end
get "/gephi_imports/:gephi_import_id" do
@gephi_import = GephiImport.find(params[:gephi_import_id])
@traces = Trace.where(gephi_import_id: @gephi_import.id)
haml :gephi_import
end
get "/traces/:trace_id" do
@trace = Trace.find(params[:trace_id])
@table_data, @max_y = transcripts_data(@trace)
@transcripts_json = format_graph_data(@table_data)
haml :trace
end
post "/upload_gephi" do
file = params[:file]
@gephi_import = GephiImport.process_file(file[:tempfile], params[:name])
redirect "/gephi_imports/#{@gephi_import.id}"
end
get "/test_charts" do
data = []
f = File.open(File.join(settings.root, "technical_files", "test_set"))
max_y = 0
f.each_with_index do |l, i|
transcripts = Transcript.find_by_sql("
select *
from transcripts
where name
like '#{l.strip}%'")
transcripts.each do |tr|
d = tr.table_items.unshift(Replicate.all_stages)
d[-1][0] = tr.name
data << d[0] if i == 0
data << d[-1]
new_max_y = d[-1][1..-1].max
max_y = new_max_y if new_max_y > max_y
end
end
@max_y = max_y
@table_data = data
@average_data = format_graph_data(get_average_data(data))
@fold_plot_data = File.read(File.join(settings.root, "technical_files",
"fold_plot_data.json")).strip
@fold_plot_max = find_fold_plot_max(@fold_plot_data)
haml :test_charts
end
get "/test_d3" do
haml :test_d3
end
private
def get_homolog(transcript)
transcript.external_names.select do |t|
t.taxon_id == current_taxon.id
end.first
end
def transcripts_data(trace)
data = [["Hours", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19]]
transcript_ids = trace.gephi_records.map(&:transcript_id)
max_y = 0
transcript_ids.each do |id|
tr, values, max_val = transcript_values(id)
max_y = max_val if max_val && max_val > max_y
homolog = get_homolog(tr)
name = homolog ? homolog.name : tr.name
values.unshift(name)
data << values
end
[data, max_y]
end
def transcript_values(transcript_id)
tr = Transcript.find(transcript_id)
values = Transcript.connection.select_values("
select sum(count)
from normalized_counts
where transcript_id = #{id}
group by stage order by stage")
[tr, values, values.max]
end
def get_average_data(data)
head = data[0]
data = data.transpose
data.shift
data = data.map do |d|
avg = d.reduce(:+) / d.size.to_f
avg = 0.0 if avg < 0
avg
end
data.unshift("Average")
[data].unshift(head)
end
def find_fold_plot_max(data)
data = JSON.parse(data)
data.shift
data = data.transpose
max_y = data.shift.max
max_x = data.shift.max
max = [max_y, max_x].max
Seabase.maxup(max)
end
def perform_search(opts)
if opts[:exact_search]
opts[:term].gsub!(/:[^:]*:.*/, "")
ExternalName.exact_search(opts)
else
ExternalName.like_search(opts)
end
end
def format_search_json(opts)
content_type "application/json", charset: "utf-8"
names_json = @external_names.to_json
names_json = format("%s(%s)", opts[:callback], names_json) if opts[:callback]
names_json
end
def format_search_html(opts)
if @external_names.size == 1
redirect "/external_names/#{@external_names[0].id}"
else
@ortholog_name = Taxon.
scientific_name_to_ortholog_name(opts[:scientific_name])
@term = opts[:term]
haml :search_result
end
end
def format_search_results(opts)
if opts[:format] == "json"
format_search_json(opts)
else
format_search_html(opts)
end
end
def current_user
return nil unless session[:current_user_id]
User.where(id: session[:current_user_id]).first
end
def authentication_required
session[:previous_location] = request.fullpath
return if session[:current_user_id] && session[:current_user_id].to_i > 0
redirect "/login",
info: "Please login to see the '#{request.path}' page."
end
def authorized_for_roles(roles = [])
return true if roles.empty?
size = current_user.roles.size
return true if (current_user.roles_names - roles).size < size
redirect "/",
error: "You are not authorized to access '#{request.path}' page."
end
def password_authorization(opts = {})
return nil unless opts[:email] && opts[:password]
user = User.where(email: opts[:email]).
where(password_hash: Digest::SHA1.hexdigest(opts[:password].strip)).first
session[:current_user_id] = user.id if user
user
end