2
# File created on 19 May 2011
4
from __future__ import division
6
__author__ = "William Van Treuren"
7
__copyright__ = "Copyright 2011, The QIIME project"
8
__credits__ = ["William Van Treuren", "Greg Caporaso", "Jai Ram Rideout"]
11
__maintainer__ = "William Van Treuren"
12
__email__ = "vantreur@colorado.edu"
13
__status__ = "Development"
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)
26
class TopLevelTests(TestCase):
27
"""Tests of top level functions"""
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']
40
self.rarefaction_data = parse_rarefaction(self.rarefaction_file)
43
['#SampleID\tTreatment\tLinker'+\
44
'PrimerSequence\tDose\tTTD\tDescription',
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'
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 = \
61
{'High': ['123'], 'Low': ['234', '456'], 'Med': ['345']}
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']),
70
[(['234', '456'], ['345']), (['234', '456'], ['123']),
73
self.Id_pairs_Treatment = \
74
[(['234'], ['345', '123', '456'])]
76
self.rarefaction_cols_dict = \
77
{'123': 0, '234': 1, '345': 2, '456':3}
79
self.extracted_mtx_10 = \
80
array([[ 1.99181, 5.42877, 2.13996, 0.002322],
81
[ 2.07163, 1.42877, 2.37055, 0.01219]])
83
self.extracted_mtx_310 = \
84
array([[ 8.83115, 6.42877, 11.00725, 0.18233],
85
[ 10.05242, 9.42877, 8.24474, 0.99229]])
87
self.extracted_mtx_910 = \
88
array([[ 12.9862 , 2.42877, 11.58642, 1.22563]])
91
(['234'], ['345', '123'])
93
self.rarefaction_mtx_for_sample_pair1_0 = \
97
self.rarefaction_mtx_for_sample_pair1_1 = \
98
array([[ 2.13996, 1.99181],
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)}}
106
def test_make_value_pairs_from_category(self):
107
"""check value pairs returns correct unique pairs for categories
111
make_value_pairs_from_category(self.mapping_data, 'Dose'),
112
self.value_pairs_Dose)
115
make_value_pairs_from_category(self.mapping_data, 'TTD'),
116
self.value_pairs_TTD)
119
make_value_pairs_from_category(self.mapping_data, 'Treatment'),
120
self.value_pairs_Treatment)
124
make_value_pairs_from_category, self.mapping_data, 'WrongCat')
126
def test_make_category_values_Id_dict(self):
127
"""check value pairs reference correct Id pairs"""
130
make_category_values_Id_dict(self.mapping_data, 'Dose'),
134
make_category_values_Id_dict(self.mapping_data, 'TTD'),
138
make_category_values_Id_dict(self.mapping_data, 'Treatment'),
139
self.cat_val_Treatment)
141
def test_map_category_value_pairs_to_Ids(self):
142
"""check value pairs converted to correct Ids"""
145
map_category_value_pairs_to_Ids(self.value_pairs_Dose,
150
map_category_value_pairs_to_Ids(self.value_pairs_TTD,
155
map_category_value_pairs_to_Ids(self.value_pairs_Treatment,
156
self.cat_val_Treatment),
157
self.Id_pairs_Treatment)
159
def test_make_SampleIds_rarefaction_columns_dict(self):
163
make_SampleIds_rarefaction_columns_dict(self.rarefaction_data),
164
self.rarefaction_cols_dict)
166
def test_extract_rarefaction_scores_at_depth(self):
167
"""check correct errors raised for wrong depths and correct mtx
170
self.assertFloatEqual(
171
extract_rarefaction_scores_at_depth(10,self.rarefaction_data),
172
self.extracted_mtx_10)
174
self.assertFloatEqual(
175
extract_rarefaction_scores_at_depth(310,self.rarefaction_data),
176
self.extracted_mtx_310)
178
self.assertFloatEqual(
179
extract_rarefaction_scores_at_depth(910,self.rarefaction_data),
180
self.extracted_mtx_910)
184
extract_rarefaction_scores_at_depth,810,self.rarefaction_data)
188
extract_rarefaction_scores_at_depth,100,self.rarefaction_data)
190
def test_convert_SampleIds_to_rarefaction_mtx(self):
191
"""check correct reduced rarefaction scores mtx produced"""
193
self.rarefaction_mtx_for_sample_pair1_0 = \
197
self.rarefaction_mtx_for_sample_pair1_1 = \
198
array([[ 2.13996, 1.99181],
199
[ 2.37055, 2.07163]])
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)
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)
215
def test_compare_alpha_diversities_parametric(self):
216
"""test main function properly compares alpha divs (parametric)"""
218
self.assertFloatEqual(
219
compare_alpha_diversities(self.rarefaction_file,
220
self.mapping_file, 'TTD', 10,
222
self.compared_alpha_diversities_TTD)
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,
229
self.compared_alpha_diversities_TTD)
231
def test_compare_alpha_diversities_nonparametric(self):
232
"""test main function properly compares alpha divs (nonparametric)"""
234
obs = compare_alpha_diversities(self.rarefaction_file,
235
self.mapping_file, 'TTD', 10,
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))
245
def test_compare_alpha_diversities_invalid_input(self):
246
"""test main function bails on bad input"""
249
self.assertRaises(ValueError, compare_alpha_diversities,
250
self.rarefaction_file, self.mapping_file, 'TTD', 10,
253
# Invalid num_permutations.
254
self.assertRaises(ValueError, compare_alpha_diversities,
255
self.rarefaction_file, self.mapping_file, 'TTD', 10,
259
if __name__ == "__main__":