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. --- .../rqtl2/create-geno-dataset-success.html | 57 ++++++++++++++++++ qc_app/upload/rqtl2.py | 67 +++++++++++++++++++--- 2 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 qc_app/templates/rqtl2/create-geno-dataset-success.html (limited to 'qc_app') diff --git a/qc_app/templates/rqtl2/create-geno-dataset-success.html b/qc_app/templates/rqtl2/create-geno-dataset-success.html new file mode 100644 index 0000000..7feb8d8 --- /dev/null +++ b/qc_app/templates/rqtl2/create-geno-dataset-success.html @@ -0,0 +1,57 @@ +{%extends "base.html"%} +{%from "flash_messages.html" import flash_messages%} + +{%block title%}Upload R/qtl2 Bundle{%endblock%} + +{%block contents%} +

Select Genotypes Dataset

+ +
+

You successfully created the genotype dataset with the following + information. +

+
ID
+
{{geno_dataset.id}}
+ +
Name
+
{{geno_dataset.name}}
+ +
Full Name
+
{{geno_dataset.fname}}
+ +
Short Name
+
{{geno_dataset.sname}}
+ +
Created On
+
{{geno_dataset.today}}
+ +
Public?
+
{%if geno_dataset.public == 0%}No{%else%}Yes{%endif%}
+
+

+
+ +
+ select from existing genotype datasets + + + + + + +
+ +
+
+ +{%endblock%} 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