From af485aff7d25c1f5128586c550ca36debe24fd66 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 18 Jan 2024 14:43:00 +0300 Subject: UI: Create new ProbeSet dataset. --- qc_app/db/averaging.py | 23 ++++++ qc_app/db/datasets.py | 33 +++++++++ .../rqtl2/create-probe-dataset-success.html | 60 ++++++++++++++++ .../templates/rqtl2/select-probeset-dataset.html | 82 +++++++++++++++++++++- qc_app/upload/rqtl2.py | 65 ++++++++++++++++- 5 files changed, 260 insertions(+), 3 deletions(-) create mode 100644 qc_app/db/averaging.py create mode 100644 qc_app/templates/rqtl2/create-probe-dataset-success.html diff --git a/qc_app/db/averaging.py b/qc_app/db/averaging.py new file mode 100644 index 0000000..62bbe67 --- /dev/null +++ b/qc_app/db/averaging.py @@ -0,0 +1,23 @@ +"""Functions for db interactions for averaging methods""" +from typing import Optional + +import MySQLdb as mdb +from MySQLdb.cursors import DictCursor + +def averaging_methods(conn: mdb.Connection) -> tuple[dict, ...]: + """Fetch all available averaging methods""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT * FROM AvgMethod") + return tuple(dict(row) for row in cursor.fetchall()) + +def averaging_method_by_id( + conn: mdb.Connection, averageid: int) -> Optional[dict]: + """Fetch the averaging method by its ID""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT * FROM AvgMethod WHERE Id=%s", + (averageid,)) + result = cursor.fetchone() + if bool(result): + return dict(result) + + return None diff --git a/qc_app/db/datasets.py b/qc_app/db/datasets.py index 5f6a2d5..5816146 100644 --- a/qc_app/db/datasets.py +++ b/qc_app/db/datasets.py @@ -85,3 +85,36 @@ def probeset_create_study(conn: mdb.Connection,#pylint: disable=[too-many-argume cursor.execute("UPDATE ProbeFreeze SET ProbeFreezeId=%s", (studyid,)) return {**studydata, "studyid": studyid} + +def probeset_create_dataset(conn: mdb.Connection,#pylint: disable=[too-many-arguments] + studyid: int, + averageid: int, + datasetname: str, + datasetfullname: str, + datasetshortname: str="", + public: bool = True, + datascale="log2") -> dict: + """Create a new ProbeSet dataset.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + dataset = { + "studyid": studyid, + "averageid": averageid, + "name2": datasetname, + "fname": datasetfullname, + "name": datasetshortname, + "sname": datasetshortname, + "today": date.today().isoformat(), + "public": 2 if public else 0, + "datascale": datascale + } + cursor.execute( + """ + insert into ProbeSetFreeze( + ProbeFreezeId, AvgId, Name, Name2, FullName, ShortName, + CreateTime, public, DataScale) + VALUES( + %(studyid)s, %(averageid)s, %(name)s, %(name2)s, %(fname)s, + %(sname)s, %(today)s, %(public)s, %(datascale)s) + """, + dataset) + return {**dataset, "datasetid": cursor.lastrowid} diff --git a/qc_app/templates/rqtl2/create-probe-dataset-success.html b/qc_app/templates/rqtl2/create-probe-dataset-success.html new file mode 100644 index 0000000..33612c4 --- /dev/null +++ b/qc_app/templates/rqtl2/create-probe-dataset-success.html @@ -0,0 +1,60 @@ +{%extends "base.html"%} +{%from "flash_messages.html" import flash_messages%} + +{%block title%}Upload R/qtl2 Bundle{%endblock%} + +{%block contents%} +

Create ProbeSet Dataset

+ +
+

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

+
Averaging Method
+
{{avgmethod.Name}}
+ +
ID
+
{{dataset.datasetid}}
+ +
Name
+
{{dataset.name2}}
+ +
Full Name
+
{{dataset.fname}}
+ +
Short Name
+
{{dataset.sname}}
+ +
Created On
+
{{dataset.today}}
+ +
DataScale
+
{{dataset.datascale}}
+
+

