Commit 8dfcf5fc authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Some fixes for export Fasta

parent 89551fe5
......@@ -142,8 +142,9 @@ class Functions:
def read_index(index_file):
index = OrderedDict()
with open(index_file, "r") as index_f:
lines = index_f.readlines()
for line in lines[1:]:
# Sample name without special chars:
sample_name = re.sub('[^A-Za-z0-9_\-.]+', '', index_f.readline().strip("\n").replace(" ", "_"))
for line in index_f:
if line != "":
parts = line.strip("\n").split("\t")
name = parts[0]
......@@ -153,7 +154,7 @@ class Functions:
"length": lenght,
"to_reverse": to_reverse
return index
return index, sample_name
......@@ -162,25 +163,26 @@ class Functions:
def send_fasta_ready(mailer, job_name):
def send_fasta_ready(mailer, job_name, sample_name, compressed=False):
config_reader = AppConfigReader()
web_url = config_reader.get_web_url()
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "mail_templates", "dl_fasta.html")) \
as t_file:
template = Template(
message_html = template.render(job_name=job_name, status="success", url_base=web_url)
message_html = template.render(job_name=job_name, status="success", url_base=web_url,
sample_name=sample_name, compressed=compressed)
message = "D-Genies\n\n" \
"Job %s - Download fasta\n\n" % job_name
message += "Query fasta file for job %s is ready to download." % job_name
message += "Query fasta file for job %s (query: %s) is ready to download.\n" % (job_name, sample_name)
message += "You can click on the link below to download it:\n\n"
message += "%s/fasta-query/%s" % (web_url, job_name)
message += "%s/fasta-query/%s/%s" % (web_url, job_name, sample_name + ".fasta" + (".gz" if compressed else ""))
mailer.send_mail([Functions.get_mail_for_job(job_name)], "Job %s - Download fasta" % job_name, message,
def sort_fasta(job_name, fasta_file, index_file, lock_file, compress=False, mailer=None):
print("Loading index...")
index = Functions.read_index(index_file)
index, sample_name = Functions.read_index(index_file)
print("Starting fasta sort...")
is_compressed = fasta_file.endswith(".gz")
if is_compressed:
......@@ -200,5 +202,13 @@ class Functions:
if mailer is not None:
Functions.send_fasta_ready(mailer, job_name)
Functions.send_fasta_ready(mailer, job_name, sample_name, compress)
print("Fasta sort done!")
def compress_and_send_mail(job_name, fasta_file, index_file, compressed, mailer):
index, sample_name = Functions.read_index(index_file)
Functions.send_fasta_ready(mailer, job_name, sample_name, compressed)
\ No newline at end of file
......@@ -28,8 +28,8 @@
{% if status == "success" %}
<p>Query fasta file for job {{ job_name }} is ready to download.
You can <a href="{{ url_base }}/fasta-query/{{ job_name }}">click here</a> to download it.</p>
<p>Query fasta file for job {{ job_name }} (query: {{ sample_name }}) is ready to download.<br/>
You can <a href="{{ url_base }}/fasta-query/{{ job_name }}/{{ sample_name }}.fasta{% if compressed %}.gz{% endif %}">click here</a> to download it.</p>
{% else %}
<p>Build of query fasta file for job {{ job_name }} has failed. You can try again. If the problem persists, please contact the support.</p>
{% endif %}
......@@ -207,6 +207,7 @@ 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"))
compressed = request.form["gzip"].lower() == "true"
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"):
......@@ -217,7 +218,7 @@ def build_fasta(id_res):
"fasta_file": query_fasta,
"index_file": os.path.join(res_dir, "query.idx.sorted"),
"lock_file": lock_query,
"compress": request.form["gzip"],
"compress": compressed,
"mailer": mailer
......@@ -227,6 +228,18 @@ def build_fasta(id_res):
return jsonify({"success": True, "status": 1, "status_message": "In progress"})
# No sort to do or sort done
if compressed and not query_fasta.endswith(".gz.fasta"):
# If compressed file is asked, we must compress it now if not done before...
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"),
"compressed": compressed,
"mailer": mailer
return jsonify({"success": True, "status": 1, "status_message": "In progress"})
return jsonify({"success": True, "status": 2, "status_message": "Done",
"gzip": query_fasta.endswith(".gz") or query_fasta.endswith(".gz.sorted")})
......@@ -234,8 +247,9 @@ def build_fasta(id_res):
"message": "Unable to get fasta file for query. Please contact us to report the bug"})
@app.route('/fasta-query/<id_res>', methods=['GET'])
def dl_fasta(id_res):
@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):
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"))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment