aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2020-11-18 12:47:30 +0300
committerGitHub2020-11-18 12:47:30 +0300
commit8e6a42bfedd465cc8cc9f4963c712377f176c12b (patch)
tree63cdabc4297e016f5635ac147f616bcb2d38c13a
parentcd01afec0bbe28bfcd1982cbf03c056a856b1d13 (diff)
parent61d5c7710d044c507d69e600e04a40067cf7320f (diff)
downloadgenenetwork2-8e6a42bfedd465cc8cc9f4963c712377f176c12b.tar.gz
Merge pull request #481 from Alexanderlacuna/test-SampleList
Test sample list
-rw-r--r--wqflask/tests/wqflask/show_trait/testSampleList.py16
-rw-r--r--wqflask/tests/wqflask/show_trait/test_show_trait.py36
-rw-r--r--wqflask/wqflask/show_trait/SampleList.py12
3 files changed, 60 insertions, 4 deletions
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
index 24150738..8c866874 100644
--- a/wqflask/tests/wqflask/show_trait/test_show_trait.py
+++ b/wqflask/tests/wqflask/show_trait/test_show_trait.py
@@ -11,6 +11,7 @@ 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:
@@ -240,3 +241,38 @@ class TestTraits(unittest.TestCase):
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)
diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py
index a535c493..00495377 100644
--- a/wqflask/wqflask/show_trait/SampleList.py
+++ b/wqflask/wqflask/show_trait/SampleList.py
@@ -8,6 +8,7 @@ from pprint import pformat as pf
from utility import Plot
from utility import Bunch
+
class SampleList(object):
def __init__(self,
dataset,
@@ -72,7 +73,8 @@ class SampleList(object):
self.sample_list.append(sample)
self.se_exists = any(sample.variance for sample in self.sample_list)
- self.num_cases_exists = any(sample.num_cases for sample in self.sample_list)
+ self.num_cases_exists = any(
+ sample.num_cases for sample in self.sample_list)
first_attr_col = self.get_first_attr_col()
for sample in self.sample_list:
@@ -115,7 +117,7 @@ class SampleList(object):
self.attributes[key].name = name
self.attributes[key].distinct_values = [
item.Value for item in values]
- natural_sort(self.attributes[key].distinct_values)
+ self.attributes[key].distinct_values=natural_sort(self.attributes[key].distinct_values)
all_numbers = True
for value in self.attributes[key].distinct_values:
try:
@@ -167,7 +169,8 @@ class SampleList(object):
return first_attr_col
-def natural_sort(list, key=lambda s: s):
+
+def natural_sort(a_list, key=lambda s: s):
"""
Sort the list into natural alphanumeric order.
"""
@@ -175,4 +178,5 @@ def natural_sort(list, key=lambda s: s):
def convert(text): return int(text) if text.isdigit() else text
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
sort_key = get_alphanum_key_func(key)
- list.sort(key=sort_key) \ No newline at end of file
+ sorted_list = sorted(a_list, key=sort_key)
+ return sorted_list