about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--gn3/api/gemma.py54
-rw-r--r--tests/integration/test_gemma.py56
2 files changed, 110 insertions, 0 deletions
diff --git a/gn3/api/gemma.py b/gn3/api/gemma.py
index 372ef82..ed698d3 100644
--- a/gn3/api/gemma.py
+++ b/gn3/api/gemma.py
@@ -505,3 +505,57 @@ def compute_k_gwa_with_loco_only(chromosomes, maf, token):
             status=128,
             # use better message
             message="Metadata file non-existent!")
+
+
+@gemma.route("/k-gwa-compute/covars/loco/<chromosomes>/maf/<maf>/<token>",
+             methods=["POST"])
+def compute_k_gwa_with_loco_and_cavar(chromosomes, maf, token):
+    """k-gwa-compute; Loco with covars; lmm defaults to 9!
+
+    """
+    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"]
+        ]
+        if not do_paths_exist([genofile, phenofile, snpsfile]):
+            raise FileNotFoundError
+        gemma_kwargs = {"g": genofile, "p": phenofile, "a": snpsfile}
+        gemma_k_cmd = generate_gemma_cmd(
+            gemma_cmd=current_app.config.get("GEMMA_"
+                                             "WRAPPER_CMD"),
+            output_dir=current_app.config.get('TMPDIR'),
+            token=token,
+            gemma_kwargs=gemma_kwargs,
+            chromosomes=chromosomes)
+        gemma_kwargs["c"] = covarfile
+        gemma_kwargs["maf"] = float(maf)
+        gemma_kwargs["lmm"] = _dict.get("lmm", 9)
+        gemma_gwa_cmd = generate_gemma_cmd(
+            gemma_cmd=current_app.config.get("GEMMA_"
+                                             "WRAPPER_CMD"),
+            output_dir=current_app.config.get('TMPDIR'),
+            token=token,
+            gemma_kwargs=gemma_kwargs,
+            gemma_wrapper_kwargs={
+                "loco":
+                ("--input "
+                 f"{os.path.join(working_dir, gemma_k_cmd.get('output_file'))}"
+                 )
+            })
+        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"{gemma_k_cmd.get('gemma_cmd')} && "
+                 f"{gemma_gwa_cmd.get('gemma_cmd')}")),
+                       status="queued",
+                       output_file=gemma_gwa_cmd.get("output_file"))
+    # 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 fdc061d..bfce37a 100644
--- a/tests/integration/test_gemma.py
+++ b/tests/integration/test_gemma.py
@@ -561,3 +561,59 @@ class GemmaAPITest(unittest.TestCase):
                 "status": "queued",
                 "output_file": "hash-output.json"
             })
+
+    @mock.patch("gn3.api.gemma.queue_cmd")
+    @mock.patch("gn3.computations.gemma.get_hash_of_files")
+    @mock.patch("gn3.api.gemma.jsonfile_to_dict")
+    @mock.patch("gn3.api.gemma.do_paths_exist")
+    @mock.patch("gn3.api.gemma.redis.Redis")
+    def test_k_gwa_compute_with_loco_and_covar(self, mock_redis,
+                                               mock_path_exist, mock_json,
+                                               mock_hash, mock_queue_cmd):
+        """Test /k-gwa-compute/covars/loco/<chromosomes>/maf/<maf>/<token>
+
+        """
+        mock_path_exist.return_value, _redis_conn = True, mock.MagicMock()
+        mock_redis.return_value = _redis_conn
+        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"
+        response = self.app.post(("/api/gemma/k-gwa-compute/covars/"
+                                  "loco/1%2C2%2C3%2C4/maf/9/my-token"))
+        mock_hash.assert_has_calls([
+            mock.call([
+                '/tmp/my-token/genofile.txt', '/tmp/my-token/phenofile.txt',
+                '/tmp/my-token/snpfile.txt'
+            ]),
+            mock.call([
+                '/tmp/my-token/genofile.txt', '/tmp/my-token/phenofile.txt',
+                '/tmp/my-token/snpfile.txt', '/tmp/my-token/covarfile.txt'
+            ]),
+        ])
+        mock_queue_cmd.assert_called_once_with(
+            conn=_redis_conn,
+            email=None,
+            job_queue='GN3::job-queue',
+            cmd=("gemma-wrapper --json --loco --input 1,2,3,4 -- "
+                 "-g /tmp/my-token/genofile.txt "
+                 "-p /tmp/my-token/phenofile.txt "
+                 "-a /tmp/my-token/snpfile.txt "
+                 "-gk > /tmp/my-token/hash-+O9bus-output.json "
+                 "&& gemma-wrapper --json --loco "
+                 "--input /tmp/my-token/hash-+O9bus-output.json -- "
+                 "-g /tmp/my-token/genofile.txt "
+                 "-p /tmp/my-token/phenofile.txt "
+                 "-a /tmp/my-token/snpfile.txt "
+                 "-c /tmp/my-token/covarfile.txt -maf 9.0 -lmm 9 "
+                 "-gk > /tmp/my-token/hash-output.json"))
+        self.assertEqual(
+            response.get_json(), {
+                "unique_id": "my-unique-id",
+                "status": "queued",
+                "output_file": "hash-output.json"
+            })