~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to tests/test_compare_alpha_diversities.py

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# File created on 19 May 2011
3
 
 
4
 
from __future__ import division
5
 
 
6
 
__author__ = "William Van Treuren"
7
 
__copyright__ = "Copyright 2011, The QIIME project"
8
 
__credits__ = ["William Van Treuren", "Greg Caporaso", "Jai Ram Rideout"]
9
 
__license__ = "GPL"
10
 
__version__ = "1.5.0"
11
 
__maintainer__ = "William Van Treuren"
12
 
__email__ = "vantreur@colorado.edu"
13
 
__status__ = "Development"
14
 
 
15
 
from cogent.util.unit_test import TestCase,main
16
 
from qiime.parse import parse_mapping_file_to_dict, parse_rarefaction
17
 
from numpy import array
18
 
from qiime.compare_alpha_diversity import (compare_alpha_diversities,
19
 
    extract_rarefaction_scores_at_depth,
20
 
    make_SampleIds_rarefaction_columns_dict,
21
 
    make_category_values_Id_dict,
22
 
    make_value_pairs_from_category,
23
 
    map_category_value_pairs_to_Ids,
24
 
    convert_SampleIds_to_rarefaction_mtx)
25
 
 
26
 
class TopLevelTests(TestCase):
27
 
    """Tests of top level functions"""
28
 
    
29
 
    def setUp(self):
30
 
        """define data for tests"""
31
 
        self.rarefaction_file = \
32
 
         ['\tsequences per sample\titeration\t123\t234\t345\t456',
33
 
          'rare10.txt\t10\t0\t1.99181\t5.42877\t2.13996\t0.002322',
34
 
          'rare10.txt\t10\t1\t2.07163\t1.42877\t2.37055\t0.01219',
35
 
          'rare310.txt\t310\t0\t8.83115\t6.42877\t11.00725\t0.18233',
36
 
          'rare310.txt\t310\t1\t10.05242\t9.42877\t8.24474\t0.99229',
37
 
          'rare810.txt\t810\t0\t12.03067\tn/a\t11.58928\t0.8993',
38
 
          'rare910.txt\t910\t1\t12.9862\t2.42877\t11.58642\t1.22563']
39
 
        
40
 
        self.rarefaction_data = parse_rarefaction(self.rarefaction_file)
41
 
        
42
 
        self.mapping_file = \
43
 
        ['#SampleID\tTreatment\tLinker'+\
44
 
         'PrimerSequence\tDose\tTTD\tDescription',
45
 
         '#Comment Line',
46
 
         '123\tAAAA\tBBBB\tHigh\t31\tM_ID_123',
47
 
         '234\tCCCC\tDDDD\tLow\t67\tM_ID_234',
48
 
         '345\tAAAA\tFFFF\tMed\t21\tM_ID_345',
49
 
         '456\tAAAA\tGGGG\tLow\t67\tM_ID_456'
50
 
         ]
51
 
        
52
 
        self.mapping_data = \
53
 
         parse_mapping_file_to_dict(self.mapping_file)[0]
54
 
        self.value_pairs_Dose = \
55
 
         [('Low','Med'),('Low','High'),('Med','High')]                         
56
 
        self.value_pairs_TTD = \
57
 
         [('67', '21'), ('67', '31'), ('21', '31')]
58
 
        self.value_pairs_Treatment = \
59
 
         [('CCCC', 'AAAA')]
60
 
        self.cat_val_Dose = \
61
 
         {'High': ['123'], 'Low': ['234', '456'], 'Med': ['345']}
62
 
        self.cat_val_TTD = \
63
 
         {'21': ['345'], '31': ['123'], '67': ['234', '456']}
64
 
        self.cat_val_Treatment = \
65
 
         {'AAAA': ['345', '123', '456'], 'CCCC': ['234']}
66
 
        self.Id_pairs_Dose = \
67
 
         [(['234', '456'], ['345']), (['234', '456'], ['123']),
68
 
          (['345'], ['123'])]
69
 
        self.Id_pairs_TTD = \
70
 
         [(['234', '456'], ['345']), (['234', '456'], ['123']),
71
 
          (['345'], ['123'])]
72
 
        
73
 
        self.Id_pairs_Treatment = \
74
 
         [(['234'], ['345', '123', '456'])]
75
 
        
76
 
        self.rarefaction_cols_dict = \
