diff options
Diffstat (limited to 'wqflask/tests/unit')
-rw-r--r-- | wqflask/tests/unit/wqflask/api/test_correlation.py | 163 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py | 183 |
2 files changed, 223 insertions, 123 deletions
diff --git a/wqflask/tests/unit/wqflask/api/test_correlation.py b/wqflask/tests/unit/wqflask/api/test_correlation.py index 50df2bb4..53e1b9a2 100644 --- a/wqflask/tests/unit/wqflask/api/test_correlation.py +++ b/wqflask/tests/unit/wqflask/api/test_correlation.py @@ -20,12 +20,7 @@ class MockDataset(AttributeSetter): return None def retrieve_genes(self, id=None): - return { - "TT-1": "GH-1", - "TT-2": "GH-2", - "TT-3": "GH-3" - - } + return {"TT-1": "GH-1", "TT-2": "GH-2", "TT-3": "GH-3"} class TestCorrelations(unittest.TestCase): @@ -37,18 +32,10 @@ class TestCorrelations(unittest.TestCase): self.app_context.pop() def test_init_corr_params(self): - start_vars = { - "return_count": "3", - "type": "T1", - "method": "spearman" - } + start_vars = {"return_count": "3", "type": "T1", "method": "spearman"} corr_params_results = init_corr_params(start_vars=start_vars) - expected_results = { - "return_count": 3, - "type": "T1", - "method": "spearman" - } + expected_results = {"return_count": 3, "type": "T1", "method": "spearman"} self.assertEqual(corr_params_results, expected_results) @@ -57,74 +44,100 @@ class TestCorrelations(unittest.TestCase): conn = mock.MagicMock() mock_db.return_value.__enter__.return_value = conn with conn.cursor() as cursor: - cursor.fetchone.side_effect = [ - ("MG-1",), ("MG-2",)] - - self.assertEqual(convert_to_mouse_gene_id( - species="Other", gene_id=""), None) - self.assertEqual(convert_to_mouse_gene_id( - species="mouse", gene_id="MG-4"), "MG-4") - self.assertEqual(convert_to_mouse_gene_id( - species="rat", gene_id="R1"), "MG-1") - self.assertEqual(convert_to_mouse_gene_id( - species="human", gene_id="H1"), "MG-2") + cursor.fetchone.side_effect = [("MG-1",), ("MG-2",)] + + self.assertEqual( + convert_to_mouse_gene_id(species="Other", gene_id=""), None + ) + self.assertEqual( + convert_to_mouse_gene_id(species="mouse", gene_id="MG-4"), "MG-4" + ) + self.assertEqual( + convert_to_mouse_gene_id(species="rat", gene_id="R1"), "MG-1" + ) + self.assertEqual( + convert_to_mouse_gene_id(species="human", gene_id="H1"), "MG-2" + ) @mock.patch("wqflask.api.correlation.database_connection") @mock.patch("wqflask.api.correlation.convert_to_mouse_gene_id") def test_do_literature_correlation_for_all_traits( - self, - mock_convert_to_mouse_geneid, mock_db): - mock_convert_to_mouse_geneid.side_effect = [ - "MG-1", "MG-2;", "MG-3", "MG-4"] - - trait_geneid_dict = { - "TT-1": "GH-1", - "TT-2": "GH-2", - "TT-3": "GH-3" + self, mock_convert_to_mouse_geneid, mock_db + ): + mock_convert_to_mouse_geneid.side_effect = ["MG-1", "MG-2;", "MG-3", "MG-4"] - } + trait_geneid_dict = {"TT-1": "GH-1", "TT-2": "GH-2", "TT-3": "GH-3"} conn = mock.MagicMock() mock_db.return_value.__enter__.return_value = conn with conn.cursor() as cursor: - cursor.fetchone.side_effect = [ - ("V1", ), ("V2", ), ("V3", )] + cursor.fetchone.side_effect = [("V1",), ("V2",), ("V3",)] this_trait = AttributeSetter({"geneid": "GH-1"}) target_dataset = AttributeSetter( - {"group": AttributeSetter({"species": "rat"})}) + {"group": AttributeSetter({"species": "rat"})} + ) results = do_literature_correlation_for_all_traits( - this_trait=this_trait, target_dataset=target_dataset, - trait_geneid_dict=trait_geneid_dict, corr_params={}) - expected_results = {'TT-1': ['GH-1', 0], - 'TT-2': ['GH-2', 'V1'], 'TT-3': ['GH-3', 'V2']} + this_trait=this_trait, + target_dataset=target_dataset, + trait_geneid_dict=trait_geneid_dict, + corr_params={}, + ) + expected_results = { + "TT-1": ["GH-1", 0], + "TT-2": ["GH-2", "V1"], + "TT-3": ["GH-3", "V2"], + } self.assertEqual(results, expected_results) @mock.patch("wqflask.api.correlation.corr_result_helpers.normalize_values") def test_get_sample_r_and_p_values(self, mock_normalize): group = AttributeSetter( - {"samplelist": ["S1", "S2", "S3", "S4", "S5", "S6", "S7"]}) + {"samplelist": ["S1", "S2", "S3", "S4", "S5", "S6", "S7"]} + ) target_dataset = AttributeSetter({"group": group}) target_vals = [3.4, 6.2, 4.1, 3.4, 1.2, 5.6] - trait_data = {"S1": AttributeSetter({"value": 2.3}), "S2": AttributeSetter({"value": 1.1}), - "S3": AttributeSetter( - {"value": 6.3}), "S4": AttributeSetter({"value": 3.6}), "S5": AttributeSetter({"value": 4.1}), - "S6": AttributeSetter({"value": 5.0})} + trait_data = { + "S1": AttributeSetter({"value": 2.3}), + "S2": AttributeSetter({"value": 1.1}), + "S3": AttributeSetter({"value": 6.3}), + "S4": AttributeSetter({"value": 3.6}), + "S5": AttributeSetter({"value": 4.1}), + "S6": AttributeSetter({"value": 5.0}), + } this_trait = AttributeSetter({"data": trait_data}) - mock_normalize.return_value = ([2.3, 1.1, 6.3, 3.6, 4.1, 5.0], - [3.4, 6.2, 4.1, 3.4, 1.2, 5.6], 6) - mock_normalize.side_effect = [([2.3, 1.1, 6.3, 3.6, 4.1, 5.0], - [3.4, 6.2, 4.1, 3.4, 1.2, 5.6], 6), - ([2.3, 1.1, 6.3, 3.6, 4.1, 5.0], - [3.4, 6.2, 4.1, 3.4, 1.2, 5.6], 6), - ([2.3, 1.1, 1.4], [3.4, 6.2, 4.1], 3)] - - results_pearsonr = get_sample_r_and_p_values(this_trait=this_trait, this_dataset={ - }, target_vals=target_vals, target_dataset=target_dataset, type="pearson") - results_spearmanr = get_sample_r_and_p_values(this_trait=this_trait, this_dataset={ - }, target_vals=target_vals, target_dataset=target_dataset, type="spearman") - results_num_overlap = get_sample_r_and_p_values(this_trait=this_trait, this_dataset={ - }, target_vals=target_vals, target_dataset=target_dataset, type="pearson") + mock_normalize.return_value = ( + [2.3, 1.1, 6.3, 3.6, 4.1, 5.0], + [3.4, 6.2, 4.1, 3.4, 1.2, 5.6], + 6, + ) + mock_normalize.side_effect = [ + ([2.3, 1.1, 6.3, 3.6, 4.1, 5.0], [3.4, 6.2, 4.1, 3.4, 1.2, 5.6], 6), + ([2.3, 1.1, 6.3, 3.6, 4.1, 5.0], [3.4, 6.2, 4.1, 3.4, 1.2, 5.6], 6), + ([2.3, 1.1, 1.4], [3.4, 6.2, 4.1], 3), + ] + + results_pearsonr = get_sample_r_and_p_values( + this_trait=this_trait, + this_dataset={}, + target_vals=target_vals, + target_dataset=target_dataset, + type="pearson", + ) + results_spearmanr = get_sample_r_and_p_values( + this_trait=this_trait, + this_dataset={}, + target_vals=target_vals, + target_dataset=target_dataset, + type="spearman", + ) + results_num_overlap = get_sample_r_and_p_values( + this_trait=this_trait, + this_dataset={}, + target_vals=target_vals, + target_dataset=target_dataset, + type="pearson", + ) expected_pearsonr = [-0.21618688834430866, 0.680771605997119, 6] expected_spearmanr = [-0.11595420713048969, 0.826848213385815, 6] for i, val in enumerate(expected_pearsonr): @@ -137,18 +150,26 @@ class TestCorrelations(unittest.TestCase): def test_calculate_results(self, literature_correlation): literature_correlation.return_value = { - 'TT-1': ['GH-1', 0], 'TT-2': ['GH-2', 3], 'TT-3': ['GH-3', 1]} + "TT-1": ["GH-1", 0], + "TT-2": ["GH-2", 3], + "TT-3": ["GH-3", 1], + } - this_dataset = MockDataset( - {"group": AttributeSetter({"species": "rat"})}) - target_dataset = MockDataset( - {"group": AttributeSetter({"species": "rat"})}) + this_dataset = MockDataset({"group": AttributeSetter({"species": "rat"})}) + target_dataset = MockDataset({"group": AttributeSetter({"species": "rat"})}) this_trait = AttributeSetter({"geneid": "GH-1"}) corr_params = {"type": "literature"} sorted_results = calculate_results( - this_trait=this_trait, this_dataset=this_dataset, target_dataset=target_dataset, corr_params=corr_params) - expected_results = {'TT-2': ['GH-2', 3], - 'TT-3': ['GH-3', 1], 'TT-1': ['GH-1', 0]} + this_trait=this_trait, + this_dataset=this_dataset, + target_dataset=target_dataset, + corr_params=corr_params, + ) + expected_results = { + "TT-2": ["GH-2", 3], + "TT-3": ["GH-3", 1], + "TT-1": ["GH-1", 0], + } self.assertTrue(isinstance(sorted_results, OrderedDict)) self.assertEqual(dict(sorted_results), expected_results) diff --git a/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py b/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py index 46a74231..a09d1538 100644 --- a/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py +++ b/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py @@ -17,48 +17,110 @@ class TestSnpBrowser(unittest.TestCase): 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"} + 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']) + 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) + 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=[]) + 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']) + [ + "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("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") + 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"),) + cursor.fetchone.return_value = ( + ("517d729f-aa13-4413" "-a885-40a3f7ff768a"), + ) results = get_gene_id( - species_id="c9c0f59e-1259-4cba-91e6-831ef1a99c83", - gene_name="INSR") + 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") + db_query_value, ("c9c0f59e-1259-4cba-91e6-831ef1a99c83", "INSR") + ) + self.assertEqual(results, "517d729f-aa13-4413-a885-40a3f7ff768a") @mock.patch("wqflask.snp_browser.snp_browser.database_connection") def test_gene_id_name_dict(self, mock_db): @@ -68,25 +130,37 @@ class TestSnpBrowser(unittest.TestCase): 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)) + [ + ("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) + 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'} + 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)") + "SpeciesId = %s AND geneSymbol in (%s, %s, %s)" + ) cursor.execute.assert_called_with( - db_query_value, ("ret3-32rf32", "GH1", "GH2", "GH3")) + db_query_value, ("ret3-32rf32", "GH1", "GH2", "GH3") + ) self.assertEqual(results_found, expected_found) self.assertEqual(no_results, {}) @@ -95,26 +169,31 @@ class TestSnpBrowser(unittest.TestCase): conn = mock.MagicMock() mock_db.return_value.__enter__.return_value = conn with conn.cursor() as cursor: - cursor.fetchone.side_effect = [ - ("fsdf-232sdf-sdf", "GHA"), ""] + 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) + 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)") + "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) + 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))]) + [ + 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("wqflask.snp_browser.snp_browser.database_connection") @@ -124,4 +203,4 @@ class TestSnpBrowser(unittest.TestCase): 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': []}) + self.assertEqual(results, {"mouse": [], "rat": []}) |