def test_alpha_rarefaction_with_filtered_metadata_columns(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        # Empty column and numeric column should both be filtered.
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut', 'summer'],
                          'foo': [np.nan, np.nan, np.nan, 'bar'],
                          'bar': [42, 4.2, 99.9, 100.0]},
                         index=pd.Index(['S1', 'S2', 'S3', 'S4'], name='id')))
        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)

            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            with open(index_fp, 'r') as fh:
                contents = fh.read()
            self.assertTrue('observed_otus' in contents)
            self.assertTrue('shannon' in contents)
            self.assertTrue('didn\'t contain categorical data' in contents)
            self.assertTrue('consisted only of missing values:' in contents)
            self.assertTrue('<strong>bar, foo' in contents)

            metric_fp = os.path.join(output_dir, 'shannon-pet.jsonp')
            with open(metric_fp) as metric_fh:
                self.assertTrue('summer' not in metric_fh.read())
            self.assertFalse(
                os.path.exists(os.path.join(output_dir, 'shannon-foo.jsonp')))
            self.assertFalse(
                os.path.exists(os.path.join(output_dir, 'shannon-bar.jsonp')))
Пример #2
0
    def test_alpha_rarefaction_with_filtered_metadata_columns(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'], ['S1', 'S2', 'S3'])
        # Empty column and numeric column should both be filtered.
        md = qiime2.Metadata(
            pd.DataFrame(
                {
                    'pet': ['russ', 'milo', 'peanut', 'summer'],
                    'foo': [np.nan, np.nan, np.nan, 'bar'],
                    'bar': [42, 4.2, 99.9, 100.0]
                },
                index=pd.Index(['S1', 'S2', 'S3', 'S4'], name='id')))
        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)

            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            with open(index_fp, 'r') as fh:
                contents = fh.read()
            self.assertTrue('observed_features' in contents)
            self.assertTrue('shannon' in contents)
            self.assertTrue('didn\'t contain categorical data' in contents)
            self.assertTrue('consisted only of missing values:' in contents)
            self.assertTrue('<strong>bar, foo' in contents)

            metric_fp = os.path.join(output_dir, 'shannon-pet.jsonp')
            with open(metric_fp) as metric_fh:
                self.assertTrue('summer' not in metric_fh.read())
            self.assertFalse(
                os.path.exists(
                    os.path.join(output_dir, 'shannon_entropy-foo.jsonp')))
            self.assertFalse(
                os.path.exists(
                    os.path.join(output_dir, 'shannon_entropy-bar.jsonp')))
 def test_alpha_rarefaction_without_metadata(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         self.assertTrue('observed_otus' in open(index_fp).read())
         self.assertTrue('shannon' in open(index_fp).read())
Пример #4
0
 def test_alpha_rarefaction_without_metadata(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'], ['S1', 'S2', 'S3'])
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         with open(index_fp) as index_fh:
             index_content = index_fh.read()
         self.assertTrue('observed_features' in index_content)
         self.assertTrue('shannon' in index_content)
 def test_alpha_rarefaction_with_fully_filtered_metadata_columns(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     # All columns should both be filtered and raise an error
     md = qiime2.Metadata(
         pd.DataFrame({'foo': [np.nan, np.nan, np.nan, 'bar'],
                       'bar': [42, 4.2, 99.9, 100.0]},
                      index=pd.Index(['S1', 'S2', 'S3', 'S4'], name='id')))
     with tempfile.TemporaryDirectory() as output_dir:
         with self.assertRaisesRegex(ValueError, "non-categorical"):
             alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
 def test_alpha_rarefaction_with_fully_filtered_metadata_columns(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     # All columns should both be filtered and raise an error
     md = qiime2.Metadata(
         pd.DataFrame({'foo': [np.nan, np.nan, np.nan, 'bar'],
                       'bar': [42, 4.2, 99.9, 100.0]},
                      index=pd.Index(['S1', 'S2', 'S3', 'S4'], name='id')))
     with tempfile.TemporaryDirectory() as output_dir:
         with self.assertRaisesRegex(ValueError, "non-categorical"):
             alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
 def test_alpha_rarefaction_with_metadata(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     md = qiime2.Metadata(
         pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                      index=['S1', 'S2', 'S3']))
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         self.assertTrue('observed_otus' in open(index_fp).read())
         self.assertTrue('shannon' in open(index_fp).read())
    def test_alpha_rarefaction_with_phylogeny(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        p = skbio.TreeNode.read(io.StringIO(
            '((O1:0.25, O2:0.50):0.25, O3:0.75)root;'))

        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, phylogeny=p)
            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            self.assertTrue('observed_otus' in open(index_fp).read())
            self.assertTrue('shannon' in open(index_fp).read())
            self.assertTrue('faith_pd' in open(index_fp).read())
Пример #9
0
 def test_alpha_rarefaction_with_metadata(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'], ['S1', 'S2', 'S3'])
     md = qiime2.Metadata(
         pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                      index=pd.Index(['S1', 'S2', 'S3'], name='id')))
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         with open(index_fp) as index_fh:
             index_content = index_fh.read()
         self.assertTrue('observed_features' in index_content)
         self.assertTrue('shannon' in index_content)
    def test_alpha_rarefaction_with_phylogeny(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'], ['S1', 'S2', 'S3'])
        p = skbio.TreeNode.read(
            io.StringIO('((O1:0.25, O2:0.50):0.25, O3:0.75)root;'))

        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, phylogeny=p)
            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            with open(index_fp) as index_fh:
                index_content = index_fh.read()
            self.assertTrue('observed_otus' in index_content)
            self.assertTrue('shannon' in index_content)
            self.assertTrue('faith_pd' in index_content)
 def test_alpha_rarefaction_with_metric_set(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     metrics = set(['observed_otus', 'shannon', 'pielou_e'])
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, metrics=metrics,
                           max_depth=200)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         with open(index_fp) as index_fh:
             index_content = index_fh.read()
         self.assertTrue('observed_otus' in index_content)
         self.assertTrue('shannon' in index_content)
         self.assertTrue('pielou_e' in index_content)
Пример #12
0
 def test_alpha_rarefaction_with_metadata_column_with_spaces(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'], ['S1', 'S2', 'S3'])
     md = qiime2.Metadata(
         pd.DataFrame({'pet name': ['russ', 'milo', 'peanut']},
                      index=pd.Index(['S1', 'S2', 'S3'], name='id')))
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         with open(index_fp) as index_fh:
             self.assertTrue('pet%2520name' in index_fh.read())
         jsonp_fp = os.path.join(output_dir, 'shannon-pet%20name.jsonp')
         self.assertTrue(os.path.exists(jsonp_fp))
         with open(jsonp_fp) as jsonp_fh:
             self.assertTrue('pet name' in jsonp_fh.read())
 def test_alpha_rarefaction_with_metadata_column_with_spaces(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     md = qiime2.Metadata(
         pd.DataFrame({'pet name': ['russ', 'milo', 'peanut']},
                      index=pd.Index(['S1', 'S2', 'S3'], name='id')))
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         with open(index_fp) as index_fh:
             self.assertTrue('pet%2520name' in index_fh.read())
         jsonp_fp = os.path.join(output_dir, 'shannon-pet%20name.jsonp')
         self.assertTrue(os.path.exists(jsonp_fp))
         with open(jsonp_fp) as jsonp_fh:
             self.assertTrue('pet name' in jsonp_fh.read())
 def test_alpha_rarefaction_with_superset_metadata(self):
     t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     md = qiime2.Metadata(
         pd.DataFrame({'pet': ['russ', 'milo', 'peanut', 'summer']},
                      index=pd.Index(['S1', 'S2', 'S3', 'S4'], name='id')))
     with tempfile.TemporaryDirectory() as output_dir:
         alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)
         index_fp = os.path.join(output_dir, 'index.html')
         self.assertTrue(os.path.exists(index_fp))
         with open(index_fp) as index_fh:
             index_content = index_fh.read()
         self.assertTrue('observed_otus' in index_content)
         self.assertTrue('shannon' in index_content)
         metric_fp = os.path.join(output_dir, 'shannon-pet.jsonp')
         with open(metric_fp) as metric_fh:
             self.assertTrue('summer' not in metric_fh.read())
    def test_alpha_rarefaction_with_phylogeny_and_metadata(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        p = skbio.TreeNode.read(io.StringIO(
            '((O1:0.25, O2:0.50):0.25, O3:0.75)root;'))
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                         index=['S1', 'S2', 'S3']))

        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, phylogeny=p,
                              metadata=md)
            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            self.assertTrue('observed_otus' in open(index_fp).read())
            self.assertTrue('shannon' in open(index_fp).read())
            self.assertTrue('faith_pd' in open(index_fp).read())
    def test_alpha_rarefaction_with_phylogeny_and_metadata(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        p = self._to_newick(skbio.TreeNode.read(io.StringIO(
            '((O1:0.25, O2:0.50):0.25, O3:0.75)root;')))
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                         index=pd.Index(['S1', 'S2', 'S3'], name='id')))

        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, phylogeny=p,
                              metadata=md)
            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            with open(index_fp) as index_fh:
                index_content = index_fh.read()
            self.assertTrue('observed_features' in index_content)
            self.assertTrue('shannon' in index_content)
            self.assertTrue('faith_pd' in index_content)
    def test_alpha_rarefaction_with_empty_column_in_metadata(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut', 'summer'],
                          'foo': [np.nan, np.nan, np.nan, 'bar']},
                         index=['S1', 'S2', 'S3', 'S4']))
        with tempfile.TemporaryDirectory() as output_dir:
            alpha_rarefaction(output_dir, t, max_depth=200, metadata=md)

            index_fp = os.path.join(output_dir, 'index.html')
            self.assertTrue(os.path.exists(index_fp))
            with open(index_fp, 'r') as fh:
                contents = fh.read()

            self.assertTrue('observed_otus' in contents)
            self.assertTrue('shannon' in contents)
            self.assertTrue('did not contain any values:' in contents)

            metric_fp = os.path.join(output_dir, 'shannon-pet.jsonp')
            self.assertTrue('summer' not in open(metric_fp).read())
            self.assertFalse(
                os.path.exists(os.path.join(output_dir, 'shannon-foo.jsonp')))
    def test_invalid_invocations(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                         index=pd.Index(['S1', 'S2', 'S3'], name='id')))

        empty_table = biom.Table(np.array([]), [], [])

        bad_metadata = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'summer']},
                         index=pd.Index(['S1', 'S2', 'S4'], name='id')))

        with tempfile.TemporaryDirectory() as output_dir:
            with self.assertRaisesRegex(ValueError, 'must be greater'):
                alpha_rarefaction(output_dir, t, min_depth=200, max_depth=1,
                                  metadata=md)

            with self.assertRaisesRegex(ValueError, 'phylogeny was not'):
                alpha_rarefaction(output_dir, t, max_depth=200,
                                  metadata=md, metrics=set(['faith_pd']))

            with self.assertRaisesRegex(ValueError, 'Unknown metric: pole'):
                alpha_rarefaction(output_dir, t, max_depth=200,
                                  metadata=md, metrics=set(['pole-position']))

            with self.assertRaisesRegex(ValueError, 'max_depth'):
                alpha_rarefaction(output_dir, t, max_depth=1000)

            with self.assertRaisesRegex(ValueError, 'steps'):
                alpha_rarefaction(output_dir, t, max_depth=2)

            with self.assertRaisesRegex(ValueError, 'empty'):
                alpha_rarefaction(output_dir, empty_table, max_depth=200)

            with self.assertRaisesRegex(ValueError, 'not present.*S3'):
                alpha_rarefaction(output_dir, t, metadata=bad_metadata,
                                  max_depth=200)

            with self.assertRaisesRegex(ValueError, 'empty set'):
                alpha_rarefaction(output_dir, t, max_depth=200,
                                  metadata=md, metrics=set())
    def test_invalid_invocations(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'],
                       ['S1', 'S2', 'S3'])
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                         index=['S1', 'S2', 'S3']))

        empty_table = biom.Table(np.array([]), [], [])

        bad_metadata = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'summer']},
                         index=['S1', 'S2', 'S4']))

        with tempfile.TemporaryDirectory() as output_dir:
            with self.assertRaisesRegex(ValueError, 'must be greater'):
                alpha_rarefaction(output_dir, t, min_depth=200, max_depth=1,
                                  metadata=md)

            with self.assertRaisesRegex(ValueError, 'phylogeny was not'):
                alpha_rarefaction(output_dir, t, max_depth=200,
                                  metadata=md, metrics=set(['faith_pd']))

            with self.assertRaisesRegex(ValueError, 'Unknown metric: pole'):
                alpha_rarefaction(output_dir, t, max_depth=200,
                                  metadata=md, metrics=set(['pole-position']))

            with self.assertRaisesRegex(ValueError, 'max_depth'):
                alpha_rarefaction(output_dir, t, max_depth=1000)

            with self.assertRaisesRegex(ValueError, 'steps'):
                alpha_rarefaction(output_dir, t, max_depth=2)

            with self.assertRaisesRegex(ValueError, 'empty'):
                alpha_rarefaction(output_dir, empty_table, max_depth=200)

            with self.assertRaisesRegex(ValueError, 'Missing'):
                alpha_rarefaction(output_dir, t, metadata=bad_metadata,
                                  max_depth=200)

            with self.assertRaisesRegex(ValueError, 'empty set'):
                alpha_rarefaction(output_dir, t, max_depth=200,
                                  metadata=md, metrics=set())

        def test_alpha_rarefaction_with_metric_set(self):
            t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                           ['O1', 'O2'],
                           ['S1', 'S2', 'S3'])
            metrics = set(['observed_otus', 'shannon', 'pielou_e'])
            with tempfile.TemporaryDirectory() as output_dir:
                alpha_rarefaction(output_dir, t, metrics=metrics,
                                  max_depth=200)
                index_fp = os.path.join(output_dir, 'index.html')
                self.assertTrue(os.path.exists(index_fp))
                self.assertTrue('observed_otus' in open(index_fp).read())
                self.assertTrue('shannon' in open(index_fp).read())
                self.assertTrue('pielou_e' in open(index_fp).read())
