From cde57e8faeb41df27587361dcf42e235545cacb3 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 17 Jan 2024 06:46:42 +0300 Subject: UI: Implement creating new genotype datasets. --- qc_app/upload/rqtl2.py | 67 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) (limited to 'qc_app/upload') diff --git a/qc_app/upload/rqtl2.py b/qc_app/upload/rqtl2.py index 9bed4f1..6ddc83f 100644 --- a/qc_app/upload/rqtl2.py +++ b/qc_app/upload/rqtl2.py @@ -1,14 +1,14 @@ """Module to handle uploading of R/qtl2 bundles.""" from pathlib import Path -from typing import Optional +from datetime import date from zipfile import ZipFile, is_zipfile +from MySQLdb.cursors import DictCursor from flask import ( flash, request, url_for, redirect, - Response, Blueprint, render_template, current_app as app) @@ -158,12 +158,12 @@ def upload_rqtl2_bundle(species_id: int, population_id: int): "rqtl2/upload-rqtl2-bundle-step-02.html", species=species, population=population, - rqtl2_bundle_file=the_file.name) + rqtl2_bundle_file=the_file.name)#type: ignore[union-attr] except (InvalidFormat, __RequestError__) as exc: flash("".join(exc.args), "alert-error alert-rqtl2") return this_page_with_errors -def check_errors(conn, *args, **kwargs) -> Optional[Response]: +def check_errors(conn, *args, **kwargs): """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") @@ -199,7 +199,7 @@ def check_errors(conn, *args, **kwargs) -> Optional[Response]: pgsrc="error"), code=307) - return False + return None @rqtl2.route(("/upload/species//population/" "/rqtl2-bundle/dataset-info"), @@ -230,7 +230,8 @@ def select_dataset_info(species_id: int, population_id: int): datasets=geno_dataset_by_species_and_population( conn, species_id, population_id)) - geno_dataset = geno_dataset_by_id(conn, form["geno-dataset-id"]) + geno_dataset = geno_dataset_by_id( + conn, int(form["geno-dataset-id"])) return render_template("rqtl2/summary-info.html", species=species, @@ -240,7 +241,7 @@ def select_dataset_info(species_id: int, population_id: int): @rqtl2.route(("/upload/species//population/" "/rqtl2-bundle/select-geno-dataset"), methods=["POST"]) -def select_geno_dataset(species_id: int, population_id: int) -> Response: +def select_geno_dataset(species_id: int, population_id: int): """Select from existing geno datasets.""" with database_connection(app.config["SQL_URI"]) as conn: error = check_errors( @@ -269,6 +270,54 @@ def select_geno_dataset(species_id: int, population_id: int) -> Response: @rqtl2.route(("/upload/species//population/" "/rqtl2-bundle/create-geno-dataset"), methods=["POST"]) -def create_geno_dataset(species_id: int, population_id: int) -> Response: +def create_geno_dataset(species_id: int, population_id: int): """Create a new geno dataset.""" - return "IMPLEMENT THIS!!!" + with database_connection(app.config["SQL_URI"]) as conn: + error = check_errors(conn, "species", "population", "rqtl2_bundle_file") + if bool(error): + return error + + sgeno_page = redirect(url_for("upload.rqtl2.select_geno_dataset", + species_id=species_id, + population_id=population_id, + pgsrc="error"), + code=307) + if not bool(request.form.get("dataset-name")): + flash("You must provide the dataset name", + "alert-error alert-rqtl2") + return sgeno_page + if not bool(request.form.get("dataset-fullname")): + flash("You must provide the dataset full name", + "alert-error alert-rqtl2") + return sgeno_page + if not bool(request.form.get("dataset-shortname")): + flash("You must provide the dataset short name", + "alert-error alert-rqtl2") + return sgeno_page + public = 2 if request.form.get("dataset-public") == "on" else 0 + + with conn.cursor(cursorclass=DictCursor) as cursor: + new_dataset = { + "name": request.form.get("dataset-name"), + "fname": request.form.get("dataset-fullname"), + "sname": request.form.get("dataset-shortname"), + "today": date.today().isoformat(), + "pub": public, + "isetid": population_id + } + cursor.execute( + "INSERT INTO GenoFreeze(" + "Name, FullName, ShortName, CreateTime, public, InbredSetId" + ") " + "VALUES(" + "%(name)s, %(fname)s, %(sname)s, %(today)s, %(pub)s, %(isetid)s" + ")", + new_dataset) + flash("Created dataset successfully.", "alert-success") + return render_template( + "rqtl2/create-geno-dataset-success.html", + species=species_by_id(conn, species_id), + population=population_by_species_and_id( + conn, species_id, population_id), + rqtl2_bundle_file=request.form["rqtl2_bundle_file"], + geno_dataset={**new_dataset, "id": cursor.lastrowid}) -- cgit v1.2.3