From 8595c75345fb5849a4d8894f3ee7a926dd8cdf8f Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 8 Jan 2024 07:25:35 +0300 Subject: Upload R/qtl2 zip bundle and check for errors. --- qc_app/upload/rqtl2.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'qc_app/upload') diff --git a/qc_app/upload/rqtl2.py b/qc_app/upload/rqtl2.py index 681e54c..53e7f3f 100644 --- a/qc_app/upload/rqtl2.py +++ b/qc_app/upload/rqtl2.py @@ -1,4 +1,6 @@ """Module to handle uploading of R/qtl2 bundles.""" +from pathlib import Path +from zipfile import ZipFile, is_zipfile from flask import ( flash, @@ -9,6 +11,10 @@ from flask import ( render_template, current_app as app) +from r_qtl import r_qtl2 +from r_qtl.errors import InvalidFormat + +from qc_app.files import save_file from qc_app.dbinsert import species as all_species from qc_app.db_utils import with_db_connection, database_connection from qc_app.db import ( @@ -96,3 +102,53 @@ def create_population(species_id: int): population_id=new_population["population_id"], pgsrc="create-population"), code=307) + +@rqtl2.route(("/upload/species//population/" + "/rqtl2-bundle"), + methods=["GET", "POST"]) +def upload_rqtl2_bundle(species_id: int, population_id: int): + """Allow upload of R/qtl2 bundle.""" + this_page_with_errors = redirect(url_for("upload.rqtl2.upload_rqtl2_bundle", + species_id=species_id, + population_id=population_id, + pgsrc="error"), + code=307) + + with database_connection(app.config["SQL_URI"]) as conn: + species = species_by_id(conn, species_id) + population = population_by_species_and_id( + conn, species["SpeciesId"], population_id) + if not bool(species): + flash("Invalid species!", "alert-error error-rqtl2") + return redirect(url_for("upload.rqtl2.select_species")) + if not bool(population): + flash("Invalid Population!", "alert-error error-rqtl2") + return redirect( + url_for("upload.rqtl2.select_population", pgsrc="error"), + code=307) + if request.method == "GET" or ( + request.method == "POST" + and bool(request.args.get("pgsrc"))): + return render_template("rqtl2/upload-rqtl2-bundle.html", + species=species, + population=population) + + the_file = save_file( + request.files.get("rqtl2_bundle"), Path(app.config["UPLOAD_FOLDER"])) + if not bool(the_file): + flash("Please provide a valid R/qtl2 zip bundle.", + "alert-error alert-danger error-rqtl2") + return this_page_with_errors + + if not is_zipfile(the_file): + flash("Invalid file! Expected a zip file.", + "alert-error alert-danger error-rqtl2") + return this_page_with_errors + + try: + with ZipFile(the_file, "r") as zfile: + r_qtl2.validate_bundle(zfile) + return "WOULD PROCESS THE BUNDLE..." + except InvalidFormat as invf: + flash("".join(invf.args), "alert-error alert-danger error-rqtl2") + return this_page_with_errors -- cgit v1.2.3