Пример #20
0
    def test_invalid_invocations(self):
        t = biom.Table(np.array([[100, 111, 113], [111, 111, 112]]),
                       ['O1', 'O2'], ['S1', 'S2', 'S3'])
        md = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'peanut']},
                         index=pd.Index(['S1', 'S2', 'S3'], name='id')))

        empty_table = biom.Table(np.array([]), [], [])

        bad_metadata = qiime2.Metadata(
            pd.DataFrame({'pet': ['russ', 'milo', 'summer']},
                         index=pd.Index(['S1', 'S2', 'S4'], name='id')))

        with tempfile.TemporaryDirectory() as output_dir:
            with self.assertRaisesRegex(ValueError, 'must be greater'):
                alpha_rarefaction(output_dir,
                                  t,
                                  min_depth=200,
                                  max_depth=1,
                                  metadata=md)

            with self.assertRaisesRegex(ValueError, 'phylogeny was not'):
                alpha_rarefaction(output_dir,
                                  t,
                                  max_depth=200,
                                  metadata=md,
                                  metrics=set(['faith_pd']))

            with self.assertRaisesRegex(TypeError, 'pole.*incompatible'):
                alpha_rarefaction(output_dir,
                                  t,
                                  max_depth=200,
                                  metadata=md,
                                  metrics=set(['pole-position']))

            with self.assertRaisesRegex(ValueError, 'max_depth'):
                alpha_rarefaction(output_dir, t, max_depth=1000)

            with self.assertRaisesRegex(ValueError, 'steps'):
                alpha_rarefaction(output_dir, t, max_depth=2)

            with self.assertRaisesRegex(ValueError, 'empty'):
                alpha_rarefaction(output_dir, empty_table, max_depth=200)

            with self.assertRaisesRegex(ValueError, 'not present.*S3'):
                alpha_rarefaction(output_dir,
                                  t,
                                  metadata=bad_metadata,
                                  max_depth=200)

            with self.assertRaisesRegex(ValueError, 'empty set'):
                alpha_rarefaction(output_dir,
                                  t,
                                  max_depth=200,
                                  metadata=md,
                                  metrics=set())