1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
# Copyright (C) 2013 by Ben Morris (ben@bendmorris.com)
# Based on Bio.Nexus, copyright 2005-2008 by Frank Kauff & Cymon J. Cox
# and Bio.Phylo.Newick, copyright 2009 by Eric Talevich.
# All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""I/O function wrappers for the NeXML file format.
See: http://www.nexml.org
"""
from Bio._py3k import StringIO
from Bio.Phylo import NeXML
from xml.dom import minidom
import sys
from ._cdao_owl import cdao_elements, cdao_namespaces, resolve_uri
# For speed try to use cElementTree rather than ElementTree
try:
if (3, 0) <= sys.version_info[:2] <= (3, 1):
# Workaround for bug in python 3.0 and 3.1,
# see http://bugs.python.org/issue9257
from xml.etree import ElementTree as ElementTree
else:
from xml.etree import cElementTree as ElementTree
except ImportError:
from xml.etree import ElementTree as ElementTree
NAMESPACES = {
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xml': 'http://www.w3.org/XML/1998/namespace',
'nex': 'http://www.nexml.org/2009',
'xsd': 'http://www.w3.org/2001/XMLSchema#',
}
NAMESPACES.update(cdao_namespaces)
DEFAULT_NAMESPACE = NAMESPACES['nex']
VERSION = '0.9'
SCHEMA = 'http://www.nexml.org/2009/nexml/xsd/nexml.xsd'
try:
register_namespace = ElementTree.register_namespace
except AttributeError:
if not hasattr(ElementTree, '_namespace_map'):
# cElementTree needs the pure-Python xml.etree.ElementTree
from xml.etree import ElementTree as ET_py
ElementTree._namespace_map = ET_py._namespace_map
def register_namespace(prefix, uri):
ElementTree._namespace_map[uri] = prefix
for prefix, uri in NAMESPACES.items():
register_namespace(prefix, uri)
def qUri(s):
"""Given a prefixed URI, return the full URI."""
return resolve_uri(s, namespaces=NAMESPACES, xml_style=True)
def cdao_to_obo(s):
"""Optionally converts a CDAO-prefixed URI into an OBO-prefixed URI."""
return 'obo:%s' % cdao_elements[s[len('cdao:'):]]
def matches(s):
"""Check for matches in both CDAO and OBO namespaces."""
if s.startswith('cdao:'):
return (s, cdao_to_obo(s))
else:
return (s,)
class NeXMLError(Exception):
"""Exception raised when NeXML object construction cannot continue."""
pass
# ---------------------------------------------------------
# Public API
def parse(handle, **kwargs):
"""Iterate over the trees in a NeXML file handle.
:returns: generator of Bio.Phylo.NeXML.Tree objects.
"""
return Parser(handle).parse(**kwargs)
def write(trees, handle, plain=False, **kwargs):
"""Write a trees in NeXML format to the given file handle.
:returns: number of trees written.
"""
return Writer(trees).write(handle, plain=plain, **kwargs)
# ---------------------------------------------------------
# Input
class Parser(object):
"""Parse a NeXML tree given a file handle.
Based on the parser in `Bio.Nexus.Trees`.
"""
def __init__(self, handle):
self.handle = handle
@classmethod
def from_string(cls, treetext):
handle = StringIO(treetext)
return cls(handle)
def add_annotation(self, node_dict, meta_node):
if 'property' in meta_node.attrib:
prop = meta_node.attrib['property']
else:
prop = 'meta'
if prop in matches('cdao:has_Support_Value'):
node_dict['confidence'] = float(meta_node.text)
else:
node_dict[prop] = meta_node.text
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)
@classmethod
def _make_tree(cls, node, node_dict, children):
"""Traverse the tree creating a nested clade structure.
Return a NeXML.Clade, and calls itself recursively for each child,
traversing the entire tree and creating a nested structure of NeXML.Clade
objects.
"""
this_node = node_dict[node]
clade = NeXML.Clade(**this_node)
if node in children:
clade.clades = [cls._make_tree(child, node_dict, children)
for child in children[node]]
return clade
# ---------------------------------------------------------
# Output
class Writer(object):
"""Based on the writer in Bio.Nexus.Trees (str, to_string)."""
def __init__(self, trees):
self.trees = trees
self.node_counter = 0
self.edge_counter = 0
self.tree_counter = 0
def new_label(self, obj_type):
counter = '%s_counter' % obj_type
setattr(self, counter, getattr(self, counter) + 1)
return '%s%s' % (obj_type, getattr(self, counter))
def write(self, handle, cdao_to_obo=True, **kwargs):
"""Write this instance's trees to a file handle."""
self.cdao_to_obo = cdao_to_obo
# set XML namespaces
root_node = ElementTree.Element('nex:nexml')
root_node.set('version', VERSION)
root_node.set('xmlns', DEFAULT_NAMESPACE)
root_node.set('xsi:schemaLocation', SCHEMA)
for prefix, uri in NAMESPACES.items():
root_node.set('xmlns:%s' % prefix, uri)
otus = ElementTree.SubElement(root_node, 'otus',
**{'id': 'tax', 'label': 'RootTaxaBlock'})
# create trees
trees = ElementTree.SubElement(root_node, 'trees',
**{'id': 'Trees', 'label': 'TreesBlockFromXML', 'otus': 'tax'})
count = 0
tus = set()
for tree in self.trees:
this_tree = ElementTree.SubElement(trees, 'tree',
**{'id': self.new_label('tree')})
first_clade = tree.clade
tus.update(self._write_tree(first_clade, this_tree, rooted=tree.rooted))
count += 1
# create OTUs
for tu in tus:
otu = ElementTree.SubElement(otus, 'otu', **{'id': tu})
# write XML document to file handle
# xml_doc = ElementTree.ElementTree(root_node)
# xml_doc.write(handle,
# xml_declaration=True, encoding='utf-8',
# method='xml')
# use xml.dom.minodom for pretty printing
rough_string = ElementTree.tostring(root_node, 'utf-8')
reparsed = minidom.parseString(rough_string)
try:
handle.write(reparsed.toprettyxml(indent=" "))
except TypeError:
# for compatibility with Python 3
handle.write(bytes(reparsed.toprettyxml(indent=" "), 'utf8'))
return count
def _write_tree(self, clade, tree, parent=None, rooted=False):
"""Recursively process tree, adding nodes and edges to Tree object.
Returns a set of all OTUs encountered.
"""
tus = set()
convert_uri = cdao_to_obo if self.cdao_to_obo else (lambda s: s)
node_id = self.new_label('node')
clade.node_id = node_id
attrib = {'id': node_id, 'label': node_id}
root = rooted and parent is None
if root:
attrib['root'] = 'true'
if clade.name:
tus.add(clade.name)
attrib['otu'] = clade.name
node = ElementTree.SubElement(tree, 'node', **attrib)
if parent is not None:
edge_id = self.new_label('edge')
attrib = {
'id': edge_id, 'source': parent.node_id, 'target': node_id,
'length': str(clade.branch_length),
'typeof': convert_uri('cdao:Edge'),
}
if hasattr(clade, 'confidence') and clade.confidence is not None:
attrib.update({
'property': convert_uri('cdao:has_Support_Value'),
'datatype': 'xsd:float',
'content': '%1.2f' % clade.confidence,
})
node = ElementTree.SubElement(tree, 'edge', **attrib)
if not clade.is_terminal():
for new_clade in clade.clades:
tus.update(self._write_tree(new_clade, tree, parent=clade))
del clade.node_id
return tus
|