12
12
jackknife_int, unifrac, unnormalized_unifrac, PD, G, unnormalized_G,
13
13
unifrac_matrix, unifrac_vector, PD_vector, weighted_unifrac,
14
14
weighted_unifrac_matrix, weighted_unifrac_vector, jackknife_array,
15
env_unique_fraction, unifrac_one_sample, weighted_one_sample)
16
16
from numpy import (arange, reshape, zeros, logical_or, array, sum, nonzero,
17
17
flatnonzero, newaxis)
18
18
from numpy.random import permutation
21
21
__copyright = "Copyright 2007, the authors."
22
22
__credits__ = ["Rob Knight", "Micah Hamady"]
23
23
__license__ = "GPL"
25
25
__maintainer__ = "Rob Knight, Micah Hamady"
26
26
__email__ = "rob@spot.colorado.edu, hamady@colorado.edu"
27
27
__status__ = "Prototype"
498
498
0.4706], [0.6154, 0.4707, 0]])
499
499
assert (abs(result - exp)).max() < 0.001
501
def test_unifrac_one_sample(self):
502
"""unifrac_one_sample should match unifrac_matrix"""
503
m = array([[1,0,1],[1,1,0],[0,1,0],[0,0,1],[0,1,0],[0,1,1],[1,1,1],\
505
bl = self.branch_lengths
506
result = unifrac_matrix(bl, m)
508
for i in range(len(result)):
509
one_sam_res = unifrac_one_sample(i, bl, m)
510
self.assertEqual(result[i], one_sam_res)
511
self.assertEqual(result[:,i], one_sam_res)
513
#should work ok on asymmetric metrics
514
result = unifrac_matrix(bl,m,metric=unnormalized_G,is_symmetric=False)
516
for i in range(len(result)):
517
one_sam_res = unifrac_one_sample(i, bl, m, metric=unnormalized_G)
518
self.assertEqual(result[i], one_sam_res)
519
# only require row for asym
520
# self.assertEqual(result[:,i], one_sam_res)
501
522
def test_unifrac_vector(self):
502
523
"""unifrac_vector should return correct results for model tree"""
503
524
m = array([[1,0,1],[1,1,0],[0,1,0],[0,0,1],[0,1,0],[0,1,1],[1,1,1],\
537
558
exp = array([[0, 9.1/11.5, 4.5/(10.5+1./3)], [9.1/11.5, 0, \
538
559
6.4/(11+1./3)], [4.5/(10.5+1./3), 6.4/(11+1./3), 0]])
539
560
assert (abs(result - exp)).max() < 0.001
562
def test_weighted_one_sample(self):
563
"""weighted one sample should match weighted matrix"""
564
#should match web site calculations
565
envs = self.count_array
566
bound_indices = bind_to_array(self.nodes, envs)
567
sum_descendants(bound_indices)
568
bl = self.branch_lengths
569
tip_indices = [n._leaf_index for n in self.t.tips()]
570
result = weighted_unifrac_matrix(bl, envs, tip_indices)
571
for i in range(len(result)):
572
one_sam_res = weighted_one_sample(i, bl, envs, tip_indices)
573
self.assertEqual(result[i], one_sam_res)
574
self.assertEqual(result[:,i], one_sam_res)
576
#should work with branch length corrections
577
td = bl.copy()[:,newaxis]
578
tip_bindings = bind_to_parent_array(self.t, td)
579
tips = [n._leaf_index for n in self.t.tips()]
580
tip_distances(td, tip_bindings, tips)
581
result = weighted_unifrac_matrix(bl, envs, tip_indices, bl_correct=True,
583
for i in range(len(result)):
584
one_sam_res = weighted_one_sample(i, bl, envs, tip_indices,
585
bl_correct=True, tip_distances=td)
586
self.assertEqual(result[i], one_sam_res)
587
self.assertEqual(result[:,i], one_sam_res)
541
590
def test_weighted_unifrac_vector(self):
542
"""weighted_unifrac_vector should return correct results for model tree"""
591
"""weighted_unifrac_vector should ret correct results for model tree"""
543
592
envs = self.count_array
544
593
bound_indices = bind_to_array(self.nodes, envs)
545
594
sum_descendants(bound_indices)