From 3e028bf0e9f8239b552bcd4b2d6133bb7965abd9 Mon Sep 17 00:00:00 2001
From: Frederick Muriuki Muriithi
Date: Wed, 25 Sep 2024 12:09:46 -0500
Subject: Create new genotype datasets.
---
uploader/genotypes/models.py | 33 +++++++++-
uploader/genotypes/views.py | 34 ++++++++++
uploader/templates/genotypes/create-dataset.html | 82 ++++++++++++++++++++++++
uploader/templates/genotypes/list-genotypes.html | 4 +-
4 files changed, 151 insertions(+), 2 deletions(-)
create mode 100644 uploader/templates/genotypes/create-dataset.html
(limited to 'uploader')
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(
+ "/