コード例 #1
0
 def test_call_phylogenetic(self):
     """AlphaDiversityCalc __call__ should call metric on phylo data
     and return correct values"""
     c = AlphaDiversityCalc(metric=PD_whole_tree, is_phylogenetic=True)
     self.assertEqual(c(data_path=self.otu_table, tree_path=self.tree, \
         taxon_names = self.otu_names, sample_names=self.sample_names),
         [13, 17, 0])
コード例 #2
0
 def test_multi_return(self):
     """AlphaDiversityCalc __call__ should call metric on data
     and return correct result for metric fn that returns a len 3 tuple
     """
     c = AlphaDiversityCalc(osd)
     res = c(data_path=self.otu_table1_fp)
     assert_almost_equal(res, array([[2, 1, 1], [4, 4, 0], [0, 0, 0]]))
コード例 #3
0
ファイル: rarefaction.py プロジェクト: Jmaldo27/Evident
def single_object_alpha(biom_object, metrics, tree_object):
    """given a metric calculates alpha diversity of a biom object

	Inputs:
	biom_object: biom formatted OTU table
	metrics: list of alpha diversity metrics
	tree_object: tree object for the phylogenetic metrics

	Output:
	calculations: tab delimitted string with the calculations for the object
	"""

    calcs = []
    for metric in metrics:
        try:
            metric_f = get_nonphylogenetic_metric(metric)
            is_phylogenetic = False
        except AttributeError:
            try:
                metric_f = get_phylogenetic_metric(metric)
                is_phylogenetic = True
            except AttributeError:
                print 'something is wrong with conf file'
        c = AlphaDiversityCalc(metric_f, is_phylogenetic)
        calcs.append(c)

    all_calcs = AlphaDiversityCalcs(calcs)

    result = all_calcs(data_path=biom_object,
                       tree_path=tree_object,
                       log_path=None)
    return all_calcs.formatResult(result)
コード例 #4
0
 def test_call_phylogenetic(self):
     """AlphaDiversityCalc __call__ should call metric on phylo data
     and return correct values"""
     c = AlphaDiversityCalc(metric=PD_whole_tree, is_phylogenetic=True)
     assert_almost_equal(
         c(data_path=self.otu_table1_fp,
           tree_path=self.tree1,
           taxon_names=self.otu_table1.ids(axis='observation'),
           sample_names=self.otu_table1.ids()), [13, 17, 0])
コード例 #5
0
def single_file_cup(otu_filepath, metrics, outfilepath, r, alpha, f, ci_type):
    """Compute variations of the conditional uncovered probability.

    otufilepath: path to otu_table file
    metrics: comma separated list of required metrics
    outfilepath: path to output file

    r: Number of new colors that are required for the next prediction
    alpha: desired confidence level
    f: ratio between upper and lower bound
    ci_type: type of confidence interval. One of:
             ULCL: upper and lower bounds with conservative lower bound
             ULCU: upper and lower woth conservative upper bound
             U: Upper bound only, lower bound fixed to 0
             L: Lower bound only, upper bound fixed to 1

    The opposite of uncovered probability is sometimes called coverage.
    """
    metrics_list = metrics.split(',')
    calcs = []

    params = {'r': r,
              'alpha': alpha,
              'f':f,
              'ci_type':ci_type}
                  
    for metric in metrics_list:
        try:
            metric_f = get_cup_metric(metric)
        except AttributeError:
            stderr.write(
                "Could not find metric %s.\n Known metrics are: %s\n" \
                    % (metric, ', '.join(list_known_metrics())))
            exit(1)
            
        c = AlphaDiversityCalc(metric_f, params=params)
        calcs.append(c)
    
    all_calcs = AlphaDiversityCalcs(calcs)

    try:
        result = all_calcs(data_path=otu_filepath,
            result_path=outfilepath, log_path=None)
        if result:  #can send to stdout instead of file
            print all_calcs.formatResult(result)
    except IOError, e:
        stderr.write("Failed because of missing files.\n")
        stderr.write(str(e)+'\n')
        exit(1)
コード例 #6
0
    def test_call_phylogenetic_escaped_names(self):
        """AlphaDiversityCalc __call__ should call metric on phylo data
        and return correct values"""
        c = AlphaDiversityCalc(metric=PD_whole_tree, is_phylogenetic=True)
        expected = [13, 17, 0]
        non_escaped_result = c(data_path=self.otu_table, tree_path=self.tree, \
            taxon_names = self.otu_names, sample_names=self.sample_names)

        otu_table = array([[2, 0, 0, 1], [1, 1, 1, 1], [0, 0, 0, 0]])
        sample_names = list('XYZ')
        otu_names = ['a', 'b', 'c', 'd_']
        tree_str = "((a:2,'b':3):2,(c:1,'d_':2):7);"
        tree_fp = get_tmp_filename(prefix='Alpha_div_tests', suffix='.tre')
        open(tree_fp, 'w').write(tree_str)
        self.files_to_remove.append(tree_fp)
        escaped_result  = c(data_path=otu_table, tree_path=tree_fp, \
            taxon_names = otu_names, sample_names=sample_names)

        self.assertEqual(non_escaped_result, escaped_result)
コード例 #7
0
    def test_call_phylogenetic_escaped_names(self):
        """AlphaDiversityCalc __call__ should call metric on phylo data
        and return correct values"""

        c = AlphaDiversityCalc(metric=PD_whole_tree, is_phylogenetic=True)
        expected = [13., 17., 0.]

        non_escaped_result = c(data_path=self.otu_table1_fp,
                               tree_path=self.tree1,
                               taxon_names=self.otu_table1.ObservationIds,
                               sample_names=self.otu_table1.SampleIds)

        escaped_result = c(data_path=self.otu_table2_fp,
                           tree_path=self.tree2,
                           taxon_names=self.otu_table2.ObservationIds,
                           sample_names=self.otu_table2.SampleIds)

        self.assertEqual(non_escaped_result, expected)
        self.assertEqual(escaped_result, expected)
        self.assertEqual(non_escaped_result, escaped_result)
コード例 #8
0
 def test_1sample(self):
     """ should work if only testing one sample as well"""
     c = AlphaDiversityCalc(observed_otus)
     self.assertEqual(c(data_path=self.single_sample_otu_table_fp), [2])
コード例 #9
0
 def test_call(self):
     """AlphaDiversityCalc __call__ should call metric on data
     and return correct result"""
     c = AlphaDiversityCalc(observed_otus)
     assert_almost_equal(c(data_path=self.otu_table1_fp), [2, 4, 0])
コード例 #10
0
 def test_init(self):
     """AlphaDiversity __init__ should store metric, name, params"""
     c = AlphaDiversityCalc(observed_otus)
     self.assertEqual(c.Metric, observed_otus)
     self.assertEqual(c.Params, {})
コード例 #11
0
 def test_1sample(self):
     """ should work if only testing one sample as well"""
     otu_table = array([[2, 0, 0, 1]])
     c = AlphaDiversityCalc(alph454.alph.observed_species)
     self.assertEqual(c(data_path=otu_table), [2])
コード例 #12
0
 def test_call(self):
     """AlphaDiversityCalc __call__ should call metric on data
     and return correct result"""
     c = AlphaDiversityCalc(observed_species)
     self.assertEqual(c(data_path=self.otu_table), [2, 4, 0])