aboutsummaryrefslogtreecommitdiff
path: root/gn2/tests/unit/wqflask/api/test_mapping.py
blob: 226e2a9b252b929b387fddbe243ce1d2f017fb4c (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import unittest
from unittest import mock
from gn2.wqflask.api.mapping import initialize_parameters
from gn2.wqflask.api.mapping import do_mapping_for_api


class AttributeSetter:
    def __init__(self, obj):
        for key, value in obj.items():
            setattr(self, key, value)


class MockGroup(AttributeSetter):
    def get_marker(self):
        self.markers = []


class TestMapping(unittest.TestCase):

    def test_initialize_parameters(self):
        expected_results = {
            "format": "json",
            "limit_to": False,
            "mapping_method": "gemma",
            "maf": 0.01,
            "use_loco": True,
            "num_perm": 0,
            "perm_check": False,
            "transform": False,
            "genofile": False
        }

        results = initialize_parameters(
            start_vars={}, dataset={}, this_trait={})
        self.assertEqual(results, expected_results)

        start_vars = {
            "format": "F1",
            "limit_to": "1",
            "mapping_method": "rqtl",
            "control_marker": True,
            "pair_scan": "true",
            "interval_mapping": "true",
            "use_loco": "true",
            "num_perm": "14",
            "transform": "qnorm",
            "genofile": "BXD.8.geno"
        }

        results_2 = initialize_parameters(
            start_vars=start_vars, dataset={}, this_trait={})
        expected_results = {
            "format": "F1",
            "limit_to": 1,
            "mapping_method": "gemma",
            "maf": 0.01,
            "use_loco": True,
            "num_perm": 14,
            "perm_check": "ON",
            "transform": "qnorm",
            "genofile": "BXD.8.geno"
        }

        self.assertEqual(results_2, expected_results)

    @mock.patch("gn2.wqflask.api.mapping.rqtl_mapping.run_rqtl")
    @mock.patch("gn2.wqflask.api.mapping.gemma_mapping.run_gemma")
    @mock.patch("gn2.wqflask.api.mapping.initialize_parameters")
    @mock.patch("gn2.wqflask.api.mapping.retrieve_sample_data")
    @mock.patch("gn2.wqflask.api.mapping.create_trait")
    @mock.patch("gn2.wqflask.api.mapping.data_set.create_dataset")
    def test_do_mapping_for_api(self, mock_create_dataset, mock_trait, mock_retrieve_sample, mock_param, run_gemma, run_rqtl_geno):
        start_vars = {
            "db": "Temp",
            "trait_id": "dewf3232rff2",
            "format": "F1",
            "mapping_method": "gemma",
            "use_loco": True

        }
        sampleList = ["S1", "S2", "S3", "S4"]
        samplelist = ["S1", "S2", "S4"]
        dataset = AttributeSetter({"group": samplelist})
        this_trait = AttributeSetter({})
        trait_data = AttributeSetter({
            "data": {
                "item1": AttributeSetter({"name": "S1", "value": "S1_value"}),
                "item2": AttributeSetter({"name": "S2", "value": "S2_value"}),
                "item3": AttributeSetter({"name": "S3", "value": "S3_value"}),

            }
        })
        trait = AttributeSetter({
            "data": trait_data
        })

        dataset.return_value = dataset
        mock_trait.return_value = this_trait

        mock_retrieve_sample.return_value = trait
        mock_param.return_value = {
            "format": "F1",
            "limit_to": False,
            "mapping_method": "gemma",
            "maf": 0.01,
            "use_loco": "True",
            "num_perm": 14,
            "perm_check": "ON"
        }

        run_gemma.return_value = ["results"]
        results = do_mapping_for_api(start_vars=start_vars)
        self.assertEqual(results, ("results", None))