This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_make_distance_boxplots.py
340 lines (292 loc) · 13.6 KB
/
test_make_distance_boxplots.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
from __future__ import division
__author__ = "Jai Ram Rideout"
__copyright__ = "Copyright 2012, The QIIME project"
__credits__ = ["Jai Ram Rideout"]
__license__ = "GPL"
__version__ = "1.9.1-dev"
__maintainer__ = "Jai Ram Rideout"
__email__ = "jai.rideout@gmail.com"
"""Test suite for the make_distance_boxplots.py module."""
from unittest import TestCase, main
from matplotlib.figure import Figure
from numpy.testing import assert_almost_equal
from qiime.make_distance_boxplots import (_cast_y_axis_extrema,
_color_field_states,
make_distance_boxplots,
_sort_distributions)
class MakeDistanceBoxplotsTests(TestCase):
"""Tests for the make_distance_boxplots.py module."""
def setUp(self):
"""Define some sample data that will be used by the tests."""
self.map_f = map_lines.split('\n')
self.dm_f = dm_lines.split('\n')
self.too_many_colors_map_f = too_many_colors_map_lines.split('\n')
def test_cast_y_axis_extrema(self):
"""Test correctly assigns colors to a field based on another field."""
obs = _cast_y_axis_extrema(1.0)
assert_almost_equal(obs, 1.0)
obs = _cast_y_axis_extrema(1)
assert_almost_equal(obs, 1.0)
obs = _cast_y_axis_extrema('1.0')
assert_almost_equal(obs, 1.0)
obs = _cast_y_axis_extrema('1')
assert_almost_equal(obs, 1.0)
obs = _cast_y_axis_extrema('auto')
self.assertIsNone(obs)
def test_cast_y_axis_extrema_invalid_input(self):
"""Test correctly raises an error on bad input."""
self.assertRaises(ValueError, _cast_y_axis_extrema, 'foo')
def test_color_field_states(self):
"""Test correctly assigns colors to a field based on another field."""
# All sample IDs and field states.
exp = ([(1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)],
{'y': (0.0, 0.0, 1.0), 'x': (1.0, 0.0, 0.0)})
obs = _color_field_states(self.map_f, ['1', '2', '3', '4', '5', '6'],
'Foo', ['a', 'b', 'c'], 'Bar')
self.assertEqual(exp[0], obs[0])
assert_almost_equal(obs[1]['x'], exp[1]['x'])
assert_almost_equal(obs[1]['y'], exp[1]['y'])
# Subset of sample IDs and field states.
exp = ([(1.0, 0.0, 0.0)], {'x': (1.0, 0.0, 0.0)})
obs = _color_field_states(self.map_f, ['1', '2'], 'Foo', ['a'], 'Bar')
self.assertEqual(exp[0], obs[0])
assert_almost_equal(obs[1]['x'], exp[1]['x'])
# Color field by itself (useless but still allowed).
exp = ([(1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.9490196078431372,
0.45098039215686275, 0.01568627450980392)], {'a':
(1.0, 0.0, 0.0),
'c': (0.9490196078431372, 0.45098039215686275,
0.01568627450980392), 'b': (0.0, 0.0, 1.0)})
obs = _color_field_states(self.map_f, ['1', '2', '3', '4', '5', '6'],
'Foo', ['a', 'b', 'c'], 'Foo')
self.assertEqual(exp[0], obs[0])
assert_almost_equal(obs[1]['a'], exp[1]['a'])
assert_almost_equal(obs[1]['b'], exp[1]['b'])
assert_almost_equal(obs[1]['c'], exp[1]['c'])
def test_color_field_states_invalid_input(self):
"""Test correctly raises error on invalid input."""
# Field to color not in mapping file.
self.assertRaises(ValueError, _color_field_states, self.map_f,
['1', '2', '3', '4', '5'], 'Fooz', ['a', 'b'], 'Bar')
# Field to color by not in mapping file.
self.assertRaises(ValueError, _color_field_states, self.map_f,
['1', '2', '3', '4', '5'], 'Foo', ['a', 'b'], 'Barz')
# Field states are not found in field (due to subset of sample IDs).
self.assertRaises(ValueError, _color_field_states, self.map_f,
['1', '2', '3', '4', '5'], 'Foo', ['a', 'c'], 'Bar')
# Field states are not found in field (not in column at all).
self.assertRaises(ValueError, _color_field_states, self.map_f,
['1', '2', '3', '4', '5', '6'], 'Foo', ['a', 'c', 'z'], 'Bar')
# Not enough colors.
samp_ids = [str(i) for i in range(1, 31)]
self.assertRaises(ValueError, _color_field_states,
self.too_many_colors_map_f, samp_ids, 'Description', samp_ids,
'Description')
# No one-to-one mapping.
self.assertRaises(ValueError, _color_field_states, self.map_f,
['1', '2', '3', '4', '5', '6'], 'Foo', ['a', 'c', 'b'], 'Baz')
def test_make_distance_boxplots(self):
"""Test correctly generates plot, raw data, and labels."""
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo', 'Bar'])
self.assertEqual(len(obs), 2)
self.assertEqual(obs[0][0], 'Foo')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 7)
self.assertEqual(len(obs[0][3]), 7)
self.assertEqual(obs[0][4], [None, None, None, None, None, None, None])
self.assertEqual(obs[1][0], 'Bar')
self.assertTrue(isinstance(obs[1][1], Figure))
self.assertEqual(len(obs[1][2]), 5)
self.assertEqual(len(obs[1][3]), 5)
self.assertEqual(obs[1][4], [None, None, None, None, None])
def test_make_distance_boxplots_suppress_plots(self):
"""Test correctly suppresses different plot types."""
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
suppress_all_within=True)
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 4)
self.assertEqual(len(obs[0][3]), 4)
self.assertEqual(obs[0][4], [None, None, None, None])
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
suppress_all_within=True,
suppress_all_between=True)
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 3)
self.assertEqual(len(obs[0][3]), 3)
self.assertEqual(obs[0][4], [None, None, None])
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
suppress_all_within=True,
suppress_all_between=True,
suppress_individual_within=True)
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 1)
self.assertEqual(len(obs[0][3]), 1)
self.assertEqual(obs[0][4], [None])
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
suppress_all_within=True,
suppress_all_between=True,
suppress_individual_between=True)
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 2)
self.assertEqual(len(obs[0][3]), 2)
self.assertEqual(obs[0][4], [None, None])
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
suppress_individual_within=True,
suppress_individual_between=True)
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 2)
self.assertEqual(len(obs[0][3]), 2)
self.assertEqual(obs[0][4], [None, None])
def test_make_distance_boxplots_box_color(self):
"""Test correctly colors boxes in a variety of ways."""
# Single box color for all.
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
box_color='r')
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 5)
self.assertEqual(len(obs[0][3]), 5)
self.assertEqual(obs[0][4], ['r', 'r', 'r', 'r', 'r'])
# Single box color, with some plots suppressed.
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Bar'],
box_color='r',
suppress_individual_within=True)
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Bar')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 3)
self.assertEqual(len(obs[0][3]), 3)
self.assertEqual(obs[0][4], ['r', 'r', 'r'])
# Color individual within boxes.
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo'],
color_individual_within_by_field='Bar')
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Foo')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 7)
self.assertEqual(len(obs[0][3]), 7)
self.assertEqual(len(obs[0][4]), 7)
self.assertIsNone(obs[0][4][0])
self.assertIsNone(obs[0][4][1])
self.assertEqual(obs[0][4][2], (1.0, 0.0, 0.0))
self.assertEqual(obs[0][4][3], (0.0, 0.0, 1.0))
self.assertIsNone(obs[0][4][4])
self.assertIsNone(obs[0][4][5])
self.assertIsNone(obs[0][4][6])
# Color individual within boxes, make sure box_color is ignored.
obs = make_distance_boxplots(self.dm_f, self.map_f, ['Foo'],
box_color='pink', color_individual_within_by_field='Bar')
self.assertEqual(len(obs), 1)
self.assertEqual(obs[0][0], 'Foo')
self.assertTrue(isinstance(obs[0][1], Figure))
self.assertEqual(len(obs[0][2]), 7)
self.assertEqual(len(obs[0][3]), 7)
self.assertEqual(len(obs[0][4]), 7)
self.assertIsNone(obs[0][4][0])
self.assertIsNone(obs[0][4][1])
self.assertEqual(obs[0][4][2], (1.0, 0.0, 0.0))
self.assertEqual(obs[0][4][3], (0.0, 0.0, 1.0))
self.assertIsNone(obs[0][4][4])
self.assertIsNone(obs[0][4][5])
self.assertIsNone(obs[0][4][6])
def test_make_distance_boxplots_invalid_input(self):
"""Test correctly raises an error on invalid input."""
# No fields provided.
self.assertRaises(ValueError, make_distance_boxplots, self.dm_f,
self.map_f, None)
self.assertRaises(ValueError, make_distance_boxplots, self.dm_f,
self.map_f, [])
# Nonexistent field.
self.assertRaises(ValueError, make_distance_boxplots, self.dm_f,
self.map_f, ['Foo', 'foobarbaz'])
# Invalid width/height.
self.assertRaises(ValueError, make_distance_boxplots, self.dm_f,
self.map_f, ['Foo', 'Bar'], width=-1, height=5)
self.assertRaises(ValueError, make_distance_boxplots, self.dm_f,
self.map_f, ['Foo', 'Bar'], width=1, height=0)
# Suppress everything.
self.assertRaises(ValueError, make_distance_boxplots, self.dm_f,
self.map_f, ['Foo', 'Bar'], suppress_all_within=True,
suppress_all_between=True, suppress_individual_within=True,
suppress_individual_between=True)
def test_sort_distributions_median(self):
"""Test correctly sorts distributions by median."""
exp = [([0, 0, 0, 1], [2, 1, 1], [1], [1, 2, 3]),
('bar', 'baz', 'zab', 'foo'), ('b', 'r', 'b', 'w')]
obs = _sort_distributions(
[[1, 2, 3], [2, 1, 1], [0, 0, 0, 1], [1]],
['foo', 'baz', 'bar', 'zab'], ['w', 'r', 'b', 'b'], 'median')
self.assertEqual(obs, exp)
def test_sort_distributions_alphabetical(self):
"""Test correctly sorts distributions alphabetically."""
exp = [([2, 1, 1], [1, 2, 3], [0, 0, 0, 1], [1]),
('baz', 'foo', 'foo', 'zab'), ('r', 'w', 'b', 'b')]
obs = _sort_distributions(
[[1, 2, 3], [2, 1, 1], [0, 0, 0, 1], [1]],
['foo', 'baz', 'foo', 'zab'], ['w', 'r', 'b', 'b'], 'alphabetical')
self.assertEqual(obs, exp)
def test_sort_distributions_invalid_input(self):
"""Correctly raises error on invalid input."""
# Unfortunately, this code doesn't support the brosort algorithm... :(
with self.assertRaises(ValueError):
_ = _sort_distributions([[1, 2, 3], [3, 2, 1]], ['foo', 'bar'],
['r', 'b'], 'brosort')
map_lines = """#SampleID\tFoo\tBar\tBaz\tDescription
1\ta\tx\tm\t1
2\tb\ty\tn\t2
3\ta\tx\tm\t3
4\ta\tx\tn\t4
5\tb\ty\tn\t5
6\tc\tx\tm\t6"""
dm_lines = """\t1\t2\t3\t4\t5\t6
1\t0\t1\t2\t4\t7\t11
2\t1\t0\t3\t5\t8\t12
3\t2\t3\t0\t6\t9\t13
4\t4\t5\t6\t0\t10\t14
5\t7\t8\t9\t10\t0\t15
6\t11\t12\t13\t14\t15\t0"""
too_many_colors_map_lines = """#SampleID\tDescription
1\t1
2\t2
3\t3
4\t4
5\t5
6\t6
7\t7
8\t8
9\t9
10\t10
11\t11
12\t12
13\t13
14\t14
15\t15
16\t16
17\t17
18\t18
19\t19
20\t20
21\t21
22\t22
23\t23
24\t24
25\t25
26\t26
27\t27
28\t28
29\t29
30\t30"""
if __name__ == "__main__":
main()