77
 
         {'123': 0, '234': 1, '345': 2, '456':3}
78
 
       
79
 
        self.extracted_mtx_10 = \
80
 
         array([[ 1.99181,  5.42877,  2.13996, 0.002322],
81
 
               [ 2.07163,  1.42877,  2.37055, 0.01219]])
82
 
        
83
 
        self.extracted_mtx_310 = \
84
 
         array([[  8.83115,   6.42877,  11.00725, 0.18233],
85
 
               [ 10.05242,   9.42877,   8.24474, 0.99229]])
86
 
        
87
 
        self.extracted_mtx_910 = \
88
 
         array([[ 12.9862 ,   2.42877,  11.58642, 1.22563]])
89
 
        
90
 
        self.sample_pair1 = \
91
 
         (['234'], ['345', '123'])
92
 
            
93
 
        self.rarefaction_mtx_for_sample_pair1_0 = \
94
 
         array([[ 5.42877],
95
 
               [ 1.42877]])
96
 
        
97
 
        self.rarefaction_mtx_for_sample_pair1_1 = \
98
 
         array([[ 2.13996,  1.99181],
99
 
               [ 2.37055,  2.07163]])
100
 
        
101
 
        self.compared_alpha_diversities_TTD = {'TTD': {('67', '21'):
102
 
            (-0.27929839680103463, 0.79386220041241184), ('21', '31'):
103
 
            (1.8321466933860993, 0.20839398129924847), ('67', '31'):
104
 
            (-0.16318504125427058, 0.87828549279958279)}}
105
 
 
106
 
    def test_make_value_pairs_from_category(self):
107
 
        """check value pairs returns correct unique pairs for categories
108
 
        """
109
 
 
110
 
        self.assertEqual(
111
 
         make_value_pairs_from_category(self.mapping_data, 'Dose'),
112
 
         self.value_pairs_Dose)
113
 
        
114
 
        self.assertEqual(
115
 
            make_value_pairs_from_category(self.mapping_data, 'TTD'),
116
 
            self.value_pairs_TTD)
117
 
        
118
 
        self.assertEqual(
119
 
            make_value_pairs_from_category(self.mapping_data, 'Treatment'),
120
 
            self.value_pairs_Treatment)
121
 
        
122
 
        self.assertRaises(
123
 
         ValueError,
124
 
         make_value_pairs_from_category, self.mapping_data, 'WrongCat')
125
 
                         
126
 
    def test_make_category_values_Id_dict(self):
127
 
        """check value pairs reference correct Id pairs"""
128
 
             
129
 
        self.assertEqual(
130
 
            make_category_values_Id_dict(self.mapping_data, 'Dose'),
131
 
            self.cat_val_Dose)
132
 
        
133
 
        self.assertEqual(
134
 
            make_category_values_Id_dict(self.mapping_data, 'TTD'),
135
 
            self.cat_val_TTD)
136
 
        
137
 
        self.assertEqual(
138
 
            make_category_values_Id_dict(self.mapping_data, 'Treatment'),
139
 
            self.cat_val_Treatment)
140
 
    
141
 
    def test_map_category_value_pairs_to_Ids(self):
142
 
        """check value pairs converted to correct Ids"""
143
 
                
144
 
        self.assertEqual(
145
 
            map_category_value_pairs_to_Ids(self.value_pairs_Dose,
146
 
                                            self.cat_val_Dose),
147
 
            self.Id_pairs_Dose)
148
 
 
149
 
        self.assertEqual(
150
 
            map_category_value_pairs_to_Ids(self.value_pairs_TTD,
151
 
                                            self.cat_val_TTD),
152
 
            self.Id_pairs_TTD)
153
 
        
154
 
        self.assertEqual(
155
 
            map_category_value_pairs_to_Ids(self.value_pairs_Treatment,
156
 
                                            self.cat_val_Treatment),
157
 
            self.Id_pairs_Treatment)
158
 
    
159
 
    def test_make_SampleIds_rarefaction_columns_dict(self):
160
 
        """ """
161
 
                
162
 
        self.assertEqual(
163
 
         make_SampleIds_rarefaction_columns_dict(self.rarefaction_data),
164
 
         self.rarefaction_cols_dict)
165
 
        
166
 
    def test_extract_rarefaction_scores_at_depth(self):
167
 
        """check correct errors raised for wrong depths and correct mtx
168
 
        """