+
+ +
+ Create ProbeSet dataset + + + + + + + + +
+ +
+
+ +{%endblock%} diff --git a/qc_app/templates/rqtl2/select-probeset-dataset.html b/qc_app/templates/rqtl2/select-probeset-dataset.html index 8b9e5ed..3e160fd 100644 --- a/qc_app/templates/rqtl2/select-probeset-dataset.html +++ b/qc_app/templates/rqtl2/select-probeset-dataset.html @@ -14,6 +14,7 @@ {{flash_all_messages()}} +{%if datasets | length > 0%}
-

Under Construction

+

OR

+{%endif%} + +
+ Create a new ProbeSet dataset + + + + + + + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
{%endblock%} diff --git a/qc_app/upload/rqtl2.py b/qc_app/upload/rqtl2.py index fc4e0e8..0b446ac 100644 --- a/qc_app/upload/rqtl2.py +++ b/qc_app/upload/rqtl2.py @@ -22,6 +22,7 @@ from qc_app.db_utils import with_db_connection, database_connection from qc_app.db.tissues import all_tissues, tissue_by_id from qc_app.db.platforms import platform_by_id, platforms_by_species +from qc_app.db.averaging import averaging_methods, averaging_method_by_id from qc_app.db import ( species_by_id, save_population, @@ -33,6 +34,7 @@ from qc_app.db.datasets import ( probeset_study_by_id, probeset_create_study, + probeset_create_dataset, probeset_datasets_by_study, probeset_studies_by_species_and_population) @@ -349,7 +351,7 @@ def select_probeset_dataset(species_id: int, population_id: int): population_id=population_id), code=307) if not bool(probeset_study_by_id(conn, int(request.form["probe-study-id"]))): - flash("Invalid dataset selected!", "alert-error alert-rqtl2") + flash("Invalid study selected!", "alert-error alert-rqtl2") return summary_page return summary_page @@ -397,6 +399,64 @@ def create_probeset_study(species_id: int, population_id: int): int(request.form["geno-dataset-id"])), study=study) +@rqtl2.route(("/upload/species//population/" + "/rqtl2-bundle/create-probeset-dataset"), + methods=["POST"]) +def create_probeset_dataset(species_id: int, population_id: int):#pylint: disable=[too-many-return-statements] + """Create a new probeset dataset.""" + with database_connection(app.config["SQL_URI"]) as conn: + error = check_errors( + conn, "species", "population", "rqtl2_bundle_file", "geno-dataset", + "probe-study-id") + if bool(error): + return error + + form = request.form + summary_page = redirect(url_for("upload.rqtl2.select_dataset_info", + species_id=species_id, + population_id=population_id), + code=307) + if not bool(form.get("averageid")): + flash("Averaging method not selected!", "alert-error alert-rqtl2") + return summary_page + if not bool(form.get("datasetname")): + flash("Dataset name not provided!", "alert-error alert-rqtl2") + return summary_page + if not bool(form.get("datasetfullname")): + flash("Dataset full name not provided!", "alert-error alert-rqtl2") + return summary_page + + study = probeset_study_by_id(conn, int(form["probe-study-id"])) + if not bool(study): + flash("Invalid ProbeSet study provided!", "alert-error alert-rqtl2") + return summary_page + + avgmethod = averaging_method_by_id(conn, int(form["averageid"])) + if not bool(avgmethod): + flash("Invalid averaging method provided!", "alert-error alert-rqtl2") + return summary_page + + dset = probeset_create_dataset(conn, + int(form["probe-study-id"]), + int(form["averageid"]), + form["datasetname"], + form["datasetfullname"], + form["datasetshortname"], + form["datasetpublic"] == "on", + form.get("datasetdatascale", "log2")) + return render_template( + "rqtl2/create-probe-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=geno_dataset_by_id( + conn, + int(request.form["geno-dataset-id"])), + study=study, + avgmethod=avgmethod, + dataset=dset) + @rqtl2.route(("/upload/species//population/" "/rqtl2-bundle/dataset-info"), methods=["POST"]) @@ -450,7 +510,8 @@ def select_dataset_info(species_id: int, population_id: int): geno_dataset=geno_dataset, probe_study=probeset_study, datasets=probeset_datasets_by_study( - conn, int(form["probe-study-id"]))) + conn, int(form["probe-study-id"])), + avgmethods=averaging_methods(conn)) return render_template("rqtl2/summary-info.html", species=species, -- cgit v1.2.3