From 87a3e2254bdd837e9647c42b9c4ab98e986c5e1d Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Wed, 3 Mar 2021 11:51:29 +0300 Subject: Add new endpoint: "/gemma/k-compute/" * gn3/api/gemma.py (compute_k): New function. * tests/integration/test_gemma.py (tesk_k_compute): New test case. --- gn3/api/gemma.py | 41 +++++++++++++++++++++++++++++++++++++++++ tests/integration/test_gemma.py | 27 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/gn3/api/gemma.py b/gn3/api/gemma.py index a351ab1..ec5f3f0 100644 --- a/gn3/api/gemma.py +++ b/gn3/api/gemma.py @@ -9,6 +9,8 @@ from flask import request from gn3.commands import queue_cmd from gn3.commands import run_cmd +from gn3.file_utils import get_hash_of_files +from gn3.file_utils import jsonfile_to_dict from gn3.computations.gemma import generate_hash_of_string from gn3.computations.gemma import generate_pheno_txt_file from gn3.computations.gemma import generate_gemma_computation_cmd @@ -92,3 +94,42 @@ gemma endpoints, return the status of the command return jsonify(status=128, error="The unique id you used does not exist!"), 500 return jsonify(status=status.decode("utf-8")) + + +@gemma.route("/k-compute/", methods=["POST"]) +def compute_k(token): + """Given a genofile, traitfile, snpsfile, and the token, compute the k-valuen +and return .json with a UNIQUE-ID of the job. The genofile, +traitfile, and snpsfile are extracted from a metadata.json file. + + """ + 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=None, + 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 e434dab..5f4e52b 100644 --- a/tests/integration/test_gemma.py +++ b/tests/integration/test_gemma.py @@ -149,3 +149,30 @@ class GemmaAPITest(unittest.TestCase): key="status") self.assertEqual(response.get_json(), {"status": "test"}) + + @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(self, mock_json, mock_hash, mock_cmd, + mock_queue_cmd): + """Test /gemma/k-compute/""" + 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("/gemma/k-compute/test-data") + self.assertEqual(response.get_json(), { + "output_file": "hash-k-output.json", + "status": "queued", + "unique_id": "my-unique-id" + }) -- cgit v1.2.3