169
 
           
170
 
        self.assertFloatEqual(
171
 
         extract_rarefaction_scores_at_depth(10,self.rarefaction_data),
172
 
         self.extracted_mtx_10)
173
 
        
174
 
        self.assertFloatEqual(
175
 
         extract_rarefaction_scores_at_depth(310,self.rarefaction_data),
176
 
         self.extracted_mtx_310)
177
 
        
178
 
        self.assertFloatEqual(
179
 
         extract_rarefaction_scores_at_depth(910,self.rarefaction_data),
180
 
         self.extracted_mtx_910)
181
 
        
182
 
        self.assertRaises(
183
 
         ValueError,
184
 
         extract_rarefaction_scores_at_depth,810,self.rarefaction_data)
185
 
        
186
 
        self.assertRaises(
187
 
         ValueError,
188
 
         extract_rarefaction_scores_at_depth,100,self.rarefaction_data)
189
 
        
190
 
    def test_convert_SampleIds_to_rarefaction_mtx(self):
191
 
        """check correct reduced rarefaction scores mtx produced"""
192
 
                 
193
 
        self.rarefaction_mtx_for_sample_pair1_0 = \
194
 
         array([[ 5.42877],
195
 
               [ 1.42877]])
196
 
        
197
 
        self.rarefaction_mtx_for_sample_pair1_1 = \
198
 
         array([[ 2.13996,  1.99181],
199
 
               [ 2.37055,  2.07163]])
200
 
        
201
 
        self.assertEqual(
202
 
            convert_SampleIds_to_rarefaction_mtx(
203
 
                self.sample_pair1[0],
204
 
                self.extracted_mtx_10,
205
 
                self.rarefaction_cols_dict),
206
 
            self.rarefaction_mtx_for_sample_pair1_0)
207
 
        
208
 
        self.assertEqual(
209
 
            convert_SampleIds_to_rarefaction_mtx(
210
 
                self.sample_pair1[1],
211
 
                self.extracted_mtx_10,
212
 
                self.rarefaction_cols_dict),
213
 
            self.rarefaction_mtx_for_sample_pair1_1)
214
 
        
215
 
    def test_compare_alpha_diversities_parametric(self):
216
 
        """test main function properly compares alpha divs (parametric)"""
217
 
                
218
 
        self.assertFloatEqual(
219
 
            compare_alpha_diversities(self.rarefaction_file,
220
 
                                      self.mapping_file, 'TTD', 10,
221
 
                                      'parametric'),
222
 
            self.compared_alpha_diversities_TTD)
223
 
 
224
 
        # Should ignore num_permutations if test_type is parametric.
225
 
        self.assertFloatEqual(
226
 
            compare_alpha_diversities(self.rarefaction_file,
227
 
                                      self.mapping_file, 'TTD', 10,
228
 
                                      'parametric', 0),
229
 
            self.compared_alpha_diversities_TTD)
230
 
 
231
 
    def test_compare_alpha_diversities_nonparametric(self):
232
 
        """test main function properly compares alpha divs (nonparametric)"""
233
 
 
234
 
        obs = compare_alpha_diversities(self.rarefaction_file,
235
 
                                        self.mapping_file, 'TTD', 10,
236
 
                                        'nonparametric')
237
 
 
238
 
        # Since p-values are stochastic, we'll check that they are sane and
239
 
        # that the t statistics are the same as we'd get for a parametric test.
240
 
        for comp, (t, p_val) in obs['TTD'].items():
241
 
            exp = self.compared_alpha_diversities_TTD['TTD'][comp]
242
 
            self.assertFloatEqual(t, exp[0])
243
 
            self.assertIsProb(float(p_val))
244
 
 
245
 
    def test_compare_alpha_diversities_invalid_input(self):
246
 
        """test main function bails on bad input"""
247
 
 
248
 
        # Invalid test_type.
249
 
        self.assertRaises(ValueError, compare_alpha_diversities,
250
 
                          self.rarefaction_file, self.mapping_file, 'TTD', 10,
251
 
                          'foo')
252
 
 
253
 
        # Invalid num_permutations.
254
 
        self.assertRaises(ValueError, compare_alpha_diversities,
255
 
                          self.rarefaction_file, self.mapping_file, 'TTD', 10,
256
 
                          'nonparametric', 0)
257
 
 
258
 
 
259
 
if __name__ == "__main__":
260
 
    main()