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

« back to all changes in this revision

Viewing changes to .pc/fix_t_test_bug/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
 
 
5
 
from __future__ import division
6
 
 
7
 
__author__ = "William Van Treuren"
8
 
__copyright__ = "Copyright 2011, The QIIME project"
9
 
__credits__ = ["William Van Treuren, Greg Caporaso"]
10
 
__license__ = "GPL"
11
 
__version__ = "1.5.0"
12
 
__maintainer__ = "William Van Treuren"
13
 
__email__ = "vantreur@colorado.edu"
14
 
__status__ = "Release"
15
 
 
16
 
 
17
 
 
18
 
from cogent.util.unit_test import TestCase,main
19
 
from qiime.parse import parse_mapping_file_to_dict, parse_rarefaction
20
 
from numpy import array
21
 
from qiime.compare_alpha_diversity import (compare_alpha_diversities,
22
 
    extract_rarefaction_scores_at_depth,
23
 
    make_SampleIds_rarefaction_columns_dict,
24
 
    make_category_values_Id_dict,
25
 
    make_value_pairs_from_category,
26
 
    map_category_value_pairs_to_Ids,
27
 
    convert_SampleIds_to_rarefaction_mtx)
28
 
 
29
 
class TopLevelTests(TestCase):
30
 
    """Tests of top level functions"""
31
 
    
32
 
    def setUp(self):
33
 
        """define data for tests"""
34
 
        self.rarefaction_file = \
35
 
         ['\tsequences per sample\titeration\t123\t234\t345',
36
 
          'rare10.txt\t10\t0\t1.99181\t5.42877\t2.13996',
37
 
          'rare10.txt\t10\t1\t2.07163\t1.42877\t2.37055',
38
 
          'rare310.txt\t310\t0\t8.83115\t6.42877\t11.00725',
39
 
          'rare310.txt\t310\t1\t10.05242\t9.42877\t8.24474',
40
 
          'rare810.txt\t810\t0\t12.03067\tn/a\t11.58928',
41
 
          'rare910.txt\t910\t1\t12.9862\t2.42877\t11.58642']
42
 
        
43
 
        self.rarefaction_data = parse_rarefaction(self.rarefaction_file)
44
 
        
45
 
        self.mapping_file = \
46
 
        ['#SampleID\tTreatment\tLinker'+\
47
 
         'PrimerSequence\tDose\tTTD\tDescription',
48
 
         '#Comment Line',
49
 
         '123\tAAAA\tBBBB\tHigh\t31\tM_ID_123',
50
 
         '234\tCCCC\tDDDD\tLow\t67\tM_ID_234',
51
 
         '345\tAAAA\tFFFF\tMed\t21\tM_ID_345']
52
 
        
53
 
        self.mapping_data = \
54
 
         parse_mapping_file_to_dict(self.mapping_file)[0]
55
 
        self.value_pairs_Dose = \
56
 
         [('Low','Med'),('Low','High'),('Med','High')]                         
57
 
        self.value_pairs_TTD = \
58
 
         [('67', '21'), ('67', '31'), ('21', '31')]
59
 
        self.value_pairs_Treatment = \
60
 
         [('CCCC', 'AAAA')]
61
 
        self.cat_val_Dose = \
62
 
         {'High': ['123'], 'Low': ['234'], 'Med': ['345']}
63
 
        self.cat_val_TTD = \
64
 
         {'21': ['345'], '31': ['123'], '67': ['234']}
65
 
        self.cat_val_Treatment = \
66
 
         {'AAAA': ['345', '123'], 'CCCC': ['234']}
67
 
        self.Id_pairs_Dose = \
68
 
         [(['234'], ['345']), (['234'], ['123']), (['345'], ['123'])]
69
 
        self.Id_pairs_TTD = \
70
 
         [(['234'], ['345']), (['234'], ['123']), (['345'], ['123'])]
71
 
        
72
 
        self.Id_pairs_Treatment = \
73
 
         [(['234'], ['345', '123'])]
74
 
        
75
 
        self.rarefaction_cols_dict = \
76
 
         {'123': 0, '234': 1, '345': 2}
77
 
       
78
 
        self.extracted_mtx_10 = \
79
 
         array([[ 1.99181,  5.42877,  2.13996],
80
 
               [ 2.07163,  1.42877,  2.37055]])
81
 
        
82
 
        self.extracted_mtx_310 = \
83
 
         array([[  8.83115,   6.42877,  11.00725],
84
 
               [ 10.05242,   9.42877,   8.24474]])
85
 
        
86
 
        self.extracted_mtx_910 = \
87
 
         array([[ 12.9862 ,   2.42877,  11.58642]])
88
 
        
89
 
        self.sample_pair1 = \
90
 
         (['234'], ['345', '123'])
91
 
            
92
 
        self.rarefaction_mtx_for_sample_pair1_0 = \
93
 
         array([[ 5.42877],
94
 
               [ 1.42877]])
95
 
        
96
 
        self.rarefaction_mtx_for_sample_pair1_1 = \
97
 
         array([[ 2.13996,  1.99181],
98
 
               [ 2.37055,  2.07163]])
99
 
        
100
 
        self.compared_alpha_diversities_TTD = \
101
 
         {'TTD':{('21','31'):(1.8321466933860993,0.20839398129924847),
102
 
         ('67', '21'): (0.58578495700890432, 0.61731739324369639),
103
 
         ('67', '31'): (0.69838596448703294, 0.55721515283248324)}}
