about summary refs log tree commit diff
path: root/qc_app/upload
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-01-17 06:46:42 +0300
committerFrederick Muriuki Muriithi2024-01-17 06:46:42 +0300
commitcde57e8faeb41df27587361dcf42e235545cacb3 (patch)
tree8eaef7b84de1cb8780434afd4dd7e3b0ae1413bb /qc_app/upload
parentbf1c2ec1b16b50f419a349a54031baf85fcd4abd (diff)
downloadgn-uploader-cde57e8faeb41df27587361dcf42e235545cacb3.tar.gz
UI: Implement creating new genotype datasets.
Diffstat (limited to 'qc_app/upload')
-rw-r--r--qc_app/upload/rqtl2.py67
1 files changed, 58 insertions, 9 deletions
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})