This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_make_rarefaction_averages.py
140 lines (119 loc) · 5.05 KB
/
test_make_rarefaction_averages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
#file test_make_rarefaction_averages.py
#from __future__ import division
__author__ = "Meg Pirrung"
__copyright__ = "Copyright 2010, The QIIME Project"
__credits__ = ["Meg Pirrung"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Meg Pirrung"
__email__ = "meg.pirrung@colorado.edu"
__status__ = "Release"
"""
Unit tests for make_rarefaction_plots.py
"""
from cogent.util.unit_test import TestCase, main
from qiime.make_rarefaction_averages import *
from qiime.parse import parse_rarefaction, parse_mapping_file
from qiime.colors import process_colorby
class makeRarefactionAveragesTests(TestCase):
def setUp(self):
self.prefs = {
'background_color':'black',
'sample_coloring':
{
'SampleID':
{
'column':'SampleID',
'colors':(('red',(0,100,100)),('blue',(240,100,100)))
},
'Sex':
{
'column':'Sex',
'colors':(('red',(0,100,100)),('blue',(240,100,100)))
},
'Age':
{
'column':'Age',
'colors':(('red',(0,100,100)),('blue',(240,100,100)))
}
},
'MONTE_CARLO_GROUP_DISTANCES':
{
'SampleID': 10,
'Sex': 10,
'Age': 10
},
'FIELDS':
[
'Age',
'SampleID',
'Sex'
],
}
self.mappingfile = ['#SampleID\tSex\tAge',
'123\tF\t32',
'234\tM\t30',
'345\tM\t32']
self.p_mappingfile = parse_mapping_file(self.mappingfile,strip_quotes=True)
self.rarefactionfile = ['\tsequences per sample\titeration\t123\t234\t345',
'rare10.txt\t10\t0\t1.99181\t0.42877\t2.13996',
'rare10.txt\t10\t1\t2.07163\t0.42877\t2.37055',
'rare310.txt\t310\t0\t8.83115\t0.42877\t11.00725',
'rare310.txt\t310\t1\t10.05242\t0.42877\t8.24474',
'rare610.txt\t610\t0\t12.03067\t0.42877\t11.58928',
'rare610.txt\t610\t1\t12.9862\t0.42877\t11.58642']
self.col_headers, self.comments, self.rarefaction_fns, \
self.rarefaction_data = parse_rarefaction(self.rarefactionfile)
self.matrix, self.seqs_per_samp, self.sampleIDs = \
get_rarefaction_data(self.rarefaction_data, self.col_headers)
self.ave_seqs_per_sample = {'123':[2.03172,9.4417849999999994,\
12.508435],'234':[0.42876999999999998,0.42876999999999998,\
0.42876999999999998],'345':[2.255255,9.625995,11.58785]}
self.collapsed_ser_sex = {'M':[1.3420125000000001,5.0273824999999999,\
6.0083099999999998], 'F':[2.03172,9.4417849999999994,12.508435]}
self.err_ser_sex = {'M':[0.91324250000000007,4.5986124999999998,\
5.5795399999999997],'F':[0.0,0.0,0.0]}
self.overall_averages = {'123':7.9939800000000005,
'234':0.42876999999999993,
'345':7.8230333333333322}
def test_ave_seqs_per_sample(self):
test = ave_seqs_per_sample(self.matrix,self.seqs_per_samp,\
self.sampleIDs)
self.assertEqual(test, self.ave_seqs_per_sample)
def test_is_max_category_ops_neg(self):
test = is_max_category_ops(self.p_mappingfile, 'Sex')
self.assertEqual(test[0], False)
def test_is_max_category_ops_pos(self):
test = is_max_category_ops(self.p_mappingfile, 'SampleID')
self.assertEqual(test[0], True)
def test_make_error_series(self):
#make_error_series(rare_mat, sampleIDs, mapping, mapping_category)
# test = make_error_series('rare.txt',self.ave_seqs_per_sample,\
# self.sampleIDs,self.p_mappingfile,'Sex')
data = {}
mapping,headers,comments = self.p_mappingfile
new_mapping=[]
new_mapping.append(headers)
for i in range(len(mapping)):
new_mapping.append(mapping[i])
data['map']=new_mapping
color_prefs, data=process_colorby(None, data, self.prefs['sample_coloring'])
groups_and_colors=iter_color_groups(data['map'],color_prefs)
groups_and_colors=list(groups_and_colors)
for i in range(len(groups_and_colors)):
try:
groups_and_colors[i].index('Sex')
break
except(ValueError):
continue
groups=groups_and_colors[i][1]
test = make_error_series('rare.txt', self.ave_seqs_per_sample, groups, 'Sex')
self.assertEqual(test[0], self.collapsed_ser_sex)
self.assertEqual(test[1], self.err_ser_sex)
def test_get_overall_averages(self):
test = get_overall_averages(self.ave_seqs_per_sample,self.sampleIDs)
self.assertEqual(test, self.overall_averages)
#run tests if called from command line
if __name__ == '__main__':
main()