about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBonfaceKilz2021-03-04 21:06:09 +0300
committerBonfaceKilz2021-03-08 21:09:58 +0300
commitd072471bc353663149203d29a82fa9b7b2b95a1d (patch)
tree430c1c64826077818bd530c20f734d2393db305e
parent13b66fddd5c905e1a7b320051cedeb9ea234e1c3 (diff)
downloadgenenetwork3-d072471bc353663149203d29a82fa9b7b2b95a1d.tar.gz
Add new endpoint: "/gwa-compute/covars/<k_filename>/<token>"
-rw-r--r--gn3/api/gemma.py41
-rw-r--r--tests/integration/test_gemma.py45
2 files changed, 86 insertions, 0 deletions
diff --git a/gn3/api/gemma.py b/gn3/api/gemma.py
index 709643d..acaec68 100644
--- a/gn3/api/gemma.py
+++ b/gn3/api/gemma.py
@@ -211,3 +211,44 @@ def compute_gwa(k_filename, token):
         return jsonify(status=128,
                        # use better message
                        message="Metadata file non-existent!")
+
+
+@gemma.route("/gwa-compute/covars/<k_filename>/<token>", methods=["POST"])
+def compute_gwa_with_covar(k_filename, token):
+    """Compute GWA values. Covariates provided.
+
+    """
+    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, covarfile = [
+            os.path.join(working_dir,
+                         _dict.get(x))
+            for x in ["geno", "pheno", "snps", "covar"]]
+        gemma_kwargs = {"g": genofile, "p": phenofile,
+                        "a": snpsfile, "c": covarfile,
+                        "lmm": _dict.get("lmm", 9)}
+        _hash = get_hash_of_files([genofile, phenofile, snpsfile, covarfile])
+        _output_filename = f"{_hash}-gwa-output.json"
+        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=generate_gemma_computation_cmd(
+                    gemma_cmd=current_app.config.get("GEMMA_WRAPPER_CMD"),
+                    gemma_wrapper_kwargs={
+                        "input": os.path.join(working_dir, k_filename)
+                    },
+                    gemma_kwargs=gemma_kwargs,
+                    output_file=(f"{current_app.config.get('TMPDIR')}/"
+                                 f"{token}/{_output_filename}"))),
+            status="queued",
+            output_file=_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 746cfb6..0c2afdc 100644
--- a/tests/integration/test_gemma.py
+++ b/tests/integration/test_gemma.py
@@ -245,3 +245,48 @@ class GemmaAPITest(unittest.TestCase):
             "status": "queued",
             "output_file": "hash-gwa-output.json"
         })
+
+    @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_gwa_compute_with_covars(self, mock_json, mock_hash, mock_cmd,
+                                     mock_queue_cmd):
+        """Test /gemma/gwa-compute/covars/<k-inputfile>/<token>"""
+        mock_queue_cmd.return_value = "my-unique-id"
+        mock_json.return_value = {
+            "geno": "genofile.txt",
+            "pheno": "phenofile.txt",
+            "snps": "snpfile.txt",
+            "covar": "covarfile.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/gwa-compute/"
+                                  "covars/hash-k-output.json/"
+                                  "my-token"))
+        mock_hash.assert_called_once_with(['/tmp/my-token/genofile.txt',
+                                           '/tmp/my-token/phenofile.txt',
+                                           '/tmp/my-token/snpfile.txt',
+                                           '/tmp/my-token/covarfile.txt'])
+        mock_cmd.assert_called_once_with(
+            gemma_cmd='gemma-wrapper',
+            gemma_wrapper_kwargs={
+                'input': '/tmp/my-token/hash-k-output.json'
+            },
+            gemma_kwargs={'g': '/tmp/my-token/genofile.txt',
+                          'p': '/tmp/my-token/phenofile.txt',
+                          'a': '/tmp/my-token/snpfile.txt',
+                          'c': '/tmp/my-token/covarfile.txt',
+                          'lmm': 9},
+            output_file='/tmp/my-token/hash-gwa-output.json')
+        self.assertEqual(response.get_json(), {
+            "unique_id": "my-unique-id",
+            "status": "queued",
+            "output_file": "hash-gwa-output.json"
+        })