diff options
author | Frederick Muriuki Muriithi | 2024-01-17 06:46:42 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-01-17 06:46:42 +0300 |
commit | cde57e8faeb41df27587361dcf42e235545cacb3 (patch) | |
tree | 8eaef7b84de1cb8780434afd4dd7e3b0ae1413bb | |
parent | bf1c2ec1b16b50f419a349a54031baf85fcd4abd (diff) | |
download | gn-uploader-cde57e8faeb41df27587361dcf42e235545cacb3.tar.gz |
UI: Implement creating new genotype datasets.
-rw-r--r-- | qc_app/templates/rqtl2/create-geno-dataset-success.html | 57 | ||||
-rw-r--r-- | qc_app/upload/rqtl2.py | 67 |
2 files changed, 115 insertions, 9 deletions
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%} +<h2 class="heading">Select Genotypes Dataset</h2> + +<div class="explainer"> + <p>You successfully created the genotype dataset with the following + information. + <dl> + <dt>ID</dt> + <dd>{{geno_dataset.id}}</dd> + + <dt>Name</dt> + <dd>{{geno_dataset.name}}</dd> + + <dt>Full Name</dt> + <dd>{{geno_dataset.fname}}</dd> + + <dt>Short Name</dt> + <dd>{{geno_dataset.sname}}</dd> + + <dt>Created On</dt> + <dd>{{geno_dataset.today}}</dd> + + <dt>Public?</dt> + <dd>{%if geno_dataset.public == 0%}No{%else%}Yes{%endif%}</dd> + </dl> + </p> +</div> + +<form id="frm-upload-rqtl2-bundle" + action="{{url_for('upload.rqtl2.select_dataset_info', + species_id=species.SpeciesId, + population_id=population.InbredSetId)}}" + method="POST" + enctype="multipart/form-data"> + <legend class="heading">select from existing genotype datasets</legend> + + <input type="hidden" name="species_id" value="{{species.SpeciesId}}" /> + <input type="hidden" name="population_id" + value="{{population.InbredSetId}}" /> + <input type="hidden" name="rqtl2_bundle_file" + value="{{rqtl2_bundle_file}}" /> + <input type="hidden" name="geno-dataset-id" + value="{{geno_dataset.id}}" /> + + <fieldset> + <input type="submit" + value="continue" + class="btn btn-main form-col-2" /> + </fieldset> +</form> + +{%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/<int:species_id>/population/<int:population_id>" "/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/<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: +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/<int:species_id>/population/<int:population_id>" "/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}) |