main.py 14.7 KB
Newer Older
1
2
#!/usr/bin/env python3

Floreal Cabanettes's avatar
Floreal Cabanettes committed
3
import os
4
5
import time
import datetime
6
import shutil
7
import re
8
import threading
Floreal Cabanettes's avatar
Floreal Cabanettes committed
9
from flask import Flask, render_template, request, url_for, jsonify, session, Response, abort
10
from pathlib import Path
11
from lib.paf import Paf
12
from config_reader import AppConfigReader
13
from lib.job_manager import JobManager
14
from lib.functions import Functions, ALLOWED_EXTENSIONS
15
from lib.upload_file import UploadFile
Floreal Cabanettes's avatar
Floreal Cabanettes committed
16
from lib.fasta import Fasta
Floreal Cabanettes's avatar
Floreal Cabanettes committed
17
from lib.mailer import Mailer
Floreal Cabanettes's avatar
Floreal Cabanettes committed
18
from lib.crons import Crons
19
20

import sys
21

22
23
24
25
26
27
app_folder = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, app_folder)
os.environ["PATH"] = os.path.join(app_folder, "bin") + ":" + os.environ["PATH"]

sqlite_file = os.path.join(app_folder, "database.sqlite")

28
29
30
31

# Init config reader:
config_reader = AppConfigReader()

Floreal Cabanettes's avatar
Floreal Cabanettes committed
32
33
UPLOAD_FOLDER = config_reader.upload_folder
APP_DATA = config_reader.app_data
34

Floreal Cabanettes's avatar
Floreal Cabanettes committed
35
app_title = "D-GENIES - Dotplot for Genomes Interactive, E-connected and Speedy"
Floreal Cabanettes's avatar
Floreal Cabanettes committed
36

37
# Init Flask:
38
app = Flask(__name__, static_url_path='/static')
39
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
40
app.config['SECRET_KEY'] = 'dsqdsq-255sdA-fHfg52-25Asd5'
41

42
# Init mail:
Floreal Cabanettes's avatar
Floreal Cabanettes committed
43
mailer = Mailer(app)
44

45
# Folder containing data:
Floreal Cabanettes's avatar
Floreal Cabanettes committed
46
app_data = config_reader.app_data
47

Floreal Cabanettes's avatar
Floreal Cabanettes committed
48
49
# Crons:
crons = Crons(app_folder)
Floreal Cabanettes's avatar
Floreal Cabanettes committed
50
crons.start_all()
Floreal Cabanettes's avatar
Floreal Cabanettes committed
51

52

Floreal Cabanettes's avatar
Floreal Cabanettes committed
53
54
55
56
57
58
@app.context_processor
def get_launched_results():
    cookie = request.cookies.get("results")
    return {"results": cookie.split("|") if cookie is not None else set()}


59
60
61
# Main
@app.route("/", methods=['GET'])
def main():
Floreal Cabanettes's avatar
Floreal Cabanettes committed
62
63
64
65
66
    return render_template("index.html", title=app_title, menu="index")


@app.route("/run", methods=['GET'])
def run():
Floreal Cabanettes's avatar
Floreal Cabanettes committed
67
    session["user_tmp_dir"] = Functions.random_string(5) + "_" + \
68
                              datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
Floreal Cabanettes's avatar
Floreal Cabanettes committed
69
    id_job = Functions.random_string(5) + "_" + datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
70
71
72
73
74
    if "id_job" in request.args:
        id_job = request.args["id_job"]
    email = ""
    if "email" in request.args:
        email = request.args["email"]
Floreal Cabanettes's avatar
Floreal Cabanettes committed
75
    return render_template("run.html", title=app_title, id_job=id_job, email=email,
76
                           menu="run", allowed_ext=ALLOWED_EXTENSIONS)
77
78
79
80
81
82
83


# Launch analysis
@app.route("/launch_analysis", methods=['POST'])
def launch_analysis():
    id_job = request.form["id_job"]
    email = request.form["email"]
84
85
86
87
    file_query = request.form["query"]
    file_query_type = request.form["query_type"]
    file_target = request.form["target"]
    file_target_type = request.form["target_type"]
88
89
90

    # Check form:
    form_pass = True
91
    errors = []
92
    if id_job == "":
93
        errors.append("Id of job not given")
94
95
96
        form_pass = False

    if email == "":
97
        errors.append("Email not given")
98
        form_pass = False
99
100
    if file_target == "":
        errors.append("No target fasta selected")
101
102
103
104
        form_pass = False

    # Form pass
    if form_pass:
