forked from marvinquiet/BART-WEB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·162 lines (131 loc) · 6.26 KB
/
app.py
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
# -*- coding: utf-8 -*-
import os
import json
from flask import (Flask, flash, request, redirect, url_for, render_template, send_from_directory, session)
from werkzeug.utils import secure_filename
import do_process
import marge_bart
PROJECT_DIR = os.path.dirname(__file__)
ALLOWED_EXTENSIONS = set(['txt', 'bam'])
# related to flask app
app = Flask(__name__)
app.secret_key = os.urandom(24)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# predict data button
if request.form['index_submit'] == 'predict_data':
return redirect(url_for('get_config'))
# retrieve result button
else:
user_key = request.form['index_submit']
# when key is null, refresh the website
if user_key == "":
return render_template('index.html')
if do_process.is_user_key_exists(user_key):
return redirect(url_for('get_result', user_key=user_key))
else:
err_msg = "User key does not exist, make sure you entered the right key"
session['err_msg'] = err_msg
return redirect(url_for('error_page'))
return render_template('index.html')
@app.route('/config-data', methods=['GET', 'POST'])
def get_config():
if request.method == 'POST':
# when "Predict your own data" button is clicked
if request.form['start_prediction'] == 'start_prediction':
# fetch user name and generate unique project path
username = request.form['username']
user_key = do_process.generate_user_key(username)
user_path = do_process.init_project_path(user_key)
# record user data
user_data = {}
user_data['user_key'] = user_key
user_data['user_path'] = user_path
user_data['dataType'] = request.form['dataType']
user_data['assembly'] = request.form['assembly']
user_data['prediction_type'] = request.form.getlist('predictionType')
if user_data['dataType'] != "ChIP-seq":
user_data['gene_exp_type'] = request.form['expTypeChoice']
user_data['gene_id_type'] = request.form['geneIdChoice']
else:
user_data['gene_exp_type'] = ""
user_data['gene_id_type'] = ""
user_data['files'] = []
# process what user has uploaded
if 'uploadFiles' not in request.files:
flash('Please choose a file')
return redirect(request.url)
files = request.files.getlist('uploadFiles')
# if user does not select file, browser also
# submit an empty part without filename
for file in files:
if file.filename == '':
flash('One of the files does not have a legal file name.')
return redirect(request.url)
# make sure the suffix of filename in [.txt, .bam]
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
upload_path = os.path.join(user_path, 'upload')
filename_abs_path = os.path.join(upload_path, filename)
file.save(filename_abs_path)
user_data['files'].append(filename_abs_path)
if u'tf' in user_data['prediction_type'] and \
len(user_data['prediction_type']) == 1 and \
user_data['dataType'] == 'ChIP-seq':
# only do bart profile with .bam file
user_data['marge'] = False
user_data['bart'] = True
# marge_bart.exe_bart_profile(user_data)
elif u'tf' not in user_data['prediction_type']:
# do marge process, repeat 3 times
user_data['marge'] = True
user_data['bart'] = False
# marge_bart.do_marge_bart(user_key, False)
else:
# do marge first to get enhancer prediction, and do bart geneset later
user_data['marge'] = True
user_data['bart'] = True
# marge_bart.do_marge_bart(user_key, True)
do_process.init_user_config(user_path, user_data)
marge_bart.do_marge_bart(user_data)
session['user_key'] = user_key
return redirect(url_for('show_key'))
return render_template('get_data_config.html')
@app.route('/result', methods=['GET', 'POST'])
def get_result():
user_key = request.args['user_key']
user_data = do_process.get_user_data(user_key)
results = do_process.generate_results(user_data)
return render_template('result_demonstration.html', results=results)
@app.route('/key', methods=['GET', 'POST'])
def show_key():
user_key = session.get('user_key', None)
return render_template('key_demonstration.html', key=user_key)
@app.route('/error', methods=['GET', 'POST'])
def error_page():
err_msg = session.get('err_msg', None)
return render_template('error.html', msg=err_msg)
@app.route('/download/<userkey_filename>')
def download_marge_file(userkey_filename):
user_key, filename = userkey_filename.split('___')
user_path = os.path.join(PROJECT_DIR, 'usercase/' + user_key)
download_path = os.path.join(user_path, 'download')
return send_from_directory(download_path, filename)
@app.route('/download/bart_output/<userkey_filename>')
def download_bart_res_file(userkey_filename):
user_key, filename = userkey_filename.split('___')
user_path = os.path.join(PROJECT_DIR, 'usercase/' + user_key)
download_path = os.path.join(user_path, 'download/bart_output')
return send_from_directory(download_path, filename)
@app.route('/download/bart_output/plot/<userkey_filename>')
def download_bart_chat_file(userkey_filename):
user_key, filename = userkey_filename.split('___')
user_path = os.path.join(PROJECT_DIR, 'usercase/' + user_key)
download_path = os.path.join(user_path, 'download/bart_output/plot')
return send_from_directory(download_path, filename)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# if __name__ == '__main__':
# app.run(debug=True,host='0.0.0.0')