about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBonfaceKilz2021-03-03 12:37:40 +0300
committerBonfaceKilz2021-03-08 21:09:58 +0300
commit62fca4bd4dc433230ce0ad8c2a2c785c3ea5b5b3 (patch)
treefd8c845d9b852b75826680ac6ffc231526d05d6d
parent396aeed35a5925cb03cc1569669b257b8ccb07cb (diff)
downloadgenenetwork3-62fca4bd4dc433230ce0ad8c2a2c785c3ea5b5b3.tar.gz
Add new endpoint: "/gemma/k-compute/loco/<chromosomes>/<token>"
-rw-r--r--gn3/api/gemma.py38
-rw-r--r--tests/integration/test_gemma.py28
2 files changed, 66 insertions, 0 deletions
diff --git a/gn3/api/gemma.py b/gn3/api/gemma.py
index ec5f3f0..e5a1a72 100644
--- a/gn3/api/gemma.py
+++ b/gn3/api/gemma.py
@@ -133,3 +133,41 @@ traitfile, and snpsfile are extracted from a metadata.json file.
         return jsonify(status=128,
                        # use better message
                        message="Metadata file non-existent!")
+
+
+@gemma.route("/k-compute/loco/<chromosomes>/<token>", methods=["POST"])
+def compute_k_loco(chromosomes, token):
+    """Similar to 'compute_k' with the extra option of using loco given chromosome
+values.
+
+    """
+    working_dir = os.path.join(current_app.config.get("TMPDIR"),
+                               token)
+    _dict = jsonfile_to_dict(os.path.join(working_dir,
+                                          "metadata.json"))
+    try:
+        genofile, phenofile, snpsfile = [os.path.join(working_dir,
+                                                      _dict.get(x))
+                                         for x in ["geno", "pheno", "snps"]]
+        gemma_kwargs = {"g": genofile, "p": phenofile, "a": snpsfile}
+        _hash = get_hash_of_files([genofile, phenofile, snpsfile])
+        k_output_filename = f"{_hash}-k-output.json"
+        k_computation_cmd = generate_gemma_computation_cmd(
+            gemma_cmd=current_app.config.get("GEMMA_WRAPPER_CMD"),
+            gemma_wrapper_kwargs={"loco": f"--input {chromosomes}"},
+            gemma_kwargs=gemma_kwargs,
+            output_file=(f"{current_app.config.get('TMPDIR')}/"
+                         f"{token}/{k_output_filename}"))
+        return jsonify(
+            unique_id=queue_cmd(
+                conn=redis.Redis(),
+                email=(request.get_json() or {}).get('email'),
+                job_queue=current_app.config.get("REDIS_JOB_QUEUE"),
+                cmd=f"{k_computation_cmd}"),
+            status="queued",
+            output_file=k_output_filename)
+    # pylint: disable=W0703
+    except Exception:
+        return jsonify(status=128,
+                       # use better message
+                       message="Metadata file non-existent!")
diff --git a/tests/integration/test_gemma.py b/tests/integration/test_gemma.py
index 18e3fb0..3e64987 100644
--- a/tests/integration/test_gemma.py
+++ b/tests/integration/test_gemma.py
@@ -176,3 +176,31 @@ class GemmaAPITest(unittest.TestCase):
             "status": "queued",
             "unique_id": "my-unique-id"
         })
+
+    @mock.patch("gn3.api.gemma.queue_cmd")
+    @mock.patch("gn3.api.gemma.generate_gemma_computation_cmd")
+    @mock.patch("gn3.api.gemma.get_hash_of_files")
+    @mock.patch("gn3.api.gemma.jsonfile_to_dict")
+    def test_k_compute_loco(self, mock_json, mock_hash, mock_cmd,
+                            mock_queue_cmd):
+        """Test /gemma/k-compute/<token>"""
+        mock_queue_cmd.return_value = "my-unique-id"
+        mock_json.return_value = {
+            "geno": "genofile.txt",
+            "pheno": "phenofile.txt",
+            "snps": "snpfile.txt",
+        }
+        mock_hash.return_value = "hash"
+        mock_cmd.return_value = ("gemma-wrapper --json -- "
+                                 "-debug -g "
+                                 "genotype_name.txt "
+                                 "-p traitfilename.txt "
+                                 "-a genotype_snps.txt "
+                                 "-gk > k_output_filename.json")
+        response = self.app.post(("/api/gemma/k-compute/loco/"
+                                  "1%2C2%2C3%2C4%2C5%2C6/test-data"))
+        self.assertEqual(response.get_json(), {
+            "output_file": "hash-k-output.json",
+            "status": "queued",
+            "unique_id": "my-unique-id"
+        })