105
        # Get final job id:
106
        id_job = re.sub('[^A-Za-z0-9_\-]+', '', id_job.replace(" ", "_"))
107
        id_job_orig = id_job
108
        i = 2
109
        while os.path.exists(os.path.join(app_data, id_job)):
110
111
            id_job = id_job_orig + ("_%d" % i)
            i += 1
112
113
114
115
116

        folder_files = os.path.join(app_data, id_job)
        os.makedirs(folder_files)

        # Save files:
117
118
119
120
121
122
123
124
125
126
        query = None
        if file_query != "":
            query_name = os.path.splitext(file_query.replace(".gz", ""))[0] if file_query_type == "local" else None
            query_path = os.path.join(app.config["UPLOAD_FOLDER"], session["user_tmp_dir"], file_query) \
                if file_query_type == "local" else file_query
            query = Fasta(name=query_name, path=query_path, type_f=file_query_type)
        target_name = os.path.splitext(file_target.replace(".gz", ""))[0] if file_target_type == "local" else None
        target_path = os.path.join(app.config["UPLOAD_FOLDER"], session["user_tmp_dir"], file_target) \
            if file_target_type == "local" else file_target
        target = Fasta(name=target_name, path=target_path, type_f=file_target_type)
127
128

        # Launch job:
Floreal Cabanettes's avatar
Floreal Cabanettes committed
129
        job = JobManager(id_job, email, query, target, mailer)
130
131
        job.launch()
        return jsonify({"success": True, "redirect": url_for(".status", id_job=id_job)})
132
    else:
133
        return jsonify({"success": False, "errors": errors})
134
135
136
137
138
139


# Status of a job
@app.route('/status/<id_job>', methods=['GET'])
def status(id_job):
    job = JobManager(id_job)
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    j_status = job.status()
    mem_peak = j_status["mem_peak"] if "mem_peak" in j_status else None
    if mem_peak is not None:
        mem_peak = "%.1f G" % (mem_peak / 1024.0 / 1024.0)
    time_e = j_status["time_elapsed"] if "time_elapsed" in j_status else None
    if time_e is not None:
        if time_e < 60:
            time_e = "%d secs" % time_e
        else:
            minutes = time_e // 60
            seconds = time_e - minutes * 60
            time_e = "%d min %d secs" % (minutes, seconds)
    return render_template("status.html", title=app_title, status=j_status["status"],
                           error=j_status["error"].replace("#ID#", ""),
                           id_job=id_job, menu="results", mem_peak=mem_peak,
                           time_elapsed=time_e)
156
157


Floreal Cabanettes's avatar
Floreal Cabanettes committed
158
# Results path
159
@app.route("/result/<id_res>", methods=['GET'])
160
def result(id_res):
Floreal Cabanettes's avatar
Floreal Cabanettes committed
161
162
163
164
165
166
167
168
    my_render = render_template("results.html", title=app_title, id=id_res, menu="results", current_result=id_res)
    response = app.make_response(my_render)
    cookie = request.cookies.get("results")
    cookie = cookie.split("|") if cookie is not None else []
    if id_res not in cookie:
        cookie.insert(0, id_res)
    response.set_cookie(key="results", value="|".join(cookie), path="/")
    return response
169
170


171
def get_file(file, gzip=False):  # pragma: no cover
Floreal Cabanettes's avatar
Floreal Cabanettes committed
172
173
174
175
176
177
    try:
        # Figure out how flask returns static files
        # Tried:
        # - render_template
        # - send_file
        # This should not be so non-obvious
178
        return open(file, "rb" if gzip else "r").read()
Floreal Cabanettes's avatar
Floreal Cabanettes committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    except IOError as exc:
        return str(exc)


@app.route("/paf/<id_res>", methods=['GET'])
def download_paf(id_res):
    map_file = os.path.join(APP_DATA, id_res, "map.paf.sorted")
    if not os.path.exists(map_file):
        map_file = os.path.join(APP_DATA, id_res, "map.paf")
    if not os.path.exists(map_file):
        abort(404)
    content = get_file(map_file)
    return Response(content, mimetype="text/plain")


Floreal Cabanettes's avatar
Floreal Cabanettes committed
194
# Get graph (ajax request)
195
196
197
@app.route('/get_graph', methods=['POST'])
def get_graph():
    id_f = request.form["id"]
198
    paf = os.path.join(app_data, id_f, "map.paf")
199
200
    idx1 = os.path.join(app_data, id_f, "query.idx")
    idx2 = os.path.join(app_data, id_f, "target.idx")
