From 4cf5c29a06001577054b3548a24d895cf1911e13 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Thu, 4 Mar 2021 23:05:34 +0300 Subject: Add new endpoint "/gwa-compute//loco/covariates/maf//" --- gn3/api/gemma.py | 50 +++++++++++++++++++++++++++++++++++++++++ tests/integration/test_gemma.py | 49 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/gn3/api/gemma.py b/gn3/api/gemma.py index e5a38e4..a341f62 100644 --- a/gn3/api/gemma.py +++ b/gn3/api/gemma.py @@ -154,6 +154,8 @@ values. genofile, phenofile, snpsfile = [os.path.join(working_dir, _dict.get(x)) for x in ["geno", "pheno", "snps"]] + if not do_paths_exist([genofile, phenofile, snpsfile]): + raise FileNotFoundError gemma_kwargs = {"g": genofile, "p": phenofile, "a": snpsfile} _hash = get_hash_of_files([genofile, phenofile, snpsfile]) k_output_filename = f"{_hash}-k-output.json" @@ -309,3 +311,51 @@ def compute_gwa_with_loco_maf(k_filename, maf, token): return jsonify(status=128, # use better message message="Metadata file non-existent!") + + +@gemma.route("/gwa-compute//loco/covariates/maf//", + methods=["POST"]) +def compute_gwa_with_loco_covar(k_filename, maf, token): + """Compute GWA values. No Covariates provided. Only loco and maf vals given. + + """ + 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, covarfile]): + raise FileNotFoundError + gemma_kwargs = {"g": genofile, "p": phenofile, + "a": snpsfile, "c": covarfile, + "lmm": _dict.get("lmm", 9), + "maf": float(maf)} + _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=compose_gemma_cmd( + gemma_wrapper_cmd=current_app.config.get("GEMMA_" + "WRAPPER_CMD"), + gemma_wrapper_kwargs={ + "loco": ("--input " + f"{os.path.join(working_dir, k_filename)}") + }, + gemma_kwargs=gemma_kwargs, + gemma_args=["-gk", ">", + (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 61421f2..6d2116b 100644 --- a/tests/integration/test_gemma.py +++ b/tests/integration/test_gemma.py @@ -324,7 +324,6 @@ class GemmaAPITest(unittest.TestCase): "geno": "genofile.txt", "pheno": "phenofile.txt", "snps": "snpfile.txt", - "covar": "covarfile.txt", } mock_hash.return_value = "hash" response = self.app.post(("/api/gemma/gwa-compute/" @@ -349,3 +348,51 @@ class GemmaAPITest(unittest.TestCase): "status": "queued", "output_file": "hash-gwa-output.json" }) + + @mock.patch("gn3.api.gemma.queue_cmd") + @mock.patch("gn3.api.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_gwa_compute_with_loco_covars(self, mock_redis, + mock_path_exist, + mock_json, mock_hash, + mock_queue_cmd): + """Test /gemma/gwa-compute//loco/covariates/maf/ + + """ + 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/gwa-compute/" + "hash-k-output.json/loco/" + "covariates/maf/21/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_queue_cmd.assert_called_once_with( + conn=_redis_conn, + email=None, + job_queue='GN3::job-queue', + cmd=("gemma-wrapper --json --loco --input " + "/tmp/my-token/hash-k-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 " + "-lmm 9 -maf 21.0 " + "-gk > /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" + }) -- cgit v1.2.3