def _stash_rewrite_and_call(self, fname, test_cases): """Safely run a series of tests on a parsed and rewritten file. Specifically: Parse a file, rename the source file to a backup, rewrite the file from the parsed object, check the rewritten file with the given series of test functions, then restore the original by renaming the backup copy. Python 2.4 support: This would make more sense as a context manager that simply handles renaming and finally restoring the original. """ phx = PhyloXMLIO.read(fname) if os.path.exists(fname + "~"): os.remove(fname + "~") os.rename(fname, fname + "~") try: PhyloXMLIO.write(phx, fname) for cls, tests in test_cases: inst = cls("setUp") for test in tests: getattr(inst, test)() finally: # XXX not safe! if os.path.exists(fname): os.remove(fname) os.rename(fname + "~", fname)
def _rewrite_and_call(self, orig_fname, test_cases): """Parse, rewrite and retest a phyloXML example file.""" with open(orig_fname) as infile: phx = PhyloXMLIO.read(infile) with open(DUMMY, "w") as outfile: PhyloXMLIO.write(phx, outfile) for cls, tests in test_cases: inst = cls("setUp") for test in tests: getattr(inst, test)()
def test_dump_tags(self): """Count and confirm the number of tags in each example XML file.""" for source, count in izip( (EX_APAF, EX_BCL2, EX_PHYLO, unzip(EX_MOLLUSCA), # unzip(EX_METAZOA), unzip(EX_NCBI), ), (509, 1496, 289, 24311, 322367, 972830)): output = StringIO() PhyloXMLIO.dump_tags(source, output) output.seek(0) self.assertEquals(len(output.readlines()), count)
def _rewrite_and_call(self, orig_fname, test_cases): """Parse, rewrite and retest a phyloXML example file.""" infile = open(orig_fname, "rb") phx = PhyloXMLIO.read(infile) infile.close() outfile = open(DUMMY, "w+b") PhyloXMLIO.write(phx, outfile) outfile.close() for cls, tests in test_cases: inst = cls("setUp") for test in tests: getattr(inst, test)()
def _rewrite_and_call(self, orig_fname, test_cases): """Parse, rewrite and retest a phyloXML example file.""" infile = open(orig_fname, "r") phx = PhyloXMLIO.read(infile) infile.close() outfile = open(DUMMY, "w") PhyloXMLIO.write(phx, outfile) outfile.close() for cls, tests in test_cases: inst = cls("setUp") for test in tests: getattr(inst, test)()
def __init__(self, file): # Get an iterable context for XML parsing events #function actually copied from the biopython.phyloxml parser class recGeneTreeXMLParser.__init__(self, file) #context = iter(ElementTree.iterparse(file, events=('start', 'end'))) #event, root = next(context) #self.root = root #self.context = context if len(PhyloXMLIO._split_namespace(self.root.tag)) == 1: self.splittedRootTag = self.root.tag else: self.splittedRootTag = PhyloXMLIO._split_namespace( self.root.tag)[1]
def test_Distribution(self): """Instantiation of Distribution objects. Also checks Point type and safe Unicode handling (?). """ tree = list(PhyloXMLIO.parse(EX_PHYLO))[10] hirschweg = tree.clade[0, 0].distributions[0] nagoya = tree.clade[0, 1].distributions[0] eth_zurich = tree.clade[0, 2].distributions[0] san_diego = tree.clade[1].distributions[0] for dist, desc, lati, longi, alti in zip( (hirschweg, nagoya, eth_zurich, san_diego), ("Hirschweg, Winterthur, Switzerland", "Nagoya, Aichi, Japan", u"ETH Z\xfcrich", "San Diego"), (47.481277, 35.155904, 47.376334, 32.880933), (8.769303, 136.915863, 8.548108, -117.217543), (472, 10, 452, 104), ): self.assertTrue(isinstance(dist, PX.Distribution)) self.assertEqual(dist.desc, desc) point = dist.points[0] self.assertTrue(isinstance(point, PX.Point)) self.assertEqual(point.geodetic_datum, "WGS84") self.assertEqual(point.lat, lati) self.assertEqual(point.long, longi) self.assertEqual(point.alt, alti)
def test_Uri(self): """Instantiation of Uri objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[9] uri = tree.clade.taxonomies[0].uri self.assertTrue(isinstance(uri, PX.Uri)) self.assertEqual(uri.desc, "EMBL REPTILE DATABASE") self.assertEqual(uri.value, "http://www.embl-heidelberg.de/~uetz/families/Varanidae.html")
def test_Distribution(self): """Instantiation of Distribution objects. Also checks Point type and safe Unicode handling (?). """ tree = list(PhyloXMLIO.parse(EX_PHYLO))[10] hirschweg = tree.clade[0, 0].distributions[0] nagoya = tree.clade[0, 1].distributions[0] eth_zurich = tree.clade[0, 2].distributions[0] san_diego = tree.clade[1].distributions[0] for dist, desc, lati, longi, alti in zip( (hirschweg, nagoya, eth_zurich, san_diego), ("Hirschweg, Winterthur, Switzerland", "Nagoya, Aichi, Japan", u"ETH Z\xfcrich", "San Diego"), (47.481277, 35.155904, 47.376334, 32.880933), (8.769303, 136.915863, 8.548108, -117.217543), (472, 10, 452, 104)): self.assertTrue(isinstance(dist, PX.Distribution)) self.assertEqual(dist.desc, desc) point = dist.points[0] self.assertTrue(isinstance(point, PX.Point)) self.assertEqual(point.geodetic_datum, "WGS84") self.assertEqual(point.lat, lati) self.assertEqual(point.long, longi) self.assertEqual(point.alt, alti)
def test_DomainArchitecture(self): """Instantiation of DomainArchitecture objects. Also checks ProteinDomain type. """ # Because we short circult interation, must close handle explicitly handle = open(EX_APAF) tree = next(PhyloXMLIO.parse(handle)) handle.close() clade = tree.clade[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] darch = clade.sequences[0].domain_architecture self.assertTrue(isinstance(darch, PX.DomainArchitecture)) self.assertEqual(darch.length, 1249) for domain, start, end, conf, value in zip( darch.domains, (6, 109, 605, 647, 689, 733, 872, 993, 1075, 1117, 1168), (90, 414, 643, 685, 729, 771, 910, 1031, 1113, 1155, 1204), (7.0e-26, 7.2e-117, 2.4e-6, 1.1e-12, 2.4e-7, 4.7e-14, 2.5e-8, 4.6e-6, 6.3e-7, 1.4e-7, 0.3), ("CARD", "NB-ARC", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40")): self.assertTrue(isinstance(domain, PX.ProteinDomain)) self.assertEqual(domain.start + 1, start) self.assertEqual(domain.end, end) self.assertAlmostEqual(domain.confidence, conf) self.assertEqual(domain.value, value)
def test_Reference(self): """Instantiation of Reference objects.""" tree = PhyloXMLIO.parse(EX_DOLLO).next() reference = tree.clade[0,0,0,0,0,0].references[0] self.assert_(isinstance(reference, PX.Reference)) self.assertEqual(reference.doi, '10.1038/nature06614') self.assertEqual(reference.desc, None)
def test_DomainArchitecture(self): """Instantiation of DomainArchitecture objects. Also checks ProteinDomain type. """ # Because we short circult interation, must close handle explicitly handle = open(EX_APAF) tree = PhyloXMLIO.parse(handle).next() handle.close() clade = tree.clade[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] darch = clade.sequences[0].domain_architecture self.assertTrue(isinstance(darch, PX.DomainArchitecture)) self.assertEqual(darch.length, 1249) for domain, start, end, conf, value in zip( darch.domains, (6, 109, 605, 647, 689, 733, 872, 993, 1075, 1117, 1168), (90, 414, 643, 685, 729, 771, 910, 1031, 1113, 1155, 1204), (7.0e-26, 7.2e-117, 2.4e-6, 1.1e-12, 2.4e-7, 4.7e-14, 2.5e-8, 4.6e-6, 6.3e-7, 1.4e-7, 0.3), ("CARD", "NB-ARC", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40", "WD40"), ): self.assertTrue(isinstance(domain, PX.ProteinDomain)) self.assertEqual(domain.start + 1, start) self.assertEqual(domain.end, end) self.assertAlmostEqual(domain.confidence, conf) self.assertEqual(domain.value, value)
def test_Reference(self): """Instantiation of Reference objects.""" with open(EX_DOLLO) as handle: tree = next(PhyloXMLIO.parse(handle)) reference = tree.clade[0, 0, 0, 0, 0, 0].references[0] self.assertIsInstance(reference, PX.Reference) self.assertEqual(reference.doi, "10.1038/nature06614") self.assertIsNone(reference.desc)
def test_shape(self): trees = PhyloXMLIO.parse(source) for tree, shape_expect in zip(trees, shapes): self.assertEqual(len(tree.clade), len(shape_expect)) for clade, sub_expect in zip(tree.clade, shape_expect): self.assertEqual(len(clade), sub_expect[0]) for subclade, len_expect in zip(clade, sub_expect[1]): self.assertEqual(len(subclade), len_expect)
def test_Uri(self): """Instantiation of Uri objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[9] uri = tree.clade.taxonomies[0].uri self.assertTrue(isinstance(uri, PX.Uri)) self.assertEqual(uri.desc, 'EMBL REPTILE DATABASE') self.assertEqual(uri.value, 'http://www.embl-heidelberg.de/~uetz/families/Varanidae.html')
def test_Phyloxml(self): """Instantiation of Phyloxml objects.""" phx = PhyloXMLIO.read(EX_PHYLO) self.assertTrue(isinstance(phx, PX.Phyloxml)) for tree in phx: self.assertTrue(isinstance(tree, PX.Phylogeny)) for otr in phx.other: self.assertTrue(isinstance(otr, PX.Other))
def test_Annotation(self): """Instantiation of Annotation objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[3] ann = tree.clade[1].sequences[0].annotations[0] self.assertTrue(isinstance(ann, PX.Annotation)) self.assertEqual(ann.desc, "alcohol dehydrogenase") self.assertAlmostEqual(ann.confidence.value, 0.67) self.assertEqual(ann.confidence.type, "probability")
def wrapped(self, obj): #print tag, obj , attribs, subnodes elem = ElementTree.Element(tag, PhyloXMLIO._clean_attrib(obj, attribs)) for subn in subnodes: if isinstance(subn, basestring): # singular object: method and attribute names are the same if getattr(obj, subn) is not None: elem.append(getattr(self, subn)(getattr(obj, subn))) else: # list: singular method, pluralized attribute name method, plural = subn for item in getattr(obj, plural): elem.append(getattr(self, method)(item)) if has_text: elem.text = PhyloXMLIO._serialize(obj.value) #print "exit _handle_complex",tag, obj, type(elem) return elem
def test_CladeRelation(self): """Instantiation of CladeRelation objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[6] crel = tree.clade_relations[0] self.assertTrue(isinstance(crel, PX.CladeRelation)) self.assertEqual(crel.id_ref_0, "b") self.assertEqual(crel.id_ref_1, "c") self.assertEqual(crel.type, "network_connection")
def test_Events(self): """Instantiation of Events objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[4] event_s = tree.clade.events self.assertTrue(isinstance(event_s, PX.Events)) self.assertEqual(event_s.speciations, 1) event_d = tree.clade[0].events self.assertTrue(isinstance(event_d, PX.Events)) self.assertEqual(event_d.duplications, 1)
def test_Reference(self): """Instantiation of Reference objects.""" #Because we short circult interation, must close handle explicitly handle = open(EX_DOLLO) tree = PhyloXMLIO.parse(handle).next() handle.close() reference = tree.clade[0, 0, 0, 0, 0, 0].references[0] self.assertTrue(isinstance(reference, PX.Reference)) self.assertEqual(reference.doi, '10.1038/nature06614') self.assertEqual(reference.desc, None)
def test_Reference(self): """Instantiation of Reference objects.""" # Because we short circult interation, must close handle explicitly handle = open(EX_DOLLO) tree = PhyloXMLIO.parse(handle).next() handle.close() reference = tree.clade[0, 0, 0, 0, 0, 0].references[0] self.assertTrue(isinstance(reference, PX.Reference)) self.assertEqual(reference.doi, "10.1038/nature06614") self.assertEqual(reference.desc, None)
def test_SequenceRelation(self): """Instantiation of SequenceRelation objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[4] for seqrel, id_ref_0, id_ref_1, type in zip( tree.sequence_relations, ("x", "x", "y"), ("y", "z", "z"), ("paralogy", "orthology", "orthology") ): self.assertTrue(isinstance(seqrel, PX.SequenceRelation)) self.assertEqual(seqrel.id_ref_0, id_ref_0) self.assertEqual(seqrel.id_ref_1, id_ref_1) self.assertEqual(seqrel.type, type)
def other(self, obj): #print "other",obj, obj.attributes elem = ElementTree.Element(PhyloXMLIO._ns(obj.tag, obj.namespace), obj.attributes) elem.text = obj.value #print "other done , doing children" for child in obj.children: elem.append(self.other(child)) #print "exit other ", obj, obj.attributes return elem
def test_SequenceRelation(self): """Instantiation of SequenceRelation objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[4] for seqrel, id_ref_0, id_ref_1, type in zip( tree.sequence_relations, ("x", "x", "y"), ("y", "z", "z"), ("paralogy", "orthology", "orthology")): self.assertTrue(isinstance(seqrel, PX.SequenceRelation)) self.assertEqual(seqrel.id_ref_0, id_ref_0) self.assertEqual(seqrel.id_ref_1, id_ref_1) self.assertEqual(seqrel.type, type)
def test_Phylogeny(self): """Instantiation of Phylogeny objects.""" trees = list(PhyloXMLIO.parse(EX_PHYLO)) # Monitor lizards self.assertEqual(trees[9].name, "monitor lizards") self.assertEqual(trees[9].description, "a pylogeny of some monitor lizards") self.assertEqual(trees[9].rooted, True) # Network (unrooted) tree6 = trees[6] self.assertEqual(trees[6].name, "network, node B is connected to TWO nodes: AB and C") self.assertEqual(trees[6].rooted, False)
def test_SequenceRelation(self): """Instantiation of SequenceRelation objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[4] for seqrel, id_ref_0, id_ref_1, type in izip( tree.sequence_relations, ('x', 'x', 'y'), ('y', 'z', 'z'), ('paralogy', 'orthology', 'orthology')): self.assert_(isinstance(seqrel, PX.SequenceRelation)) self.assertEqual(seqrel.id_ref_0, id_ref_0) self.assertEqual(seqrel.id_ref_1, id_ref_1) self.assertEqual(seqrel.type, type)
def test_Property(self): """Instantiation of Property objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[8] for prop, id_ref, value in zip(tree.properties, ("id_a", "id_b", "id_c"), ("1200", "2300", "200")): self.assertTrue(isinstance(prop, PX.Property)) self.assertEqual(prop.id_ref, id_ref) self.assertEqual(prop.datatype, "xsd:integer") self.assertEqual(prop.ref, "NOAA:depth") self.assertEqual(prop.applies_to, "node") self.assertEqual(prop.unit, "METRIC:m") self.assertEqual(prop.value, value)
def test_Clade(self): """Instantiation of Clade objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[6] clade_ab, clade_c = tree.clade.clades clade_a, clade_b = clade_ab.clades for clade, id_source, name, blen in zip( (clade_ab, clade_a, clade_b, clade_c), ("ab", "a", "b", "c"), ("AB", "A", "B", "C"), (0.06, 0.102, 0.23, 0.4)): self.assertTrue(isinstance(clade, PX.Clade)) self.assertEqual(clade.id_source, id_source) self.assertEqual(clade.name, name) self.assertAlmostEqual(clade.branch_length, blen)
def prepare_species_tree(FILE_TREE_IN,FILE_TREE_OUT): clan_taxa = {} treexml = PhyloXMLIO.read(open(FILE_TREE_IN, 'r')) tree = treexml[0] treexml.attributes.pop('schemaLocation', None) # not supported by Forester tree.rooted = True leaf_dict = {} for node in tree.clade.find_clades(): if node.name: tax_id = node.name if tax_id.startswith('INT'): tax_id = tax_id[3:] taxon = PhyloXML.Taxonomy(id=PhyloXML.Id(tax_id, provider='ncbi_taxonomy')) try: taxon.scientific_name = find_tax_name(tax_id) except KeyError: taxon.scientific_name = '(NA)' node._set_taxonomy(taxon) node.name = None else: pass PhyloXMLIO.write(treexml, FILE_TREE_OUT)
def test_Phylogeny(self): """Instantiation of Phylogeny objects.""" trees = list(PhyloXMLIO.parse(EX_PHYLO)) # Monitor lizards self.assertEqual(trees[9].name, "monitor lizards") self.assertEqual(trees[9].description, "a pylogeny of some monitor lizards") self.assertEqual(trees[9].rooted, True) # Network (unrooted) self.assertEqual( trees[6].name, "network, node B is connected to TWO nodes: AB and C") self.assertEqual(trees[6].rooted, False)
def test_Other(self): """Instantiation of Other objects.""" phx = PhyloXMLIO.read(EX_PHYLO) otr = phx.other[0] self.assertTrue(isinstance(otr, PX.Other)) self.assertEqual(otr.tag, 'alignment') self.assertEqual(otr.namespace, 'http://example.org/align') self.assertEqual(len(otr.children), 3) for child, name, value in zip(otr, ('A', 'B', 'C'), ( 'acgtcgcggcccgtggaagtcctctcct', 'aggtcgcggcctgtggaagtcctctcct', 'taaatcgc--cccgtgg-agtccc-cct')): self.assertEqual(child.tag, 'seq') self.assertEqual(child.attributes['name'], name) self.assertEqual(child.value, value)
def test_Clade(self): """Instantiation of Clade objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[6] clade_ab, clade_c = tree.clade.clades clade_a, clade_b = clade_ab.clades for clade, id_source, name, blen in zip( (clade_ab, clade_a, clade_b, clade_c), ('ab', 'a', 'b', 'c'), ('AB', 'A', 'B', 'C'), (0.06, 0.102, 0.23, 0.4)): self.assertTrue(isinstance(clade, PX.Clade)) self.assertEqual(clade.id_source, id_source) self.assertEqual(clade.name, name) self.assertAlmostEqual(clade.branch_length, blen)
def test_BinaryCharacters(self): """Instantiation of BinaryCharacters objects.""" tree = PhyloXMLIO.parse(EX_DOLLO).next() bchars = tree.clade[0, 0].binary_characters self.assertTrue(isinstance(bchars, PX.BinaryCharacters)) self.assertEqual(bchars.type, "parsimony inferred") for name, count, value in ( ("gained", 2, ["Cofilin_ADF", "Gelsolin"]), ("lost", 0, []), ("present", 2, ["Cofilin_ADF", "Gelsolin"]), ("absent", None, []), ): self.assertEqual(getattr(bchars, name + "_count"), count) self.assertEqual(getattr(bchars, name), value)
def test_BinaryCharacters(self): """Instantiation of BinaryCharacters objects.""" tree = PhyloXMLIO.parse(EX_DOLLO).next() bchars = tree.clade[0,0].binary_characters self.assert_(isinstance(bchars, PX.BinaryCharacters)) self.assertEqual(bchars.type, 'parsimony inferred') for name, count, value in ( ('gained', 2, ['Cofilin_ADF', 'Gelsolin']), ('lost', 0, []), ('present', 2, ['Cofilin_ADF', 'Gelsolin']), ('absent', None, []), ): self.assertEqual(getattr(bchars, name+'_count'), count) self.assertEqual(getattr(bchars, name), value)
def test_BinaryCharacters(self): """Instantiation of BinaryCharacters objects.""" tree = PhyloXMLIO.parse(EX_DOLLO).next() bchars = tree.clade[0, 0].binary_characters self.assertTrue(isinstance(bchars, PX.BinaryCharacters)) self.assertEqual(bchars.type, 'parsimony inferred') for name, count, value in ( ('gained', 2, ['Cofilin_ADF', 'Gelsolin']), ('lost', 0, []), ('present', 2, ['Cofilin_ADF', 'Gelsolin']), ('absent', None, []), ): self.assertEqual(getattr(bchars, name + '_count'), count) self.assertEqual(getattr(bchars, name), value)
def prepare_species_tree(FILE_TREE_IN, FILE_TREE_OUT): clan_taxa = {} treexml = PhyloXMLIO.read(open(FILE_TREE_IN, 'r')) tree = treexml[0] treexml.attributes.pop('schemaLocation', None) # not supported by Forester tree.rooted = True leaf_dict = {} for node in tree.clade.find_clades(): if node.name: tax_id = node.name if tax_id.startswith('INT'): tax_id = tax_id[3:] taxon = PhyloXML.Taxonomy( id=PhyloXML.Id(tax_id, provider='ncbi_taxonomy')) try: taxon.scientific_name = find_tax_name(tax_id) except KeyError: taxon.scientific_name = '(NA)' node._set_taxonomy(taxon) node.name = None else: pass PhyloXMLIO.write(treexml, FILE_TREE_OUT)
def test_Confidence(self): """Instantiation of Confidence objects.""" tree = PhyloXMLIO.parse(EX_MADE).next() self.assertEqual(tree.name, "testing confidence") for conf, type, val in izip(tree.confidences, ("bootstrap", "probability"), (89.0, 0.71)): self.assert_(isinstance(conf, PX.Confidence)) self.assertEqual(conf.type, type) self.assertAlmostEqual(conf.value, val) self.assertEqual(tree.clade.name, "b") self.assertAlmostEqual(tree.clade.width, 0.2) for conf, val in izip(tree.clade[0].confidences, (0.9, 0.71)): self.assert_(isinstance(conf, PX.Confidence)) self.assertEqual(conf.type, "probability") self.assertAlmostEqual(conf.value, val)
def test_Clade(self): """Instantiation of Clade objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[6] clade_ab, clade_c = tree.clade.clades clade_a, clade_b = clade_ab.clades for clade, id_source, name, blen in izip( (clade_ab, clade_a, clade_b, clade_c), ("ab", "a", "b", "c"), ("AB", "A", "B", "C"), (0.06, 0.102, 0.23, 0.4), ): self.assert_(isinstance(clade, PX.Clade)) self.assertEqual(clade.id_source, id_source) self.assertEqual(clade.name, name) self.assertAlmostEqual(clade.branch_length, blen)
def test_BinaryCharacters(self): """Instantiation of BinaryCharacters objects.""" with open(EX_DOLLO) as handle: tree = next(PhyloXMLIO.parse(handle)) bchars = tree.clade[0, 0].binary_characters self.assertTrue(isinstance(bchars, PX.BinaryCharacters)) self.assertEqual(bchars.type, "parsimony inferred") for name, count, value in ( ("gained", 2, ["Cofilin_ADF", "Gelsolin"]), ("lost", 0, []), ("present", 2, ["Cofilin_ADF", "Gelsolin"]), ("absent", None, []), ): self.assertEqual(getattr(bchars, name + "_count"), count) self.assertEqual(getattr(bchars, name), value)
def test_Other(self): """Instantiation of Other objects.""" phx = PhyloXMLIO.read(EX_PHYLO) otr = phx.other[0] self.assertTrue(isinstance(otr, PX.Other)) self.assertEqual(otr.tag, "alignment") self.assertEqual(otr.namespace, "http://example.org/align") self.assertEqual(len(otr.children), 3) for child, name, value in zip( otr, ("A", "B", "C"), ("acgtcgcggcccgtggaagtcctctcct", "aggtcgcggcctgtggaagtcctctcct", "taaatcgc--cccgtgg-agtccc-cct")): self.assertEqual(child.tag, "seq") self.assertEqual(child.attributes["name"], name) self.assertEqual(child.value, value)
def test_Date(self): """Instantiation of Date objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[11] silurian = tree.clade[0, 0].date devonian = tree.clade[0, 1].date ediacaran = tree.clade[1].date for date, desc, val in zip( (silurian, devonian, ediacaran), # (10, 20, 30), # range is deprecated ("Silurian", "Devonian", "Ediacaran"), (425, 320, 600)): self.assertTrue(isinstance(date, PX.Date)) self.assertEqual(date.unit, "mya") # self.assertAlmostEqual(date.range, rang) self.assertEqual(date.desc, desc) self.assertAlmostEqual(date.value, val)
def test_Confidence(self): """Instantiation of Confidence objects.""" with open(EX_MADE) as handle: tree = next(PhyloXMLIO.parse(handle)) self.assertEqual(tree.name, "testing confidence") for conf, type, val in zip(tree.confidences, ("bootstrap", "probability"), (89.0, 0.71)): self.assertIsInstance(conf, PX.Confidence) self.assertEqual(conf.type, type) self.assertAlmostEqual(conf.value, val) self.assertEqual(tree.clade.name, "b") self.assertAlmostEqual(tree.clade.width, 0.2) for conf, val in zip(tree.clade[0].confidences, (0.9, 0.71)): self.assertIsInstance(conf, PX.Confidence) self.assertEqual(conf.type, "probability") self.assertAlmostEqual(conf.value, val)
def test_Other(self): """Instantiation of Other objects.""" phx = PhyloXMLIO.read(EX_PHYLO) otr = phx.other[0] self.assertTrue(isinstance(otr, PX.Other)) self.assertEqual(otr.tag, "alignment") self.assertEqual(otr.namespace, "http://example.org/align") self.assertEqual(len(otr.children), 3) for child, name, value in zip( otr, ("A", "B", "C"), ("acgtcgcggcccgtggaagtcctctcct", "aggtcgcggcctgtggaagtcctctcct", "taaatcgc--cccgtgg-agtccc-cct"), ): self.assertEqual(child.tag, "seq") self.assertEqual(child.attributes["name"], name) self.assertEqual(child.value, value)
def test_Date(self): """Instantiation of Date objects.""" tree = list(PhyloXMLIO.parse(EX_PHYLO))[11] silurian = tree.clade[0, 0].date devonian = tree.clade[0, 1].date ediacaran = tree.clade[1].date for date, desc, val in zip( (silurian, devonian, ediacaran), # (10, 20, 30), # range is deprecated ('Silurian', 'Devonian', 'Ediacaran'), (425, 320, 600)): self.assertTrue(isinstance(date, PX.Date)) self.assertEqual(date.unit, 'mya') # self.assertAlmostEqual(date.range, rang) self.assertEqual(date.desc, desc) self.assertAlmostEqual(date.value, val)
def test_Sequence(self): """Instantiation of Sequence objects. Also checks Accession and Annotation types. """ trees = list(PhyloXMLIO.parse(EX_PHYLO)) # Simple element with id_source seq0 = trees[4].clade[1].sequences[0] self.assertTrue(isinstance(seq0, PX.Sequence)) self.assertEqual(seq0.id_source, "z") self.assertEqual(seq0.symbol, "ADHX") self.assertEqual(seq0.accession.source, "ncbi") self.assertEqual(seq0.accession.value, "Q17335") self.assertEqual(seq0.name, "alcohol dehydrogenase") self.assertEqual(seq0.annotations[0].ref, "InterPro:IPR002085") # More complete elements seq1 = trees[5].clade[0, 0].sequences[0] seq2 = trees[5].clade[0, 1].sequences[0] seq3 = trees[5].clade[1].sequences[0] for seq, sym, acc, name, mol_seq, ann_refs in zip( (seq1, seq2, seq3), ("ADHX", "RT4I1", "ADHB"), ("P81431", "Q54II4", "Q04945"), ( "Alcohol dehydrogenase class-3", "Reticulon-4-interacting protein 1 homolog, " "mitochondrial precursor", "NADH-dependent butanol dehydrogenase B", ), ( "TDATGKPIKCMAAIAWEAKKPLSIEEVEVAPPKSGEVRIKILHSGVCHTD", "MKGILLNGYGESLDLLEYKTDLPVPKPIKSQVLIKIHSTSINPLDNVMRK", "MVDFEYSIPTRIFFGKDKINVLGRELKKYGSKVLIVYGGGSIKRNGIYDK", ), ( ("EC:1.1.1.1", "GO:0004022"), ("GO:0008270", "GO:0016491"), ("GO:0046872", "KEGG:Tetrachloroethene degradation"), ), ): self.assertTrue(isinstance(seq, PX.Sequence)) self.assertEqual(seq.symbol, sym) self.assertEqual(seq.accession.source, "UniProtKB") self.assertEqual(seq.accession.value, acc) self.assertEqual(seq.name, name) self.assertEqual(seq.mol_seq.value, mol_seq) self.assertEqual(seq.annotations[0].ref, ann_refs[0]) self.assertEqual(seq.annotations[1].ref, ann_refs[1])
def test_Sequence(self): """Instantiation of Sequence objects. Also checks Accession and Annotation types. """ trees = list(PhyloXMLIO.parse(EX_PHYLO)) # Simple element with id_source seq0 = trees[4].clade[1].sequences[0] self.assertIsInstance(seq0, PX.Sequence) self.assertEqual(seq0.id_source, "z") self.assertEqual(seq0.symbol, "ADHX") self.assertEqual(seq0.accession.source, "ncbi") self.assertEqual(seq0.accession.value, "Q17335") self.assertEqual(seq0.name, "alcohol dehydrogenase") self.assertEqual(seq0.annotations[0].ref, "InterPro:IPR002085") # More complete elements seq1 = trees[5].clade[0, 0].sequences[0] seq2 = trees[5].clade[0, 1].sequences[0] seq3 = trees[5].clade[1].sequences[0] for seq, sym, acc, name, mol_seq, ann_refs in zip( (seq1, seq2, seq3), ("ADHX", "RT4I1", "ADHB"), ("P81431", "Q54II4", "Q04945"), ( "Alcohol dehydrogenase class-3", "Reticulon-4-interacting protein 1 homolog, mitochondrial precursor", "NADH-dependent butanol dehydrogenase B", ), ( "TDATGKPIKCMAAIAWEAKKPLSIEEVEVAPPKSGEVRIKILHSGVCHTD", "MKGILLNGYGESLDLLEYKTDLPVPKPIKSQVLIKIHSTSINPLDNVMRK", "MVDFEYSIPTRIFFGKDKINVLGRELKKYGSKVLIVYGGGSIKRNGIYDK", ), ( ("EC:1.1.1.1", "GO:0004022"), ("GO:0008270", "GO:0016491"), ("GO:0046872", "KEGG:Tetrachloroethene degradation"), ), ): self.assertIsInstance(seq, PX.Sequence) self.assertEqual(seq.symbol, sym) self.assertEqual(seq.accession.source, "UniProtKB") self.assertEqual(seq.accession.value, acc) self.assertEqual(seq.name, name) self.assertEqual(seq.mol_seq.value, mol_seq) self.assertEqual(seq.annotations[0].ref, ann_refs[0]) self.assertEqual(seq.annotations[1].ref, ann_refs[1])
def test_Confidence(self): """Instantiation of Confidence objects.""" # Because we short circult interation, must close handle explicitly handle = open(EX_MADE) tree = PhyloXMLIO.parse(handle).next() handle.close() self.assertEqual(tree.name, "testing confidence") for conf, type, val in zip(tree.confidences, ("bootstrap", "probability"), (89.0, 0.71)): self.assertTrue(isinstance(conf, PX.Confidence)) self.assertEqual(conf.type, type) self.assertAlmostEqual(conf.value, val) self.assertEqual(tree.clade.name, "b") self.assertAlmostEqual(tree.clade.width, 0.2) for conf, val in zip(tree.clade[0].confidences, (0.9, 0.71)): self.assertTrue(isinstance(conf, PX.Confidence)) self.assertEqual(conf.type, "probability") self.assertAlmostEqual(conf.value, val)
def _parse_eventsRec(self, parent): """ parsing reconciliation events """ eventsRec = BPrecPhyloXML.eventsRec(None) for event, elem in self.context: namespace, tag = PhyloXMLIO._split_namespace(elem.tag) if event == 'end': if tag == EVENTSRECTAG: parent.clear() break if tag in self._events_recPhyloXML_tags: eventsRec.events.append( BPrecPhyloXML.RecEvent(tag, elem.attrib)) else: print 'non valid tag event ' + tag raise Exception('non valid tag event ' + tag) return eventsRec
def test_Confidence(self): """Instantiation of Confidence objects.""" # Because we short circult interation, must close handle explicitly handle = open(EX_MADE) tree = next(PhyloXMLIO.parse(handle)) handle.close() self.assertEqual(tree.name, "testing confidence") for conf, type, val in zip(tree.confidences, ("bootstrap", "probability"), (89.0, 0.71)): self.assertTrue(isinstance(conf, PX.Confidence)) self.assertEqual(conf.type, type) self.assertAlmostEqual(conf.value, val) self.assertEqual(tree.clade.name, "b") self.assertAlmostEqual(tree.clade.width, 0.2) for conf, val in zip(tree.clade[0].confidences, (0.9, 0.71)): self.assertTrue(isinstance(conf, PX.Confidence)) self.assertEqual(conf.type, "probability") self.assertAlmostEqual(conf.value, val)
def test_BinaryCharacters(self): """Instantiation of BinaryCharacters objects.""" # Because we short circult interation, must close handle explicitly # to avoid a ResourceWarning handle = open(EX_DOLLO) tree = next(PhyloXMLIO.parse(handle)) handle.close() bchars = tree.clade[0, 0].binary_characters self.assertTrue(isinstance(bchars, PX.BinaryCharacters)) self.assertEqual(bchars.type, 'parsimony inferred') for name, count, value in ( ('gained', 2, ['Cofilin_ADF', 'Gelsolin']), ('lost', 0, []), ('present', 2, ['Cofilin_ADF', 'Gelsolin']), ('absent', None, []), ): self.assertEqual(getattr(bchars, name + '_count'), count) self.assertEqual(getattr(bchars, name), value)
def test_BinaryCharacters(self): """Instantiation of BinaryCharacters objects.""" # Because we short circult interation, must close handle explicitly # to avoid a ResourceWarning handle = open(EX_DOLLO) tree = next(PhyloXMLIO.parse(handle)) handle.close() bchars = tree.clade[0, 0].binary_characters self.assertTrue(isinstance(bchars, PX.BinaryCharacters)) self.assertEqual(bchars.type, 'parsimony inferred') for name, count, value in ( ('gained', 2, ['Cofilin_ADF', 'Gelsolin']), ('lost', 0, []), ('present', 2, ['Cofilin_ADF', 'Gelsolin']), ('absent', None, []), ): self.assertEqual(getattr(bchars, name+'_count'), count) self.assertEqual(getattr(bchars, name), value)
def test_Polygon(self): """Instantiation of Polygon objects.""" tree = PhyloXMLIO.read(EX_MADE).phylogenies[1] self.assertEqual(tree.name, "testing polygon") dist = tree.clade[0].distributions[0] for poly in dist.polygons: self.assertTrue(isinstance(poly, PX.Polygon)) self.assertEqual(len(poly.points), 3) self.assertEqual(dist.polygons[0].points[0].alt_unit, "m") for point, lati, longi, alti in zip( chain(dist.polygons[0].points, dist.polygons[1].points), (47.481277, 35.155904, 47.376334, 40.481277, 25.155904, 47.376334), (8.769303, 136.915863, 8.548108, 8.769303, 136.915863, 7.548108), (472, 10, 452, 42, 10, 452), ): self.assertTrue(isinstance(point, PX.Point)) self.assertEqual(point.geodetic_datum, "WGS84") self.assertEqual(point.lat, lati) self.assertEqual(point.long, longi) self.assertEqual(point.alt, alti)