201

202
    paf = Paf(paf, idx1, idx2)
203

204
205
    if paf.parsed:
        res = paf.get_d3js_data()
206
        res["success"] = True
207
208
209
210
211
212
        return jsonify(res)
    return jsonify({"success": False, "message": paf.error})


@app.route('/sort/<id_res>', methods=['POST'])
def sort_graph(id_res):
213
    if not os.path.exists(os.path.join(APP_DATA, id_res, ".all-vs-all")):
214
        paf_file = os.path.join(app_data, id_res, "map.paf")
215
216
        idx1 = os.path.join(app_data, id_res, "query.idx")
        idx2 = os.path.join(app_data, id_res, "target.idx")
217
        paf = Paf(paf_file, idx1, idx2, False)
218
219
220
221
222
223
224
        paf.sort()
        if paf.parsed:
            res = paf.get_d3js_data()
            res["success"] = True
            return jsonify(res)
        return jsonify({"success": False, "message": paf.error})
    return jsonify({"success": False, "message": "Sort is not available for All-vs-All mode"})
Floreal Cabanettes's avatar
Floreal Cabanettes committed
225

Floréal Cabanettes's avatar
Floréal Cabanettes committed
226

227
228
229
230
231
232
233
234
235
236
237
238
239
240
@app.route('/freenoise/<id_res>', methods=['POST'])
def free_noise(id_res):
    paf_file = os.path.join(app_data, id_res, "map.paf")
    idx1 = os.path.join(app_data, id_res, "query.idx")
    idx2 = os.path.join(app_data, id_res, "target.idx")
    paf = Paf(paf_file, idx1, idx2, False)
    paf.parse_paf(noise=request.form["noise"] == "1")
    if paf.parsed:
        res = paf.get_d3js_data()
        res["success"] = True
        return jsonify(res)
    return jsonify({"success": False, "message": paf.error})


241
242
243
244
245
@app.route('/get-fasta-query/<id_res>', methods=['POST'])
def build_fasta(id_res):
    res_dir = os.path.join(app_data, id_res)
    lock_query = os.path.join(res_dir, ".query-fasta-build")
    is_sorted = os.path.exists(os.path.join(res_dir, ".sorted"))
246
    compressed = request.form["gzip"].lower() == "true"
247
248
249
250
251
    query_fasta = Functions.get_fasta_file(res_dir, "query", is_sorted)
    if query_fasta is not None:
        if is_sorted and not query_fasta.endswith(".sorted"):
            # Do the sort
            Path(lock_query).touch()
252
253
            if not compressed:  # If compressed, it will took a long time, so not wait
                Path(lock_query + ".pending").touch()
254
255
256
257
258
            thread = threading.Timer(1, Functions.sort_fasta, kwargs={
                "job_name": id_res,
                "fasta_file": query_fasta,
                "index_file": os.path.join(res_dir, "query.idx.sorted"),
                "lock_file": lock_query,
259
                "compress": compressed,
260
261
262
                "mailer": mailer
            })
            thread.start()
263
264
265
266
267
268
            if not compressed:
                i = 0
                time.sleep(5)
                while os.path.exists(lock_query) and i < 2:
                    i += 1
                    time.sleep(5)
Floreal Cabanettes's avatar
Floreal Cabanettes committed
269
                os.remove(lock_query + ".pending")
270
271
272
                if os.path.exists(lock_query):
                    return jsonify({"success": True, "status": 1, "status_message": "In progress"})
                return jsonify({"success": True, "status": 2, "status_message": "Done",
273
                                "gzip": False})
274
275
            else:
                return jsonify({"success": True, "status": 1, "status_message": "In progress"})
276
277
278
279
280
        elif is_sorted and os.path.exists(lock_query):
            # Sort is already in progress
            return jsonify({"success": True, "status": 1, "status_message": "In progress"})
        else:
            # No sort to do or sort done
281
282
283
284
285
286
287
            if compressed and not query_fasta.endswith(".gz.fasta"):
                # If compressed file is asked, we must compress it now if not done before...
                Path(lock_query).touch()
                thread = threading.Timer(1, Functions.compress_and_send_mail, kwargs={
                    "job_name": id_res,
                    "fasta_file": query_fasta,
                    "index_file": os.path.join(res_dir, "query.idx.sorted"),
288
                    "lock_file": lock_query,
289
290
291
292
293
                    "compressed": compressed,
                    "mailer": mailer
                })
                thread.start()
                return jsonify({"success": True, "status": 1, "status_message": "In progress"})
