about summary refs log tree commit diff
path: root/uploader
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-09-25 12:09:46 -0500
committerFrederick Muriuki Muriithi2024-09-25 12:11:58 -0500
commit3e028bf0e9f8239b552bcd4b2d6133bb7965abd9 (patch)
tree9a58c1b95748c99c04bec5e5f34d70f7070af287 /uploader
parent44a07c95a3ae77441e1b45b9f5fba9c6d77a2871 (diff)
downloadgn-uploader-3e028bf0e9f8239b552bcd4b2d6133bb7965abd9.tar.gz
Create new genotype datasets.
Diffstat (limited to 'uploader')
-rw-r--r--uploader/genotypes/models.py33
-rw-r--r--uploader/genotypes/views.py34
-rw-r--r--uploader/templates/genotypes/create-dataset.html82
-rw-r--r--uploader/templates/genotypes/list-genotypes.html4
4 files changed, 151 insertions, 2 deletions
diff --git a/uploader/genotypes/models.py b/uploader/genotypes/models.py
index db8cc3e..44c98b1 100644
--- a/uploader/genotypes/models.py
+++ b/uploader/genotypes/models.py
@@ -1,8 +1,9 @@
 """Functions for handling genotypes."""
 from typing import Optional
+from datetime import datetime
 
 import MySQLdb as mdb
-from MySQLdb.cursors import DictCursor
+from MySQLdb.cursors import Cursor, DictCursor
 
 from uploader.db_utils import debug_query
 
