about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--uploader/phenotypes/views.py62
-rw-r--r--uploader/templates/phenotypes/add-phenotypes.html2
2 files changed, 61 insertions, 3 deletions
diff --git a/uploader/phenotypes/views.py b/uploader/phenotypes/views.py
index 5f6e4a6..02e8078 100644
--- a/uploader/phenotypes/views.py
+++ b/uploader/phenotypes/views.py
@@ -1,6 +1,11 @@
 """Views handling ('classical') phenotypes."""
+import sys
+import uuid
+import json
+from pathlib import Path
 from functools import wraps
 
+from redis import Redis
 from requests.models import Response
 from MySQLdb.cursors import DictCursor
 from flask import (flash,
@@ -11,6 +16,12 @@ from flask import (flash,
                    render_template,
                    current_app as app)
 
+# from r_qtl import r_qtl2 as rqtl2
+from r_qtl import r_qtl2_qc as rqc
+from r_qtl import exceptions as rqe
+
+from uploader import jobs
+from uploader.files import save_file#, fullpath
 from uploader.oauth2.client import oauth2_post
 from uploader.authorisation import require_login
 from uploader.db_utils import database_connection
@@ -299,12 +310,59 @@ def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable=
     redirect_uri="species.populations.phenotypes.list_datasets")
 def add_phenotypes(species: dict, population: dict, dataset: dict, **kwargs):# pylint: disable=[unused-argument, too-many-locals]
     """Add one or more phenotypes to the dataset."""
-    with (database_connection(app.config["SQL_URI"]) as conn,
-          conn.cursor(cursorclass=DictCursor) as cursor):
+    add_phenos_uri = redirect(url_for(
+        "species.populations.phenotypes.add_phenotypes",
+        species_id=species["SpeciesId"],
+        population_id=population["Id"],
+        dataset_id=dataset["Id"]))
+    _redisuri = app.config["REDIS_URL"]
+    _sqluri = app.config["SQL_URI"]
+    with (Redis.from_url(_redisuri, decode_responses=True) as rconn,
+          # database_connection(_sqluri) as conn,
+          # conn.cursor(cursorclass=DictCursor) as cursor
+          ):
         if request.method == "GET":
             return render_template("phenotypes/add-phenotypes.html",
                                    species=species,
                                    population=population,
                                    dataset=dataset,
                                    activelink="add-phenotypes")
+
+        try:
+            ## Handle huge files here...
+            phenobundle = save_file(request.files["phenotypes-bundle"],
+                                    Path(app.config["UPLOAD_FOLDER"]))
+            rqc.validate_bundle(phenobundle)
+        except AssertionError as _aerr:
+            app.logger.debug("File upload error!", exc_info=True)
+            flash("Expected a zipped bundle of files with phenotypes' "
+                  "information.",
+                  "alert-danger")
+            return add_phenos_uri
+        except rqe.RQTLError as rqtlerr:
+            app.logger.debug("Bundle validation error!", exc_info=True)
+            flash("R/qtl2 Error: " + " ".join(rqtlerr.args), "alert-danger")
+            return add_phenos_uri
+
+        _jobid = uuid.uuid4()
+        _namespace = jobs.jobsnamespace()
+        _ttl_seconds = app.config["JOBS_TTL_SECONDS"]
+        _job = jobs.initialise_job(
+            rconn,
+            _namespace,
+            str(_jobid),
+            [sys.executable, "-m", "scripts.rqtl2.phenotypes_qc", _sqluri,
+             _redisuri, _namespace, str(_jobid), str(species["SpeciesId"]),
+             str(population["Id"]), str(dataset["Id"]), "--redisexpiry",
+             str(_ttl_seconds)], "phenotype_qc", _ttl_seconds,
+            {"job-metadata": json.dumps({
+                "speciesid": species["SpeciesId"],
+                "populationid": population["Id"],
+                "datasetid": dataset["Id"],
+                "bundle": str(phenobundle.absolute())})})
+        # jobs.launch_job(
+        #     _job,
+        #     redisuri,
+        #     f"{app.config['UPLOAD_FOLDER']}/job_errors")
+
     raise NotImplementedError("Please implement this...")
diff --git a/uploader/templates/phenotypes/add-phenotypes.html b/uploader/templates/phenotypes/add-phenotypes.html
index 8ad6999..196bc69 100644
--- a/uploader/templates/phenotypes/add-phenotypes.html
+++ b/uploader/templates/phenotypes/add-phenotypes.html
@@ -57,7 +57,7 @@
     <div class="form-group">
       <input type="submit"
              value="upload phenotypes"
-             class="not-implemented btn btn-primary" />
+             class="btn btn-primary" />
     </div>
   </form>
 </div>