aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2021-02-12 16:36:21 +0300
committerBonfaceKilz2021-02-12 21:13:30 +0300
commitf887baaf66cc157f0c77237c41e70e42fe03549c (patch)
tree0d1cc2c8b01f910f436906c109be7b26d9be4e04
parentfcf6fbdaaae460bed917b73db033a3d063443389 (diff)
downloadgenenetwork3-f887baaf66cc157f0c77237c41e70e42fe03549c.tar.gz
Add new procedure for composing a gemma cmd
* gn3/commands.py: New file * tests/unit/test_commands.py: New test-cases for ^^.
-rw-r--r--gn3/commands.py42
-rw-r--r--tests/unit/test_commands.py29
2 files changed, 71 insertions, 0 deletions
diff --git a/gn3/commands.py b/gn3/commands.py
new file mode 100644
index 0000000..50b6d29
--- /dev/null
+++ b/gn3/commands.py
@@ -0,0 +1,42 @@
+"""Procedures used to work with the various bio-informatics cli
+commands"""
+from typing import Dict
+from typing import List
+from typing import Optional
+
+from gn3.file_utils import lookup_file
+from gn3.file_utils import jsonfile_to_dict
+
+
+# pylint: disable=locally-disabled, too-many-arguments
+def compose_gemma_cmd(
+ token: str,
+ metadata_filename: str,
+ gemma_wrapper_cmd: str = "gemma-wrapper",
+ gemma_wrapper_kwargs: Optional[Dict] = None,
+ gemma_kwargs: Optional[Dict] = None,
+ gemma_args: Optional[List] = None) -> str:
+ """Compose a valid GEMMA command given the correct values"""
+ cmd = f"{gemma_wrapper_cmd} --json"
+ if gemma_wrapper_kwargs:
+ cmd += (" " # Add extra space between commands
+ " ".join([f" --{key} {val}" for key, val
+ in gemma_wrapper_kwargs.items()]))
+ data = jsonfile_to_dict(lookup_file("TMPDIR",
+ token,
+ metadata_filename))
+ geno_file = lookup_file(environ_var="TMPDIR",
+ root_dir="genotype",
+ file_name=data.get("geno", ""))
+ pheno_file = lookup_file(environ_var="TMPDIR",
+ root_dir=token,
+ file_name=data.get("geno", ""))
+ cmd += f" -- -g {geno_file} -p {pheno_file}"
+ if gemma_kwargs:
+ cmd += (" "
+ " ".join([f" -{key} {val}"
+ for key, val in gemma_kwargs.items()]))
+ if gemma_args:
+ cmd += (" "
+ " ".join([f" {arg}" for arg in gemma_args]))
+ return cmd
diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py
new file mode 100644
index 0000000..a319332
--- /dev/null
+++ b/tests/unit/test_commands.py
@@ -0,0 +1,29 @@
+"""Test cases for procedures defined in commands.py"""
+import os
+import unittest
+
+from unittest import mock
+from gn3.commands import compose_gemma_cmd
+
+
+class TestCommands(unittest.TestCase):
+ """Test cases for commands.py"""
+
+ @mock.patch("gn3.commands.lookup_file")
+ def test_compose_gemma_cmd_no_extra_args(self, mock_lookup_file):
+ """Test that thhe gemma cmd is composed correctly"""
+ metadata_file = os.path.join(os.path.dirname(__file__),
+ "test_data/metadata.json")
+ mock_lookup_file.side_effect = [metadata_file,
+ "/tmp/genofile.txt",
+ "/tmp/gf13Ad0tRX/phenofile.txt"]
+ self.assertEqual(compose_gemma_cmd("gf13Ad0t",
+ "metadata.json",
+ gemma_wrapper_cmd="gemma-wrapper",
+ gemma_wrapper_kwargs=None,
+ gemma_kwargs=None,
+ gemma_args=["-gk"]),
+ ("gemma-wrapper --json -- "
+ "-g /tmp/genofile.txt "
+ "-p /tmp/gf13Ad0tRX/phenofile.txt"
+ " -gk"))