aboutsummaryrefslogtreecommitdiff
path: root/gn2/tests/unit/wqflask/snp_browser/test_snp_browser.py
blob: bf0421c0f3931bb50c1c6843a206dce6f6ab7fdd (about) (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import unittest
from unittest import mock
from gn2.wqflask import app
from gn2.wqflask.snp_browser.snp_browser import get_gene_id
from gn2.wqflask.snp_browser.snp_browser import get_gene_id_name_dict
from gn2.wqflask.snp_browser.snp_browser import check_if_in_gene
from gn2.wqflask.snp_browser.snp_browser import get_browser_sample_lists
from gn2.wqflask.snp_browser.snp_browser import get_header_list


class TestSnpBrowser(unittest.TestCase):
    def setUp(self):
        self.app_context = app.app_context()
        self.app_context.push()

    def tearDown(self):
        self.app_context.pop()

    def test_get_header_list(self):
        empty_columns = {
            "snp_source": "false",
            "conservation_score": "true",
            "gene_name": "false",
            "transcript": "false",
            "exon": "false",
            "domain_2": "true",
            "function": "false",
            "function_details": "true",
        }
        strains = {"mouse": ["S1", "S2", "S3", "S4", "S5"], "rat": []}
        expected_results = (
            [
                [
                    "Index",
                    "SNP ID",
                    "Chr",
                    "Mb",
                    "Alleles",
                    "ConScore",
                    "Domain 1",
                    "Domain 2",
                    "Details",
                ],
                ["S1", "S2", "S3", "S4", "S5"],
            ],
            5,
            [
                "index",
                "snp_name",
                "chr",
                "mb_formatted",
                "alleles",
                "conservation_score",
                "domain_1",
                "domain_2",
                "function_details",
                "S1",
                "S2",
                "S3",
                "S4",
                "S5",
            ],
        )

        results_with_snp = get_header_list(
            variant_type="SNP",
            strains=strains,
            species="Mouse",
            empty_columns=empty_columns,
        )
        results_with_indel = get_header_list(
            variant_type="InDel", strains=strains, species="rat", empty_columns=[]
        )
        expected_results_with_indel = (
            [
                "Index",
                "ID",
                "Type",
                "InDel Chr",
                "Mb Start",
                "Mb End",
                "Strand",
                "Size",
                "Sequence",
                "Source",
            ],
            0,
            [
                "index",
                "indel_name",
                "indel_type",
                "indel_chr",
                "indel_mb_s",
                "indel_mb_e",
                "indel_strand",
                "indel_size",
                "indel_sequence",
                "source_name",
            ],
        )

        self.assertEqual(expected_results, results_with_snp)
        self.assertEqual(expected_results_with_indel, results_with_indel)

    @mock.patch("gn2.wqflask.snp_browser.snp_browser.database_connection")
    def test_get_gene_id(self, mock_db):
        db_query_value = (
            "SELECT geneId FROM GeneList WHERE " "SpeciesId = %s AND geneSymbol = %s"
        )
        conn = mock.MagicMock()
        mock_db.return_value.__enter__.return_value = conn
        with conn.cursor() as cursor:
            cursor.fetchone.return_value = (
                ("517d729f-aa13-4413" "-a885-40a3f7ff768a"),
            )

            results = get_gene_id(
                species_id="c9c0f59e-1259-4cba-91e6-831ef1a99c83", gene_name="INSR"
            )
            cursor.execute.assert_called_once_with(
                db_query_value, ("c9c0f59e-1259-4cba-91e6-831ef1a99c83", "INSR")
            )
            self.assertEqual(results, "517d729f-aa13-4413-a885-40a3f7ff768a")

    @mock.patch("gn2.wqflask.snp_browser.snp_browser.database_connection")
    def test_gene_id_name_dict(self, mock_db):
        no_gene_names = []
        conn = mock.MagicMock()
        mock_db.return_value.__enter__.return_value = conn
        with conn.cursor() as cursor:
            cursor.fetchall.side_effect = [
                [],
                [
                    ("fsdf43-fseferger-f22", "GH1"),
                    ("1sdf43-fsewferger-f22", "GH2"),
                    ("fwdj43-fstferger-f22", "GH3"),
                ],
            ]
            self.assertEqual(
                "",
                get_gene_id_name_dict(
                    species_id="fregb343bui43g4", gene_name_list=no_gene_names
                ),
            )
            gene_name_list = ["GH1", "GH2", "GH3"]
            no_results = get_gene_id_name_dict(
                species_id="ret3-32rf32", gene_name_list=gene_name_list
            )
            results_found = get_gene_id_name_dict(
                species_id="ret3-32rf32", gene_name_list=gene_name_list
            )
            expected_found = {
                "GH1": "fsdf43-fseferger-f22",
                "GH2": "1sdf43-fsewferger-f22",
                "GH3": "fwdj43-fstferger-f22",
            }
            db_query_value = (
                "SELECT geneId, geneSymbol FROM GeneList WHERE "
                "SpeciesId = %s AND geneSymbol in (%s, %s, %s)"
            )
            cursor.execute.assert_called_with(
                db_query_value, ("ret3-32rf32", "GH1", "GH2", "GH3")
            )
            self.assertEqual(results_found, expected_found)
            self.assertEqual(no_results, {})

    @mock.patch("gn2.wqflask.snp_browser.snp_browser.database_connection")
    def test_check_if_in_gene(self, mock_db):
        conn = mock.MagicMock()
        mock_db.return_value.__enter__.return_value = conn
        with conn.cursor() as cursor:
            cursor.fetchone.side_effect = [("fsdf-232sdf-sdf", "GHA"), ""]
            results_found = check_if_in_gene(
                species_id="517d729f-aa13-4413-a885-40a3f7ff768a", chr_="CH1", mb=12.09
            )
            self.assertEqual(results_found, ["fsdf-232sdf-sdf", "GHA"])
            db_query_value = (
                "SELECT geneId, geneSymbol FROM GeneList "
                "WHERE SpeciesId = %s AND chromosome = %s "
                "AND (txStart < %s AND txEnd > %s)"
            )
            gene_not_found = check_if_in_gene(
                species_id="517d729f-aa13-4413-a885-40a3f7ff768a", chr_="CH1", mb=12.09
            )
            cursor.execute.assert_has_calls(
                [
                    mock.call(
                        db_query_value,
                        ("517d729f-aa13-4413-a885-40a3f7ff768a", "CH1", 12.09, 12.09),
                    ),
                    mock.call(
                        db_query_value,
                        ("517d729f-aa13-4413-a885-40a3f7ff768a", "CH1", 12.09, 12.09),
                    ),
                ]
            )
            self.assertEqual(gene_not_found, "")

    @mock.patch("gn2.wqflask.snp_browser.snp_browser.database_connection")
    def test_get_browser_sample_lists(self, mock_db):
        conn = mock.MagicMock()
        mock_db.return_value.__enter__.return_value = conn
        with conn.cursor() as cursor:
            cursor.execute.return_value.fetchall.return_value = []
            results = get_browser_sample_lists(species_id="12")
            self.assertEqual(results, {"mouse": [], "rat": []})