about summary refs log tree commit diff
path: root/uploader/genotypes
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2026-09-10 14:13:26 -0500
committerFrederick Muriuki Muriithi2026-09-10 17:43:06 -0500
commitbfb581115cb5306cf2b95bb940d22b4c1dfca7d3 (patch)
treec3cc106b5af04f399aae97350e00dc372c50b8c2 /uploader/genotypes
parent59a10f711af4a97f6b9fd98bf421f60f8fec1584 (diff)
downloadgn-uploader-bfb581115cb5306cf2b95bb940d22b4c1dfca7d3.tar.gz
PoC: UI for genotype records.
Implement a proof-of-concept (PoC) UI for genotype records.
Diffstat (limited to 'uploader/genotypes')
-rw-r--r--uploader/genotypes/views.py66
1 files changed, 48 insertions, 18 deletions
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py
index f27671c..c18c330 100644
--- a/uploader/genotypes/views.py
+++ b/uploader/genotypes/views.py
@@ -4,11 +4,13 @@ import logging
 from MySQLdb.cursors import DictCursor
 from pymonad.either import Left, Right, Either
 from gn_libs.mysqldb import database_connection
+from werkzeug.exceptions import UnsupportedMediaType
 from flask import (flash,
                    request,
                    jsonify,
                    redirect,
                    Blueprint,
+                   make_response,
                    render_template,
                    current_app as app)
 
@@ -16,17 +18,16 @@ from uploader.flask_extensions import url_for
 from uploader.ui import make_template_renderer
 from uploader.oauth2.client import oauth2_post
 from uploader.authorisation import require_login
-from uploader.route_utils import generic_select_population
-from uploader.datautils import safe_int, enumerate_sequence
-from uploader.species.models import all_species, species_by_id
+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_species, with_dataset, with_population
+from uploader.request_checks import with_population
+
 
 from .models import (genotype_markers,
+                     genotype_records,
                      genotype_dataset,
                      save_new_dataset,
-                     genotype_markers_count,
                      genocode_by_population)
 
 logger = logging.getLogger(__name__)
@@ -40,20 +41,49 @@ render_template = make_template_renderer("genotypes")
 @require_login
 @with_population(species_redirect_uri="species.list_species",
                  redirect_uri="species.populations.list_species_populations")
-def list_genotypes(species: dict, population: dict, **kwargs):# pylint: disable=[unused-argument]
-    """List genotype details for species and population."""
+def index(species: dict, population: dict, **kwargs):# pylint: disable=[unused-argument]
+    """Entry-point to the genotypes management section."""
     with database_connection(app.config["SQL_URI"]) as conn:
-        return render_template("genotypes/list-genotypes.html",
-                               species=species,
-                               population=population,
-                               genocode=genocode_by_population(
-                                   conn, population["Id"]),
-                               total_markers=genotype_markers_count(
-                                   conn, species["SpeciesId"]),
-                               dataset=genotype_dataset(conn,
-                                                        species["SpeciesId"],
-                                                        population["Id"]),
-                               activelink="list-genotypes")
+        offset = int(request.args.get("start", "0"))
+        number_of_records = int(request.args.get("count", "10"))
+        _markers, _total_markers, = genotype_markers(
+            conn, species["SpeciesId"], population["Id"])
+        _genotype_records, _count = genotype_records(
+            conn,
+            species["SpeciesId"],
+            population["Id"],
+            offset,
+            number_of_records)
+        _genotype_records = tuple(
+            {**_record, "index": _idx}
+            for _idx, _record
+            in enumerate(_genotype_records, start=offset+1))
+
+        ## Order these correctly
+        _samples = tuple(_genotype_records[0]["data"].keys())
+
+        if "application/json" in request.headers["Accept"]:
+            return make_response(
+                jsonify({
+                    "genotype_records": _genotype_records,
+                    "total_genotype_records": _count,
+                    "fetched_genotype_records": len(_genotype_records),
+                    "sample_order": _samples
+                }), 200)
+
+        if "text/html" in request.headers["Accept"]:
+            return render_template(
+                "genotypes/index.html",
+                species=species,
+                population=population,
+                genocode=genocode_by_population(conn, population["Id"]),
+                dataset=genotype_dataset(
+                    conn, species["SpeciesId"], population["Id"]),
+                genotype_records=_genotype_records,
+                samples=_samples,
+                activelink="list-genotypes")
+
+        raise UnsupportedMediaType("This endpoint can only server HTML or JSON")
 
 
 @genotypesbp.route(