about summary refs log tree commit diff
path: root/uploader/genotypes
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/genotypes')
-rw-r--r--uploader/genotypes/models.py15
-rw-r--r--uploader/genotypes/views.py25
2 files changed, 34 insertions, 6 deletions
diff --git a/uploader/genotypes/models.py b/uploader/genotypes/models.py
index 1e09cd3..41270da 100644
--- a/uploader/genotypes/models.py
+++ b/uploader/genotypes/models.py
@@ -1,14 +1,17 @@
 """Functions for handling genotypes."""
+import logging
 from typing import Optional
 from functools import reduce
 from datetime import datetime
 
 import MySQLdb as mdb
 from MySQLdb.cursors import Cursor, DictCursor
-from flask import current_app as app
 
 from gn_libs.mysqldb import debug_query
 
+logger = logging.getLogger(__name__)
+
+
 def genocode_by_population(
         conn: mdb.Connection, population_id: int) -> tuple[dict, ...]:
     """Get the allele/genotype codes."""
@@ -62,13 +65,13 @@ def genotype_markers(
             (species_id, population_id))
         _total_records = cursor.fetchone()["total_records"]
         cursor.execute(
-            _query_template.replace("%%COLS%%", "gno.*").replace(
+            _query_template.replace("%%COLS%%", "gno.*, gxr.cM").replace(
                 "%%LIMIT%%",
                 (f"LIMIT {int(limit)} OFFSET {int(offset)}"
                  if bool(limit) and limit >= 0
                  else "")),
             (species_id, population_id))
-        debug_query(cursor, app.logger)
+        debug_query(cursor, logger)
         _records = tuple(dict(row) for row in cursor.fetchall())
         return _records, _total_records
 
@@ -107,11 +110,15 @@ def genotype_records(
     with conn.cursor(cursorclass=DictCursor) as cursor:
         _markers, _num_records = genotype_markers(
             conn, species_id, population_id, offset, limit)
+        if len(_markers) == 0:
+            return (tuple(), 0)
+
         _genoids = tuple(_marker["Id"] for _marker in _markers)
         cursor.execute(
             _query_template.replace(
                 "%%PARAMS_STR%%", ",".join(["%s"] * len(_genoids))),
             _genoids)
+        debug_query(cursor, logger)
         _records: dict[str, dict] = reduce(
             __organise_geno_records__, cursor.fetchall(), {})
         return (
@@ -146,7 +153,7 @@ def genotype_dataset(
 
     with conn.cursor(cursorclass=DictCursor) as cursor:
         cursor.execute(_query, _params)
-        debug_query(cursor, app.logger)
+        debug_query(cursor, logger)
         result = cursor.fetchone()
         if bool(result):
             return dict(result)
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py
index 8c0795d..648b38e 100644
--- a/uploader/genotypes/views.py
+++ b/uploader/genotypes/views.py
@@ -22,7 +22,7 @@ from uploader.authorisation import require_login
 from uploader.species.models import species_by_id
 from uploader.monadic_requests import make_either_error_handler
 from uploader.population.models import population_by_species_and_id
-from uploader.request_checks import with_population
+from uploader.request_checks import with_dataset, with_population
 
 
 from .models import (genotype_markers,
@@ -62,7 +62,8 @@ def index(species: dict, population: dict, **kwargs):# pylint: disable=[unused-a
             in enumerate(_genotype_records, start=offset+1))
 
         ## Order these correctly
-        _samples = tuple(_genotype_records[0]["data"].keys())
+        _samples = (tuple() if len(_genotype_records) == 0
+                    else tuple(_genotype_records[0]["data"].keys()))
 
         if "application/json" in request.headers["Accept"]:
             return make_response(
@@ -211,3 +212,23 @@ def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable=
             make_either_error_handler(
                 "There was an error creating the genotype dataset."),
             __success__)
+
+
+@genotypesbp.route(
+    "/<int:species_id>/populations/<int:population_id>/genotypes/datasets/"
+    "<int:dataset_id>/add-records",
+    methods=["GET", "POST"])
+@require_login
+@with_population(species_redirect_uri="species.list_species",
+                 redirect_uri="species.populations.list_species_populations")
+@with_dataset(species_redirect_uri="species.list_species",
+              population_redirect_uri="species.populations.list_species_populations",
+              redirect_uri="species.populations.genotypes.index",
+              dataset_by_id=genotype_dataset)
+def add_genotype_records(species: dict, population: dict, dataset: dict, **kwargs):
+    """Add new Genotype records to the dataset."""
+    return render_template("genotypes/add-genotypes-records-csv.html",
+                           species=species,
+                           population=population,
+                           dataset=dataset,
+                           activelink="add-genotypes-records")