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

3
4
import time
import datetime
5
from flask import Flask, render_template, request, redirect, flash, url_for, jsonify
6
from werkzeug.utils import secure_filename
7
from lib.paf import Paf
8
from config_reader import AppConfigReader
9
from lib.job_manager import JobManager
Floreal Cabanettes's avatar
Floreal Cabanettes committed
10
from lib.functions import *
11
12
13
14
15
16
17
18

import sys
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")

19
20
21
22

# Init config reader:
config_reader = AppConfigReader()

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

25
# Init Flask:
26
app = Flask(__name__, static_url_path='/static')
27
app.config['SECRET_KEY'] = 'dsqdsq-255sdA-fHfg52-25Asd5'
28
29

# Folder containing data:
30
31
32
app_data = config_reader.get_app_data()


Floreal Cabanettes's avatar
Floreal Cabanettes committed
33
34
35
36
37
38
@app.context_processor
def get_launched_results():
    cookie = request.cookies.get("results")
    return {"results": cookie.split("|") if cookie is not None else set()}


39
40
41
# Main
@app.route("/", methods=['GET'])
def main():
Floreal Cabanettes's avatar
Floreal Cabanettes committed
42
43
44
45
46
    return render_template("index.html", title=app_title, menu="index")


@app.route("/run", methods=['GET'])
def run():
47
48
49
50
51
52
    id_job = random_string(5) + "_" + datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
    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
53
54
    return render_template("run.html", title=app_title, id_job=id_job, email=email,
                           menu="run")
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


# Launch analysis
@app.route("/launch_analysis", methods=['POST'])
def launch_analysis():
    id_job = request.form["id_job"]
    email = request.form["email"]
    file_query = request.files["file_query"]
    file_target = request.files["file_target"]

    # Check form:
    form_pass = True
    if id_job == "":
        flash("Id of job not given")
        form_pass = False

    if email == "":
        flash("Email not given")
        form_pass = False
    if file_query.filename == "":
        flash("No query fasta selected")
        form_pass = False

    # Form pass
    if form_pass:
        # Check files are correct:
        if not allowed_file(file_query.filename):
82
            flash("Format of query fasta must be in fasta format (*.%s)" % ", *.".join(ALLOWED_EXTENSIONS))
83
            form_pass = False
84
        if file_target.filename != "" and not allowed_file(file_target.filename):
85
            flash("Format of target fasta must be in fasta format (*.%s)" % ", *.".join(ALLOWED_EXTENSIONS))
86
87
            form_pass = False
        if form_pass:
88
89
90
91
92
93
94
95
            # Get final job id:
            id_job_orig = id_job
            while os.path.exists(os.path.join(app_data, id_job)):
                id_job = id_job_orig + "_2"

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

96
            # Save files:
97
            query_name = os.path.splitext(os.path.basename(file_query.filename))[0]
98
            filename_query = get_valid_uploaded_filename(secure_filename(file_query.filename), folder_files)
99
            target_name = os.path.splitext(os.path.basename(file_target.filename))[0]
100
            query_path = os.path.join(folder_files, filename_query)
101
            file_query.save(query_path)
102
103
            target_path = None
            if file_target.filename != "":
104
105
                filename_target = get_valid_uploaded_filename(secure_filename(file_target.filename), folder_files)
                target_path = os.path.join(folder_files, filename_target)
106
                file_target.save(target_path)
107

108
            # Launch job:
109
            job = JobManager(id_job, email, query_path, target_path, query_name, target_name)
110
            job.launch()
111
            return redirect(url_for(".status", id_job=id_job))
112
        else:
Floreal Cabanettes's avatar
Floreal Cabanettes committed
113
            return redirect(url_for(".run", id_job=id_job, email=email))
114
115
    else:
        return redirect(url_for(".main", id_job=id_job, email=email))
116
117
118
119
120
121
122


# Status of a job
@app.route('/status/<id_job>', methods=['GET'])
def status(id_job):
    job = JobManager(id_job)
    status = job.status()
Floreal Cabanettes's avatar
Floreal Cabanettes committed
123
124
    return render_template("status.html", title=app_title, status=status, id_job=id_job,
                           menu="results")
125
126


Floreal Cabanettes's avatar
Floreal Cabanettes committed
127
# Results path
128
@app.route("/result/<id_res>", methods=['GET'])
129
def result(id_res):
Floreal Cabanettes's avatar
Floreal Cabanettes committed
130
131
132
133
134
135
136
137
    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
138
139


Floreal Cabanettes's avatar
Floreal Cabanettes committed
140
# Get graph (ajax request)
141
142
143
@app.route('/get_graph', methods=['POST'])
def get_graph():
    id_f = request.form["id"]
144
    paf = os.path.join(app_data, id_f, "map.paf")
145
146
    idx1 = os.path.join(app_data, id_f, "query.idx")
    idx2 = os.path.join(app_data, id_f, "target.idx")
147

148
    paf = Paf(paf, idx1, idx2)
149

150
151
    if paf.parsed:
        res = paf.get_d3js_data()
152
        res["success"] = True
153
154
155
156
157
158
        return jsonify(res)
    return jsonify({"success": False, "message": paf.error})


@app.route('/sort/<id_res>', methods=['POST'])
def sort_graph(id_res):
Floreal Cabanettes's avatar
Floreal Cabanettes committed
159
160
161
162
163
164
165
166
167
168
169
    paf = 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, idx1, idx2)
    paf.sort()
    if paf.parsed:
        res = paf.get_d3js_data()
        res["success"] = True
        return jsonify(res)
    return jsonify({"success": False, "message": paf.error})

Floréal Cabanettes's avatar
Floréal Cabanettes committed
170
171
172

if __name__ == '__main__':
    app.run()