diff --git a/src/common/functions.py b/src/common/functions.py index 628daaec..fbdf41c4 100644 --- a/src/common/functions.py +++ b/src/common/functions.py @@ -546,4 +546,9 @@ def enbrace(string): def enpercent(string): string = str(string).strip('%') - return '%' + string + '%' \ No newline at end of file + return '%' + string + '%' + + +def rm(path): + if os.path.exists(path): + os.remove(path) \ No newline at end of file diff --git a/src/frontend_celery/webapp/variant/variant_functions.py b/src/frontend_celery/webapp/variant/variant_functions.py index 2a7ad466..84ed9dde 100644 --- a/src/frontend_celery/webapp/variant/variant_functions.py +++ b/src/frontend_celery/webapp/variant/variant_functions.py @@ -5,6 +5,7 @@ from ..io.download_routes import calculate_class from functools import cmp_to_key from flask import render_template +import uuid @@ -31,7 +32,7 @@ def validate_and_insert_variant(chrom, pos, ref, alt, genome_build, allowed_sequ - tmp_file_path = tempfile.gettempdir() + "/new_variant.vcf" + tmp_file_path = tempfile.gettempdir() + "/" + str(uuid.uuid4()) + ".vcf" functions.variant_to_vcf(chrom, pos, ref, alt, tmp_file_path) do_liftover = genome_build == 'GRCh37' @@ -41,10 +42,12 @@ def validate_and_insert_variant(chrom, pos, ref, alt, genome_build, allowed_sequ if returncode != 0: flash(err_msg, 'alert-danger') was_successful = False + functions.rm(tmp_file_path) return was_successful if 'ERROR:' in vcf_errors_pre: flash(vcf_errors_pre.replace('\n', ' '), 'alert-danger') was_successful = False + functions.rm(tmp_file_path) return was_successful if genome_build == 'GRCh37': unmapped_variants_vcf = open(tmp_file_path + '.lifted.unmap', 'r') @@ -58,10 +61,14 @@ def validate_and_insert_variant(chrom, pos, ref, alt, genome_build, allowed_sequ if unmapped_variant is not None: flash('ERROR: could not lift variant ' + unmapped_variant, ' alert-danger') was_successful = False + functions.rm(tmp_file_path) + functions.rm(tmp_file_path + ".lifted.unmap") return was_successful if 'ERROR:' in vcf_errors_post: flash(vcf_errors_post.replace('\n', ' '), 'alert-danger') was_successful = False + functions.rm(tmp_file_path) + functions.rm(tmp_file_path + ".lifted.unmap") return was_successful if was_successful: @@ -91,6 +98,8 @@ def validate_and_insert_variant(chrom, pos, ref, alt, genome_build, allowed_sequ flash(Markup("Variant not imported: already in database!! View it " + "here"), "alert-danger") was_successful = False + functions.rm(tmp_file_path) + functions.rm(tmp_file_path + ".lifted.unmap") return was_successful