File: test_group.py

package info (click to toggle)
qiime 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 29,704 kB
  • sloc: python: 77,837; haskell: 379; sh: 113; makefile: 103
file content (356 lines) | stat: -rwxr-xr-x 18,555 bytes parent folder | download
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python

"""Tests public and private functions in the group module."""

__author__ = "Jai Rideout"
__copyright__ = "Copyright 2011, The QIIME project"
__credits__ = ["Jai Rideout", "Jeremy Widmann"]
__license__ = "GPL"
__version__ = "1.4.0"
__maintainer__ = "Jai Rideout"
__email__ = "jr378@nau.edu"
__status__ = "Release"

from cogent.util.unit_test import TestCase, main
from qiime.parse import parse_mapping_file, parse_distmat, group_by_field
from qiime.group import get_grouped_distances, get_all_grouped_distances,\
    get_field_state_comparisons, _get_indices, _get_groupings, _validate_input

class GroupTests(TestCase):
    """Tests of the group module."""
    def setUp(self):
        """Create some data to be used in the tests."""
        # Create the mapping file/distance matrix combo from the overview
        # tutorial.
        self.dist_matrix_string = ["\tPC.354\tPC.355\tPC.356\tPC.481\tPC.593\
                                    \tPC.607\tPC.634\tPC.635\tPC.636",
                                   "PC.354\t0.0\t0.625\t0.623\t0.61\t0.577\
                                    \t0.729\t0.8\t0.721\t0.765",
                                   "PC.355\t0.625\t0.0\t0.615\t0.642\t0.673\
                                    \t0.776\t0.744\t0.749\t0.677",
                                   "PC.356\t0.623\t0.615\t0.0\t0.682\t0.737\
                                    \t0.734\t0.777\t0.733\t0.724",
                                   "PC.481\t0.61\t0.642\t0.682\t0.0\t0.704\
                                    \t0.696\t0.675\t0.654\t0.696",
                                   "PC.593\t0.577\t0.673\t0.737\t0.704\t0.0\
                                    \t0.731\t0.758\t0.738\t0.737",
                                   "PC.607\t0.729\t0.776\t0.734\t0.696\t0.731\
                                    \t0.0\t0.718\t0.666\t0.727",
                                   "PC.634\t0.8\t0.744\t0.777\t0.675\t0.758\
                                    \t0.718\t0.0\t0.6\t0.578",
                                   "PC.635\t0.721\t0.749\t0.733\t0.654\t0.738\
                                    \t0.666\t0.6\t0.0\t0.623",
                                   "PC.636\t0.765\t0.677\t0.724\t0.696\t0.737\
                                    \t0.727\t0.578\t0.623\t0.0"]

        self.mapping_string = ["#SampleID\tBarcodeSequence\tTreatment\tDOB",
                               "PC.354\tAGCACGAGCCTA\tControl\t20061218",
                               "PC.355\tAACTCGTCGATG\tControl\t20061218",
                               "PC.356\tACAGACCACTCA\tControl\t20061126",
                               "PC.481\tACCAGCGACTAG\tControl\t20070314",
                               "PC.593\tAGCAGCACTTGT\tControl\t20071210",
                               "PC.607\tAACTGTGCGTAC\tFast\t20071112",
                               "PC.634\tACAGAGTCGGCT\tFast\t20080116",
                               "PC.635\tACCGCAGAGTCA\tFast\t20080116",
                               "PC.636\tACGGTGAGTGTC\tFast\t20080116"]

        # Field to test on. Field values are either "Control" or "Fast".
        self.field = 'Treatment'

        # Create a tiny distancy matrix/mapping file with a single sample for
        # additional testing.
        self.tiny_dist_matrix_string = ["\tSamp.1", "Samp.1\t0"]
        self.tiny_mapping_string = ["#SampleID\tBarcodeSequence\tSampleField",
                               "Samp.1\tAGCACGAGCCTA\tSampleFieldState1"]
        self.tiny_field = 'SampleField'

        self.small_dist_matrix_string = ["\tSamp.1\tSamp.2", "Samp.1\t0\t0.5",
                                         "Samp.2\t0.5\t0"]
        self.small_mapping_string = ["#SampleID\tBarcodeSequence\tSampleField",
                               "Samp.1\tAGCACGAGCCTA\tSampleFieldState1",
                               "Samp.2\tAGCACGAGCCTG\tSampleFieldState2"]
        self.small_field = 'SampleField'

        # Parse mapping "files" (faked here).
        self.mapping, self.mapping_header, self.comments = parse_mapping_file(
                self.mapping_string)
        mapping_data = [self.mapping_header]
        mapping_data.extend(self.mapping)
        self.groups = group_by_field(mapping_data, self.field)

        self.tiny_mapping, self.tiny_mapping_header, self.tiny_comments = \
                parse_mapping_file(self.tiny_mapping_string)
        tiny_mapping_data = [self.tiny_mapping_header]
        tiny_mapping_data.extend(self.tiny_mapping)
        self.tiny_groups = group_by_field(tiny_mapping_data, self.tiny_field)

        self.small_mapping, self.small_mapping_header, self.small_comments = \
                parse_mapping_file(self.small_mapping_string)
        small_mapping_data = [self.small_mapping_header]
        small_mapping_data.extend(self.small_mapping)
        self.small_groups = group_by_field(small_mapping_data,
                self.small_field)

        # Parse distance matrix "files" (faked here).
        self.dist_matrix_header, self.dist_matrix = parse_distmat(
                self.dist_matrix_string)

        self.tiny_dist_matrix_header, self.tiny_dist_matrix = parse_distmat(
                self.tiny_dist_matrix_string)

        self.small_dist_matrix_header, self.small_dist_matrix = parse_distmat(
                self.small_dist_matrix_string)

    def test_get_grouped_distances_within(self):
        """get_grouped_distances() should return a list of within distance
        groupings."""
        groupings = get_grouped_distances(self.dist_matrix_header,
            self.dist_matrix, self.mapping_header, self.mapping,
            self.field, within=True)
        expected = [
            ('Control', 'Control', [0.625, 0.623, 0.60999999999999999, \
                                    0.57699999999999996, 0.61499999999999999, \
                                    0.64200000000000002, 0.67300000000000004, \
                                    0.68200000000000005, 0.73699999999999999, \
                                    0.70399999999999996]),
            ('Fast', 'Fast', [0.71799999999999997, 0.66600000000000004, \
                              0.72699999999999998, 0.59999999999999998, \
                              0.57799999999999996, 0.623])]
        self.assertEqual(groupings, expected)

    def test_get_grouped_distances_between(self):
        """get_grouped_distances() should return a list of between distance
        groupings."""
        groupings = get_grouped_distances(self.dist_matrix_header,
            self.dist_matrix, self.mapping_header, self.mapping,
            self.field, within=False)
        expected = [
            ('Control', 'Fast', [0.72899999999999998, 0.80000000000000004, \
                                 0.72099999999999997, 0.76500000000000001, \
                                 0.77600000000000002, 0.74399999999999999, \
                                 0.749, 0.67700000000000005, \
                                 0.73399999999999999, 0.77700000000000002, \
                                 0.73299999999999998, 0.72399999999999998, \
                                 0.69599999999999995, 0.67500000000000004, \
                                 0.65400000000000003, 0.69599999999999995, \
                                 0.73099999999999998, 0.75800000000000001, \
                                 0.73799999999999999, 0.73699999999999999])]
        self.assertEqual(groupings, expected)

    def test_get_all_grouped_distances_within(self):
        """get_all_grouped_distances() should return a list of distances for
        all samples with the same field value."""
        groupings = get_all_grouped_distances(self.dist_matrix_header,
            self.dist_matrix, self.mapping_header, self.mapping,
            self.field, within=True)
        expected =  [0.625, 0.623, 0.60999999999999999, 0.57699999999999996,
                     0.61499999999999999, 0.64200000000000002,
                     0.67300000000000004, 0.68200000000000005,
                     0.73699999999999999, 0.70399999999999996,
                     0.71799999999999997, 0.66600000000000004,
                     0.72699999999999998, 0.59999999999999998,
                     0.57799999999999996, 0.623]
        self.assertEqual(groupings, expected)

    def test_get_all_grouped_distances_between(self):
        """get_all_grouped_distances() should return a list of distances
        between samples of all different field values."""
        groupings = get_all_grouped_distances(self.dist_matrix_header,
            self.dist_matrix, self.mapping_header, self.mapping,
            self.field, within=False)
        expected = [0.72899999999999998, 0.80000000000000004,
                    0.72099999999999997, 0.76500000000000001,
                    0.77600000000000002, 0.74399999999999999, 0.749,
                    0.67700000000000005, 0.73399999999999999,
                    0.77700000000000002, 0.73299999999999998,
                    0.72399999999999998, 0.69599999999999995,
                    0.67500000000000004, 0.65400000000000003,
                    0.69599999999999995, 0.73099999999999998,
                    0.75800000000000001, 0.73799999999999999,
                    0.73699999999999999]
        self.assertEqual(groupings, expected)

    def test_get_field_state_comparisons(self):
        """get_field_state_comparisons() should return a 2D dictionary of
        distances between a field state and its comparison field states."""
        comparison_groupings = get_field_state_comparisons(
                self.dist_matrix_header, self.dist_matrix, self.mapping_header,
                self.mapping, self.field, ['Control'])
        expected = {'Fast': {'Control': [0.72899999999999998,
            0.80000000000000004, 0.72099999999999997, 0.76500000000000001,
            0.77600000000000002, 0.74399999999999999, 0.749,
            0.67700000000000005, 0.73399999999999999, 0.77700000000000002,
            0.73299999999999998, 0.72399999999999998, 0.69599999999999995,
            0.67500000000000004, 0.65400000000000003, 0.69599999999999995,
            0.73099999999999998, 0.75800000000000001, 0.73799999999999999,
            0.73699999999999999]}}
        self.assertFloatEqual(comparison_groupings, expected)

        comparison_groupings = get_field_state_comparisons(
                self.dist_matrix_header, self.dist_matrix, self.mapping_header,
                self.mapping, self.field, ['Fast'])
        expected = {'Control': {'Fast': [0.72899999999999998,
            0.80000000000000004, 0.72099999999999997, 0.76500000000000001,
            0.77600000000000002, 0.74399999999999999, 0.749,
            0.67700000000000005, 0.73399999999999999, 0.77700000000000002,
            0.73299999999999998, 0.72399999999999998, 0.69599999999999995,
            0.67500000000000004, 0.65400000000000003, 0.69599999999999995,
            0.73099999999999998, 0.75800000000000001, 0.73799999999999999,
            0.73699999999999999]}}
        self.assertFloatEqual(comparison_groupings, expected)

    def test_get_field_state_comparisons_small(self):
        """get_field_state_comparisons() should return a 2D dictionary of
        distances between a field state and its comparison field states."""
        comparison_groupings = get_field_state_comparisons(
                self.small_dist_matrix_header, self.small_dist_matrix,
                self.small_mapping_header, self.small_mapping,
                self.small_field, ['SampleFieldState1'])
        expected = {'SampleFieldState2': {'SampleFieldState1': [0.5]}}
        self.assertFloatEqual(comparison_groupings, expected)

    def test_get_field_state_comparisons_tiny(self):
        """get_field_state_comparisons() should return an empty dictionary."""
        comparison_groupings = get_field_state_comparisons(
                self.tiny_dist_matrix_header, self.tiny_dist_matrix,
                self.tiny_mapping_header, self.tiny_mapping, self.tiny_field,
                ['SampleFieldState1'])
        self.assertEqual(comparison_groupings, {})

    def test_get_field_state_comparisons_no_comp_states(self):
        """get_field_state_comparisons() should raise a ValueError if no
        comparison field states are provided."""
        self.assertRaises(ValueError, get_field_state_comparisons,
                self.dist_matrix_header, self.dist_matrix,
                self.mapping_header, self.mapping, self.field,
                [])

    def test_get_field_state_comparisons_bad_comp_state(self):
        """get_field_state_comparisons() should raise a ValueError if a
        non-existent comparison field state is provided."""
        self.assertRaises(ValueError, get_field_state_comparisons,
                self.dist_matrix_header, self.dist_matrix,
                self.mapping_header, self.mapping, self.field,
                ['T0', 'Fast'])
        self.assertRaises(ValueError, get_field_state_comparisons,
                self.dist_matrix_header, self.dist_matrix,
                self.mapping_header, self.mapping, self.field,
                ['Fast', 'T0'])

    def test_validate_input_bad_input(self):
        """_validate_input() should raise ValueErrors on bad input."""
        self.assertRaises(ValueError, _validate_input,
                          None, None, None, None, None)
        self.assertRaises(ValueError, _validate_input,
                          self.dist_matrix_header, self.dist_matrix,
                          self.mapping_header, self.mapping, None)
        self.assertRaises(ValueError, _validate_input,
                          self.dist_matrix_header, 12,
                          self.mapping_header, self.mapping, None)
        self.assertRaises(ValueError, _validate_input,
                          self.dist_matrix_header, self.dist_matrix,
                          self.mapping_header, self.mapping, 42)
        self.assertRaises(ValueError, _validate_input,
                          self.dist_matrix_header, self.dist_matrix,
                          self.mapping_header, self.mapping, "aeiou")

    def test_validate_input_good_input(self):
        """_validate_input() should not raise any errors on good input."""
        _validate_input(self.dist_matrix_header, self.dist_matrix,
                          self.mapping_header, self.mapping, "Treatment")

    def test_get_indices_several_existing_items(self):
        """_get_indices() should return a list of valid indices for several
        existing items."""
        control_ids = ['PC.354', 'PC.355', 'PC.356', 'PC.481', 'PC.593']
        exp_control_indices = [0,1,2,3,4]
        
        fast_ids = ['PC.607', 'PC.634', 'PC.635', 'PC.636']
        exp_fast_indices = [5,6,7,8]
        
        obs_control = _get_indices(self.dist_matrix_header, control_ids)
        self.assertEqual(obs_control, exp_control_indices)
        
        obs_fast = _get_indices(self.dist_matrix_header, fast_ids)
        self.assertEqual(obs_fast, exp_fast_indices)

    def test_get_indices_one_existing_item_list(self):
        """_get_indices() should return a list of size 1 for a single item in a
        list that exists in the search list."""
        item_to_find = ['PC.355']
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [1])

    def test_get_indices_one_existing_item_scalar(self):
        """_get_indices() should return a list of size 1 for a single item that
        exists in the search list."""
        item_to_find = 'PC.355'
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [1])

    def test_get_indices_no_existing_item(self):
        """_get_indices() should return an empty list if no items exist in the
        search list."""
        item_to_find = 'PC.4242'
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])
        item_to_find = 42
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])
        item_to_find = ['PC.4242', 'CP.2424']
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])

        item_to_find = ['PC.4242', 'CP.2424', 56]
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])

    def test_get_indices_no_items_to_search(self):
        """_get_indices() should return an empty list if no search items are
        given."""
        item_to_find = []
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])
        item_to_find = ''
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])
        item_to_find = None
        self.assertEqual(_get_indices(self.dist_matrix_header, item_to_find),
                         [])

    def test_get_indices_null_or_empty_search_list(self):
        """_get_indices() should throw an error if the search list is None, and
        return an empty list if the search list is empty."""
        search_list = None
        self.assertRaises(ValueError, _get_indices, search_list, 'item')

        search_list = []
        self.assertEqual(_get_indices(search_list, 'item'), [])

        search_list = '' 
        self.assertEqual(_get_indices(search_list, 'item'), [])

    def test_get_groupings_no_field_states(self):
        """_get_groupings() should return an empty list if there are no field
        states in the groupings dictionary."""
        self.assertEqual(_get_groupings(self.dist_matrix_header,
            self.dist_matrix, {}, within=True), [])

        self.assertEqual(_get_groupings(self.dist_matrix_header,
            self.dist_matrix, {}, within=False), [])

    def test_get_groupings_within_tiny_dataset(self):
        """_get_groupings() should return an empty list for a single-sample
        dataset as the diagonal is omitted for within distances."""
        self.assertEqual(_get_groupings(self.tiny_dist_matrix_header,
            self.tiny_dist_matrix, self.tiny_groups, within=True), [])

    def test_get_groupings_between_tiny_dataset(self):
        """_get_groupings() should return an empty list for a single-sample
        dataset as there is only one field state, so no between distances can
        be computed."""
        self.assertEqual(_get_groupings(self.tiny_dist_matrix_header,
            self.tiny_dist_matrix, self.tiny_groups, within=False), [])

if __name__ == '__main__':
    main()