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
|
#! /usr/bin/env python
##############################################################################
## DendroPy Phylogenetic Computing Library.
##
## Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
## All rights reserved.
##
## See "LICENSE.rst" for terms and conditions of usage.
##
## If you use this work or any portion thereof in published work,
## please cite it as:
##
## Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
## for phylogenetic computing. Bioinformatics 26: 1569-1571.
##
##############################################################################
"""
Wrappers for interacting with the ETE library.
"""
import tempfile
import re
from dendropy.utility import messaging
_LOG = messaging.get_logger(__name__)
import dendropy
DENDROPY_ETE_INTEROPERABILITY = False
try:
import ete2
DENDROPY_ETE_INTEROPERABILITY = True
except ImportError:
_LOG.warn("ete2 not installed: ETE interoperability not available")
else:
def as_ete_object(o):
if isinstance(o, ete2.Tree):
return o
elif isinstance(o, dendropy.Tree) or isinstance(o, dendropy.Node):
s = o.as_newick_string() + ";"
# _LOG.debug(s)
return ete2.Tree(s)
elif isinstance(o, list) or isinstance(o, dendropy.TreeList):
return [as_ete_object(t) for t in o]
else:
raise ValueError("Object of type '%s' does not have a native ete2 representation" % type(o))
def as_dendropy_object(o, taxon_set=None):
if isinstance(o, dendropy.Tree) or isinstance(o, dendropy.Node) or isinstance(o, dendropy.TreeList):
return o
elif isinstance(o, ete2.Tree):
s = o.write()
# _LOG.debug(s)
return dendropy.Tree.get_from_string(s, 'newick', taxon_set=taxon_set)
elif isinstance(o, list) or isinstance(o, dendropy.TreeList):
return dendropy.TreeList([as_dendropy_object(t, taxon_set=taxon_set) for t in o], taxon_set=taxon_set)
else:
raise ValueError("Object of type '%s' does not have a DendroPy representation" % type(o))
def show(o):
if not isinstance(o, dendropy.Tree) \
and not isinstance(o, dendropy.Node)\
and not isinstance(o, ete2.Tree):
raise ValueError("Object of type '%s' cannot be rendered by ETE2" % type(o))
ete_o = as_ete_object(o)
ete_o.show()
|