Coverage for /home/deng/Projects/ete4/hackathon/ete4/ete4/phyloxml/_phyloxml_tree.py: 25%
81 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-03-21 09:19 +0100
« prev ^ index » next coverage.py v7.2.7, created at 2024-03-21 09:19 +0100
1"""
2This module implements the interoperability between Phylogeny and
3Clade attributes in the phyloXMl schema and the ETE Tree objects.
5The PhyloxmlTree class should be use as a substitute for base Clade
6and Phylogeny classes.
7"""
9import sys
10from ._phyloxml import Clade, Phylogeny, Confidence, Tag_pattern_
11from .. import PhyloTree
13class PhyloxmlTree(PhyloTree):
14 """PhyloTree object supporting phyloXML format."""
16 def __repr__(self):
17 return "PhyloXML ETE tree <%s>" %hex(hash(self))
19 def _get_dist(self):
20 v = self.phyloxml_clade.get_branch_length_attr()
21 if v is None:
22 v = self.phyloxml_clade.get_branch_length()
23 if v is None:
24 self._set_dist(self._dist)
25 v = self.phyloxml_clade.get_branch_length_attr()
26 return float(v)
28 def _set_dist(self, value):
29 try:
30 self.phyloxml_clade.set_branch_length(float(value))
31 self.phyloxml_clade.set_branch_length_attr(float(value))
32 except ValueError:
33 raise
35 def _get_support(self):
36 if len(self.phyloxml_clade.confidence) == 0:
37 _c = Confidence(valueOf_=1.0, type_="branch_support")
38 self.phyloxml_clade.add_confidence(_c)
39 return float(self.phyloxml_clade.confidence[0].valueOf_)
41 def _set_support(self, value):
42 self._get_support()
43 self.phyloxml_clade.confidence[0].valueOf_ = float(value)
45 def _get_name(self):
46 return self.phyloxml_clade.get_name()
48 def _set_name(self, value):
49 try:
50 self.phyloxml_clade.set_name(value)
51 except ValueError:
52 raise
54 def _get_children(self):
55 return self.phyloxml_clade.clade
57 dist = property(fget=_get_dist, fset=_set_dist)
58 support = property(fget=_get_support, fset=_set_support)
59 children = property(fget=_get_children)
60 name = property(fget=_get_name, fset=_set_name)
62 def __init__(self, phyloxml_clade=None, phyloxml_phylogeny=None, **kargs):
63 if not phyloxml_phylogeny:
64 self.phyloxml_phylogeny = Phylogeny()
65 else:
66 self.phyloxml_phylogeny = phyloxml_phylogeny
67 if not phyloxml_clade:
68 self.phyloxml_clade = Clade()
69 self.phyloxml_clade.set_branch_length(0.0)
70 self.phyloxml_clade.set_name("NoName")
71 #self.__support = Confidence(valueOf_=1.0, type_="branch_support")
72 #self.phyloxml_clade.add_confidence(self.__support)
73 else:
74 self.phyloxml_clade = phyloxml_clade
75 super(PhyloxmlTree, self).__init__(**kargs)
77 def build(self, node):
78 nodetype = Tag_pattern_.match(node.tag).groups()[-1]
79 if nodetype == 'phylogeny':
80 self.phyloxml_phylogeny.buildAttributes(node, node.attrib, [])
81 elif nodetype == 'clade':
82 if "branch_length" in node.attrib:
83 node.attrib["branch_length_attr"] = node.attrib["branch_length"]
84 self.phyloxml_clade.buildAttributes(node, node.attrib, [])
85 for child in node:
86 nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
87 self.buildChildren(child, node, nodeName_, nodetype=nodetype)
89 def buildChildren(self, child_, node, nodeName_, fromsubclass=False, nodetype=None):
90 if nodetype == 'phylogeny':
91 baseclass = self.phyloxml_phylogeny
92 if nodeName_ == 'clade':
93 self.build(child_)
94 else:
95 baseclass.buildChildren(child_, node, nodeName_)
96 elif nodetype == 'clade':
97 baseclass = self.phyloxml_clade
98 if nodeName_ == 'clade':
99 new_node = self.add_child()
100 new_node.build(child_)
101 else:
102 baseclass.buildChildren(child_, node, nodeName_)
104 def export(self, outfile=sys.stdout, level=0, namespace_='phy:', name_='Phylogeny', namespacedef_=''):
105 if not self.up:
106 self.phyloxml_phylogeny.clade = self.phyloxml_clade
107 self.phyloxml_clade.clade = self.children
108 self.phyloxml_phylogeny.export(outfile=outfile, level=level, name_=name_, namespacedef_=namespacedef_)
109 else:
110 self.phyloxml_clade.clade = self.children
111 self.phyloxml_clade.export(outfile=outfile, level=level, name_=name_, namespacedef_=namespacedef_)