Skip to content

Commit

Permalink
issue #1171 - generate download
Browse files Browse the repository at this point in the history
  • Loading branch information
davmlaw committed Sep 19, 2024
1 parent 4f3d973 commit 91f8e9a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 72 deletions.
8 changes: 8 additions & 0 deletions analysis/grid_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def _grid_export_vcf(genome_build, colmodels, items, sample_ids, sample_names_by
pseudo_buffer = StashFile()

vcf_writer = Writer(pseudo_buffer, vcf_reader)
# Need to pass escapechar
vcf_writer.writer = csv.writer(
pseudo_buffer,
delimiter="\t",
lineterminator="\n",
quoting=csv.QUOTE_NONE,
escapechar="\\",
)

def iter_row_writer():
for obj in items:
Expand Down
2 changes: 2 additions & 0 deletions analysis/views/views_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def cohort_grid_export(request, cohort_id, export_type):
params_hash = get_grid_downloadable_file_params_hash(cohort_id, export_type)
task = export_cohort_to_downloadable_file.si(cohort_id, export_type)
cgf = CachedGeneratedFile.get_or_create_and_launch("export_cohort_to_downloadable_file", params_hash, task)
if cgf.exception:
raise ValueError(cgf.exception)
return redirect(cgf)


Expand Down
2 changes: 1 addition & 1 deletion library/django_utils/jqgrid_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def grid_export_csv(colmodels, items) -> Iterator[str]:
pseudo_buffer = StashFile()
header, labels = colmodel_header_labels(colmodels, label_overrides=label_overrides)
# Don't use dictwriter as some sample names may be the same
writer = csv.writer(pseudo_buffer, dialect='excel')
writer = csv.writer(pseudo_buffer, dialect='excel', escapechar='\\', quoting=csv.QUOTE_NONE)
writer.writerow(header)

def iter_row_writer():
Expand Down
2 changes: 1 addition & 1 deletion snpdb/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CachedGeneratedFile(models.Model):
generator = models.TextField()
params_hash = models.TextField() # sha256 of params
task_id = models.CharField(max_length=36, null=True)
task_status = models.TextField(null=True) # TODO: what's the actual size?
task_status = models.TextField(null=True)
generate_start = models.DateTimeField(null=True)
generate_end = models.DateTimeField(null=True)

Expand Down
111 changes: 57 additions & 54 deletions snpdb/templates/snpdb/data/view_vcf.html
Original file line number Diff line number Diff line change
Expand Up @@ -283,42 +283,67 @@
});
});

function pollAndDownloadFile(selector, fileType) {
const spinnerId = 'spinner-' + fileType;
function downloadFile(data) {
/*
$("#" + spinnerId).replaceWith('<i class="fas fa-xmark"></i> Error retrieving download...');
<a href="{{ annotated_vcf_url }}">
<div class="icon24 left margin-r-5 vcf-icon"></div> Annotated VCF
</a>
class AnnotatedFileDownload {
constructor(selector, pollUrl, fileType) {
this.selector = $(selector);
this.pollUrl = pollUrl;
this.fileType = fileType;
this.ucFileType = this.fileType.toUpperCase()
}

setGenerateDownloadLink() {
// console.log("setGenerateDownloadLink");
const that = this;
const generateLink = $(`<a href="#" id="generate-${this.fileType}-download"><div class="icon24 left margin-r-5 ${this.fileType}-icon"></div> Annotated ${this.ucFileType}</a>`);
generateLink.click(function(event) {
event.preventDefault();
that.setPolling(true);
});
this.selector.empty();
this.selector.append(generateLink);
console.log("/setGenerateDownloadLink");
}

*/
window.location.href = data.url;
setDownloadLink(url) {
// console.log("setDownloadLink");
this.selector.html(`<a href="${url}"><div class="icon24 left margin-r-5 ${this.fileType}-icon"></div> Annotated ${this.ucFileType}</a>`);
}

let failureFunc = function(data) {
console.log("Fail!");
$("#" + spinnerId).replaceWith('<i class="fas fa-xmark"></i> Error retrieving download...');
setError() {
// console.log("setError");
this.selector.html('<i class="fas fa-xmark"></i> Error retrieving download...');
}

$(selector).replaceWith(`<span id="${spinnerId}"><i class="fas fa-spinner fa-spin"></i> Preparing download...</span>`);
let pollUrl = Urls.cohort_grid_export({{ vcf.cohort.pk }}, fileType);
poll_cached_generated_file(pollUrl, downloadFile, failureFunc);
setPolling(download) {
// console.log("setPolling");
let that = this;
function downloadFile(data) {
that.setDownloadLink(data.url);
if (download) {
window.location.href = data.url;
}
}
$(this.selector).html(`<span><i class="fas fa-spinner fa-spin"></i> Preparing download...</span>`);
poll_cached_generated_file(this.pollUrl, downloadFile, this.setError)
}
}

$("#generate-vcf-download").click(function(event) {
event.preventDefault();
pollAndDownloadFile(this, "vcf");
});


$("#generate-csv-download").click(function(event) {
event.preventDefault();
pollAndDownloadFile(this, "csv");
});

{% for file_type, cgf in annotated_download_files.items %}
var pollUrl = Urls.cohort_grid_export({{ vcf.cohort.pk }}, "{{ file_type }}");
var afdSelector = $("div#generate-{{ file_type }}-download");
var afd = new AnnotatedFileDownload(afdSelector, pollUrl, "{{ file_type }}");
{% if cgf %}
{% if cgf.exception %}
afd.setError()
{% elif cgf.status == "SUCCESS" %}
afd.setDownloadLink("{{ cgf.get_media_url }}");
{% else %}
afd.setPolling(false);
{% endif %}
{% else %}
afd.setGenerateDownloadLink();
{% endif %}
{% endfor %}
});
</script>
<script type="text/javascript" src="{% static 'js/grid.js' %}"></script>
Expand Down Expand Up @@ -351,38 +376,16 @@ <h1>VCF: {{ vcf.name }}</h1>
{% csrf_token %}
{% crispy vcf_form form_helper.horizontal %}