294
295
296
297
298
299
300
            return jsonify({"success": True, "status": 2, "status_message": "Done",
                            "gzip": query_fasta.endswith(".gz") or query_fasta.endswith(".gz.sorted")})
    else:
        return jsonify({"success": False,
                        "message": "Unable to get fasta file for query. Please contact us to report the bug"})


301
302
303
@app.route('/fasta-query/<id_res>', defaults={'filename': ""}, methods=['GET'])
@app.route('/fasta-query/<id_res>/<filename>', methods=['GET'])  # Use fake URL in mail to set download file name
def dl_fasta(id_res, filename):
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    res_dir = os.path.join(app_data, id_res)
    lock_query = os.path.join(res_dir, ".query-fasta-build")
    is_sorted = os.path.exists(os.path.join(res_dir, ".sorted"))
    if not os.path.exists(lock_query) or not is_sorted:
        query_fasta = Functions.get_fasta_file(res_dir, "query", is_sorted)
        if query_fasta is not None:
            if query_fasta.endswith(".gz") or query_fasta.endswith(".gz.sorted"):
                content = get_file(query_fasta, True)
                return Response(content, mimetype="application/gzip")
            content = get_file(query_fasta)
            return Response(content, mimetype="text/plain")
    abort(404)


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
@app.route('/qt-assoc/<id_res>', methods=['GET'])
def qt_assoc(id_res):
    res_dir = os.path.join(app_data, id_res)
    if os.path.exists(res_dir) and os.path.isdir(res_dir):
        paf_file = os.path.join(app_data, id_res, "map.paf")
        idx1 = os.path.join(app_data, id_res, "query.idx")
        idx2 = os.path.join(app_data, id_res, "target.idx")
        try:
            paf = Paf(paf_file, idx1, idx2, False)
            paf.parse_paf(False)
        except FileNotFoundError:
            print("Unable to load data!")
            abort(404)
            return False
        csv_content = paf.build_query_on_target_association_file()
        return Response(csv_content, mimetype="text/plain")
    abort(404)


337
338
@app.route("/upload", methods=['POST'])
def upload():
339
340
341
342
343
344
345
346
347
    if "user_tmp_dir" in session and session["user_tmp_dir"] != "":
        folder = session["user_tmp_dir"]
        files = request.files[list(request.files.keys())[0]]

        if files:
            filename = files.filename
            folder_files = os.path.join(app.config["UPLOAD_FOLDER"], folder)
            if not os.path.exists(folder_files):
                os.makedirs(folder_files)
Floreal Cabanettes's avatar
Floreal Cabanettes committed
348
            filename = Functions.get_valid_uploaded_filename(filename, folder_files)
349
350
            mime_type = files.content_type

Floreal Cabanettes's avatar
Floreal Cabanettes committed
351
            if not Functions.allowed_file(files.filename):
352
                result = UploadFile(name=filename, type_f=mime_type, size=0, not_allowed_msg="File type not allowed")
353
                shutil.rmtree(folder_files)
354
355
356
357
358
359
360
361
362
363

            else:
                # save file to disk
                uploaded_file_path = os.path.join(folder_files, filename)
                files.save(uploaded_file_path)

                # get file size after saving
                size = os.path.getsize(uploaded_file_path)

                # return json for js call back
364
                result = UploadFile(name=filename, type_f=mime_type, size=size)
365
366
367
368
369

            return jsonify({"files": [result.get_file()], "success": "OK"})

        return jsonify({"files": [], "success": "404", "message": "No file provided"})
    return jsonify({"files": [], "success": "ERR", "message": "Session not initialized. Please refresh the page."})
370
371


372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@app.route("/send-mail/<id_res>", methods=['POST'])
def send_mail(id_res):
    allowed = False
    key_file = None
    if "key" in request.form:
        key = request.form["key"]
        res_dir = os.path.join(app_data, id_res)
        key_file = os.path.join(res_dir, ".key")
        if os.path.exists(key_file):
            with open(key_file) as k_f:
                true_key = k_f.readline().strip("\n")
                allowed = key == true_key
    if allowed:
        os.remove(key_file)
        job_mng = JobManager(id_job=id_res, mailer=mailer)
        job_mng.set_inputs_from_res_dir()
        job_mng.send_mail()
        return "OK"
    else:
        abort(403)


Floréal Cabanettes's avatar
Floréal Cabanettes committed
394
if __name__ == '__main__':
Floreal Cabanettes's avatar
Floreal Cabanettes committed
395
    app.run()