def parse(self, values_are_confidence=False, rooted=False): """Parse the text stream this object was initialized with.""" nexml_doc = ElementTree.iterparse(self.handle, events=('end', )) for event, node in nexml_doc: if node.tag == qUri('nex:tree'): node_dict = {} node_children = {} root = None child_tags = node.getchildren() nodes = [] edges = [] for child in child_tags: if child.tag == qUri('nex:node'): nodes.append(child) if child.tag == qUri('nex:edge'): edges.append(child) for node in nodes: node_id = node.attrib['id'] this_node = node_dict[node_id] = {} if 'otu' in node.attrib and node.attrib['otu']: this_node['name'] = node.attrib['otu'] if 'root' in node.attrib and node.attrib['root'] == 'true': root = node_id for child in node.getchildren(): if child.tag == qUri('nex:meta'): self.add_annotation(node_dict[node_id], child) srcs = set() tars = set() for edge in edges: src, tar = edge.attrib['source'], edge.attrib['target'] srcs.add(src) tars.add(tar) if src not in node_children: node_children[src] = set() node_children[src].add(tar) if 'length' in edge.attrib: node_dict[tar]['branch_length'] = float( edge.attrib['length']) if 'property' in edge.attrib and edge.attrib[ 'property'] in matches('cdao:has_Support_Value'): node_dict[tar]['confidence'] = float( edge.attrib['content']) for child in edge.getchildren(): if child.tag == qUri('nex:meta'): self.add_annotation(node_dict[tar], child) if root is None: # if no root specified, start the recursive tree creation function # with the first node that's not a child of any other nodes rooted = False possible_roots = (node.attrib['id'] for node in nodes if node.attrib['id'] in srcs and node.attrib['id'] not in tars) root = next(possible_roots) else: rooted = True yield NeXML.Tree(root=self._make_tree(root, node_dict, node_children), rooted=rooted)
def parse(self, values_are_confidence=False, rooted=False): """Parse the text stream this object was initialized with.""" nexml_doc = ElementTree.iterparse(self.handle, events=("end",)) for event, node in nexml_doc: if node.tag == qUri("nex:tree"): node_dict = {} node_children = {} root = None child_tags = node.getchildren() nodes = [] edges = [] for child in child_tags: if child.tag == qUri("nex:node"): nodes.append(child) if child.tag == qUri("nex:edge"): edges.append(child) for node in nodes: node_id = node.attrib["id"] this_node = node_dict[node_id] = {} if "otu" in node.attrib and node.attrib["otu"]: this_node["name"] = node.attrib["otu"] if "root" in node.attrib and node.attrib["root"] == "true": root = node_id for child in node.getchildren(): if child.tag == qUri("nex:meta"): self.add_annotation(node_dict[node_id], child) srcs = set() tars = set() for edge in edges: src, tar = edge.attrib["source"], edge.attrib["target"] srcs.add(src) tars.add(tar) if src not in node_children: node_children[src] = set() node_children[src].add(tar) if "length" in edge.attrib: node_dict[tar]["branch_length"] = float(edge.attrib["length"]) if "property" in edge.attrib and edge.attrib["property"] in matches("cdao:has_Support_Value"): node_dict[tar]["confidence"] = float(edge.attrib["content"]) for child in edge.getchildren(): if child.tag == qUri("nex:meta"): self.add_annotation(node_dict[tar], child) if root is None: # if no root specified, start the recursive tree creation function # with the first node that's not a child of any other nodes rooted = False possible_roots = (node.attrib["id"] for node in nodes if node.attrib["id"] in srcs and node.attrib["id"] not in tars) root = next(possible_roots) else: rooted = True yield NeXML.Tree(root=self._make_tree(root, node_dict, node_children), rooted=rooted)