Skip to content

Commit

Permalink
issue #1171 - get working for sample as well
Browse files Browse the repository at this point in the history
  • Loading branch information
davmlaw committed Sep 19, 2024
1 parent 91f8e9a commit dca7df2
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 84 deletions.
19 changes: 19 additions & 0 deletions analysis/tasks/analysis_grid_export_tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import uuid
from typing import Optional

import celery

Expand All @@ -16,6 +17,24 @@
from snpdb.models import Cohort, Sample, CachedGeneratedFile


def get_annotated_download_files_cgf(generator, pk) -> dict[str, Optional[CachedGeneratedFile]]:
annotated_download_files = {}
try:
AnalysisTemplate.get_template_from_setting("ANALYSIS_TEMPLATES_AUTO_COHORT_EXPORT")
params_hash_vcf = get_grid_downloadable_file_params_hash(pk, "vcf")
cgf_vcf = CachedGeneratedFile.objects.filter(generator=generator,
params_hash=params_hash_vcf).first()
params_hash_csv = get_grid_downloadable_file_params_hash(pk, "csv")
cgf_csv = CachedGeneratedFile.objects.filter(generator=generator,
params_hash=params_hash_csv).first()

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

return annotated_download_files


def get_grid_downloadable_file_params_hash(pk, export_type):
return sha256sum_str(f"{pk}-{export_type}")

Expand Down
2 changes: 1 addition & 1 deletion analysis/templates/analysis/node_data/node_data_graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load static %}
{% block top %}
<link rel="stylesheet" href="{% static 'css/graphs.css' %}" />
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
{% endblock top %}

{% block css %}
Expand Down
4 changes: 3 additions & 1 deletion analysis/views/views_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def sample_grid_export(request, sample_id, export_type):
params_hash = get_grid_downloadable_file_params_hash(sample_id, export_type)
task = export_sample_to_downloadable_file.si(sample_id, export_type)
cgf = CachedGeneratedFile.get_or_create_and_launch("export_sample_to_downloadable_file", params_hash, task)
return JsonResponse({"celery_task": cgf.task_id})
if cgf.exception:
raise ValueError(cgf.exception)
return redirect(cgf)


def node_grid_export(request, analysis_id):
Expand Down
2 changes: 1 addition & 1 deletion genes/templates/genes/view_gene_symbol.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<link rel="stylesheet" href="{% static 'css/graphs.css' %}" />
<script type="text/javascript" src="{% static 'js/lib/plotly-latest.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/grid.js' %}"></script>
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
<style>
#hotspot-graph, #hotspot-graph img {
width: 100%;
Expand Down
2 changes: 1 addition & 1 deletion pedigree/templates/pedigree/view_ped_file.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block head %}
{{ block.super }}
<link rel="stylesheet" href="{% static 'css/graphs.css' %}" />
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
{% endblock %}

{% block css %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}

</style>
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
<script>
{% if qc.data_state == 'C' and exec_summary.data_state == 'C' %}
EXEC_SUMMARY_ID = {{ exec_summary.pk }};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% load seqauto_record_tags %}
<link rel="stylesheet" href="{% static 'css/graphs.css' %}" />
<link rel="stylesheet" href="{% static 'css/seqauto.css' %}" />
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
<script>
var DELETE_URL = "{% url 'cached_generated_file_delete' %}";
var BASE_QC_POLL_URL = "{% url 'sequencing_run_qc_graph' sequencing_run.pk 'QC_COMPARE_TYPE' %}";
Expand Down
2 changes: 1 addition & 1 deletion seqauto/templates/seqauto/view_sequencing_run.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block head %}
<link rel="stylesheet" href="{% static 'css/graphs.css' %}" />
<link rel="stylesheet" href="{% static 'css/seqauto.css' %}" />
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>

<style>
input#id_path {
Expand Down
12 changes: 12 additions & 0 deletions snpdb/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import json
import logging
import os
import re
from dataclasses import dataclass
from datetime import datetime
Expand All @@ -25,6 +26,8 @@
from django.db import models
from django.db.models import QuerySet, TextChoices
from django.db.models.deletion import SET_NULL, CASCADE, PROTECT
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.urls import reverse
from django.utils import timezone
from django.utils.safestring import SafeString
Expand Down Expand Up @@ -121,6 +124,15 @@ def save(self, **kwargs):
logging.debug("Not caching generated files - skipped save!")


@receiver(pre_delete, sender=CachedGeneratedFile)
def cgf_pre_delete_handler(sender, instance, **kwargs): # pylint: disable=unused-argument
if instance.filename:
if os.path.exists(instance.filename):
os.unlink(instance.filename)
dirname = os.path.dirname(instance.filename)
if os.path.isdir(dirname):
os.rmdir(dirname)

class Company(models.Model):
name = models.TextField(primary_key=True)

