~ubuntu-branches/ubuntu/natty/python-cogent/natty

« back to all changes in this revision

Viewing changes to tests/test_maths/test_unifrac/test_fast_tree.py

  • Committer: Bazaar Package Importer
  • Author(s): Steffen Moeller
  • Date: 2010-12-04 22:30:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101204223035-j11kinhcrrdgg2p2
Tags: 1.5-1
* Bumped standard to 3.9.1, no changes required.
* New upstream version.
  - major additions to Cookbook
  - added AlleleFreqs attribute to ensembl Variation objects.
  - added getGeneByStableId method to genome objects.
  - added Introns attribute to Transcript objects and an Intron class.
  - added Mann-Whitney test and a Monte-Carlo version
  - exploratory and confirmatory period estimation techniques (suitable for
    symbolic and continuous data)
  - Information theoretic measures (AIC and BIC) added
  - drawing of trees with collapsed nodes
  - progress display indicator support for terminal and GUI apps
  - added parser for illumina HiSeq2000 and GAiix sequence files as 
    cogent.parse.illumina_sequence.MinimalIlluminaSequenceParser.
  - added parser to FASTQ files, one of the output options for illumina's
    workflow, also added cookbook demo.
  - added functionality for parsing of SFF files without the Roche tools in
    cogent.parse.binary_sff
  - thousand fold performance improvement to nmds
  - >10-fold performance improvements to some Table operations

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
 
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"
24
 
__version__ = "1.4.1"
 
24
__version__ = "1.5.0"
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
500
500
 
 
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],\
 
504
            [0,1,1],[1,1,1]])
 
505
        bl = self.branch_lengths
 
506
        result = unifrac_matrix(bl, m)
 
507
        
 
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)
 
512
        
 
513
        #should work ok on asymmetric metrics
 
514
        result = unifrac_matrix(bl,m,metric=unnormalized_G,is_symmetric=False)
 
515
        
 
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)
 
521
        
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],\
516
537
 
517
538
 
518
539
    def test_weighted_unifrac_matrix(self):
519
 
        """weighted unifrac matrix should return correct results for model tree"""
 
540
        """weighted unifrac matrix should ret correct results for model tree"""
520
541
        #should match web site calculations
521
542
        envs = self.count_array
522
543
        bound_indices = bind_to_array(self.nodes, envs)
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
 
561
        
 
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)
 
575
 
 
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,
 
582
            tip_distances=td)
 
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)
 
588
 
540
589
 
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)