Skip to content

Commit

Permalink
export now uses temp file to overcome file size limit when cramming i…
Browse files Browse the repository at this point in the history
…t into base64 encoded string
  • Loading branch information
proost committed Nov 8, 2017
1 parent 6bce197 commit dbcbd0e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
28 changes: 24 additions & 4 deletions conekt/controllers/expression_profile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json

from flask import Blueprint, redirect, url_for, render_template, Response, request
from flask import Blueprint, redirect, url_for, render_template, Response, request, current_app, send_from_directory
from sqlalchemy.orm import undefer

from statistics import mean
import sys
import tempfile
import os

from conekt import cache
from conekt.helpers.chartjs import prepare_expression_profile, prepare_profile_comparison
Expand Down Expand Up @@ -266,7 +268,7 @@ def __generate(species_id, method_id, condition):
:param condition: Condition to be exported
:return: output
"""
yield "Sequence\tAvg.Expression\tMin.Expression\tMax.Expression\n"
yield "Sequence\tAliases\tDescription\tAvg.Expression\tMin.Expression\tMax.Expression\n"

profiles = ExpressionProfile.query.filter(ExpressionProfile.species_id == species_id). \
filter(ExpressionProfile.sequence_id is not None). \
Expand All @@ -289,7 +291,10 @@ def __generate(species_id, method_id, condition):
data, use_means=True)
values = converted_profile["data"][condition]

yield "%s\t%f\t%f\t%f\n" % (p.sequence.name, mean(values), min(values), max(values))
aliases = p.sequence.aliases if p.sequence.aliases is not None else ""
description = p.sequence.description if p.sequence.description is not None else ""

yield "%s\t%s\t%s\t%f\t%f\t%f\n" % (p.sequence.name, aliases, description, mean(values), min(values), max(values))

except Exception as e:
print("An error occured exporting a profile with conditions %s for species %d." % (condition, species_id),
Expand All @@ -312,6 +317,21 @@ def export_expression_levels():
method_id = int(request.form.get('methods'))
condition = request.form.get('conditions')

return Response(__generate(species_id, method_id, condition), mimetype="text/plain")
_, filepath = tempfile.mkstemp(prefix='expr_', dir=current_app.config['TMP_DIR'])

filename = os.path.basename(filepath)
print(filepath, filename)

with open(filepath, "w") as fout:
for l in __generate(species_id, method_id, condition):
print(l, end='', file=fout)

return Response(json.dumps({"url": url_for('expression_profile.export_expression_levels_file', name=filename)}), mimetype='application/json')
else:
return render_template("export_condition.html", form=form)


@expression_profile.route('/export/get_file/<name>')
def export_expression_levels_file(name):
return send_from_directory(current_app.config['TMP_DIR'], name, as_attachment=True, attachment_filename='expression.tab')

6 changes: 4 additions & 2 deletions conekt/templates/export_condition.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ <h1>Export expression levels</h1>
success: function(data) {
$("#leafy_loader_text").html("Your Download is ready ! ");

pdata = JSON.parse(data);

var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data));
element.setAttribute('href', pdata.url);
element.setAttribute('download', 'expression.tab');

document.body.appendChild(element);
element.click();
document.body.removeChild(element);

$("#leafy_loader_text").append($("<a/>").attr('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data)).attr('download', 'expression.tab').attr('id', 'download_ready_link').text("Click here"))
$("#leafy_loader_text").append($("<a/>").attr('href', pdata.url).attr('download', 'expression.tab').attr('id', 'download_ready_link').text("Click here"))
.append($("<span> if the download doesn't start automatically.</span>"));
$("#leafy_loader_close_btn").show();
}
Expand Down
3 changes: 3 additions & 0 deletions config.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
WHOOSHEE_MEMORY_STORAGE = False
WHOOSHEE_ENABLE_INDEXING = True

# temp dir
TMP_DIR = tempfile.mkdtemp()

# BLAST settings
BLAST_ENABLED = False
BLAST_TMP_DIR = tempfile.mkdtemp()
Expand Down

0 comments on commit dbcbd0e

Please sign in to comment.