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: ...@@ -142,8 +142,9 @@ class Functions:
def read_index(index_file): def read_index(index_file):
index = OrderedDict() index = OrderedDict()
with open(index_file, "r") as index_f: with open(index_file, "r") as index_f:
lines = index_f.readlines() # Sample name without special chars:
for line in lines[1:]: sample_name = re.sub('[^A-Za-z0-9_\-.]+', '', index_f.readline().strip("\n").replace(" ", "_"))
for line in index_f:
if line != "": if line != "":
parts = line.strip("\n").split("\t") parts = line.strip("\n").split("\t")
name = parts[0] name = parts[0]
...@@ -153,7 +154,7 @@ class Functions: ...@@ -153,7 +154,7 @@ class Functions:
"length": lenght, "length": lenght,
"to_reverse": to_reverse "to_reverse": to_reverse
} }
return index return index, sample_name
@staticmethod @staticmethod
@db_session @db_session
...@@ -162,25 +163,26 @@ class Functions: ...@@ -162,25 +163,26 @@ class Functions:
return j1.email return j1.email
@staticmethod @staticmethod
def send_fasta_ready(mailer, job_name): def send_fasta_ready(mailer, job_name, sample_name, compressed=False):
config_reader = AppConfigReader() config_reader = AppConfigReader()
web_url = config_reader.get_web_url() web_url = config_reader.get_web_url()
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "mail_templates", "dl_fasta.html")) \ with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "mail_templates", "dl_fasta.html")) \
as t_file: as t_file:
template = Template(t_file.read()) template = Template(t_file.read())
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" \ message = "D-Genies\n\n" \
"Job %s - Download fasta\n\n" % job_name "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 += "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, mailer.send_mail([Functions.get_mail_for_job(job_name)], "Job %s - Download fasta" % job_name, message,
message_html) message_html)
@staticmethod @staticmethod
def sort_fasta(job_name, fasta_file, index_file, lock_file, compress=False, mailer=None): def sort_fasta(job_name, fasta_file, index_file, lock_file, compress=False, mailer=None):
print("Loading index...") print("Loading index...")
index = Functions.read_index(index_file) index, sample_name = Functions.read_index(index_file)
print("Starting fasta sort...") print("Starting fasta sort...")
is_compressed = fasta_file.endswith(".gz") is_compressed = fasta_file.endswith(".gz")
if is_compressed: if is_compressed:
...@@ -200,5 +202,13 @@ class Functions: ...@@ -200,5 +202,13 @@ class Functions:
Functions.compress(fasta_file_o) Functions.compress(fasta_file_o)
os.remove(lock_file) os.remove(lock_file)
if mailer is not None: 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!") print("Fasta sort done!")
@staticmethod
def compress_and_send_mail(job_name, fasta_file, index_file, compressed, mailer):
print("Compress!")
Functions.compress(fasta_file)
index, sample_name = Functions.read_index(index_file)
Functions.send_fasta_ready(mailer, job_name, sample_name, compressed)
print("DONE!")
\ No newline at end of file
...@@ -28,8 +28,8 @@ ...@@ -28,8 +28,8 @@
</h3> </h3>
<p>Hi,</p> <p>Hi,</p>
{% if status == "success" %} {% if status == "success" %}
<p>Query fasta file for job {{ job_name }} is ready to download. <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 }}">click here</a> to download it.</p> 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 %} {% 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> <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 %} {% endif %}
......
...@@ -207,6 +207,7 @@ def build_fasta(id_res): ...@@ -207,6 +207,7 @@ def build_fasta(id_res):
res_dir = os.path.join(app_data, id_res) res_dir = os.path.join(app_data, id_res)
lock_query = os.path.join(res_dir, ".query-fasta-build") lock_query = os.path.join(res_dir, ".query-fasta-build")
is_sorted = os.path.exists(os.path.join(res_dir, ".sorted")) 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) query_fasta = Functions.get_fasta_file(res_dir, "query", is_sorted)
if query_fasta is not None: if query_fasta is not None:
if is_sorted and not query_fasta.endswith(".sorted"): if is_sorted and not query_fasta.endswith(".sorted"):
...@@ -217,7 +218,7 @@ def build_fasta(id_res): ...@@ -217,7 +218,7 @@ def build_fasta(id_res):
"fasta_file": query_fasta, "fasta_file": query_fasta,
"index_file": os.path.join(res_dir, "query.idx.sorted"), "index_file": os.path.join(res_dir, "query.idx.sorted"),
"lock_file": lock_query, "lock_file": lock_query,
"compress": request.form["gzip"], "compress": compressed,
"mailer": mailer "mailer": mailer
}) })
thread.start() thread.start()
...@@ -227,6 +228,18 @@ def build_fasta(id_res): ...@@ -227,6 +228,18 @@ def build_fasta(id_res):
return jsonify({"success": True, "status": 1, "status_message": "In progress"}) return jsonify({"success": True, "status": 1, "status_message": "In progress"})
else: else:
# No sort to do or sort done # 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...
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"),
"compressed": compressed,
"mailer": mailer
})
thread.start()
return jsonify({"success": True, "status": 1, "status_message": "In progress"})
return jsonify({"success": True, "status": 2, "status_message": "Done", return jsonify({"success": True, "status": 2, "status_message": "Done",
"gzip": query_fasta.endswith(".gz") or query_fasta.endswith(".gz.sorted")}) "gzip": query_fasta.endswith(".gz") or query_fasta.endswith(".gz.sorted")})
else: else:
...@@ -234,8 +247,9 @@ def build_fasta(id_res): ...@@ -234,8 +247,9 @@ def build_fasta(id_res):
"message": "Unable to get fasta file for query. Please contact us to report the bug"}) "message": "Unable to get fasta file for query. Please contact us to report the bug"})
@app.route('/fasta-query/<id_res>', methods=['GET']) @app.route('/fasta-query/<id_res>', defaults={'filename': ""}, methods=['GET'])
def dl_fasta(id_res): @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) res_dir = os.path.join(app_data, id_res)
lock_query = os.path.join(res_dir, ".query-fasta-build") lock_query = os.path.join(res_dir, ".query-fasta-build")
is_sorted = os.path.exists(os.path.join(res_dir, ".sorted")) 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