104
 
        
105
 
    def test_make_value_pairs_from_category(self):
106
 
        """check value pairs returns correct unique pairs for categories
107
 
        """
108
 
        
109
 
          
110
 
        self.assertEqual(
111
 
         self.value_pairs_Dose,
112
 
         make_value_pairs_from_category(self.mapping_data, 'Dose'))
113
 
        
114
 
        self.assertEqual(
115
 
            self.value_pairs_TTD, 
116
 
            make_value_pairs_from_category(self.mapping_data, 'TTD'))
117
 
        
118
 
        self.assertEqual(
119
 
            self.value_pairs_Treatment, 
120
 
            make_value_pairs_from_category(self.mapping_data, 
121
 
                                            'Treatment'))
122
 
        
123
 
        self.assertRaises(
124
 
         ValueError,
125
 
         make_value_pairs_from_category, self.mapping_data, 'WrongCat')
126
 
                         
127
 
    def test_make_category_values_Id_dict(self):
128
 
        """check value pairs reference correct Id pairs"""
129
 
             
130
 
        self.assertEqual(
131
 
            self.cat_val_Dose,
132
 
            make_category_values_Id_dict(self.mapping_data, 'Dose'))
133
 
        
134
 
        self.assertEqual(
135
 
            self.cat_val_TTD,
136
 
            make_category_values_Id_dict(self.mapping_data, 'TTD'))
137
 
        
138
 
        self.assertEqual(
139
 
            self.cat_val_Treatment,
140
 
            make_category_values_Id_dict(self.mapping_data,
141
 
                                         'Treatment'))
142
 
    
143
 
    def test_map_category_value_pairs_to_Ids(self):
144
 
        """check value pairs converted to correct Ids"""
145
 
                
146
 
        self.assertEqual(
147
 
            self.Id_pairs_Dose,
148
 
            map_category_value_pairs_to_Ids(self.value_pairs_Dose,
149
 
                                            self.cat_val_Dose))
150
 
 
151
 
        self.assertEqual(
152
 
            self.Id_pairs_TTD,
153
 
            map_category_value_pairs_to_Ids(self.value_pairs_TTD,
154
 
                                            self.cat_val_TTD))
155
 
        
156
 
        self.assertEqual(
157
 
            self.Id_pairs_Treatment,
158
 
            map_category_value_pairs_to_Ids(
159
 
            self.value_pairs_Treatment,
160
 
            self.cat_val_Treatment))
161
 
    
162
 
    def test_make_SampleIds_rarefaction_columns_dict(self):
163
 
        """ """
164
 
                
165
 
        self.assertEqual(
166
 
         self.rarefaction_cols_dict,
167
 
         make_SampleIds_rarefaction_columns_dict(self.rarefaction_data))
168
 
        
169
 
    def test_extract_rarefaction_scores_at_depth(self):
170
 
        """check correct errors raised for wrong depths and correct mtx
171
 
        """
172
 
           
173
 
        self.assertEqual(
174
 
         self.extracted_mtx_10,
175
 
         extract_rarefaction_scores_at_depth(10,self.rarefaction_data))
176
 
        
177
 
        self.assertEqual(
178
 
         self.extracted_mtx_310,
179
 
         extract_rarefaction_scores_at_depth(310,self.rarefaction_data))
180
 
        
181
 
        self.assertEqual(
182
 
         self.extracted_mtx_910,
183
 
         extract_rarefaction_scores_at_depth(910,self.rarefaction_data))
184
 
        
185
 
        self.assertRaises(
186
 
         ValueError,
187
 
         extract_rarefaction_scores_at_depth,810,self.rarefaction_data)
188
 
        
189
 
        self.assertRaises(
190
 
         ValueError,
191
 
         extract_rarefaction_scores_at_depth,100,self.rarefaction_data)
192
 
        
193
 
    def test_convert_SampleIds_to_rarefaction_mtx(self):
194
 
        """check correct reduced rarefaction scores mtx produced"""
195
 
                 
196
 
        self.rarefaction_mtx_for_sample_pair1_0 = \
197
 
         array([[ 5.42877],
198
 
               [ 1.42877]])
199
 
        
200
 
        self.rarefaction_mtx_for_sample_pair1_1 = \
201
 
         array([[ 2.13996,  1.99181],
202
 
               [ 2.37055,  2.07163]])
203
 
        
204
 
        self.assertEqual(
205
 
            convert_SampleIds_to_rarefaction_mtx(
206
 
                self.sample_pair1[0],
207
 
                self.extracted_mtx_10,
208
 
                self.rarefaction_cols_dict),
209
 
            self.rarefaction_mtx_for_sample_pair1_0)
210
 
        
211
 
        self.assertEqual(
212
 
            convert_SampleIds_to_rarefaction_mtx(
213
 
                self.sample_pair1[1],
214
 
                self.extracted_mtx_10,
215
 
                self.rarefaction_cols_dict),
216
 
            self.rarefaction_mtx_for_sample_pair1_1)
217
 
        
218
 
    def test_compare_alpha_diversity(self):
219
 
        """test main function properly compares alpha diversities"""
220
 
                
221
 
        self.assertEqual(
222
 
            self.compared_alpha_diversities_TTD,
223
 
            compare_alpha_diversities(self.rarefaction_file,
224
 
                                      self.mapping_file, 'TTD', 10))
225
 
 
226
 
 
227
 
if __name__ == "__main__":
228
 
    main()