diff options
Diffstat (limited to 'qc_app/upload/rqtl2.py')
-rw-r--r-- | qc_app/upload/rqtl2.py | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/qc_app/upload/rqtl2.py b/qc_app/upload/rqtl2.py index 0eca6ae..e020d5a 100644 --- a/qc_app/upload/rqtl2.py +++ b/qc_app/upload/rqtl2.py @@ -1,5 +1,6 @@ """Module to handle uploading of R/qtl2 bundles.""" from pathlib import Path +from typing import Optional from zipfile import ZipFile, is_zipfile from flask import ( @@ -7,6 +8,7 @@ from flask import ( request, url_for, redirect, + Response, Blueprint, render_template, current_app as app) @@ -14,14 +16,15 @@ from flask import ( from r_qtl import r_qtl2 from r_qtl.errors import InvalidFormat -from qc_app.files import save_file +from qc_app.files import save_file, fullpath 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 ( species_by_id, save_population, populations_by_species, - population_by_species_and_id) + population_by_species_and_id, + geno_dataset_by_species_and_population) rqtl2 = Blueprint("rqtl2", __name__) @@ -158,3 +161,69 @@ def upload_rqtl2_bundle(species_id: int, population_id: int): except (InvalidFormat, __RequestError__) as exc: flash("".join(exc.args), "alert-error alert-danger error-rqtl2") return this_page_with_errors + +def check_errors(conn, *args, **kwargs) -> Optional[Response]: + """Check for select errors in the forms and return a page to redirect to.""" + species_id = kwargs.get("species_id") or request.form.get("species_id") + population_id = (kwargs.get("population_id") + or request.form.get("population_id")) + species = species_by_id(conn, species_id) + population = population_by_species_and_id(conn, species_id, population_id) + + if "species" in args and not bool(species): + flash("Invalid species!", "alert-error error-rqtl2") + return redirect(url_for("upload.rqtl2.select_species")) + + if "population" in args and not bool(population): + flash("Invalid Population!", "alert-error error-rqtl2") + return redirect( + url_for("upload.rqtl2.select_population", pgsrc="error"), + code=307) + + if ("rqtl2_bundle_file" in args + and not bool(request.form.get("rqtl2_bundle_file"))): + flash("There is no file to process.", + "alert-error alert-danger error-rqtl2") + return redirect(url_for("upload.rqtl2.upload_rqtl2_bundle", + species_id=species_id, + population_id=population_id, + pgsrc="error"), + code=307) + + return False + +@rqtl2.route(("/upload/species/<int:species_id>/population/<int:population_id>" + "/rqtl2-bundle/dataset-info"), + methods=["POST"]) +def select_dataset_info(species_id: int, population_id: int): + """ + If `geno` files exist in the R/qtl2 bundle, prompt user to provide the + dataset the genotypes belong to. + """ + form = request.form + with database_connection(app.config["SQL_URI"]) as conn: + error_page = check_errors(conn, "species", "population", "rqtl2_bundle_file") + if bool(error_page): + return error_page + + thefile = fullpath(form["rqtl2_bundle_file"]) + with ZipFile(str(thefile), "r") as zfile: + cdata = r_qtl2.control_data(zfile) + if "geno" in cdata and not bool(form.get("geno_datasetid")): + return render_template( + "rqtl2/select-geno-dataset.html", + species=species_by_id(conn, species_id), + population=population_by_species_and_id( + conn, species_id, population_id), + rqtl2_bundle_file=thefile.name, + datasets=geno_dataset_by_species_and_population( + conn, species_id, population_id)) + + return "All data points collected. Should proceed to launching the job." + +@rqtl2.route(("/upload/species/<int:species_id>/population/<int:population_id>" + "/rqtl2-bundle/select-geno-dataset"), + methods=["POST"]) +def select_geno_dataset(species_id: int, population_id: int) -> Response: + """Select from existing geno datasets.""" + return "IMPLEMENT THIS!!!" |