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

« back to all changes in this revision

Viewing changes to qiime/make_distance_histograms.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:
3
3
 
4
4
__author__ = "Jeremy Widmann"
5
5
__copyright__ = "Copyright 2011, The QIIME Project"
6
 
__credits__ = ["Jeremy Widmann","Rob Knight","Jesse Stombaugh"]
 
6
__credits__ = ["Jeremy Widmann","Rob Knight","Jesse Stombaugh",
 
7
               "Jai Ram Rideout"]
7
8
__license__ = "GPL"
8
 
__version__ = "1.5.0"
 
9
__version__ = "1.7.0"
9
10
__maintainer__ = "Jeremy Widmann"
10
11
__email__ = "jeremy.widmann@colorado.edu"
11
12
__status__ = "Release"
14
15
use('Agg',warn=False)
15
16
from qiime.parse import parse_mapping_file, parse_distmat, group_by_field,\
16
17
    group_by_fields
17
 
from cogent.maths.stats.test import t_two_sample
 
18
from qiime.pycogent_backports.test import t_two_sample
18
19
from numpy import array, mean, average, arange, concatenate
19
20
from collections import defaultdict
20
21
from string import strip
97
98
 
98
99
def between_sample_distances(dmat):
99
100
    """Returns all upper triangle distances from dmat.
 
101
 
 
102
    WARNING: Only symmetric, hollow distance matrices may be used as input.
 
103
    Asymmetric distance matrices, such as those obtained by the UniFrac Gain
 
104
    metric (i.e. beta_diversity.py -m unifrac_g), should not be used as input.
100
105
    """
101
106
    distances = []
102
107
    dmat_len = len(dmat)
510
515
    """Splits distances by group membership, returns vals for each pair.
511
516
    
512
517
    Omits the zeros along the diagonal.
 
518
 
 
519
    WARNING: Only symmetric, hollow distance matrices may be used as input.
 
520
    Asymmetric distance matrices, such as those obtained by the UniFrac Gain
 
521
    metric (i.e. beta_diversity.py -m unifrac_g), should not be used as input.
513
522
    """
514
523
    result = []
515
524
    group_items = groups.items()
544
553
        pass
545
554
        
546
555
    for field, data in group_distance_dict.items(): #skip sample id field
547
 
        fname = path.join(path_prefix, 'dist_' + field + '.xls')
 
556
        fname = path.join(path_prefix, 'dist_' + field + '.txt')
548
557
        outfile = open(fname, 'w')
549
558
        for d in data:
550
559
            if subdir_prefix.endswith('pairs'):
559
568
 
560
569
def group_distances(mapping_file,dmatrix_file,fields,dir_prefix='',\
561
570
    subdir_prefix='group_distances'):
562
 
    """Calculate all lists of distance groups."""
 
571
    """Calculate all lists of distance groups.
 
572
    
 
573
    WARNING: Only symmetric, hollow distance matrices may be used as input.
 
574
    Asymmetric distance matrices, such as those obtained by the UniFrac Gain
 
575
    metric (i.e. beta_diversity.py -m unifrac_g), should not be used as input.
 
576
    """
563
577
    distance_groups = {}
564
578
    mapping, header, comments = parse_mapping_file(open(mapping_file,'U'))
565
579
    header = [header]
617
631
    - do t test between each pair of groups
618
632
    - randomize matrix n times and find empirical value of t for each pair
619
633
    - compare the actual value of t to the randomized values
 
634
 
 
635
    WARNING: Only symmetric, hollow distance matrices may be used as input.
 
636
    Asymmetric distance matrices, such as those obtained by the UniFrac Gain
 
637
    metric (i.e. beta_diversity.py -m unifrac_g), should not be used as input.
620
638
    """
621
639
    mapping, header, comments = parse_mapping_file(open(mapping_file,'U'))
622
640
    header = [header]
650
668
        else:
651
669
            groups = group_by_field(mapping, field)
652
670
        outfile = open(path.join(path_prefix,
653
 
                                 'group_distances_'+field+'.xls'), 'w')
 
671
                                 'group_distances_'+field+'.txt'), 'w')
654
672
        outfile.write('\t'.join(['Category_1a','Category_1b','Avg',\
655
673
            'Category_2a','Category_2b','Avg','t','p',\
656
674
            'p_greater','p_less','Iterations\n']))
699
717
    - do t test between each pair of groups
700
718
    - randomize matrix n times and find empirical value of t for each pair
701
719
    - compare the actual value of t to the randomized values
 
720
 
 
721
    WARNING: Only symmetric, hollow distance matrices may be used as input.
 
722
    Asymmetric distance matrices, such as those obtained by the UniFrac Gain
 
723
    metric (i.e. beta_diversity.py -m unifrac_g), should not be used as input.
702
724
    """
703
725
 
704
726
    path_prefix = path.join(dir_prefix,subdir_prefix)
726
748
        field, distances in within_and_between.items()])
727
749
    
728
750
    outfile = open(path.join(path_prefix,
729
 
                            'group_distances_within_and_between.xls'), 'w')
 
751
                            'group_distances_within_and_between.txt'), 'w')
730
752
    outfile.write('\t'.join(['Comparison','Category_1','Avg',\
731
753
        'Comparison','Category_2','Avg','t','p',\
732
754
        'p_greater','p_less','Iterations\n']))
788
810
            [[first_group, second_group, distancdes],...]
789
811
        - dmat: full distance matrix
790
812
        - num_iters: integer number of random dmats to make.
 
813
 
 
814
    WARNING: Only symmetric, hollow distance matrices may be used as input.
 
815
    Asymmetric distance matrices, such as those obtained by the UniFrac Gain
 
816
    metric (i.e. beta_diversity.py -m unifrac_g), should not be used as input.
791
817
    """
792
818
    rand_dists = []
793
819
    upper_triangle = between_sample_distances(dmat).values()[0]