This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_beta_metrics.py
279 lines (260 loc) · 11.5 KB
/
test_beta_metrics.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
#!/usr/bin/env python
__author__ = "Justin Kuczynski"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Rob Knight", "Justin Kuczynski"]
__license__ = "GPL"
__version__ = "1.9.0"
__maintainer__ = "justin kuczynski"
__email__ = "justinak@gmail.com"
"""Contains tests for beta_metrics functions."""
import os.path
import numpy
from unittest import TestCase, main
from numpy.testing import assert_almost_equal
from cogent.maths.unifrac.fast_unifrac import fast_unifrac
from qiime.parse import make_envs_dict
from qiime.beta_metrics import (
_reorder_unifrac_res,
make_unifrac_metric,
make_unifrac_row_metric)
from qiime.parse import parse_newick
from cogent.core.tree import PhyloNode
from cogent.maths.unifrac.fast_tree import (unifrac)
import warnings
class FunctionTests(TestCase):
def setUp(self):
self.l19_data = numpy.array([
[7, 1, 0, 0, 0, 0, 0, 0, 0],
[4, 2, 0, 0, 0, 1, 0, 0, 0],
[2, 4, 0, 0, 0, 1, 0, 0, 0],
[1, 7, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 1, 0, 0, 0, 0, 0, 0],
[0, 4, 2, 0, 0, 0, 2, 0, 0],
[0, 2, 4, 0, 0, 0, 1, 0, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 7, 1, 0, 0, 0, 0, 0],
[0, 0, 4, 2, 0, 0, 0, 3, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0],
[0, 0, 1, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 1, 0, 0, 0, 0],
[0, 0, 0, 4, 2, 0, 0, 0, 4],
[0, 0, 0, 2, 4, 0, 0, 0, 1],
[0, 0, 0, 1, 7, 0, 0, 0, 0]
])
self.l19_sample_names = [
'sam1', 'sam2', 'sam3', 'sam4', 'sam5', 'sam6',
'sam7', 'sam8', 'sam9', 'sam_middle', 'sam11', 'sam12', 'sam13',
'sam14', 'sam15', 'sam16', 'sam17', 'sam18', 'sam19']
self.l19_taxon_names = ['tax1', 'tax2', 'tax3', 'tax4', 'endbigtaxon',
'tax6', 'tax7', 'tax8', 'tax9']
self.l19_treestr = '((((tax7:0.1,tax3:0.2):.98,tax8:.3, tax4:.3):.4, ' +\
'((tax1:0.3, tax6:.09):0.43,tax2:0.4):0.5):.2,' +\
'(tax9:0.3, endbigtaxon:.08));'
def test_reorder_unifrac_res(self):
""" reorder_unifrac_res should correctly reorder a misordered 3x3 matrix
"""
mtx = numpy.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], 'float')
unifrac_mtx = numpy.array([[1, 3, 2],
[7, 9, 8],
[4, 6, 5]], 'float')
sample_names = ['yo', "it's", "samples"]
unifrac_sample_names = ['yo', "samples", "it's"]
reordered_mtx = _reorder_unifrac_res(
[unifrac_mtx, unifrac_sample_names],
sample_names)
assert_almost_equal(reordered_mtx, mtx)
def test_make_unifrac_metric(self):
""" exercise of the unweighted unifrac metric should not throw errors"""
tree = parse_newick(self.l19_treestr, PhyloNode)
unif = make_unifrac_metric(False, unifrac, True)
res = unif(self.l19_data, self.l19_taxon_names, tree,
self.l19_sample_names)
envs = make_envs_dict(self.l19_data, self.l19_sample_names,
self.l19_taxon_names)
unifrac_mat, unifrac_names = fast_unifrac(tree, envs,
modes=['distance_matrix'])['distance_matrix']
assert_almost_equal(res, _reorder_unifrac_res([unifrac_mat,
unifrac_names], self.l19_sample_names))
self.assertEqual(res[0, 0], 0)
self.assertEqual(res[0, 3], 0.0)
self.assertNotEqual(res[0, 1], 1.0)
def test_make_unifrac_metric2(self):
""" samples with no seqs, and identical samples, should behave correctly
"""
tree = parse_newick(self.l19_treestr, PhyloNode)
unif = make_unifrac_metric(False, unifrac, True)
otu_data = numpy.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0], # sam1 zeros
[4, 2, 0, 0, 0, 1, 0, 0, 0],
[2, 4, 0, 0, 0, 1, 0, 0, 0],
[1, 7, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 1, 0, 0, 0, 0, 0, 0],
[0, 4, 2, 0, 0, 0, 2, 0, 0],
[0, 2, 4, 0, 0, 0, 1, 0, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 7, 1, 0, 0, 0, 0, 0],
[0, 0, 4, 2, 0, 0, 0, 3, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0], # sam14 zeros
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0], # sam 16 now like sam 13
[0, 0, 0, 4, 2, 0, 0, 0, 4],
[0, 0, 0, 2, 4, 0, 0, 0, 1],
[0, 0, 0, 1, 7, 0, 0, 0, 0]
])
warnings.filterwarnings('ignore')
res = unif(otu_data, self.l19_taxon_names, tree,
self.l19_sample_names)
envs = make_envs_dict(self.l19_data, self.l19_sample_names,
self.l19_taxon_names)
self.assertEqual(res[0, 0], 0)
self.assertEqual(res[0, 13], 0.0)
self.assertEqual(res[12, 15], 0.0)
self.assertEqual(res[0, 1], 1.0)
warnings.resetwarnings()
def test_make_unifrac_metric3(self):
treestr = '((((tax7:0.1):.98,tax8:.3, tax4:.3):.4, ' +\
'((tax6:.09):0.43):0.5):.2,' +\
'(tax9:0.3, endbigtaxon:.08));' # taxa 1,2,3 removed
tree = parse_newick(treestr, PhyloNode)
otu_data = numpy.array([
[7, 1, 0, 0, 0, 0, 0, 0, 0], # 1 now zeros
[4, 2, 0, 0, 0, 1, 0, 0, 0],
[2, 4, 0, 0, 0, 1, 0, 0, 0],
[1, 7, 0, 0, 0, 0, 0, 0, 0], # 4 now zeros
[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 1, 0, 0, 0, 0, 0, 0],
[0, 4, 2, 0, 0, 0, 2, 0, 0],
[0, 2, 4, 0, 0, 0, 1, 0, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 7, 1, 0, 0, 0, 0, 0],
[0, 0, 4, 2, 0, 0, 0, 3, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0],
[0, 0, 1, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 1, 0, 0, 0, 0],
[0, 0, 0, 4, 2, 0, 0, 0, 4],
[0, 0, 0, 2, 4, 0, 0, 0, 1],
[0, 0, 0, 1, 7, 0, 0, 0, 0]
])
unif = make_unifrac_metric(False, unifrac, True)
warnings.filterwarnings('ignore')
res = unif(otu_data, self.l19_taxon_names, tree,
self.l19_sample_names)
warnings.resetwarnings()
envs = make_envs_dict(self.l19_data, self.l19_sample_names,
self.l19_taxon_names)
self.assertEqual(res[0, 0], 0)
self.assertEqual(res[0, 3], 0.0)
self.assertEqual(res[0, 1], 1.0)
def test_make_unifrac_row_metric3(self):
treestr = '((((tax7:0.1):.98,tax8:.3, tax4:.3):.4, ' +\
'((tax6:.09):0.43):0.5):.2,' +\
'(tax9:0.3, endbigtaxon:.08));' # taxa 1,2,3 removed
tree = parse_newick(treestr, PhyloNode)
otu_data = numpy.array([
[7, 1, 0, 0, 0, 0, 0, 0, 0], # 1 now zeros
[4, 2, 0, 0, 0, 1, 0, 0, 0],
[2, 4, 0, 0, 0, 1, 0, 0, 0],
[1, 7, 0, 0, 0, 0, 0, 0, 0], # 4 now zeros
[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 1, 0, 0, 0, 0, 0, 0],
[0, 4, 2, 0, 0, 0, 2, 0, 0],
[0, 2, 4, 0, 0, 0, 1, 0, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 7, 1, 0, 0, 0, 0, 0],
[0, 0, 4, 2, 0, 0, 0, 3, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0],
[0, 0, 1, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 1, 0, 0, 0, 0],
[0, 0, 0, 4, 2, 0, 0, 0, 4],
[0, 0, 0, 2, 4, 0, 0, 0, 1],
[0, 0, 0, 1, 7, 0, 0, 0, 0]
])
unif = make_unifrac_metric(False, unifrac, True)
warnings.filterwarnings('ignore')
res = unif(otu_data, self.l19_taxon_names, tree,
self.l19_sample_names)
warnings.resetwarnings()
envs = make_envs_dict(self.l19_data, self.l19_sample_names,
self.l19_taxon_names)
self.assertEqual(res[0, 0], 0)
self.assertEqual(res[0, 3], 0.0)
self.assertEqual(res[0, 1], 1.0)
warnings.filterwarnings('ignore')
unif_row = make_unifrac_row_metric(False, unifrac, True)
for i, sam_name in enumerate(self.l19_sample_names):
if i in [0, 3, 4, 5, 8, 9]:
continue
# these have no data and are warned "meaningless".
# I Would prefer if they matched res anyway though
res_row = unif_row(otu_data, self.l19_taxon_names, tree,
self.l19_sample_names, sam_name)
for j in range(len(self.l19_sample_names)):
if j in [0, 3, 4, 5, 8, 9]:
continue # ok if meaningless number in zero sample
self.assertAlmostEqual(res_row[j], res[i, j])
warnings.resetwarnings()
def test_make_unifrac_row_metric2(self):
""" samples with no seqs, and identical samples, should behave correctly
"""
tree = parse_newick(self.l19_treestr, PhyloNode)
unif = make_unifrac_metric(False, unifrac, True)
otu_data = numpy.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0], # sam1 zeros
[4, 2, 0, 0, 0, 1, 0, 0, 0],
[2, 4, 0, 0, 0, 1, 0, 0, 0],
[1, 7, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 1, 0, 0, 0, 0, 0, 0],
[0, 4, 2, 0, 0, 0, 2, 0, 0],
[0, 2, 4, 0, 0, 0, 1, 0, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 7, 1, 0, 0, 0, 0, 0],
[0, 0, 4, 2, 0, 0, 0, 3, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0], # sam14 zeros
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0], # sam 16 now like sam 13
[0, 0, 0, 4, 2, 0, 0, 0, 4],
[0, 0, 0, 2, 4, 0, 0, 0, 1],
[0, 0, 0, 1, 7, 0, 0, 0, 0]
])
warnings.filterwarnings('ignore')
res = unif(otu_data, self.l19_taxon_names, tree,
self.l19_sample_names)
envs = make_envs_dict(self.l19_data, self.l19_sample_names,
self.l19_taxon_names)
self.assertEqual(res[0, 0], 0)
self.assertEqual(res[0, 13], 0.0)
self.assertEqual(res[12, 15], 0.0)
self.assertEqual(res[0, 1], 1.0)
warnings.resetwarnings()
warnings.filterwarnings('ignore')
unif_row = make_unifrac_row_metric(False, unifrac, True)
for i, sam_name in enumerate(self.l19_sample_names):
if i in [0]:
continue
# these have no data and are warned "meaningless".
# I Would prefer if they matched res anyway though
res_row = unif_row(otu_data, self.l19_taxon_names, tree,
self.l19_sample_names, sam_name)
for j in range(len((self.l19_sample_names))):
if j in [0]:
continue # ok if meaningless number in zero sample
self.assertEqual(res_row[j], res[i, j])
warnings.resetwarnings()
# run tests if called from command line
if __name__ == '__main__':
main()