@@ -68,3 +69,33 @@ def genotype_dataset(
         if bool(result):
             return dict(result)
         return None
+
+
+def save_new_dataset(
+        cursor: Cursor,
+        population_id: int,
+        name: str,
+        fullname: str,
+        shortname: str
+) -> dict:
+    """Save a new genotype dataset into the database."""
+    params = {
+        "InbredSetId": population_id,
+        "Name": name,
+        "FullName": fullname,
+        "ShortName": shortname,
+        "CreateTime": datetime.now().date().isoformat(),
+        "public": 2,
+        "confidentiality": 0,
+        "AuthorisedUsers": None
+    }
+    cursor.execute(
+        "INSERT INTO GenoFreeze("
+        "Name, FullName, ShortName, CreateTime, public, InbredSetId, "
+        "confidentiality, AuthorisedUsers"
+        ") VALUES ("
+        "%(Name)s, %(FullName)s, %(ShortName)s, %(CreateTime)s, %(public)s, "
+        "%(InbredSetId)s, %(confidentiality)s, %(AuthorisedUsers)s"
+        ")",
+        params)
+    return {**params, "Id": cursor.lastrowid}
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py
index 41cd21e..f79caba 100644
--- a/uploader/genotypes/views.py
+++ b/uploader/genotypes/views.py
@@ -1,4 +1,5 @@
 """Views for the genotypes."""
+from MySQLdb.cursors import DictCursor
 from flask import (flash,
                    request,
                    url_for,
@@ -18,6 +19,7 @@ from uploader.population.models import (populations_by_species,
 
 from .models import (genotype_markers,
                      genotype_dataset,
+                     save_new_dataset,
                      genotype_markers_count,
                      genocode_by_population)
 
@@ -148,3 +150,35 @@ def view_dataset(species_id: int, population_id: int, dataset_id: int):
                                population=population,
                                dataset=dataset,
                                activelink="view-dataset")
+
+
+@genotypesbp.route(
+    "/<int:species_id>/populations/<int:population_id>/genotypes/datasets/"
+    "create",
+    methods=["GET", "POST"])
+@require_login
+@with_population(species_redirect_uri="species.populations.genotypes.index",
+                 redirect_uri="species.populations.genotypes.select_population")
+def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable=[unused-argument]
+    """Create a genotype dataset."""
+    with (database_connection(app.config["SQL_URI"]) as conn,
+          conn.cursor(cursorclass=DictCursor) as cursor):
+        if request.method == "GET":
+            return render_template("genotypes/create-dataset.html",
+                                   species=species,
+                                   population=population,
+                                   activelink="create-dataset")
+
+        form = request.form
+        _new_dataset = save_new_dataset(
+            cursor,
+            population["Id"],
+            form["geno-dataset-name"],
+            form["geno-dataset-fullname"],
+            form["geno-dataset-shortname"])
+
+        flash("Successfully created genotype dataset."
+              "alert-success")
+        return redirect(url_for("species.populations.genotypes.list_genotypes",
+                                species_id=species["SpeciesId"],
+                                population_id=population["Id"]))
diff --git a/uploader/templates/genotypes/create-dataset.html b/uploader/templates/genotypes/create-dataset.html
new file mode 100644
index 0000000..10331c1
--- /dev/null
+++ b/uploader/templates/genotypes/create-dataset.html
@@ -0,0 +1,82 @@
+{%extends "genotypes/base.html"%}
+{%from "flash_messages.html" import flash_all_messages%}
+{%from "populations/macro-display-population-card.html" import display_population_card%}
+
+{%block title%}Genotypes — Create Dataset{%endblock%}
+
+{%block pagetitle%}Genotypes — Create Dataset{%endblock%}
+
+{%block lvl4_breadcrumbs%}
+<li {%if activelink=="create-dataset"%}
+    class="breadcrumb-item active"
+    {%else%}
+    class="breadcrumb-item"
+    {%endif%}>
+  <a href="{{url_for('species.populations.genotypes.create_dataset',
+           species_id=species.SpeciesId,
+           population_id=population.Id)}}">Create Dataset</a>
+</li>
+{%endblock%}
+
+{%block contents%}
+{{flash_all_messages()}}
+
+<div class="row">
+  <form id="frm-geno-create-dataset"
+        method="POST"
+        action="{{url_for('species.populations.genotypes.create_dataset',
+                species_id=species.SpeciesId,
+                population_id=population.Id)}}">
+    <legend>Create a new Genotype Dataset</legend>
+
+    <div class="form-group">
+      <label for="txt-geno-dataset-name" class="form-label">Name</label>
+      <input type="text"
+             id="txt-geno-dataset-name"
+             name="geno-dataset-name"
+             required="required"
+             class="form-control" />
+      <small class="form-text text-muted">
+        <p>This is a short representative, but constrained name for the genotype
+          dataset.<br />
+          The field will only accept letters ('A-Za-z'), numbers (0-9), hyphens
+          and underscores. Any other character will cause the name to be
+          rejected.</p></small>
+    </div>
+
+    <div class="form-group">
+      <label for="txt-geno-dataset-fullname" class="form-label">Full Name</label>
+      <input type="text"
+             id="txt-geno-dataset-fullname"
+             name="geno-dataset-fullname"
+             required="required"
+             class="form-control" />
+      <small class="form-text text-muted">
+        <p>This is a longer, more descriptive name for your dataset.</p></small>
+    </div>
+
+    <div class="form-group">
+      <label for="txt-geno-dataset-shortname"
+             class="form-label">Short Name</label>
+      <input type="text"
+             id="txt-geno-dataset-shortname"
+             name="geno-dataset-shortname"
+             class="form-control" />
+      <small class="form-text text-muted">
+        <p>A short name for your dataset. If you leave this field blank, the
+          short name will be set to the same value as the
+          "<strong>Name</strong>" field above.</p></small>
+    </div>
+
+    <div class="form-group">
+      <input type="submit"
+             class="btn btn-primary"
+             value="create dataset"  />
+    </div>
+  </form>
+</div>
+{%endblock%}
+
+{%block sidebarcontents%}
+{{display_population_card(species, population)}}
+{%endblock%}
diff --git a/uploader/templates/genotypes/list-genotypes.html b/uploader/templates/genotypes/list-genotypes.html
index cecb5a3..e4c39eb 100644
--- a/uploader/templates/genotypes/list-genotypes.html
+++ b/uploader/templates/genotypes/list-genotypes.html
@@ -123,7 +123,9 @@
     There is no genotype dataset defined for this population.
   </p>
   <p>
-    <a href="#create-new-genotype-dataset"
+    <a href="{{url_for('species.populations.genotypes.create_dataset',
+             species_id=species.SpeciesId,
+             population_id=population.Id)}}"
        title="Create a new genotype dataset for the '{{population.FullName}}' population for the '{{species.FullName}}' species."
        class="btn btn-primary">
       create new genotype dataset</a></p>