Permalink
Browse files

Parse NeXML trees, write them out, and re-parse, then ensure trees ar…

…e identical.
  • Loading branch information...
1 parent beb1020 commit d149089aa40729ace409214c7117b0a4f2ddc522 @bendmorris bendmorris committed with etal Feb 13, 2013
Showing with 60 additions and 19 deletions.
  1. +60 −19 Tests/test_Phylo_NeXML.py
View
@@ -15,21 +15,21 @@
from Bio.Phylo import NeXML, NeXMLIO
# Example NeXML files
-example_files = (
- 'characters.xml',
- 'edgelabels.xml',
- 'meta_taxa.xml',
- 'meta_types.xml',
- 'nexml.xml',
- 'phenoscape.xml',
- 'sets.xml',
- 'taxa.xml',
- 'timetree.xml',
- 'tolweb.xml',
- 'treebase-record.xml',
- 'trees-uris.xml',
- 'trees.xml',
- )
+nexml_files = (
+ 'characters.xml',
+ 'edgelabels.xml',
+ 'meta_taxa.xml',
+ 'meta_types.xml',
+ 'nexml.xml',
+ 'phenoscape.xml',
+ 'sets.xml',
+ 'taxa.xml',
+ 'timetree.xml',
+ 'tolweb.xml',
+ 'treebase-record.xml',
+ 'trees-uris.xml',
+ 'trees.xml',
+ )
tree_counts = {
'taxa.xml': 0,
'timetree.xml': 38,
@@ -54,7 +54,7 @@ def _test_parse_factory(source):
The generated function extracts each phylogenetic tree using the parse()
function and counts the total number of trees extracted.
"""
- filename = 'NeXML/%s' % source
+ filename = os.path.join('NeXML/', source)
if source in tree_counts: count = tree_counts[source]
else: count = 1
@@ -64,16 +64,57 @@ def test_parse(self):
test_parse.__doc__ = "Parse the phylogenies in %s." % source
return test_parse
+
+def _test_write_factory(source):
+ """Tests for serialization of objects to NeXML format.
+
+ Modifies the globally defined filenames in order to run the other parser
+ tests on files (re)generated by NeXMLIO's own writer.
+ """
+ filename = os.path.join('NeXML/', source)
+ if source in tree_counts: count = tree_counts[source]
+ else: count = 1
+
+ def test_write(self):
+ """Parse, rewrite and retest an example file."""
+ infile = open(filename, 'rb')
+ t1 = NeXMLIO.Parser(infile).parse().next()
+ infile.close()
+ outfile = open(DUMMY, 'w+b')
+ NeXMLIO.write([t1], outfile)
+ outfile.close()
+
+ t2 = NeXMLIO.Parser(open(DUMMY, 'rb')).parse().next()
+
+ def assert_property(prop_name):
+ p1 = sorted([getattr(n, prop_name) for n in t1.get_terminals()])
+ p2 = sorted([getattr(n, prop_name) for n in t2.get_terminals()])
+ self.assertEqual(p1,p2)
+
+ for prop_name in ('name', 'node_id', 'branch_length', 'confidence'):
+ assert_property(prop_name)
+
+ test_write.__doc__ = "Parse the phylogenies in %s." % source
+ return test_write
class ParseTests(unittest.TestCase):
- """Tests for proper parsing of example phyloXML files."""
+ """Tests for proper parsing of example NeXML files."""
-for n, ex in enumerate(example_files):
+for n, ex in enumerate(nexml_files):
parse_test = _test_parse_factory(ex)
parse_test.__name__ = 'test_parse_%s' % n
setattr(ParseTests, parse_test.__name__, parse_test)
-
+
+
+class WriterTests(unittest.TestCase):
+ pass
+
+for n, ex in enumerate(nexml_files):
+ if ex in tree_counts and tree_counts[ex] > 0:
+ write_test = _test_write_factory(ex)
+ write_test.__name__ = 'test_write_%s' % n
+ setattr(WriterTests, write_test.__name__, write_test)
if __name__ == '__main__':

0 comments on commit d149089

Please sign in to comment.