diff options
author | Frederick Muriuki Muriithi | 2024-01-08 07:25:35 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-01-08 07:25:35 +0300 |
commit | 8595c75345fb5849a4d8894f3ee7a926dd8cdf8f (patch) | |
tree | ee4a18e3aed146028b5a9463ecc2d5c12252d81c /qc_app/upload/rqtl2.py | |
parent | ea89a07e6d04c2fe58f94839eb9916b365b2f3f2 (diff) | |
download | gn-uploader-8595c75345fb5849a4d8894f3ee7a926dd8cdf8f.tar.gz |
Upload R/qtl2 zip bundle and check for errors.
Diffstat (limited to 'qc_app/upload/rqtl2.py')
-rw-r--r-- | qc_app/upload/rqtl2.py | 56 |
1 files changed, 56 insertions, 0 deletions
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/<int:species_id>/population/<int:population_id>" + "/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 |