Expand Down
2 changes: 1 addition & 1 deletion snpdb/templates/snpdb/data/sample_graphs_tab.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% load static %}
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
<style>
.generated-graph, .generated-graph img {
width: 640px;
Expand Down
2 changes: 1 addition & 1 deletion snpdb/templates/snpdb/data/view_genomic_intervals.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block title %}{{ gic.name }}{% endblock %}
{% block head %}
<link rel="stylesheet" href="{% static 'css/graphs.css' %}" />
<script src="{% static 'js/generated_graphs.js' %}"></script>
<script src="{% static 'js/cached_generated_files.js' %}"></script>
<style>
#genomic-intervals-graph, #genomic-intervals-graph img {
width: 640px;
Expand Down
28 changes: 23 additions & 5 deletions snpdb/templates/snpdb/data/view_sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
margin-left: 2px;
}
</style>
<script type="text/javascript" src="{% static 'js/cached_generated_files.js' %}"></script>
<script>
function getBams() {
const bams = {{ bam_list | jsonify }};
Expand Down Expand Up @@ -68,6 +69,24 @@
const dialog = setupCreatePatientDialog();
const sampleName = $("#id_name").val();
addCreatePatientButton(dialog, patientSelect, sampleName, "p");

{% for file_type, cgf in annotated_download_files.items %}
var pollUrl = Urls.sample_grid_export({{ sample.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>
<!-- jqGrid is used in variants tab -->
Expand Down Expand Up @@ -122,14 +141,13 @@ <h1>Sample: {{ sample.name }}</h1>
<form method="post" id='sample-form'>
{% csrf_token %}
{% crispy form form_helper.horizontal %}
{% if can_download_vcf %}
{% if annotated_download_files %}
<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 can_download_annotated_vcf %}
<div class="p-2"><a href="{% url 'sample_grid_export' sample.pk 'vcf' %}"><div class="icon24 left margin-r-5 vcf-icon"></div> Annotated Sample VCF</a></div>
<div class="p-2"><a href="{% url 'sample_grid_export' sample.pk 'csv' %}"><div class="icon24 left margin-r-5 csv-icon"></div> Annotated Sample CSV</a></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
47 changes: 1 addition & 46 deletions snpdb/templates/snpdb/data/view_vcf.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% block title %}{{ vcf.name }}{% endblock %}
{% block head %}
<script type="text/javascript" src="{% static 'js/lib/plotly-latest.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/generated_graphs.js' %}"></script>
<script type="text/javascript" src="{% static 'js/cached_generated_files.js' %}"></script>
<script type="text/javascript" src="{% static 'js/plotly_helper.js' %}"></script>

<style>
Expand Down Expand Up @@ -283,51 +283,6 @@
});
});

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");
}

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>`);
}

setError() {
// console.log("setError");
this.selector.html('<i class="fas fa-xmark"></i> Error retrieving download...');
}

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)
}
}

{% 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");
Expand Down
34 changes: 10 additions & 24 deletions snpdb/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from analysis.analysis_templates import get_sample_analysis
from analysis.forms import AnalysisOutputNodeChoiceForm
from analysis.models import AnalysisTemplate
from analysis.tasks.analysis_grid_export_tasks import get_grid_downloadable_file_params_hash
from analysis.tasks.analysis_grid_export_tasks import get_grid_downloadable_file_params_hash, \
get_annotated_download_files_cgf
from annotation.forms import GeneCountTypeChoiceForm
from annotation.manual_variant_entry import create_manual_variants, can_create_variants
from annotation.models import AnnotationVersion, SampleVariantAnnotationStats, SampleGeneAnnotationStats, \
Expand Down Expand Up @@ -283,19 +284,9 @@ def view_vcf(request, vcf_id):
can_view_upload_pipeline = False

annotated_download_files = {}
if vcf.import_status == ImportStatus.SUCCESS and cohort_id:
try:
AnalysisTemplate.get_template_from_setting("ANALYSIS_TEMPLATES_AUTO_COHORT_EXPORT")
params_hash_vcf = get_grid_downloadable_file_params_hash(cohort_id, "vcf")
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")
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
if not settings.VCF_DOWNLOAD_ADMIN_ONLY or request.user.is_superuser:
if vcf.import_status == ImportStatus.SUCCESS and cohort_id:
annotated_download_files = get_annotated_download_files_cgf("export_cohort_to_downloadable_file", cohort_id)

context = {
'vcf': vcf,
Expand All @@ -307,7 +298,6 @@ def view_vcf(request, vcf_id):
'samples_form': samples_form,
'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_view_upload_pipeline': can_view_upload_pipeline,
'annotated_download_files': annotated_download_files,
"variant_zygosity_count_collections": variant_zygosity_count_collections,
Expand Down Expand Up @@ -406,22 +396,18 @@ def view_sample(request, sample_id):
related_samples = SomalierRelatePairs.get_for_sample(sample).order_by("relate")

sample_stats_variant_class_df, sample_stats_zygosity_df = _sample_stats(sample)
can_download_annotated_vcf = False
if sample.import_status == ImportStatus.SUCCESS:
try:
AnalysisTemplate.get_template_from_setting("ANALYSIS_TEMPLATES_AUTO_SAMPLE")
can_download_annotated_vcf = True
except ValueError:
pass
annotated_download_files = {}
if not settings.VCF_DOWNLOAD_ADMIN_ONLY or request.user.is_superuser:
if sample.import_status == ImportStatus.SUCCESS:
annotated_download_files = get_annotated_download_files_cgf("export_sample_to_downloadable_file", sample.pk)

context = {
'annotated_download_files': annotated_download_files,
'sample': sample,
'samples': [sample],
'sample_locus_count': sample_locus_count,
'form': form,
'patient_form': patient_form,
'can_download_vcf': (not settings.VCF_DOWNLOAD_ADMIN_ONLY) or request.user.is_superuser,
'can_download_annotated_vcf': can_download_annotated_vcf,
'cohorts': cohorts,
'has_write_permission': has_write_permission,
'igv_data': igv_data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,49 @@ function poll_cached_generated_file(poll_url, success_func, failure_func) {
}
}).fail(failure_func);
}


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");
}

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>`);
}

setError() {
// console.log("setError");
this.selector.html('<i class="fas fa-xmark"></i> Error retrieving download...');
}

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)
}
}

0 comments on commit dca7df2

Please sign in to comment.