diff options
author | uditgulati | 2021-01-05 13:13:28 -0600 |
---|---|---|
committer | uditgulati | 2021-01-05 13:13:28 -0600 |
commit | 1e7a4d76fa36d1476fe37ae9b4c0a946fe99bce1 (patch) | |
tree | 0d18520e09fa0edce0080c773d0edd14750b855c /wqflask/tests | |
parent | e106f5888cb4d761a6dcb71dc0273e461ef3c71a (diff) | |
parent | e36d40d64c1cf8476002c46b5c9dba308151f75c (diff) | |
download | genenetwork2-1e7a4d76fa36d1476fe37ae9b4c0a946fe99bce1.tar.gz |
Merge branch 'testing' of https://github.com/genenetwork/genenetwork2 into snp_browser_changes
Diffstat (limited to 'wqflask/tests')
-rw-r--r-- | wqflask/tests/integration/__init__.py (renamed from wqflask/tests/base/__init__.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/integration/test_markdown_routes.py | 21 | ||||
-rw-r--r-- | wqflask/tests/unit/__init__.py (renamed from wqflask/tests/utility/__init__.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/base/__init__.py (renamed from wqflask/tests/wqflask/__init__.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/base/data.py (renamed from wqflask/tests/base/data.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/base/test_data_set.py (renamed from wqflask/tests/base/test_data_set.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/base/test_general_object.py (renamed from wqflask/tests/base/test_general_object.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/base/test_trait.py (renamed from wqflask/tests/base/test_trait.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/base/test_webqtl_case_data.py (renamed from wqflask/tests/base/test_webqtl_case_data.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/__init__.py (renamed from wqflask/tests/wqflask/api/__init__.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/test_authentication_tools.py (renamed from wqflask/tests/utility/test_authentication_tools.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/test_chunks.py (renamed from wqflask/tests/utility/test_chunks.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/test_corestats.py (renamed from wqflask/tests/utility/test_corestats.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/test_corr_result_helpers.py (renamed from wqflask/tests/utility/test_corr_result_helpers.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/test_formatting.py (renamed from wqflask/tests/utility/test_formatting.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/utility/test_hmac.py (renamed from wqflask/tests/utility/test_hmac.py) | 1 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/__init__.py (renamed from wqflask/tests/wqflask/marker_regression/__init__.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/api/__init__.py (renamed from wqflask/tests/wqflask/show_trait/__init__.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/api/test_gen_menu.py (renamed from wqflask/tests/wqflask/api/test_gen_menu.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/__init__.py | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/test_display_mapping_results.py (renamed from wqflask/tests/wqflask/marker_regression/test_display_mapping_results.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/test_gemma_mapping.py | 181 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/test_plink_mapping.py | 85 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/test_qtlreaper_mapping.py | 21 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 48 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py | 284 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/show_trait/__init__.py | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/show_trait/test_export_trait_data.py (renamed from wqflask/tests/wqflask/show_trait/test_export_trait_data.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/test_collect.py (renamed from wqflask/tests/wqflask/test_collect.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/test_markdown_routes.py | 54 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/test_pbkdf2.py (renamed from wqflask/tests/wqflask/test_pbkdf2.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/test_user_login.py (renamed from wqflask/tests/wqflask/test_user_login.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/unit/wqflask/test_user_session.py (renamed from wqflask/tests/wqflask/test_user_session.py) | 0 | ||||
-rw-r--r-- | wqflask/tests/wqflask/show_trait/testSampleList.py | 16 | ||||
-rw-r--r-- | wqflask/tests/wqflask/show_trait/test_show_trait.py | 278 |
35 files changed, 988 insertions, 1 deletions
diff --git a/wqflask/tests/base/__init__.py b/wqflask/tests/integration/__init__.py index e69de29b..e69de29b 100644 --- a/wqflask/tests/base/__init__.py +++ b/wqflask/tests/integration/__init__.py diff --git a/wqflask/tests/integration/test_markdown_routes.py b/wqflask/tests/integration/test_markdown_routes.py new file mode 100644 index 00000000..5e3e5045 --- /dev/null +++ b/wqflask/tests/integration/test_markdown_routes.py @@ -0,0 +1,21 @@ +"Integration tests for markdown routes" +import unittest + +from bs4 import BeautifulSoup + +from wqflask import app + + +class TestGenMenu(unittest.TestCase): + """Tests for glossary""" + + def setUp(self): + self.app = app.test_client() + + def tearDown(self): + pass + + def test_glossary_page(self): + """Test that the glossary page is rendered properly""" + response = self.app.get('/glossary', follow_redirects=True) + pass diff --git a/wqflask/tests/utility/__init__.py b/wqflask/tests/unit/__init__.py index e69de29b..e69de29b 100644 --- a/wqflask/tests/utility/__init__.py +++ b/wqflask/tests/unit/__init__.py diff --git a/wqflask/tests/wqflask/__init__.py b/wqflask/tests/unit/base/__init__.py index e69de29b..e69de29b 100644 --- a/wqflask/tests/wqflask/__init__.py +++ b/wqflask/tests/unit/base/__init__.py diff --git a/wqflask/tests/base/data.py b/wqflask/tests/unit/base/data.py index 06a5a989..06a5a989 100644 --- a/wqflask/tests/base/data.py +++ b/wqflask/tests/unit/base/data.py diff --git a/wqflask/tests/base/test_data_set.py b/wqflask/tests/unit/base/test_data_set.py index 96563a16..96563a16 100644 --- a/wqflask/tests/base/test_data_set.py +++ b/wqflask/tests/unit/base/test_data_set.py diff --git a/wqflask/tests/base/test_general_object.py b/wqflask/tests/unit/base/test_general_object.py index 00fd3c72..00fd3c72 100644 --- a/wqflask/tests/base/test_general_object.py +++ b/wqflask/tests/unit/base/test_general_object.py diff --git a/wqflask/tests/base/test_trait.py b/wqflask/tests/unit/base/test_trait.py index 826ccefd..826ccefd 100644 --- a/wqflask/tests/base/test_trait.py +++ b/wqflask/tests/unit/base/test_trait.py diff --git a/wqflask/tests/base/test_webqtl_case_data.py b/wqflask/tests/unit/base/test_webqtl_case_data.py index 8e8ba482..8e8ba482 100644 --- a/wqflask/tests/base/test_webqtl_case_data.py +++ b/wqflask/tests/unit/base/test_webqtl_case_data.py diff --git a/wqflask/tests/wqflask/api/__init__.py b/wqflask/tests/unit/utility/__init__.py index e69de29b..e69de29b 100644 --- a/wqflask/tests/wqflask/api/__init__.py +++ b/wqflask/tests/unit/utility/__init__.py diff --git a/wqflask/tests/utility/test_authentication_tools.py b/wqflask/tests/unit/utility/test_authentication_tools.py index 5c391be5..5c391be5 100644 --- a/wqflask/tests/utility/test_authentication_tools.py +++ b/wqflask/tests/unit/utility/test_authentication_tools.py diff --git a/wqflask/tests/utility/test_chunks.py b/wqflask/tests/unit/utility/test_chunks.py index 8d90a1ec..8d90a1ec 100644 --- a/wqflask/tests/utility/test_chunks.py +++ b/wqflask/tests/unit/utility/test_chunks.py diff --git a/wqflask/tests/utility/test_corestats.py b/wqflask/tests/unit/utility/test_corestats.py index cf91a248..cf91a248 100644 --- a/wqflask/tests/utility/test_corestats.py +++ b/wqflask/tests/unit/utility/test_corestats.py diff --git a/wqflask/tests/utility/test_corr_result_helpers.py b/wqflask/tests/unit/utility/test_corr_result_helpers.py index e196fbdf..e196fbdf 100644 --- a/wqflask/tests/utility/test_corr_result_helpers.py +++ b/wqflask/tests/unit/utility/test_corr_result_helpers.py diff --git a/wqflask/tests/utility/test_formatting.py b/wqflask/tests/unit/utility/test_formatting.py index 9d3033d1..9d3033d1 100644 --- a/wqflask/tests/utility/test_formatting.py +++ b/wqflask/tests/unit/utility/test_formatting.py diff --git a/wqflask/tests/utility/test_hmac.py b/wqflask/tests/unit/utility/test_hmac.py index 4e3652f8..13d6261d 100644 --- a/wqflask/tests/utility/test_hmac.py +++ b/wqflask/tests/unit/utility/test_hmac.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """Test hmac utility functions""" import unittest diff --git a/wqflask/tests/wqflask/marker_regression/__init__.py b/wqflask/tests/unit/wqflask/__init__.py index e69de29b..e69de29b 100644 --- a/wqflask/tests/wqflask/marker_regression/__init__.py +++ b/wqflask/tests/unit/wqflask/__init__.py diff --git a/wqflask/tests/wqflask/show_trait/__init__.py b/wqflask/tests/unit/wqflask/api/__init__.py index e69de29b..e69de29b 100644 --- a/wqflask/tests/wqflask/show_trait/__init__.py +++ b/wqflask/tests/unit/wqflask/api/__init__.py diff --git a/wqflask/tests/wqflask/api/test_gen_menu.py b/wqflask/tests/unit/wqflask/api/test_gen_menu.py index 84898bd1..84898bd1 100644 --- a/wqflask/tests/wqflask/api/test_gen_menu.py +++ b/wqflask/tests/unit/wqflask/api/test_gen_menu.py diff --git a/wqflask/tests/unit/wqflask/marker_regression/__init__.py b/wqflask/tests/unit/wqflask/marker_regression/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/wqflask/tests/unit/wqflask/marker_regression/__init__.py diff --git a/wqflask/tests/wqflask/marker_regression/test_display_mapping_results.py b/wqflask/tests/unit/wqflask/marker_regression/test_display_mapping_results.py index 8ae0f09f..8ae0f09f 100644 --- a/wqflask/tests/wqflask/marker_regression/test_display_mapping_results.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_display_mapping_results.py diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_gemma_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_gemma_mapping.py new file mode 100644 index 00000000..fe2569b8 --- /dev/null +++ b/wqflask/tests/unit/wqflask/marker_regression/test_gemma_mapping.py @@ -0,0 +1,181 @@ +# test for wqflask/marker_regression/gemma_mapping.py +import unittest +import random +from unittest import mock +from wqflask.marker_regression.gemma_mapping import run_gemma +from wqflask.marker_regression.gemma_mapping import gen_pheno_txt_file +from wqflask.marker_regression.gemma_mapping import gen_covariates_file +from wqflask.marker_regression.gemma_mapping import parse_loco_output + + +class AttributeSetter: + def __init__(self, obj): + for key, val in obj.items(): + setattr(self, key, val) + + +class MockGroup(AttributeSetter): + def get_samplelist(self): + return None + + +class TestGemmaMapping(unittest.TestCase): + + @mock.patch("wqflask.marker_regression.gemma_mapping.parse_loco_output") + def test_run_gemma_firstrun_set_false(self, mock_parse_loco): + """add tests for gemma function where first run is set to false""" + dataset = AttributeSetter( + {"group": AttributeSetter({"genofile": "genofile.geno"})}) + + output_file = "file1" + mock_parse_loco.return_value = [] + this_trait = AttributeSetter({"name": "t1"}) + + result = run_gemma(this_trait=this_trait, this_dataset=dataset, samples=[], vals=[ + ], covariates="", use_loco=True, first_run=False, output_files=output_file) + + expected_results = ([], "file1") + self.assertEqual(expected_results, result) + + @mock.patch("wqflask.marker_regression.gemma_mapping.webqtlConfig.GENERATED_IMAGE_DIR", "/home/user/img") + @mock.patch("wqflask.marker_regression.gemma_mapping.GEMMAOPTS", "-debug") + @mock.patch("wqflask.marker_regression.gemma_mapping.GEMMA_WRAPPER_COMMAND", "ghc") + @mock.patch("wqflask.marker_regression.gemma_mapping.TEMPDIR", "/home/user/data/") + @mock.patch("wqflask.marker_regression.gemma_mapping.parse_loco_output") + @mock.patch("wqflask.marker_regression.gemma_mapping.flat_files") + @mock.patch("wqflask.marker_regression.gemma_mapping.gen_covariates_file") + @mock.patch("wqflask.marker_regression.run_mapping.random.choice") + @mock.patch("wqflask.marker_regression.gemma_mapping.os") + @mock.patch("wqflask.marker_regression.gemma_mapping.gen_pheno_txt_file") + def test_run_gemma_firstrun_set_true(self, mock_gen_pheno_txt, mock_os, mock_choice, mock_gen_covar, mock_flat_files,mock_parse_loco): + """add tests for run_gemma where first run is set to true""" + this_chromosomes={} + for i in range(1, 5): + this_chromosomes[f'CH{i}']=(AttributeSetter({"name": f"CH{i}"})) + chromosomes = AttributeSetter({"chromosomes": this_chromosomes}) + + dataset_group = MockGroup( + {"name": "GP1", "genofile": "file_geno"}) + dataset = AttributeSetter({"group": dataset_group, "name": "dataset1_name", + "species": AttributeSetter({"chromosomes": chromosomes})}) + trait = AttributeSetter({"name": "trait1"}) + samples = [] + mock_gen_pheno_txt.return_value = None + mock_os.path.isfile.return_value = True + mock_gen_covar.return_value = None + mock_choice.return_value = "R" + mock_flat_files.return_value = "/home/genotype/bimbam" + mock_parse_loco.return_value = [] + results = run_gemma(this_trait=trait, this_dataset=dataset, samples=[ + ], vals=[], covariates="", use_loco=True) + self.assertEqual(mock_os.system.call_count,2) + mock_gen_pheno_txt.assert_called_once() + mock_parse_loco.assert_called_once_with(dataset, "GP1_GWA_RRRRRR",True) + mock_os.path.isfile.assert_called_once_with( + ('/home/user/imgfile_output.assoc.txt')) + self.assertEqual(mock_flat_files.call_count, 4) + self.assertEqual(results, ([], "GP1_GWA_RRRRRR")) + + @mock.patch("wqflask.marker_regression.gemma_mapping.TEMPDIR", "/home/user/data") + def test_gen_pheno_txt_file(self): + """add tests for generating pheno txt file""" + with mock.patch("builtins.open", mock.mock_open())as mock_open: + gen_pheno_txt_file(this_dataset={}, genofile_name="", vals=[ + "x", "w", "q", "we", "R"], trait_filename="fitr.re") + mock_open.assert_called_once_with( + '/home/user/data/gn2/fitr.re.txt', 'w') + filehandler = mock_open() + values = ["x", "w", "q", "we", "R"] + write_calls = [mock.call('NA\n'), mock.call('w\n'), mock.call( + 'q\n'), mock.call('we\n'), mock.call('R\n')] + + filehandler.write.assert_has_calls(write_calls) + + @mock.patch("wqflask.marker_regression.gemma_mapping.flat_files") + @mock.patch("wqflask.marker_regression.gemma_mapping.create_trait") + @mock.patch("wqflask.marker_regression.gemma_mapping.create_dataset") + def test_gen_covariates_file(self, create_dataset, create_trait, flat_files): + """add tests for generating covariates files""" + covariates = "X1:X2,Y1:Y2,M1:M3,V1:V2" + samplelist = ["X1", "X2", "X3", "X4"] + create_dataset_side_effect = [] + create_trait_side_effect = [] + + for i in range(4): + create_dataset_side_effect.append(AttributeSetter({"name": f'name_{i}'})) + create_trait_side_effect.append( + AttributeSetter({"data": [f'data_{i}']})) + + create_dataset.side_effect = create_trait_side_effect + create_trait.side_effect = create_trait_side_effect + + group = MockGroup({"name": "group_X", "samplelist": samplelist}) + this_dataset = AttributeSetter({"group": group}) + flat_files.return_value = "Home/Genenetwork" + + with mock.patch("builtins.open", mock.mock_open())as mock_open: + gen_covariates_file(this_dataset=this_dataset, covariates=covariates, + samples=["x1", "x2", "X3"]) + + create_dataset.assert_has_calls( + [mock.call('X2'), mock.call('Y2'), mock.call('M3'), mock.call('V2')]) + mock_calls = [] + trait_names = ["X1", "Y1", "M1", "V1"] + + for i, trait in enumerate(create_trait_side_effect): + mock_calls.append( + mock.call(dataset=trait, name=trait_names[i], cellid=None)) + + create_trait.assert_has_calls(mock_calls) + + flat_files.assert_called_once_with('mapping') + mock_open.assert_called_once_with( + 'Home/Genenetwork/group_X_covariates.txt', 'w') + filehandler = mock_open() + filehandler.write.assert_has_calls([mock.call( + '-9\t'), mock.call('-9\t'), mock.call('-9\t'), mock.call('-9\t'), mock.call('\n')]) + + @mock.patch("wqflask.marker_regression.gemma_mapping.TEMPDIR", "/home/tmp") + @mock.patch("wqflask.marker_regression.gemma_mapping.os") + @mock.patch("wqflask.marker_regression.gemma_mapping.json") + def test_parse_loco_outputfile_found(self, mock_json, mock_os): + """add tests for parse loco output file found""" + mock_json.load.return_value = { + "files": [["file_name", "user", "~/file1"], + ["file_name", "user", "~/file2"]] + } + return_file="""X/Y\tM1\t28.457155\tQ\tE\tA\tMMB\t23.3\tW\t0.9\t0.85\t +chr4\tM2\t12\tQ\tE\tMMB\tR\t24\tW\t0.87\t0.5 +Y\tM4\t12\tQ\tE\tMMB\tR\t11.6\tW\t0.21\t0.7 +X\tM5\t12\tQ\tE\tMMB\tR\t21.1\tW\t0.65\t0.6""" + + return_file_2 = """chr\tother\t21322\tQ\tE\tA\tP\tMMB\tCDE\t0.5\t0.4""" + mock_os.path.isfile.return_value = True + file_to_write = """{"files":["file_1","file_2"]}""" + with mock.patch("builtins.open") as mock_open: + + handles = (mock.mock_open(read_data="gwas").return_value, mock.mock_open( + read_data=return_file).return_value, mock.mock_open(read_data=return_file_2).return_value) + mock_open.side_effect = handles + results = parse_loco_output( + this_dataset={}, gwa_output_filename=".xw/") + expected_results= [ + {'name': 'M1', 'chr': 'X/Y', 'Mb': 2.8457155e-05, 'p_value': 0.85, 'additive': 23.3, 'lod_score': 0.07058107428570727}, + {'name': 'M2', 'chr': 4, 'Mb': 1.2e-05, 'p_value': 0.5, 'additive': 24.0, 'lod_score': 0.3010299956639812}, + {'name': 'M4', 'chr': 'Y', 'Mb': 1.2e-05, 'p_value': 0.7, 'additive': 11.6, 'lod_score': 0.1549019599857432}, + {'name': 'M5', 'chr': 'X', 'Mb': 1.2e-05, 'p_value': 0.6, 'additive': 21.1, 'lod_score': 0.22184874961635637}] + + self.assertEqual(expected_results, results) + + @mock.patch("wqflask.marker_regression.gemma_mapping.TEMPDIR", "/home/tmp") + @mock.patch("wqflask.marker_regression.gemma_mapping.os") + def test_parse_loco_outputfile_not_found(self, mock_os): + """add tests for parse loco output where output file not found""" + + mock_os.path.isfile.return_value = False + file_to_write = """{"files":["file_1","file_2"]}""" + + with mock.patch("builtins.open", mock.mock_open(read_data=file_to_write)) as mock_open: + results = parse_loco_output( + this_dataset={}, gwa_output_filename=".xw/") + self.assertEqual(results, []) diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_plink_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_plink_mapping.py new file mode 100644 index 00000000..5eec93f1 --- /dev/null +++ b/wqflask/tests/unit/wqflask/marker_regression/test_plink_mapping.py @@ -0,0 +1,85 @@ +# test for wqflask/marker_regression/plink_mapping.py +import unittest +from unittest import mock +from wqflask.marker_regression.plink_mapping import build_line_list +from wqflask.marker_regression.plink_mapping import get_samples_from_ped_file +from wqflask.marker_regression.plink_mapping import flat_files +from wqflask.marker_regression.plink_mapping import gen_pheno_txt_file_plink +from wqflask.marker_regression.plink_mapping import parse_plink_output + + +class AttributeSetter: + def __init__(self, obj): + for key, val in obj.items(): + setattr(self, key, val) +class TestPlinkMapping(unittest.TestCase): + + + def test_build_line_list(self): + """test for building line list""" + line_1 = "this is line one test" + irregular_line = " this is an, irregular line " + exp_line1 = ["this", "is", "line", "one", "test"] + + results = build_line_list(irregular_line) + self.assertEqual(exp_line1, build_line_list(line_1)) + self.assertEqual([], build_line_list()) + self.assertEqual(["this", "is", "an,", "irregular", "line"], results) + + @mock.patch("wqflask.marker_regression.plink_mapping.flat_files") + def test_get_samples_from_ped_file(self, mock_flat_files): + """test for getting samples from ped file""" + dataset = AttributeSetter({"group": AttributeSetter({"name": "n_1"})}) + file_sample = """Expected_1\tline test +Expected_2\there + Expected_3\tthree""" + mock_flat_files.return_value = "/home/user/" + with mock.patch("builtins.open", mock.mock_open(read_data=file_sample)) as mock_open: + results = get_samples_from_ped_file(dataset) + mock_flat_files.assert_called_once_with("mapping") + mock_open.assert_called_once_with("/home/user/n_1.ped", "r") + self.assertEqual( + ["Expected_1", "Expected_2", "Expected_3"], results) + + @mock.patch("wqflask.marker_regression.plink_mapping.TMPDIR", "/home/user/data/") + @mock.patch("wqflask.marker_regression.plink_mapping.get_samples_from_ped_file") + def test_gen_pheno_txt_file_plink(self, mock_samples): + """test for getting gen_pheno txt file""" + mock_samples.return_value = ["Expected_1", "Expected_2", "Expected_3"] + + trait = AttributeSetter({"name": "TX"}) + dataset = AttributeSetter({"group": AttributeSetter({"name": "n_1"})}) + vals = ["value=K1", "value=K2", "value=K3"] + with mock.patch("builtins.open", mock.mock_open()) as mock_open: + results = gen_pheno_txt_file_plink(this_trait=trait, dataset=dataset, + vals=vals, pheno_filename="ph_file") + mock_open.assert_called_once_with( + "/home/user/data/ph_file.txt", "wb") + filehandler = mock_open() + calls_expected = [mock.call('FID\tIID\tTX\n'), + mock.call('Expected_1\tExpected_1\tK1\nExpected_2\tExpected_2\tK2\nExpected_3\tExpected_3\tK3\n')] + + filehandler.write.assert_has_calls(calls_expected) + + filehandler.close.assert_called_once() + + @mock.patch("wqflask.marker_regression.plink_mapping.TMPDIR", "/home/user/data/") + @mock.patch("wqflask.marker_regression.plink_mapping.build_line_list") + def test_parse_plink_output(self, mock_line_list): + """test for parsing plink output""" + chromosomes = [0, 34, 110, 89, 123, 23, 2] + species = AttributeSetter( + {"name": "S1", "chromosomes": AttributeSetter({"chromosomes": chromosomes})}) + + fake_file = """0 AACCAT T98.6 0.89\n2 AATA B45 0.3\n121 ACG B56.4 NA""" + + mock_line_list.side_effect = [["0", "AACCAT", "T98.6", "0.89"], [ + "2", "AATA", "B45", "0.3"], ["121", "ACG", "B56.4", "NA"]] + with mock.patch("builtins.open", mock.mock_open(read_data=fake_file)) as mock_open: + parse_results = parse_plink_output( + output_filename="P1_file", species=species) + mock_open.assert_called_once_with( + "/home/user/data/P1_file.qassoc", "rb") + expected = (2, {'AACCAT': 0.89, 'AATA': 0.3}) + + self.assertEqual(parse_results, expected) diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_qtlreaper_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_qtlreaper_mapping.py new file mode 100644 index 00000000..b47f877a --- /dev/null +++ b/wqflask/tests/unit/wqflask/marker_regression/test_qtlreaper_mapping.py @@ -0,0 +1,21 @@ +import unittest +from unittest import mock +from wqflask.marker_regression.qtlreaper_mapping import gen_pheno_txt_file + +#issues some methods in genofile object are not defined +#modify samples should equal to vals +class TestQtlReaperMapping(unittest.TestCase): + @mock.patch("wqflask.marker_regression.qtlreaper_mapping.TEMPDIR", "/home/user/data") + def test_gen_pheno_txt_file(self): + vals=["V1","x","V4","V3","x"] + samples=["S1","S2","S3","S4","S5"] + trait_filename="trait_file" + with mock.patch("builtins.open", mock.mock_open())as mock_open: + gen_pheno_txt_file(samples=samples,vals=vals,trait_filename=trait_filename) + mock_open.assert_called_once_with("/home/user/data/gn2/trait_file.txt","w") + filehandler=mock_open() + write_calls= [mock.call('Trait\t'),mock.call('S1\tS3\tS4\n'),mock.call('T1\t'),mock.call('V1\tV4\tV3')] + + filehandler.write.assert_has_calls(write_calls) + + diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py new file mode 100644 index 00000000..c585f1df --- /dev/null +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -0,0 +1,48 @@ +import unittest +from unittest import mock +from wqflask import app +from wqflask.marker_regression.rqtl_mapping import get_trait_data_type +from wqflask.marker_regression.rqtl_mapping import sanitize_rqtl_phenotype +from wqflask.marker_regression.rqtl_mapping import sanitize_rqtl_names + +class TestRqtlMapping(unittest.TestCase): + + def setUp(self): + self.app_context=app.app_context() + self.app_context.push() + + def tearDown(self): + self.app_context.pop() + + + @mock.patch("wqflask.marker_regression.rqtl_mapping.g") + @mock.patch("wqflask.marker_regression.rqtl_mapping.logger") + def test_get_trait_data(self,mock_logger,mock_db): + """test for getting trait data_type return True""" + query_value="""SELECT value FROM TraitMetadata WHERE type='trait_data_type'""" + mock_db.db.execute.return_value.fetchone.return_value=["""{"type":"trait_data_type","name":"T1","traid_id":"fer434f"}"""] + results=get_trait_data_type("traid_id") + mock_db.db.execute.assert_called_with(query_value) + self.assertEqual(results,"fer434f") + + def test_sanitize_rqtl_phenotype(self): + """test for sanitizing rqtl phenotype""" + vals=['f',"x","r","x","x"] + results=sanitize_rqtl_phenotype(vals) + expected_phenotype_string='c(f,NA,r,NA,NA)' + + self.assertEqual(results,expected_phenotype_string) + + def test_sanitize_rqtl_names(self): + """test for sanitzing rqtl names""" + vals=['f',"x","r","x","x"] + expected_sanitized_name="c('f',NA,'r',NA,NA)" + results=sanitize_rqtl_names(vals) + self.assertEqual(expected_sanitized_name,results) + + + + + + + diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py new file mode 100644 index 00000000..4129cc0c --- /dev/null +++ b/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py @@ -0,0 +1,284 @@ +import unittest +import datetime +from unittest import mock + +from wqflask.marker_regression.run_mapping import get_genofile_samplelist +from wqflask.marker_regression.run_mapping import geno_db_exists +from wqflask.marker_regression.run_mapping import write_input_for_browser +from wqflask.marker_regression.run_mapping import export_mapping_results +from wqflask.marker_regression.run_mapping import trim_markers_for_figure +from wqflask.marker_regression.run_mapping import get_perm_strata +from wqflask.marker_regression.run_mapping import get_chr_lengths + + +class AttributeSetter: + def __init__(self, obj): + for k, v in obj.items(): + setattr(self, k, v) + + +class MockGroup(AttributeSetter): + + def get_genofiles(self): + return [{"location": "~/genofiles/g1_file", "sample_list": ["S1", "S2", "S3", "S4"]}] + + +class TestRunMapping(unittest.TestCase): + def setUp(self): + + self.group = MockGroup( + {"genofile": "~/genofiles/g1_file", "name": "GP1_", "species": "Human"}) + chromosomes = { + "3": AttributeSetter({ + "name": "C1", + "length": "0.04" + }), + "4": AttributeSetter({ + "name": "C2", + "length": "0.03" + }), + "5": AttributeSetter({ + "name": "C4", + "length": "0.01" + }) + } + self.dataset = AttributeSetter( + {"fullname": "dataser_1", "group": self.group, "type": "ProbeSet"}) + + self.chromosomes = AttributeSetter({"chromosomes": chromosomes}) + self.trait = AttributeSetter( + {"symbol": "IGFI", "chr": "X1", "mb": 123313}) + + def tearDown(self): + self.dataset = AttributeSetter( + {"group": {"location": "~/genofiles/g1_file"}}) + + def test_get_genofile_samplelist(self): + + results_1 = get_genofile_samplelist(self.dataset) + self.assertEqual(results_1, ["S1", "S2", "S3", "S4"]) + self.group.genofile = "~/genofiles/g2_file" + result_2 = get_genofile_samplelist(self.dataset) + self.assertEqual(result_2, []) + + @mock.patch("wqflask.marker_regression.run_mapping.data_set") + def test_if_geno_db_exists(self, mock_data_set): + mock_data_set.create_dataset.side_effect = [ + AttributeSetter({}), Exception()] + results_no_error = geno_db_exists(self.dataset) + results_with_error = geno_db_exists(self.dataset) + + self.assertEqual(mock_data_set.create_dataset.call_count, 2) + self.assertEqual(results_with_error, "False") + self.assertEqual(results_no_error, "True") + + def test_trim_markers_for_figure(self): + + markers = [{ + "name": "MK1", + "chr": "C1", + "cM": "1", + "Mb": "12000", + "genotypes": [], + "dominance":"TT", + "additive":"VA", + "lod_score":0.5 + }, + { + "name": "MK2", + "chr": "C2", + "cM": "15", + "Mb": "10000", + "genotypes": [], + "lod_score":0.7 + }, + { + "name": "MK1", + "chr": "C3", + "cM": "45", + "Mb": "1", + "genotypes": [], + "dominance":"Tt", + "additive":"VE", + "lod_score":1 + }] + + marker_2 = [{ + "name": "MK1", + "chr": "C1", + "cM": "1", + "Mb": "12000", + "genotypes": [], + "dominance":"TT", + "additive":"VA", + "p_wald":4.6 + }] + results = trim_markers_for_figure(markers) + result_2 = trim_markers_for_figure(marker_2) + expected = [ + { + "name": "MK1", + "chr": "C1", + "cM": "1", + "Mb": "12000", + "genotypes": [], + "dominance":"TT", + "additive":"VA", + "lod_score":0.5 + }, + { + "name": "MK1", + "chr": "C3", + "cM": "45", + "Mb": "1", + "genotypes": [], + "dominance":"Tt", + "additive":"VE", + "lod_score":1 + } + + ] + self.assertEqual(results, expected) + self.assertEqual(result_2, marker_2) + + def test_export_mapping_results(self): + """test for exporting mapping results""" + datetime_mock = mock.Mock(wraps=datetime.datetime) + datetime_mock.now.return_value = datetime.datetime( + 2019, 9, 1, 10, 12, 12) + + markers = [{ + "name": "MK1", + "chr": "C1", + "cM": "1", + "Mb": "12000", + "genotypes": [], + "dominance":"TT", + "additive":"VA", + "lod_score":3 + }, + { + "name": "MK2", + "chr": "C2", + "cM": "15", + "Mb": "10000", + "genotypes": [], + "lod_score":7 + }, + { + "name": "MK1", + "chr": "C3", + "cM": "45", + "Mb": "1", + "genotypes": [], + "dominance":"Tt", + "additive":"VE", + "lod_score":7 + }] + + with mock.patch("builtins.open", mock.mock_open()) as mock_open: + + with mock.patch("wqflask.marker_regression.run_mapping.datetime.datetime", new=datetime_mock): + export_mapping_results(dataset=self.dataset, trait=self.trait, markers=markers, + results_path="~/results", mapping_scale="physic", score_type="-log(p)") + + write_calls = [ + mock.call('Time/Date: 09/01/19 / 10:12:12\n'), + mock.call('Population: Human GP1_\n'), mock.call( + 'Data Set: dataser_1\n'), + mock.call('Gene Symbol: IGFI\n'), mock.call( + 'Location: X1 @ 123313 Mb\n'), + mock.call('\n'), mock.call('Name,Chr,'), + mock.call('Mb,-log(p)'), mock.call('Cm,-log(p)'), + mock.call(',Additive'), mock.call(',Dominance'), + mock.call('\n'), mock.call('MK1,C1,'), + mock.call('12000,'), mock.call('1,'), + mock.call('3'), mock.call(',VA'), + mock.call(',TT'), mock.call('\n'), + mock.call('MK2,C2,'), mock.call('10000,'), + mock.call('15,'), mock.call('7'), + mock.call('\n'), mock.call('MK1,C3,'), + mock.call('1,'), mock.call('45,'), + mock.call('7'), mock.call(',VE'), + mock.call(',Tt') + + ] + mock_open.assert_called_once_with("~/results", "w+") + filehandler = mock_open() + filehandler.write.assert_has_calls(write_calls) + + @mock.patch("wqflask.marker_regression.run_mapping.random.choice") + def test_write_input_for_browser(self, mock_choice): + """test for writing input for browser""" + mock_choice.side_effect = ["F", "i", "l", "e", "s", "x"] + with mock.patch("builtins.open", mock.mock_open()) as mock_open: + expected = ['GP1__Filesx_GWAS', 'GP1__Filesx_ANNOT'] + + results = write_input_for_browser( + this_dataset=self.dataset, gwas_results={}, annotations={}) + self.assertEqual(results, expected) + + def test_get_perm_strata(self): + categorical_vars = ["C1", "C2", "W1"] + used_samples = ["S1", "S2"] + sample_list = AttributeSetter({"sample_attribute_values": { + "S1": { + "C1": "c1_value", + "C2": "c2_value", + "W1": "w1_value" + + }, + "S2": { + "W1": "w2_value", + "W2": "w2_value" + + }, + "S3": { + + "C1": "c1_value", + "C2": "c2_value" + + }, + + }}) + + results = get_perm_strata(this_trait={}, sample_list=sample_list, + categorical_vars=categorical_vars, used_samples=used_samples) + self.assertEqual(results, [2, 1]) + + def test_get_chr_length(self): + """test for getting chromosome length""" + chromosomes = AttributeSetter({"chromosomes": self.chromosomes}) + dataset = AttributeSetter({"species": chromosomes}) + results = get_chr_lengths( + mapping_scale="physic", mapping_method="reaper", dataset=dataset, qtl_results=[]) + chr_lengths = [] + for key, chromo in self.chromosomes.chromosomes.items(): + chr_lengths.append({"chr": chromo.name, "size": chromo.length}) + + self.assertEqual(chr_lengths, results) + + qtl_results = [{ + "chr": "16", + "cM": "0.2" + }, + { + "chr": "12", + "cM": "0.5" + }, + { + "chr": "18", + "cM": "0.1" + }, + { + "chr": "22", + "cM": "0.4" + }, + ] + + result_with_other_mapping_scale = get_chr_lengths( + mapping_scale="other", mapping_method="reaper", dataset=dataset, qtl_results=qtl_results) + expected_value = [{'chr': '1', 'size': '0'}, { + 'chr': '16', 'size': '500000.0'}, {'chr': '18', 'size': '400000.0'}] + + self.assertEqual(result_with_other_mapping_scale, expected_value) diff --git a/wqflask/tests/unit/wqflask/show_trait/__init__.py b/wqflask/tests/unit/wqflask/show_trait/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/wqflask/tests/unit/wqflask/show_trait/__init__.py diff --git a/wqflask/tests/wqflask/show_trait/test_export_trait_data.py b/wqflask/tests/unit/wqflask/show_trait/test_export_trait_data.py index 41761944..41761944 100644 --- a/wqflask/tests/wqflask/show_trait/test_export_trait_data.py +++ b/wqflask/tests/unit/wqflask/show_trait/test_export_trait_data.py diff --git a/wqflask/tests/wqflask/test_collect.py b/wqflask/tests/unit/wqflask/test_collect.py index 9a36132d..9a36132d 100644 --- a/wqflask/tests/wqflask/test_collect.py +++ b/wqflask/tests/unit/wqflask/test_collect.py diff --git a/wqflask/tests/unit/wqflask/test_markdown_routes.py b/wqflask/tests/unit/wqflask/test_markdown_routes.py new file mode 100644 index 00000000..90e0f17c --- /dev/null +++ b/wqflask/tests/unit/wqflask/test_markdown_routes.py @@ -0,0 +1,54 @@ +"""Test functions in markdown utils""" + +import unittest +from unittest import mock + +from dataclasses import dataclass +from wqflask.markdown_routes import render_markdown + + +@dataclass +class MockRequests404: + status_code: int = 404 + + +@dataclass +class MockRequests200: + status_code: int = 200 + content: str = b""" +# Glossary +This is some content + +## Sub-heading +This is another sub-heading""" + + +class TestMarkdownRoutesFunctions(unittest.TestCase): + """Test cases for functions in markdown_routes""" + + @mock.patch('wqflask.markdown_routes.requests.get') + def test_render_markdown_when_fetching_locally(self, requests_mock): + requests_mock.return_value = MockRequests404() + markdown_content = render_markdown("general/glossary/glossary.md") + requests_mock.assert_called_with( + "https://raw.githubusercontent.com" + "/genenetwork/gn-docs/" + "master/general/" + "glossary/glossary.md") + self.assertRegexpMatches(markdown_content, + "Content for general/glossary/glossary.md not available.") + + @mock.patch('wqflask.markdown_routes.requests.get') + def test_render_markdown_when_fetching_remotely(self, requests_mock): + requests_mock.return_value = MockRequests200() + markdown_content = render_markdown("general/glossary/glossary.md") + requests_mock.assert_called_with( + "https://raw.githubusercontent.com" + "/genenetwork/gn-docs/" + "master/general/" + "glossary/glossary.md") + self.assertEqual("""<h1>Glossary</h1> +<p>This is some content</p> +<h2>Sub-heading</h2> +<p>This is another sub-heading</p>""", + markdown_content) diff --git a/wqflask/tests/wqflask/test_pbkdf2.py b/wqflask/tests/unit/wqflask/test_pbkdf2.py index a33fbd4f..a33fbd4f 100644 --- a/wqflask/tests/wqflask/test_pbkdf2.py +++ b/wqflask/tests/unit/wqflask/test_pbkdf2.py diff --git a/wqflask/tests/wqflask/test_user_login.py b/wqflask/tests/unit/wqflask/test_user_login.py index 61cd9ab9..61cd9ab9 100644 --- a/wqflask/tests/wqflask/test_user_login.py +++ b/wqflask/tests/unit/wqflask/test_user_login.py diff --git a/wqflask/tests/wqflask/test_user_session.py b/wqflask/tests/unit/wqflask/test_user_session.py index ebb0334a..ebb0334a 100644 --- a/wqflask/tests/wqflask/test_user_session.py +++ b/wqflask/tests/unit/wqflask/test_user_session.py diff --git a/wqflask/tests/wqflask/show_trait/testSampleList.py b/wqflask/tests/wqflask/show_trait/testSampleList.py new file mode 100644 index 00000000..34c51e3e --- /dev/null +++ b/wqflask/tests/wqflask/show_trait/testSampleList.py @@ -0,0 +1,16 @@ +import unittest +import re +from unittest import mock +from wqflask.show_trait.SampleList import natural_sort + + +class TestSampleList(unittest.TestCase): + def test_natural_sort(self): + "Sort the list into natural alphanumeric order." + + characters_list = ["z", "f", "q", "s", "t", "a", "g"] + names_list = ["temp1", "publish", "Sample", "Dataset"] + sorted_list_a=natural_sort(characters_list) + sorted_list_b=natural_sort(names_list) + self.assertEqual(sorted_list_a, ["a", "f", "g", "q", "s", "t", "z"]) + self.assertEqual(sorted_list_b,["Dataset", "Sample", "publish", "temp1"]) diff --git a/wqflask/tests/wqflask/show_trait/test_show_trait.py b/wqflask/tests/wqflask/show_trait/test_show_trait.py new file mode 100644 index 00000000..8c866874 --- /dev/null +++ b/wqflask/tests/wqflask/show_trait/test_show_trait.py @@ -0,0 +1,278 @@ +"""test for wqflask/show_trait/test_show_trait.py""" +import unittest +from unittest import mock +from wqflask import app +from wqflask.show_trait.show_trait import check_if_attr_exists +from wqflask.show_trait.show_trait import get_ncbi_summary +from wqflask.show_trait.show_trait import has_num_cases +from wqflask.show_trait.show_trait import get_table_widths +from wqflask.show_trait.show_trait import get_categorical_variables +from wqflask.show_trait.show_trait import get_trait_units +from wqflask.show_trait.show_trait import get_nearest_marker +from wqflask.show_trait.show_trait import get_genotype_scales +from wqflask.show_trait.show_trait import requests +from wqflask.show_trait.show_trait import get_scales_from_genofile + + +class TraitObject: + def __init__(self, obj): + for key, value in obj.items(): + setattr(self, key, value) + + +class TestTraits(unittest.TestCase): + def setUp(self): + self.app_context = app.app_context() + self.app_context.push() + + def tearDown(self): + self.app_context.pop() + + def test_check_if_attr_exists_truthy(self): + """"test if attributes exists with true return""" + trait_obj = TraitObject({"id_type": "id"}) + trait_obj2 = TraitObject({"sample_name": ['samp1']}) + results = check_if_attr_exists(trait_obj, "id_type") + result2 = check_if_attr_exists(trait_obj2, "sample_name") + self.assertIsInstance(trait_obj, TraitObject) + self.assertTrue(results) + self.assertTrue(result2) + + def test_check_if_attr_exists_empty_attr(self): + """test if attributes exists with false return""" + trait_obj = TraitObject({"sample": ""}) + trait_obj2 = TraitObject({"group": None}) + result = check_if_attr_exists(trait_obj, "sample") + result2 = check_if_attr_exists(trait_obj, "group") + self.assertFalse(result) + self.assertFalse(result2) + + def test_check_if_attr_exists_falsey(self): + """check if attribute exists with empty attributes""" + trait_obj = TraitObject({}) + results = check_if_attr_exists(trait_obj, "any") + self.assertFalse(results) + + @mock.patch("wqflask.show_trait.show_trait.requests.get") + @mock.patch("wqflask.show_trait.show_trait.check_if_attr_exists") + def test_get_ncbi_summary_request_success(self, mock_exists, mock_get): + """test for getting ncbi summary with + successful request""" + trait = TraitObject({"geneid": "id"}) + mock_exists.return_value = True + content_json_string = """{ + "result":{ + "id":{ + "summary":"this is a summary of the geneid" + } + } + } + """ + get_return_obj = TraitObject({"content": content_json_string}) + mock_get.return_value = get_return_obj + results = get_ncbi_summary(trait) + mock_exists.assert_called_once() + mock_get.assert_called_once_with(f"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gene&id={trait.geneid}&retmode=json") + + self.assertEqual(results, "this is a summary of the geneid") + + @mock.patch("wqflask.show_trait.show_trait.requests.get") + @mock.patch("wqflask.show_trait.show_trait.check_if_attr_exists") + def test_get_ncbi_summary_request_fail(self, mock_exists, mock_get_fail): + """test for getting ncbi summary with request fail""" + trait = TraitObject({"geneid": "id"}) + mock_exists.return_value = True + mock_get_fail.side_effect = Exception("an error occurred") + content_json_string = """{ + "result":{ + "id":{ + "summary":"this is a summary of the geneid" + } + } + } + """ + results = get_ncbi_summary(trait) + self.assertEqual(results, None) + + def test_hash_num_cases_is_probeset(self): + """test for hash num_cases with dataset.type set to Probeset""" + create_dataset = TraitObject({"type": "ProbeSet"}) + create_trait = TraitObject({"dataset": create_dataset}) + self.assertFalse(has_num_cases(create_trait)) + + def test_hash_num_cases_no_probeset(self): + """test for hash num cases with dataset.type not Probeset""" + create_dataset = TraitObject({"type": "Temp"}) + construct_data = { + "nm1": TraitObject({"num_cases": False}), + "nm2": TraitObject({"num_cases": True}), + "nm3": TraitObject({"num_cases": False}) + } + construct_data2 = { + "nm1": TraitObject({"num_cases": False}), + "nm2": TraitObject({"num_cases": False}), + "nm3": TraitObject({"num_cases": False}) + } + create_trait = TraitObject( + {"dataset": create_dataset, "data": construct_data}) + create_trait2 = TraitObject( + {"dataset": create_dataset, "data": construct_data2}) + + results = has_num_cases(create_trait) + self.assertTrue(has_num_cases(create_trait)) + self.assertFalse(has_num_cases(create_trait2)) + + def test_get_table_widths(self): + """test for getting table widths""" + sample_groups = [TraitObject({'se_exists': True, "attributes": ["attr1", "attr2", "attr3"]} + ), TraitObject( + {"se_exists": False, "attributes": ["at1", "at2"] + })] + + results_with_numcase = get_table_widths(sample_groups, True) + result_no_numcase = get_table_widths(sample_groups, False) + + results_one_sample = get_table_widths( + [TraitObject({"se_exists": True, "attributes": []})], True) + expected_with_numcase = (450, "750px") + expected_no_numcase = (450, "670px") + expected_one_sample = (250, "540px") + self.assertEqual(results_with_numcase, expected_with_numcase) + self.assertEqual(result_no_numcase, expected_no_numcase) + self.assertEqual(results_one_sample, + expected_one_sample) + + def test_get_categorical_variables_no_sample_attributes(self): + """test for getting categorical variable names with no samples""" + trait = TraitObject({}) + sample_list = TraitObject({"se_exists": True, "attributes": []}) + self.assertEqual(get_categorical_variables(trait, sample_list), []) + + def test_get_categorical_variables_with_sample_attributes(self): + """test for getting categorical variable names with no samples""" + this_trait = TraitObject({"data": { + "Gene1": TraitObject({"extra_attributes": {"ex1": "ex1value"}}), + "Gene2": TraitObject({"extra_attributes": {"ex2": "ex2value"}}), + "Gene3": TraitObject({"extra_attributes": {"ex3": "ex3value"}}) + }}) + sample_list = TraitObject({"attributes": { + "sample_attribute_1": TraitObject({"name": "ex1"}), + "sample_attribute_2": TraitObject({"name": "ex2"}), + "sample_attribute_3": TraitObject({"name": "ex3"}), + "sample_attribute_4": TraitObject({"name": "not_in_extra_attributes"}) + }}) + results = get_categorical_variables(this_trait, sample_list) + self.assertEqual( + ["ex1", "ex2", "ex3", "not_in_extra_attributes"], results) + + def test_get_trait_units(self): + """test for getting trait units""" + trait = TraitObject( + {"description_fmt": "[this is a description] another test [N/A]"}) + trait_no_unit_type = TraitObject({"description_fmt": ""}) + results = get_trait_units(trait) + results_no_unit = get_trait_units(trait_no_unit_type) + self.assertEqual(results, "this is a descriptionN/A") + self.assertEqual(results_no_unit, "Value") + + @mock.patch("wqflask.show_trait.show_trait.g") + def test_get_nearest_marker(self, mock_db): + """test for getting nearest marker with non-empty db""" + + mock_db.db.execute.return_value.fetchall.return_value = [ + ["Geno1", "Geno2"], ["Geno3"]] + + trait = TraitObject({"locus_chr": "test_chr", "locus_mb": "test_mb"}) + group_name = TraitObject({"name": "group_name"}) + this_db = TraitObject({"group": group_name}) + results_with_item_db = get_nearest_marker(trait, this_db) + called_with_value = """SELECT Geno.Name + FROM Geno, GenoXRef, GenoFreeze + WHERE Geno.Chr = 'test_chr' AND + GenoXRef.GenoId = Geno.Id AND + GenoFreeze.Id = GenoXRef.GenoFreezeId AND + GenoFreeze.Name = 'group_nameGeno' + ORDER BY ABS( Geno.Mb - test_mb) LIMIT 1""" + + mock_db.db.execute.assert_called_with(called_with_value) + + self.assertEqual(results_with_item_db, "Geno1") + + @mock.patch("wqflask.show_trait.show_trait.g") + def test_get_nearest_marker_empty_db(self, mock_db): + """test for getting nearest marker with empty db""" + mock_db.db.execute.return_value.fetchall.return_value = [] + trait = TraitObject({"locus_chr": "test_chr", "locus_mb": "test_mb"}) + group_name = TraitObject({"name": "group_name"}) + this_db = TraitObject({"group": group_name}) + results_empty_db = get_nearest_marker(trait, this_db) + mock_db.db.execute.assert_called_once() + self.assertEqual(results_empty_db, "") + + @mock.patch("wqflask.show_trait.show_trait.get_scales_from_genofile") + def test_get_genotype_scales_with_genofile_is_list(self, mock_get_scales): + """test for getting genotype scales with genofile as list """ + # where genofile is instance of list + genofiles_list = [{"filename": "file1", "location": "~/data/files/f1"}, + {"filename": "file2", "location": "~/data/files/f2"}, + {"filename": "file3", "location": "~/data/files/f3"}] + + mock_get_scales.side_effect = [[["morgan", "cM"]], + [["morgan", "cM"]], + [["physic", "Mb"]]] + + results = get_genotype_scales(genofiles_list) + expected_results = { + "~/data/files/f1": [["morgan", "cM"]], + "~/data/files/f2": [["morgan", "cM"]], + "~/data/files/f3": [["physic", "Mb"]] + } + + multiple_calls = [mock.call('~/data/files/f1'), mock.call('~/data/files/f2'), + mock.call('~/data/files/f3')] + mock_get_scales.assert_has_calls(multiple_calls) + self.assertEqual(results, expected_results) + + @mock.patch("wqflask.show_trait.show_trait.get_scales_from_genofile") + def test_genotype_scales_with_genofile_other(self, mock_get_scales): + """test for getting genotype scales with genofile as a string""" + file_location = "~/another_file_location" + mock_get_scales.return_value = [["physic", "Mb"]] + expected_results = {f"{file_location}": [["physic", "Mb"]]} + self.assertEqual(get_genotype_scales(file_location), expected_results) + mock_get_scales.assert_called_once_with(file_location) + + + @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") + def test_get_scales_from_genofile_found(self, mock_ignore_location): + """"add test for get scales from genofile where file is found""" + mock_ignore_location.return_value = True + geno_file = """ + #sample line with no @scales:other\n + #sample line @scales and :separated by semicolon\n + This attempts to check whether\n + """ + + geno_file_string = "@line start with @ and has @scale:morgan" + + file_location = "~/data/file.geno" + + mock_open_geno_file = mock.mock_open(read_data=geno_file) + with mock.patch("builtins.open", mock_open_geno_file): + results = get_scales_from_genofile(file_location) + self.assertEqual(results, [["morgan", "cM"]]) + + mock_open_string = mock.mock_open(read_data=geno_file_string) + + with mock.patch("builtins.open", mock_open_string): + result2 = get_scales_from_genofile(file_location) + self.assertEqual([['morgan', 'cM']], result2) + + @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") + def test_get_scales_from_genofile_not_found(self, mock_location_ignore): + mock_location_ignore.return_value = False + + expected_results = [["physic", "Mb"]] + results = get_scales_from_genofile("~/data/file") + mock_location_ignore.assert_called_once_with("~/data/file", "genotype") + self.assertEqual(results, expected_results) |