{% if can_download_vcf %}
{% if annotated_download_files or vcf.uploadedvcf.uploaded_file.exists %}
<div class="form-group-row row">
<label class="col-form-label col-12 col-md-3 text-md-right">Download</label>
<div class="d-flex flex-row">
{% if vcf.uploadedvcf.uploaded_file.exists %}
<div class="p-2"><a href="{% url 'download_uploaded_file' vcf.uploadedvcf.uploaded_file.pk %}"><div class="icon24 left margin-r-5 vcf-icon"></div> Original VCF</a> ({{ vcf.uploadedvcf.uploaded_file.size|filesizeformat }})</div>
{% endif %}
{% if can_download_annotated_vcf %}
<div class="p-2">
{% if annotated_vcf_url %}
<a href="{{ annotated_vcf_url }}">
<div class="icon24 left margin-r-5 vcf-icon"></div> Annotated VCF
</a>
{% else %}
<a href="#" id="generate-vcf-download">
<div class="icon24 left margin-r-5 vcf-icon"></div> Annotated VCF
</a>
{% endif %}
</div>
<div class="p-2">
{% if annotated_csv_url %}
<a href="{{ annotated_csv_url }}">
<div class="icon24 left margin-r-5 csv-icon"></div> Annotated CSV
</a>
{% else %}
<a href="#" id="generate-csv-download">
<div class="icon24 left margin-r-5 csv-icon"></div> Annotated CSV
</a>
{% endif %}

</div>
{% endif %}
{% for file_type in annotated_download_files %}
<div id="generate-{{ file_type }}-download" class="p-2"></div>
{% endfor %}
</div>
</div>
{% endif %}
Expand Down
21 changes: 8 additions & 13 deletions snpdb/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,18 @@ def view_vcf(request, vcf_id):
except UploadedVCF.DoesNotExist:
can_view_upload_pipeline = False

can_download_annotated_vcf = False
annotated_vcf_url = None
annotated_csv_url = None
annotated_download_files = {}
if vcf.import_status == ImportStatus.SUCCESS and cohort_id:
try:
AnalysisTemplate.get_template_from_setting("ANALYSIS_TEMPLATES_AUTO_COHORT_EXPORT")
can_download_annotated_vcf = True
params_hash_vcf = get_grid_downloadable_file_params_hash(cohort_id, "vcf")
if cgf_vcf := CachedGeneratedFile.objects.filter(generator="export_cohort_to_downloadable_file",
params_hash=params_hash_vcf).first():
annotated_vcf_url = cgf_vcf.get_media_url()
cgf_vcf = CachedGeneratedFile.objects.filter(generator="export_cohort_to_downloadable_file",
params_hash=params_hash_vcf).first()
params_hash_csv = get_grid_downloadable_file_params_hash(cohort_id, "csv")
if cgf_csv := CachedGeneratedFile.objects.filter(generator="export_cohort_to_downloadable_file",
params_hash=params_hash_csv).first():
annotated_csv_url = cgf_csv.get_media_url()
cgf_csv = CachedGeneratedFile.objects.filter(generator="export_cohort_to_downloadable_file",
params_hash=params_hash_csv).first()

annotated_download_files = {"vcf": cgf_vcf, "csv": cgf_csv}
except ValueError:
pass

Expand All @@ -311,10 +308,8 @@ def view_vcf(request, vcf_id):
'patient_form': PatientForm(user=request.user), # blank
'has_write_permission': has_write_permission,
'can_download_vcf': (not settings.VCF_DOWNLOAD_ADMIN_ONLY) or request.user.is_superuser,
'can_download_annotated_vcf': can_download_annotated_vcf,
'can_view_upload_pipeline': can_view_upload_pipeline,
'annotated_vcf_url': annotated_vcf_url,
'annotated_csv_url': annotated_csv_url,
'annotated_download_files': annotated_download_files,
"variant_zygosity_count_collections": variant_zygosity_count_collections,
}
return render(request, 'snpdb/data/view_vcf.html', context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function poll_graph_status(graph_selector, poll_url, delete_url) {


function poll_cached_generated_file(poll_url, success_func, failure_func) {
$.getJSON(poll_url, function(data) {
$.getJSON(poll_url, function (data) {
if (data.status == "SUCCESS") {
success_func(data);
} else if (data.status == 'FAILURE') {
Expand All @@ -59,5 +59,5 @@ function poll_cached_generated_file(poll_url, success_func, failure_func) {
};
window.setTimeout(retry_func, freq);
}
});
}
}).fail(failure_func);
}

0 comments on commit 91f8e9a

Please sign in to comment.