2
# File created on 19 May 2011
5
from __future__ import division
7
__author__ = "William Van Treuren"
8
__copyright__ = "Copyright 2011, The QIIME project"
9
__credits__ = ["William Van Treuren, Greg Caporaso"]
12
__maintainer__ = "William Van Treuren"
13
__email__ = "vantreur@colorado.edu"
14
__status__ = "Release"
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)
29
class TopLevelTests(TestCase):
30
"""Tests of top level functions"""
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']
43
self.rarefaction_data = parse_rarefaction(self.rarefaction_file)
46
['#SampleID\tTreatment\tLinker'+\
47
'PrimerSequence\tDose\tTTD\tDescription',
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']
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 = \
62
{'High': ['123'], 'Low': ['234'], 'Med': ['345']}
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'])]
70
[(['234'], ['345']), (['234'], ['123']), (['345'], ['123'])]
72
self.Id_pairs_Treatment = \
73
[(['234'], ['345', '123'])]
75
self.rarefaction_cols_dict = \
76
{'123': 0, '234': 1, '345': 2}
78
self.extracted_mtx_10 = \
79
array([[ 1.99181, 5.42877, 2.13996],
80
[ 2.07163, 1.42877, 2.37055]])
82
self.extracted_mtx_310 = \
83
array([[ 8.83115, 6.42877, 11.00725],
84
[ 10.05242, 9.42877, 8.24474]])
86
self.extracted_mtx_910 = \
87
array([[ 12.9862 , 2.42877, 11.58642]])
90
(['234'], ['345', '123'])
92
self.rarefaction_mtx_for_sample_pair1_0 = \
96
self.rarefaction_mtx_for_sample_pair1_1 = \
97
array([[ 2.13996, 1.99181],
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)}}
105
def test_make_value_pairs_from_category(self):
106
"""check value pairs returns correct unique pairs for categories
111
self.value_pairs_Dose,
112
make_value_pairs_from_category(self.mapping_data, 'Dose'))
115
self.value_pairs_TTD,
116
make_value_pairs_from_category(self.mapping_data, 'TTD'))
119
self.value_pairs_Treatment,
120
make_value_pairs_from_category(self.mapping_data,
125
make_value_pairs_from_category, self.mapping_data, 'WrongCat')
127
def test_make_category_values_Id_dict(self):
128
"""check value pairs reference correct Id pairs"""
132
make_category_values_Id_dict(self.mapping_data, 'Dose'))
136
make_category_values_Id_dict(self.mapping_data, 'TTD'))
139
self.cat_val_Treatment,
140
make_category_values_Id_dict(self.mapping_data,
143
def test_map_category_value_pairs_to_Ids(self):
144
"""check value pairs converted to correct Ids"""
148
map_category_value_pairs_to_Ids(self.value_pairs_Dose,
153
map_category_value_pairs_to_Ids(self.value_pairs_TTD,
157
self.Id_pairs_Treatment,
158
map_category_value_pairs_to_Ids(
159
self.value_pairs_Treatment,
160
self.cat_val_Treatment))
162
def test_make_SampleIds_rarefaction_columns_dict(self):
166
self.rarefaction_cols_dict,
167
make_SampleIds_rarefaction_columns_dict(self.rarefaction_data))
169
def test_extract_rarefaction_scores_at_depth(self):
170
"""check correct errors raised for wrong depths and correct mtx
174
self.extracted_mtx_10,
175
extract_rarefaction_scores_at_depth(10,self.rarefaction_data))
178
self.extracted_mtx_310,
179
extract_rarefaction_scores_at_depth(310,self.rarefaction_data))
182
self.extracted_mtx_910,
183
extract_rarefaction_scores_at_depth(910,self.rarefaction_data))
187
extract_rarefaction_scores_at_depth,810,self.rarefaction_data)
191
extract_rarefaction_scores_at_depth,100,self.rarefaction_data)
193
def test_convert_SampleIds_to_rarefaction_mtx(self):
194
"""check correct reduced rarefaction scores mtx produced"""
196
self.rarefaction_mtx_for_sample_pair1_0 = \
200
self.rarefaction_mtx_for_sample_pair1_1 = \
201
array([[ 2.13996, 1.99181],
202
[ 2.37055, 2.07163]])
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)
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)
218
def test_compare_alpha_diversity(self):
219
"""test main function properly compares alpha diversities"""
222
self.compared_alpha_diversities_TTD,
223
compare_alpha_diversities(self.rarefaction_file,
224
self.mapping_file, 'TTD', 10))
227
if __name__ == "__main__":