~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to tests/test_beta_metrics.py

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
__copyright__ = "Copyright 2011, The QIIME Project"
5
5
__credits__ = ["Rob Knight", "Justin Kuczynski"]
6
6
__license__ = "GPL"
7
 
__version__ = "1.5.0"
 
7
__version__ = "1.7.0"
8
8
__maintainer__ = "justin kuczynski"
9
9
__email__ = "justinak@gmail.com"
10
10
__status__ = "Release"
16
16
from cogent.util.unit_test import TestCase, main
17
17
from cogent.maths.unifrac.fast_unifrac import fast_unifrac
18
18
from qiime.parse import make_envs_dict
19
 
from qiime.beta_metrics import (_reorder_unifrac_res, make_unifrac_metric)
 
19
from qiime.beta_metrics import (_reorder_unifrac_res, make_unifrac_metric, make_unifrac_row_metric)
20
20
from qiime.parse import parse_newick
21
21
from cogent.core.tree import PhyloNode
22
22
from cogent.maths.unifrac.fast_tree import (unifrac)
163
163
        self.assertEqual(res[0,0], 0)
164
164
        self.assertEqual(res[0,3], 0.0)
165
165
        self.assertEqual(res[0,1], 1.0)
166
 
        
167
 
        
 
166
 
 
167
    def test_make_unifrac_row_metric3(self):
 
168
        treestr = '((((tax7:0.1):.98,tax8:.3, tax4:.3):.4, '+\
 
169
            '((tax6:.09):0.43):0.5):.2,'+\
 
170
            '(tax9:0.3, endbigtaxon:.08));' # taxa 1,2,3 removed
 
171
        tree = parse_newick(treestr, PhyloNode)
 
172
 
 
173
        otu_data = numpy.array([
 
174
            [7,1,0,0,0,0,0,0,0], # 1 now zeros
 
175
            [4,2,0,0,0,1,0,0,0], 
 
176
            [2,4,0,0,0,1,0,0,0],
 
177
            [1,7,0,0,0,0,0,0,0], # 4 now zeros
 
178
            [0,8,0,0,0,0,0,0,0],
 
179
            [0,7,1,0,0,0,0,0,0],
 
180
            [0,4,2,0,0,0,2,0,0],
 
181
            [0,2,4,0,0,0,1,0,0],
 
182
            [0,1,7,0,0,0,0,0,0],
 
183
            [0,0,8,0,0,0,0,0,0],
 
184
            [0,0,7,1,0,0,0,0,0],
 
185
            [0,0,4,2,0,0,0,3,0],
 
186
            [0,0,2,4,0,0,0,1,0],
 
187
            [0,0,1,7,0,0,0,0,0],
 
188
            [0,0,0,8,0,0,0,0,0],
 
189
            [0,0,0,7,1,0,0,0,0],
 
190
            [0,0,0,4,2,0,0,0,4],
 
191
            [0,0,0,2,4,0,0,0,1],
 
192
            [0,0,0,1,7,0,0,0,0]
 
193
            ])
 
194
 
 
195
        unif = make_unifrac_metric(False, unifrac, True)
 
196
        warnings.filterwarnings('ignore')
 
197
        res = unif(otu_data, self.l19_taxon_names, tree,
 
198
            self.l19_sample_names)
 
199
        warnings.resetwarnings()
 
200
        envs = make_envs_dict(self.l19_data, self.l19_sample_names,
 
201
            self.l19_taxon_names)
 
202
        self.assertEqual(res[0,0], 0)
 
203
        self.assertEqual(res[0,3], 0.0)
 
204
        self.assertEqual(res[0,1], 1.0)
 
205
 
 
206
        warnings.filterwarnings('ignore')
 
207
        unif_row = make_unifrac_row_metric(False, unifrac, True)
 
208
        for i, sam_name in enumerate(self.l19_sample_names):
 
209
            if i in [0,3,4,5,8,9]: continue 
 
210
            # these have no data and are warned "meaningless".
 
211
            # I Would prefer if they matched res anyway though
 
212
            res_row = unif_row(otu_data, self.l19_taxon_names, tree,
 
213
            self.l19_sample_names, sam_name)
 
214
            for j in range(len(self.l19_sample_names)):
 
215
                if j in [0,3,4,5,8,9]: continue # ok if meaningless number in zero sample
 
216
                self.assertEqual(res_row[j], res[i,j])
 
217
        warnings.resetwarnings()
 
218
 
 
219
 
 
220
    def test_make_unifrac_row_metric2(self):
 
221
        """ samples with no seqs, and identical samples, should behave correctly
 
222
        """
 
223
        tree = parse_newick(self.l19_treestr, PhyloNode)
 
224
        unif = make_unifrac_metric(False, unifrac, True)
 
225
        otu_data = numpy.array([
 
226
            [0,0,0,0,0,0,0,0,0],#sam1 zeros
 
227
            [4,2,0,0,0,1,0,0,0],
 
228
            [2,4,0,0,0,1,0,0,0],
 
229
            [1,7,0,0,0,0,0,0,0],
 
230
            [0,8,0,0,0,0,0,0,0],
 
231
            [0,7,1,0,0,0,0,0,0],
 
232
            [0,4,2,0,0,0,2,0,0],
 
233
            [0,2,4,0,0,0,1,0,0],
 
234
            [0,1,7,0,0,0,0,0,0],
 
235
            [0,0,8,0,0,0,0,0,0],
 
236
            [0,0,7,1,0,0,0,0,0],
 
237
            [0,0,4,2,0,0,0,3,0],
 
238
            [0,0,2,4,0,0,0,1,0],
 
239
            [0,0,0,0,0,0,0,0,0],#sam14 zeros
 
240
            [0,0,0,8,0,0,0,0,0],
 
241
            [0,0,2,4,0,0,0,1,0], #sam 16 now like sam 13
 
242
            [0,0,0,4,2,0,0,0,4],
 
243
            [0,0,0,2,4,0,0,0,1],
 
244
            [0,0,0,1,7,0,0,0,0]
 
245
            ])
 
246
        warnings.filterwarnings('ignore')
 
247
        res = unif(otu_data, self.l19_taxon_names, tree,
 
248
            self.l19_sample_names)
 
249
        envs = make_envs_dict(self.l19_data, self.l19_sample_names,
 
250
            self.l19_taxon_names)
 
251
        self.assertEqual(res[0,0], 0)
 
252
        self.assertEqual(res[0,13], 0.0)
 
253
        self.assertEqual(res[12,15], 0.0)
 
254
        self.assertEqual(res[0,1], 1.0)
 
255
        warnings.resetwarnings()
 
256
 
 
257
        warnings.filterwarnings('ignore')
 
258
        unif_row = make_unifrac_row_metric(False, unifrac, True)
 
259
        for i, sam_name in enumerate(self.l19_sample_names):
 
260
            if i in [0]: continue 
 
261
            # these have no data and are warned "meaningless".
 
262
            # I Would prefer if they matched res anyway though
 
263
            res_row = unif_row(otu_data, self.l19_taxon_names, tree,
 
264
            self.l19_sample_names, sam_name)
 
265
            for j in range(len((self.l19_sample_names))):
 
266
                if j in [0]: continue # ok if meaningless number in zero sample
 
267
                self.assertEqual(res_row[j], res[i,j])
 
268
        warnings.resetwarnings()
 
269
 
168
270
#run tests if called from command line
169
271
if __name__ == '__main__':
170
272
    main()