Skip to content

Commit

Permalink
Adds validation to other Unifracs (#106)
Browse files Browse the repository at this point in the history
* adds _validate_meta(), validates remaining functions

* typo and linting

* adds test for _validate_meta()

* fixes variable name issue with meta_validation test

* moves meta_validate guard down to allow other calls first

* uncouples tests from guard condition order in method
  • Loading branch information
ChrisKeefe authored May 13, 2020
1 parent e0b51ec commit 8ab40f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
19 changes: 17 additions & 2 deletions unifrac/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def _validate(table, phylogeny):
raise ValueError("The phylogeny does not appear to be newick")


def _validate_meta(tables, phylogenies):
for idx, (table, phylogeny) in enumerate(zip(tables, phylogenies)):
if not is_biom_v210(table):
raise ValueError(f"Table at position {idx} does not appear to be a"
" BIOM-Format v2.1")
if not is_newick(phylogeny):
raise ValueError(f"The phylogeny at position {idx} does not appear"
" to be newick")


def unweighted(table: str,
phylogeny: str,
threads: int = 1,
Expand Down Expand Up @@ -153,6 +163,7 @@ def weighted_normalized(table: str,
powerful beta diversity measure for comparing communities based on
phylogeny. BMC Bioinformatics 12:118 (2011).
"""
_validate(table, phylogeny)
return qsu.ssu(str(table), str(phylogeny), 'weighted_normalized',
variance_adjusted, 1.0, bypass_tips, threads)

Expand Down Expand Up @@ -208,6 +219,7 @@ def weighted_unnormalized(table: str,
powerful beta diversity measure for comparing communities based on
phylogeny. BMC Bioinformatics 12:118 (2011).
"""
_validate(table, phylogeny)
return qsu.ssu(str(table), str(phylogeny), 'weighted_unnormalized',
variance_adjusted, 1.0, bypass_tips, threads)

Expand Down Expand Up @@ -274,6 +286,7 @@ def generalized(table: str,
powerful beta diversity measure for comparing communities based on
phylogeny. BMC Bioinformatics 12:118 (2011).
"""
_validate(table, phylogeny)
if alpha == 1.0:
warn("alpha of 1.0 is weighted-normalized UniFrac. "
"Weighted-normalized is being used instead as it is more "
Expand Down Expand Up @@ -302,10 +315,10 @@ def meta(tables: tuple, phylogenies: tuple, weights: tuple = None,
Parameters
----------
tables : tuple of str
Filepaths to a BIOM-Format 2.1 files. This tuple is expected to be in
Filepaths to BIOM-Format 2.1 files. This tuple is expected to be in
index order with phylogenies.
phylogenies : tuple of str
Filepaths to a Newick formatted trees. This tuple is expected to be in
Filepaths to Newick formatted trees. This tuple is expected to be in
index order with tables.
weights : tuple of float, optional
The weight applied to each tree/table pair. This tuple is expected to
Expand Down Expand Up @@ -406,6 +419,8 @@ def meta(tables: tuple, phylogenies: tuple, weights: tuple = None,
"is set as 'generalized', the selected method is "
"'%s'." % method)

_validate_meta(tables, phylogenies)

kwargs = {'threads': threads,
'bypass_tips': bypass_tips,
'variance_adjusted': variance_adjusted}
Expand Down
47 changes: 32 additions & 15 deletions unifrac/tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
class StateUnifracTests(unittest.TestCase):
package = 'unifrac.tests'

def setUp(self):
super().setUp()
self.table1 = self.get_data_path('e1.biom')
self.table2 = self.get_data_path('e2.biom')
self.tree1 = self.get_data_path('t1.newick')
self.tree2 = self.get_data_path('t2.newick')
self.not_a_table = self.tree1
self.not_a_tree = self.table1

def get_data_path(self, filename):
# adapted from qiime2.plugin.testing.TestPluginBase
return pkg_resources.resource_filename(self.package,
'data/%s' % filename)

def test_meta_unifrac(self):
"""meta_unifrac should give correct result on sample trees"""
t1 = self.get_data_path('t1.newick')
t2 = self.get_data_path('t2.newick')
e1 = self.get_data_path('e1.biom')
e2 = self.get_data_path('e2.biom')

result = meta([e1, e2], [t1, t2],
result = meta([self.table1, self.table2], [self.tree1, self.tree2],
weights=[1, 1],
consolidation='skipping-missing-values',
method='unweighted')
Expand All @@ -47,43 +51,56 @@ def test_meta_unifrac(self):
def test_meta_unifrac_unbalanced(self):
with self.assertRaisesRegex(ValueError, ("Number of trees and tables "
"must be the same.")):
meta(('a', ), ('a', 'b'))
meta((self.table1, ), (self.tree1, self.tree2),
method='unweighted')

with self.assertRaisesRegex(ValueError, ("Number of trees and tables "
"must be the same.")):
meta(('a', 'b'), ('a', ))
meta((self.table1, self.table2), (self.tree1, ),
method='unweighted')

def test_meta_unifrac_unbalanced_weights(self):
with self.assertRaisesRegex(ValueError, "Number of weights does not "
"match number of trees and "
"tables."):
meta(('c', 'd'), ('a', 'b'), weights=(1, 2, 3))
meta((self.table1, self.table2), (self.tree1, self.tree2),
weights=(1, 2, 3), )

def test_meta_unifrac_missing(self):
with self.assertRaisesRegex(ValueError, "No trees specified."):
meta(('a', ), tuple())
meta((self.table1, ), tuple(), method='unweighted')

with self.assertRaisesRegex(ValueError, "No tables specified."):
meta(tuple(), ('a', ))
meta(tuple(), (self.tree1, ), method='unweighted')

def test_meta_validation(self):
with self.assertRaisesRegex(ValueError, "position 1.*not.*BIOM"):
meta((self.table1, self.not_a_table), (self.tree1, self.tree2),
method='unweighted')

with self.assertRaisesRegex(ValueError, "position 1.*not.*newick"):
meta((self.table1, self.table2), (self.tree1, self.not_a_tree),
method='unweighted')

def test_meta_unifrac_no_method(self):
with self.assertRaisesRegex(ValueError, "No method specified."):
meta(('a', ), ('b', ))
meta((self.table1, ), (self.tree1, ))

def test_meta_unifrac_bad_method(self):
with self.assertRaisesRegex(ValueError, r"Method \(bar\) "
"unrecognized."):
meta(('a', ), ('b', ), method='bar')
meta((self.table1, ), (self.tree1, ), method='bar')

def test_meta_unifrac_bad_consolidation(self):
with self.assertRaisesRegex(ValueError,
r"Consolidation \(foo\) unrecognized."):
meta(('a', ), ('b', ), method='unweighted', consolidation='foo')
meta((self.table1, ), (self.tree1, ), method='unweighted',
consolidation='foo')

def test_meta_unifrac_alpha_not_generalized(self):
with self.assertRaisesRegex(ValueError,
"The alpha parameter can"):
meta(('a', ), ('b', ), method='generalized',
meta((self.table1, ), (self.tree1, ), method='generalized',
alpha=1, consolidation='skipping_missing_matrices')


Expand Down

0 comments on commit 8ab40f0

Please sign in to comment.