source: trunk/brs2010p35/src/jag/test/test_utilities.py @ 2

Last change on this file since 2 was 2, checked in by tim.te.beek@…, 8 years ago

Add revision 338 snapshot of previous SVN instance, omitting PLINK binaries

File size: 10.2 KB
Line 
1'''
2Test cases for JAG project relating to small utility functions.
3'''
4
5import jag.common as common
6import jag.drawrandom as drawrandom
7import jag.file_fetch_and_write as in_out
8import jag.permutation as permutation
9import jag.permutationresults
10import jag.mergepermutations
11import os
12import unittest
13import jag.plink
14
15
16class TestUtilities(unittest.TestCase):
17    '''
18    Test cases for JAG project relating to small utility functions.
19   
20    Extending unittest.TestCase to take advantage of the self.assertXYZ methods.
21    '''
22    def  test_point_outputfile_to_phenotype(self):
23        """
24        """
25     
26        plink = jag.plink.Plink()
27        plink.phenotypes_present=["1"]
28        actual=plink.point_outputfile_to_phenotype( [1,2,3,4,5], "resultfile", "ext")
29        print(actual)
30        result={ 1: ["resultfile.P1.ext","resultfile.P2.ext","resultfile.P3.ext","resultfile.P4.ext","resultfile.P5.ext"]}
31        self.assertEqual(result,actual," mapping phenotypes to right plink output with one phenotype")
32       
33        plink.phenotypes_present=["1","2"]
34        actual=plink.point_outputfile_to_phenotype( [1,2,3,4,5], "resultfile", "ext")
35        print()
36        result={1: ['resultfile.P1.ext', 'resultfile.P3.ext', 'resultfile.P5.ext', 'resultfile.P7.ext', 'resultfile.P9.ext'],
37                2: ['resultfile.P2.ext', 'resultfile.P4.ext', 'resultfile.P6.ext', 'resultfile.P8.ext', 'resultfile.P10.ext']}
38
39        self.assertEqual(result,actual," mapping phenotypes to right plink output with two phenotypes")
40       
41        plink.phenotypes_present=["1","2","3","4"]
42        actual=plink.point_outputfile_to_phenotype( [1,2], "resultfile", "ext")
43        result={1: ['resultfile.P1.ext', 'resultfile.P5.ext'], 2: ['resultfile.P2.ext', 'resultfile.P6.ext'], 
44         3: ['resultfile.P3.ext', 'resultfile.P7.ext'], 4: ['resultfile.P4.ext', 'resultfile.P8.ext']}
45        print(str(actual))
46        self.assertEqual(result,actual," mapping phenotypes to right plink output with four phenotypes")
47    def  test_get__get_ordered_files_from_list(self):
48        """
49        testing method to create form a list of files
50        a dict with as key phenotype and a list of filenames corresponding to phenotype
51        """
52        filenames = ['/home/maartenk/workspace/jag_3/src/jag/perm.79DLfuGi.P1.out', \
53                      '/home/maartenk/workspace/jag_3/src/jag/perm.aUfZ4QuT.P1.out']
54        ordered_results = (jag.mergepermutations._get_ordered_files_from_list(filenames))
55        self.assertEqual(2, len(ordered_results["P1"]))
56        filenames.append("/home/maartenk/workspace/jag_3/src/jag/perm.79DLfuGi.P2.out")
57        ordered_results = (jag.mergepermutations._get_ordered_files_from_list(filenames))
58
59        self.assertEqual(2, len(ordered_results["P1"]))
60        self.assertEqual(1, len(ordered_results["P2"]))
61        filenames.append("/home/maartenk/workspace/jag_3/src/jag/perm.merged.P2.out")
62
63        ordered_results = (jag.mergepermutations._get_ordered_files_from_list(filenames))
64        self.assertEqual(2, len(ordered_results["P1"]))
65        self.assertEqual(1, len(ordered_results["P2"]))
66
67    def test_drawRandom(self):
68
69        draw = drawrandom.DrawRandom("", "", "")
70        file_as_list = ["rs1\t4\n", "rs2\t5\n", "rs3\t6\n"]
71        draw.accessed = set()
72        snp = drawrandom.Snp(draw)
73        snp.exclude_snps = ['rs2', 'rs3']
74        results = 'rs1', '4'
75        actual = snp._get_random_snp(file_as_list, 2)
76        self.assertEqual(results, actual, 'got wrong random value: {exp} vs {act}'.format(exp=results, act=actual))
77
78    def test_create_seed_for_jobs(self):
79        """
80        testing createSeedsForJobs
81        """
82        expected = [[0.45237955350981862, 0.55977238608049595, 0.92421058402372935], \
83                     [0.46565007009977333, 0.50784127306227111]]
84        actual = jag.permutation._create_seeds_for_jobs(5, 2, 11)
85        self.assertEqual(expected, actual, 'Created expected should match precalculated expected')
86
87        expected = [[0.45237955350981862]]
88        actual = jag.permutation._create_seeds_for_jobs(1, 2, 11)
89        self.assertEqual(expected, actual, 'Created expected should match precalculated expected')
90
91    def test_variance(self):
92        """
93        testing variance : function should behave in the same way
94         as R-project's var()
95        """
96        not_a_list = 5
97        self.failUnlessRaises(AssertionError, jag.common.variance, not_a_list)
98
99        self.assertEqual("NA", jag.common.variance([10]), \
100                    'Variance for a single element list not available, but do not raise Error')
101        self.assertEqual(7.5, jag.common.variance(range(1, 10)), \
102                     'Variance for numbers in the range 1 through 10 is 7.5')
103
104    def test_average(self):
105        self.assertEqual(50.5, jag.common.average(range(1, 101)),
106                        'Average of numbers in the range 1 through 101 is 50.5')\
107
108    def test_calculate_emp_p_value(self):
109        """
110        testing calculate_emperical_p_value
111        """
112        actual = jag.permutationresults.calculate_emperical_p_value(0, range(1, 101))
113        self.assertEqual(1, actual)
114        actual = jag.permutationresults.calculate_emperical_p_value(100, range(1, 101))
115        self.assertEqual(0, actual)
116        actual = jag.permutationresults.calculate_emperical_p_value(50, range(1, 101))
117        self.assertEqual(0.5, actual)
118        actual = jag.permutationresults.calculate_emperical_p_value(100, range(1, 101))
119        self.assertEqual(0, actual)
120        actual = jag.permutationresults.calculate_emperical_p_value(50, [90, 40, 0, 30, 79])
121        self.assertAlmostEqual(0.4, actual)
122
123        actual = jag.permutationresults.calculate_emperical_p_value(479.848687484, \
124                                         [ 481.56952502556868, 456.98322586618065])
125        self.assertEqual(0.5, actual)
126
127    def test_negsumlog10(self):
128        """test for negsumlog10"""
129        actual = jag.common.negsumlog10([0.1, 0.2, 0.3, 0.9])
130        self.assertAlmostEqual(2.2676062, actual)
131
132    def test_effective_snps(self):
133        """
134        # in R: trunc(snp*snp*0.189/var(1:99))
135        """
136        actual = jag.permutationresults.calculate_effective_snps(100, range(1, 100))
137        self.assertEqual(2, actual)
138        actual = jag.permutationresults.calculate_effective_snps(1000, range(1, 100))
139        self.assertEqual(229, actual)
140        actual = jag.permutationresults.calculate_effective_snps(1000, [1])
141        self.assertEqual("NA", actual)
142
143    def test_merge_list_of_dict_of_list(self):
144        l_of_d_of_l = [{"a":[1, 2, 3, 4, 5], "b":["as", "ad", "ac"]},
145                       {"a":[1, 2, 3, 4, 5], "b":[1, 2, 3, 4, 5]}]
146        actual = permutation.merge_list_of_dict_of_list(l_of_d_of_l)
147        result = {'a': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], 'b': ['as', 'ad', 'ac', 1, 2, 3, 4, 5]}
148
149        self.assertEqual(actual, result)
150
151    def test_get_assoc_file_number(self):
152        '''test for returning filenumber'''
153
154        actual = in_out.get_assoc_file_number('/tmp/tmpJ0oHM_', '/tmp/tmpJ0oHM_.P2.assoc')
155        self.assertEqual(actual, "2")
156        actual = in_out.get_assoc_file_number('/tmp/tmprBcql7', '/tmp/tmprBcql7.assoc')
157        self.assertEqual(actual, 1)
158        resultfile = "/var/folders/qU/qUoS+hLAEzaaKIN8iYK6Ik+++TQ/-Tmp-/tmpOe0tka"
159        assoc_file = "/var/folders/qU/qUoS+hLAEzaaKIN8iYK6Ik+++TQ/-Tmp-/tmpOe0tka.assoc"
160        actual = in_out.get_assoc_file_number(resultfile, assoc_file)
161
162        self.assertEqual(actual, 1)
163        resultfile = "/tmp/tmpGxqBDu"
164        assoc_file = "/tmp/tmpGxqBDu.assoc.logistic"
165        actual = in_out.get_assoc_file_number(resultfile, assoc_file)
166        self.assertEqual(actual, 1)
167
168    def test_get_number_of_assoc_file(self):
169        '''
170        get the number which corresponds to phenotype number from the output files
171        of plink in the association analysis
172       
173        '''
174        actual = in_out.get_number_of_assoc_file("/tmp/tmphGdhM9.assoc")
175        self.assertEqual(actual, ".P1")
176
177    def test_plink_results_extention(self):
178        testfiles = ["/tmp/rfftysP1.assoc", "/tmp/rfftysP2.assoc"]
179        actual = common._plink_results_extention(testfiles)
180        self.assertEqual("assoc", actual
181                         )
182        testfiles = ["/tmp/rfftysP1.qassoc", "/tmp/rfftysP2.qassoc"]
183        actual = common._plink_results_extention(testfiles)
184        self.assertEqual("qassoc", actual)
185
186        testfiles = ["/tmp/rfftys.qassoc"]
187        actual = common._plink_results_extention(testfiles)
188        self.assertEqual("qassoc", actual)
189
190        testfiles = ["/tmp/rfftys.qassoc.linear"]
191        actual = common._plink_results_extention(testfiles)
192        self.assertEqual("qassoc.linear", actual)
193
194    def test_get_association_analysis_filename(self):
195        """
196        create files and check it returns the right files
197        """
198        testfiles = ["test.txt", "sumlog.out"]
199        currentdir = os.path.abspath(os.path.curdir) + "/"
200        for filename in testfiles:
201            pathfile = currentdir + filename
202
203            if not os.path.exists(pathfile):
204                open(pathfile, 'w').close()
205        in_and_out = in_out.InAndOut()
206        actual = in_and_out.get_sumlog_filenames()
207
208        self.assertEqual([currentdir + "sumlog.out"], (actual))
209        self.assertEqual(1, len(actual))
210
211        for filename in testfiles:
212            pathfile = currentdir + filename
213            if os.path.exists(pathfile):
214                os.remove(pathfile)
215
216        testfiles = ["test.txt", "sumlog.p1.out"]
217        for filename in testfiles:
218            if not os.path.exists(currentdir + filename):
219                open(currentdir + filename, 'w').close()
220
221        actual = in_and_out.get_sumlog_filenames()
222        self.assertEqual([currentdir + "sumlog.p1.out"], actual)
223        self.assertEqual(1, len(actual))
224
225        for filename in testfiles:
226            if os.path.exists(currentdir + filename):
227                os.remove(currentdir + filename)
228       
229    def test_check_and_create_output_dir(self):
230        uncreatable = "/usr/NOT_MADE_WITHOUT_SUDO"
231        actual = in_out._create_output_dir(uncreatable)
232        self.assertEqual(False, actual)
233       
234        actual = in_out._create_output_dir("/usr")
235        self.assertEqual(True, actual)
236       
237        actual = in_out._check_writability("/usr")
238        self.assertEqual(False, actual)
239
240       
Note: See TracBrowser for help on using the repository browser.