aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/computations/test_gemma.py
blob: 4a76bef18eee0a3f39a03c8bead3b1c900c694aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Test cases for procedures defined in computations.gemma"""
import unittest

from unittest import mock
from gn3.computations.gemma import generate_gemma_cmd
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


class TestGemma(unittest.TestCase):
    """Test cases for computations.gemma module"""
    def test_generate_pheno_txt_file(self):
        """Test that the pheno text file is generated correctly"""
        open_mock = mock.mock_open()
        with mock.patch("gn3.computations.gemma.open", open_mock, create=True):
            _file = generate_pheno_txt_file(tmpdir="/tmp",
                                            trait_filename="phenotype.txt",
                                            values=["x", "x", "BXD07 438.700"])
            self.assertEqual(_file, ("/tmp/gn2/phenotype_"
                                     "P7y6QWnwBPedSZdL0+m/GQ.txt"))
        open_mock.assert_called_with(("/tmp/gn2/phenotype_"
                                      "P7y6QWnwBPedSZdL0+m/GQ.txt"), "w")
        open_mock.return_value.write.assert_has_calls([
            mock.call("NA\n"),
            mock.call("NA\n"),
            mock.call("BXD07 438.700\n")
        ])

    def test_generate_hash_of_string(self):
        """Test that a string is hashed correctly"""
        self.assertEqual(generate_hash_of_string("I^iQP&TlSR^z"),
                         "hMVRw8kbEp49rOmoIkhMjA")

    @mock.patch("gn3.computations.gemma.do_paths_exist")
    def test_compose_k_computation_cmd(self, mock_pathsp):
        """Test that a K computation cmd is constructed properly"""
        mock_pathsp.return_value = True
        self.assertEqual(
            generate_gemma_computation_cmd(
                gemma_cmd="gemma-wrapper",
                gemma_wrapper_kwargs=None,
                gemma_kwargs={
                    "geno_filename": "genofile.txt",
                    "trait_filename": "test.txt",
                    "covar_filename": "genofile_snps.txt"
                },
                output_file="/tmp/gn2/k_output_gUFhGu4rLG7k+CXLPk1OUg.txt",
            ), ("gemma-wrapper --json -- "
                "-g genofile.txt -p "
                "test.txt -a genofile_snps.txt "
                "-gk > /tmp/gn2/"
                "k_output_gUFhGu4rLG7k+CXLPk1OUg.txt"))

    @mock.patch("gn3.computations.gemma.get_hash_of_files")
    def test_compute_k_values_without_loco(self, mock_get_hash):
        """Test computing k valuse without loco"""
        mock_get_hash.return_value = "my-hash"
        self.assertEqual(
            generate_gemma_cmd(gemma_cmd="gemma-wrapper",
                               output_dir="/tmp",
                               token="my-token",
                               gemma_kwargs={
                                   "g": "genofile",
                                   "p": "phenofile",
                                   "a": "snpsfile"
                               }), {
                                   "output_file":
                                   "my-hash-output.json",
                                   "gemma_cmd":
                                   ("gemma-wrapper --json -- -g genofile "
                                    "-p phenofile -a snpsfile "
                                    "-gk > /tmp/my-token/my-hash-output.json")
                               })

    @mock.patch("gn3.computations.gemma.get_hash_of_files")
    def test_generate_gemma_cmd_with_loco(self, mock_get_hash):
        """Test computing k valuse with loco"""
        mock_get_hash.return_value = "my-hash"
        self.assertEqual(
            generate_gemma_cmd(gemma_cmd="gemma-wrapper",
                               output_dir="/tmp",
                               token="my-token",
                               chromosomes="1,2,3,4,5",
                               gemma_kwargs={
                                   "g": "genofile",
                                   "p": "phenofile",
                                   "a": "snpsfile"
                               }), {
                                   "output_file":
                                   "my-hash-r+gF5a-output.json",
                                   "gemma_cmd": ("gemma-wrapper --json "
                                                 "--loco --input 1,2,3,4,5 "
                                                 "-- -g genofile "
                                                 "-p phenofile -a snpsfile "
                                                 "-gk > /tmp/my-token/"
                                                 "my-hash-r+gF5a-output.json")
                               })