index
int64 0
731k
| package
stringlengths 2
98
⌀ | name
stringlengths 1
76
| docstring
stringlengths 0
281k
⌀ | code
stringlengths 4
1.07M
⌀ | signature
stringlengths 2
42.8k
⌀ |
---|---|---|---|---|---|
720,244 |
ete3.clustering.clustertree
|
__init__
| null |
def __init__(self, newick = None, text_array = None, \
fdist=clustvalidation.default_dist):
# Default dist is spearman_dist when scipy module is loaded
# otherwise, it is set to euclidean_dist.
# Initialize basic tree features and loads the newick (if any)
TreeNode.__init__(self, newick)
self._fdist = None
self._silhouette = None
self._intercluster_dist = None
self._intracluster_dist = None
self._profile = None
self._std_profile = None
# Cluster especific features
self.features.add("intercluster_dist")
self.features.add("intracluster_dist")
self.features.add("silhouette")
self.features.add("profile")
self.features.add("deviation")
# Initialize tree with array data
if text_array:
self.link_to_arraytable(text_array)
if newick:
self.set_distance_function(fdist)
|
(self, newick=None, text_array=None, fdist=<function spearman_dist at 0x7ff39b713d90>)
|
720,245 |
ete3.coretype.tree
|
__iter__
|
Iterator over leaf nodes
|
def __iter__(self):
""" Iterator over leaf nodes"""
return self.iter_leaves()
|
(self)
|
720,246 |
ete3.coretype.tree
|
__len__
|
Node len returns number of children.
|
def __len__(self):
"""Node len returns number of children."""
return len(self.get_leaves())
|
(self)
|
720,248 |
ete3.clustering.clustertree
|
__repr__
| null |
def __repr__(self):
return "ClusterTree node (%s)" %hex(self.__hash__())
|
(self)
|
720,249 |
ete3.coretype.tree
|
__str__
|
Print tree in newick format.
|
def __str__(self):
""" Print tree in newick format. """
return self.get_ascii(compact=DEFAULT_COMPACT, \
show_internal=DEFAULT_SHOWINTERNAL)
|
(self)
|
720,250 |
ete3.coretype.tree
|
_asciiArt
|
Returns the ASCII representation of the tree.
Code based on the PyCogent GPL project.
|
def _asciiArt(self, char1='-', show_internal=True, compact=False, attributes=None):
"""
Returns the ASCII representation of the tree.
Code based on the PyCogent GPL project.
"""
if not attributes:
attributes = ["name"]
node_name = ', '.join(map(str, [getattr(self, v) for v in attributes if hasattr(self, v)]))
LEN = max(3, len(node_name) if not self.children or show_internal else 3)
PAD = ' ' * LEN
PA = ' ' * (LEN-1)
if not self.is_leaf():
mids = []
result = []
for c in self.children:
if len(self.children) == 1:
char2 = '/'
elif c is self.children[0]:
char2 = '/'
elif c is self.children[-1]:
char2 = '\\'
else:
char2 = '-'
(clines, mid) = c._asciiArt(char2, show_internal, compact, attributes)
mids.append(mid+len(result))
result.extend(clines)
if not compact:
result.append('')
if not compact:
result.pop()
(lo, hi, end) = (mids[0], mids[-1], len(result))
prefixes = [PAD] * (lo+1) + [PA+'|'] * (hi-lo-1) + [PAD] * (end-hi)
mid = int((lo + hi) / 2)
prefixes[mid] = char1 + '-'*(LEN-2) + prefixes[mid][-1]
result = [p+l for (p,l) in zip(prefixes, result)]
if show_internal:
stem = result[mid]
result[mid] = stem[0] + node_name + stem[len(node_name)+1:]
return (result, mid)
else:
return ([char1 + '-' + node_name], 0)
|
(self, char1='-', show_internal=True, compact=False, attributes=None)
|
720,251 |
ete3.clustering.clustertree
|
_calculate_avg_profile
|
This internal function updates the mean profile
associated to an internal node.
|
def _calculate_avg_profile(self):
""" This internal function updates the mean profile
associated to an internal node. """
# Updates internal values
self._profile, self._std_profile = clustvalidation.get_avg_profile(self)
|
(self)
|
720,252 |
ete3.coretype.tree
|
_diff
|
.. versionadded:: 2.3
Show or return the difference between two tree topologies.
:param [raw|table|topology|diffs|diffs_tab] output: Output type
|
def _diff(self, t2, output='topology', attr_t1='name', attr_t2='name', color=True):
"""
.. versionadded:: 2.3
Show or return the difference between two tree topologies.
:param [raw|table|topology|diffs|diffs_tab] output: Output type
"""
from ..tools import ete_diff
difftable = ete_diff.treediff(self, t2, attr1=attr_t1, attr2=attr_t2)
if output == "topology":
ete_diff.show_difftable_topo(difftable, attr_t1, attr_t2, usecolor=color)
elif output == "diffs":
ete_diff.show_difftable(difftable)
elif output == "diffs_tab":
ete_diff.show_difftable_tab(difftable)
elif output == 'table':
rf, rf_max, _, _, _, _, _ = self.robinson_foulds(t2, attr_t1=attr_t1, attr_t2=attr_t2)[:2]
ete_diff.show_difftable_summary(difftable, rf, rf_max)
else:
return difftable
|
(self, t2, output='topology', attr_t1='name', attr_t2='name', color=True)
|
720,253 |
ete3.coretype.tree
|
_get_children
| null |
def _get_children(self):
return self._children
|
(self)
|
720,254 |
ete3.coretype.tree
|
_get_dist
| null |
def _get_dist(self):
return self._dist
|
(self)
|
720,255 |
ete3.coretype.tree
|
_get_face_areas
| null |
def _get_face_areas(self):
if not hasattr(self, "_faces"):
self._faces = _FaceAreas()
return self._faces
|
(self)
|
720,256 |
ete3.coretype.tree
|
_get_farthest_and_closest_leaves
| null |
def _get_farthest_and_closest_leaves(self, topology_only=False, is_leaf_fn=None):
# if called from a leaf node, no necessary to compute
if (is_leaf_fn and is_leaf_fn(self)) or self.is_leaf():
return self, 0.0, self, 0.0
min_dist = None
min_node = None
max_dist = None
max_node = None
d = 0.0
for post, n in self.iter_prepostorder(is_leaf_fn=is_leaf_fn):
if n is self:
continue
if post:
d -= n.dist if not topology_only else 1.0
else:
if (is_leaf_fn and is_leaf_fn(n)) or n.is_leaf():
total_d = d + n.dist if not topology_only else d
if min_dist is None or total_d < min_dist:
min_dist = total_d
min_node = n
if max_dist is None or total_d > max_dist:
max_dist = total_d
max_node = n
else:
d += n.dist if not topology_only else 1.0
return min_node, min_dist, max_node, max_dist
|
(self, topology_only=False, is_leaf_fn=None)
|
720,257 |
ete3.clustering.clustertree
|
_get_inter
| null |
def _get_inter(self):
if self._silhouette is None:
self.get_silhouette()
return self._intercluster_dist
|
(self)
|
720,258 |
ete3.clustering.clustertree
|
_get_intra
| null |
def _get_intra(self):
if self._silhouette is None:
self.get_silhouette()
return self._intracluster_dist
|
(self)
|
720,259 |
ete3.clustering.clustertree
|
_get_prof
| null |
def _get_prof(self):
if self._profile is None:
self._calculate_avg_profile()
return self._profile
|
(self)
|
720,260 |
ete3.clustering.clustertree
|
_get_silh
| null |
def _get_silh(self):
if self._silhouette is None:
self.get_silhouette()
return self._silhouette
|
(self)
|
720,261 |
ete3.clustering.clustertree
|
_get_std
| null |
def _get_std(self):
if self._std_profile is None:
self._calculate_avg_profile()
return self._std_profile
|
(self)
|
720,262 |
ete3.coretype.tree
|
_get_style
| null |
def _get_style(self):
if self._img_style is None:
self._set_style(None)
return self._img_style
|
(self)
|
720,263 |
ete3.coretype.tree
|
_get_support
| null |
def _get_support(self):
return self._support
|
(self)
|
720,264 |
ete3.coretype.tree
|
_get_up
| null |
def _get_up(self):
return self._up
|
(self)
|
720,265 |
ete3.coretype.tree
|
_iter_descendants_levelorder
|
Iterate over all desdecendant nodes.
|
def _iter_descendants_levelorder(self, is_leaf_fn=None):
"""
Iterate over all desdecendant nodes.
"""
tovisit = deque([self])
while len(tovisit)>0:
node = tovisit.popleft()
yield node
if not is_leaf_fn or not is_leaf_fn(node):
tovisit.extend(node.children)
|
(self, is_leaf_fn=None)
|
720,266 |
ete3.coretype.tree
|
_iter_descendants_postorder
| null |
def _iter_descendants_postorder(self, is_leaf_fn=None):
to_visit = [self]
if is_leaf_fn is not None:
_leaf = is_leaf_fn
else:
_leaf = self.__class__.is_leaf
while to_visit:
node = to_visit.pop(-1)
try:
node = node[1]
except TypeError:
# PREORDER ACTIONS
if not _leaf(node):
# ADD CHILDREN
to_visit.extend(reversed(node.children + [[1, node]]))
else:
yield node
else:
#POSTORDER ACTIONS
yield node
|
(self, is_leaf_fn=None)
|
720,267 |
ete3.coretype.tree
|
_iter_descendants_preorder
|
Iterator over all descendant nodes.
|
def _iter_descendants_preorder(self, is_leaf_fn=None):
"""
Iterator over all descendant nodes.
"""
to_visit = deque()
node = self
while node is not None:
yield node
if not is_leaf_fn or not is_leaf_fn(node):
to_visit.extendleft(reversed(node.children))
try:
node = to_visit.popleft()
except:
node = None
|
(self, is_leaf_fn=None)
|
720,268 |
ete3.coretype.tree
|
_set_children
| null |
def _set_children(self, value):
if type(value) == list:
for n in value:
if type(n) != type(self):
raise TreeError("Incorrect child node type")
self._children = value
else:
raise TreeError("Incorrect children type")
|
(self, value)
|
720,269 |
ete3.coretype.tree
|
_set_dist
| null |
def _set_dist(self, value):
try:
self._dist = float(value)
except ValueError:
raise TreeError('node dist must be a float number')
|
(self, value)
|
720,270 |
ete3.coretype.tree
|
_set_face_areas
| null |
def _set_face_areas(self, value):
if isinstance(value, _FaceAreas):
self._faces = value
else:
raise ValueError("[%s] is not a valid FaceAreas instance" %type(value))
|
(self, value)
|
720,271 |
ete3.clustering.clustertree
|
_set_forbidden
| null |
def _set_forbidden(self, value):
raise ValueError("This attribute can not be manually set.")
|
(self, value)
|
720,272 |
ete3.clustering.clustertree
|
_set_profile
| null |
def _set_profile(self, value):
self._profile = value
|
(self, value)
|
720,273 |
ete3.coretype.tree
|
_set_style
| null |
def _set_style(self, value):
self.set_style(value)
|
(self, value)
|
720,274 |
ete3.coretype.tree
|
_set_support
| null |
def _set_support(self, value):
try:
self._support = float(value)
except ValueError:
raise TreeError('node support must be a float number')
|
(self, value)
|
720,275 |
ete3.coretype.tree
|
_set_up
| null |
def _set_up(self, value):
if type(value) == type(self) or value is None:
self._up = value
else:
raise TreeError("bad node_up type")
|
(self, value)
|
720,276 |
ete3.coretype.tree
|
add_child
|
Adds a new child to this node. If child node is not suplied
as an argument, a new node instance will be created.
:argument None child: the node instance to be added as a child.
:argument None name: the name that will be given to the child.
:argument None dist: the distance from the node to the child.
:argument None support: the support value of child partition.
:returns: The child node instance
|
def add_child(self, child=None, name=None, dist=None, support=None):
"""
Adds a new child to this node. If child node is not suplied
as an argument, a new node instance will be created.
:argument None child: the node instance to be added as a child.
:argument None name: the name that will be given to the child.
:argument None dist: the distance from the node to the child.
:argument None support: the support value of child partition.
:returns: The child node instance
"""
if child is None:
child = self.__class__()
if name is not None:
child.name = name
if dist is not None:
child.dist = dist
if support is not None:
child.support = support
self.children.append(child)
child.up = self
return child
|
(self, child=None, name=None, dist=None, support=None)
|
720,277 |
ete3.coretype.tree
|
add_face
|
.. versionadded: 2.1
Add a fixed face to the node. This type of faces will be
always attached to nodes, independently of the layout
function.
:argument face: a Face or inherited instance
:argument column: An integer number starting from 0
:argument "branch-right" position: Posible values are:
"branch-right", "branch-top", "branch-bottom", "float",
"aligned"
|
def add_face(self, face, column, position="branch-right"):
"""
.. versionadded: 2.1
Add a fixed face to the node. This type of faces will be
always attached to nodes, independently of the layout
function.
:argument face: a Face or inherited instance
:argument column: An integer number starting from 0
:argument "branch-right" position: Posible values are:
"branch-right", "branch-top", "branch-bottom", "float",
"aligned"
"""
if not hasattr(self, "_faces"):
self._faces = _FaceAreas()
if position not in FACE_POSITIONS:
raise ValueError("face position not in %s" %FACE_POSITIONS)
if isinstance(face, Face):
getattr(self._faces, position).add_face(face, column=column)
else:
raise ValueError("not a Face instance")
|
(self, face, column, position='branch-right')
|
720,278 |
ete3.coretype.tree
|
add_feature
|
Add or update a node's feature.
|
def add_feature(self, pr_name, pr_value):
"""
Add or update a node's feature.
"""
setattr(self, pr_name, pr_value)
self.features.add(pr_name)
|
(self, pr_name, pr_value)
|
720,279 |
ete3.coretype.tree
|
add_features
|
Add or update several features.
|
def add_features(self, **features):
"""
Add or update several features. """
for fname, fvalue in six.iteritems(features):
setattr(self, fname, fvalue)
self.features.add(fname)
|
(self, **features)
|
720,280 |
ete3.coretype.tree
|
add_sister
|
Adds a sister to this node. If sister node is not supplied
as an argument, a new TreeNode instance will be created and
returned.
|
def add_sister(self, sister=None, name=None, dist=None):
"""
Adds a sister to this node. If sister node is not supplied
as an argument, a new TreeNode instance will be created and
returned.
"""
if self.up is None:
raise TreeError("A parent node is required to add a sister")
else:
return self.up.add_child(child=sister, name=name, dist=dist)
|
(self, sister=None, name=None, dist=None)
|
720,281 |
ete3.coretype.tree
|
check_monophyly
|
.. versionadded: 2.2
Returns True if a given target attribute is monophyletic under
this node for the provided set of values.
If not all values are represented in the current tree
structure, a ValueError exception will be raised to warn that
strict monophyly could never be reached (this behaviour can be
avoided by enabling the `ignore_missing` flag.
:param values: a set of values for which monophyly is
expected.
:param target_attr: node attribute being used to check
monophyly (i.e. species for species trees, names for gene
family trees, or any custom feature present in the tree).
:param False ignore_missing: Avoid raising an Exception when
missing attributes are found.
.. versionchanged: 2.3
:param False unrooted: If True, tree will be treated as unrooted, thus
allowing to find monophyly even when current outgroup is splitting a
monophyletic group.
:returns: the following tuple
IsMonophyletic (boolean),
clade type ('monophyletic', 'paraphyletic' or 'polyphyletic'),
leaves breaking the monophyly (set)
|
def check_monophyly(self, values, target_attr, ignore_missing=False,
unrooted=False):
"""
.. versionadded: 2.2
Returns True if a given target attribute is monophyletic under
this node for the provided set of values.
If not all values are represented in the current tree
structure, a ValueError exception will be raised to warn that
strict monophyly could never be reached (this behaviour can be
avoided by enabling the `ignore_missing` flag.
:param values: a set of values for which monophyly is
expected.
:param target_attr: node attribute being used to check
monophyly (i.e. species for species trees, names for gene
family trees, or any custom feature present in the tree).
:param False ignore_missing: Avoid raising an Exception when
missing attributes are found.
.. versionchanged: 2.3
:param False unrooted: If True, tree will be treated as unrooted, thus
allowing to find monophyly even when current outgroup is splitting a
monophyletic group.
:returns: the following tuple
IsMonophyletic (boolean),
clade type ('monophyletic', 'paraphyletic' or 'polyphyletic'),
leaves breaking the monophyly (set)
"""
if type(values) != set:
values = set(values)
# This is the only time I traverse the tree, then I use cached
# leaf content
n2leaves = self.get_cached_content()
# Raise an error if requested attribute values are not even present
if ignore_missing:
found_values = set([getattr(n, target_attr) for n in n2leaves[self]])
missing_values = values - found_values
values = values & found_values
# Locate leaves matching requested attribute values
targets = set([leaf for leaf in n2leaves[self]
if getattr(leaf, target_attr) in values])
if not ignore_missing:
if values - set([getattr(leaf, target_attr) for leaf in targets]):
raise ValueError('The monophyly of the provided values could never be reached, as not all of them exist in the tree.'
' Please check your target attribute and values, or set the ignore_missing flag to True')
if unrooted:
smallest = None
for side1, side2 in self.iter_edges(cached_content=n2leaves):
if targets.issubset(side1) and (not smallest or len(side1) < len(smallest)):
smallest = side1
elif targets.issubset(side2) and (not smallest or len(side2) < len(smallest)):
smallest = side2
if smallest is not None and len(smallest) == len(targets):
break
foreign_leaves = smallest - targets
else:
# Check monophyly with get_common_ancestor. Note that this
# step does not require traversing the tree again because
# targets are node instances instead of node names, and
# get_common_ancestor function is smart enough to detect it
# and avoid unnecessary traversing.
common = self.get_common_ancestor(targets)
observed = n2leaves[common]
foreign_leaves = set([leaf for leaf in observed
if getattr(leaf, target_attr) not in values])
if not foreign_leaves:
return True, "monophyletic", foreign_leaves
else:
# if the requested attribute is not monophyletic in this
# node, let's differentiate between poly and paraphyly.
poly_common = self.get_common_ancestor(foreign_leaves)
# if the common ancestor of all foreign leaves is self
# contained, we have a paraphyly. Otherwise, polyphyly.
polyphyletic = [leaf for leaf in poly_common if
getattr(leaf, target_attr) in values]
if polyphyletic:
return False, "polyphyletic", foreign_leaves
else:
return False, "paraphyletic", foreign_leaves
|
(self, values, target_attr, ignore_missing=False, unrooted=False)
|
720,282 |
ete3.coretype.tree
|
compare
|
compare this tree with another using robinson foulds symmetric difference
and number of shared edges. Trees of different sizes and with duplicated
items allowed.
returns: a Python dictionary with results
|
def compare(self, ref_tree, use_collateral=False, min_support_source=0.0, min_support_ref=0.0,
has_duplications=False, expand_polytomies=False, unrooted=False,
max_treeko_splits_to_be_artifact=1000, ref_tree_attr='name', source_tree_attr='name'):
"""compare this tree with another using robinson foulds symmetric difference
and number of shared edges. Trees of different sizes and with duplicated
items allowed.
returns: a Python dictionary with results
"""
source_tree = self
def _safe_div(a, b):
if a != 0:
return a / float(b)
else: return 0.0
def _compare(src_tree, ref_tree):
# calculate partitions and rf distances
rf, maxrf, common, ref_p, src_p, ref_disc, src_disc = ref_tree.robinson_foulds(src_tree,
expand_polytomies=expand_polytomies,
unrooted_trees=unrooted,
attr_t1=ref_tree_attr,
attr_t2=source_tree_attr,
min_support_t2=min_support_source,
min_support_t1=min_support_ref)
# if trees share leaves, count their distances
if len(common) > 0 and src_p and ref_p:
if unrooted:
valid_ref_edges = set([p for p in (ref_p - ref_disc) if len(p[0])>1 and len(p[1])>0])
valid_src_edges = set([p for p in (src_p - src_disc) if len(p[0])>1 and len(p[1])>0])
common_edges = valid_ref_edges & valid_src_edges
else:
valid_ref_edges = set([p for p in (ref_p - ref_disc) if len(p)>1])
valid_src_edges = set([p for p in (src_p - src_disc) if len(p)>1])
common_edges = valid_ref_edges & valid_src_edges
else:
valid_ref_edges = set()
valid_src_edges = set()
common_edges = set()
# # % of ref edges found in tree
# ref_found.append(float(len(p2 & p1)) / reftree_edges)
# # valid edges in target, discard also leaves
# p2bis = set([p for p in (p2-d2) if len(p[0])>1 and len(p[1])>1])
# if p2bis:
# incompatible_target_branches = float(len((p2-d2) - p1))
# target_found.append(1 - (incompatible_target_branches / (len(p2-d2))))
return rf, maxrf, len(common), valid_ref_edges, valid_src_edges, common_edges
total_valid_ref_edges = len([n for n in ref_tree.traverse() if n.children and n.support > min_support_ref])
result = {}
if has_duplications:
orig_target_size = len(source_tree)
ntrees, ndups, sp_trees = source_tree.get_speciation_trees(
autodetect_duplications=True, newick_only=True,
target_attr=source_tree_attr, map_features=[source_tree_attr, "support"])
if ntrees < max_treeko_splits_to_be_artifact:
all_rf = []
ref_found = []
src_found = []
tree_sizes = []
all_max_rf = []
common_names = 0
for subtree_nw in sp_trees:
#if seedid and not use_collateral and (seedid not in subtree_nw):
# continue
subtree = source_tree.__class__(subtree_nw, sp_naming_function = source_tree._speciesFunction)
if not subtree.children:
continue
# only necessary if rf function is going to filter by support
# value. It slows downs the analysis, obviously, as it has to
# find the support for each node in the treeko tree from the
# original one.
if min_support_source > 0:
subtree_content = subtree.get_cached_content(store_attr='name')
for n in subtree.traverse():
if n.children:
n.support = source_tree.get_common_ancestor(subtree_content[n]).support
total_rf, max_rf, ncommon, valid_ref_edges, valid_src_edges, common_edges = _compare(subtree, ref_tree)
all_rf.append(total_rf)
all_max_rf.append(max_rf)
tree_sizes.append(ncommon)
if unrooted:
ref_found_in_src = len(common_edges)/float(len(valid_ref_edges)) if valid_ref_edges else None
src_found_in_ref = len(common_edges)/float(len(valid_src_edges)) if valid_src_edges else None
else:
# in rooted trees, we want to discount the root edge
# from the percentage of congruence. Otherwise we will never see a 0%
# congruence for totally different trees
ref_found_in_src = (len(common_edges)-1)/float(len(valid_ref_edges)-1) if len(valid_ref_edges)>1 else None
src_found_in_ref = (len(common_edges)-1)/float(len(valid_src_edges)-1) if len(valid_src_edges)>1 else None
if ref_found_in_src is not None:
ref_found.append(ref_found_in_src)
if src_found_in_ref is not None:
src_found.append(src_found_in_ref)
if all_rf:
# Treeko speciation distance
alld = [_safe_div(all_rf[i], float(all_max_rf[i])) for i in range(len(all_rf))]
a = sum([alld[i] * tree_sizes[i] for i in range(len(all_rf))])
b = float(sum(tree_sizes))
treeko_d = a/b if a else 0.0
result["treeko_dist"] = treeko_d
result["rf"] = utils.mean(all_rf)
result["max_rf"] = max(all_max_rf)
result["effective_tree_size"] = utils.mean(tree_sizes)
result["norm_rf"] = utils.mean([_safe_div(all_rf[i], float(all_max_rf[i])) for i in range(len(all_rf))])
result["ref_edges_in_source"] = utils.mean(ref_found)
result["source_edges_in_ref"] = utils.mean(src_found)
result["source_subtrees"] = len(all_rf)
result["common_edges"] = set()
result["source_edges"] = set()
result["ref_edges"] = set()
else:
total_rf, max_rf, ncommon, valid_ref_edges, valid_src_edges, common_edges = _compare(source_tree, ref_tree)
result["rf"] = float(total_rf) if max_rf else "NA"
result["max_rf"] = float(max_rf)
if unrooted:
result["ref_edges_in_source"] = len(common_edges)/float(len(valid_ref_edges)) if valid_ref_edges else "NA"
result["source_edges_in_ref"] = len(common_edges)/float(len(valid_src_edges)) if valid_src_edges else "NA"
else:
# in rooted trees, we want to discount the root edge from the
# percentage of congruence. Otherwise we will never see a 0%
# congruence for totally different trees
result["ref_edges_in_source"] = (len(common_edges)-1)/float(len(valid_ref_edges)-1) if len(valid_ref_edges)>1 else "NA"
result["source_edges_in_ref"] = (len(common_edges)-1)/float(len(valid_src_edges)-1) if len(valid_src_edges)>1 else "NA"
result["effective_tree_size"] = ncommon
result["norm_rf"] = total_rf/float(max_rf) if max_rf else "NA"
result["treeko_dist"] = "NA"
result["source_subtrees"] = 1
result["common_edges"] = common_edges
result["source_edges"] = valid_src_edges
result["ref_edges"] = valid_ref_edges
return result
|
(self, ref_tree, use_collateral=False, min_support_source=0.0, min_support_ref=0.0, has_duplications=False, expand_polytomies=False, unrooted=False, max_treeko_splits_to_be_artifact=1000, ref_tree_attr='name', source_tree_attr='name')
|
720,283 |
ete3.coretype.tree
|
convert_to_ultrametric
|
.. versionadded: 2.1
Converts a tree into ultrametric topology (all leaves must have
the same distance to root). Note that, for visual inspection
of ultrametric trees, node.img_style["size"] should be set to
0.
|
def convert_to_ultrametric(self, tree_length=None, strategy='balanced'):
"""
.. versionadded: 2.1
Converts a tree into ultrametric topology (all leaves must have
the same distance to root). Note that, for visual inspection
of ultrametric trees, node.img_style["size"] should be set to
0.
"""
# Could something like this replace the old algorithm?
#most_distant_leaf, tree_length = self.get_farthest_leaf()
#for leaf in self:
# d = leaf.get_distance(self)
# leaf.dist += (tree_length - d)
#return
# pre-calculate how many splits remain under each node
node2max_depth = {}
for node in self.traverse("postorder"):
if not node.is_leaf():
max_depth = max([node2max_depth[c] for c in node.children]) + 1
node2max_depth[node] = max_depth
else:
node2max_depth[node] = 1
node2dist = {self: 0.0}
if not tree_length:
most_distant_leaf, tree_length = self.get_farthest_leaf()
else:
tree_length = float(tree_length)
step = tree_length / node2max_depth[self]
for node in self.iter_descendants("levelorder"):
if strategy == "balanced":
node.dist = (tree_length - node2dist[node.up]) / node2max_depth[node]
node2dist[node] = node.dist + node2dist[node.up]
elif strategy == "fixed":
if not node.is_leaf():
node.dist = step
else:
node.dist = tree_length - ((node2dist[node.up]) * step)
node2dist[node] = node2dist[node.up] + 1
node.dist = node.dist
|
(self, tree_length=None, strategy='balanced')
|
720,284 |
ete3.coretype.tree
|
cophenetic_matrix
|
.. versionadded: 3.1.1
Generate a cophenetic distance matrix of the treee to standard output
The `cophenetic matrix <https://en.wikipedia.org/wiki/Cophenetic>` is a matrix representation of the
distance between each node.
if we have a tree like
----A
_____________|y
| |
| ----B
________|z
| ----C
| |
|____________|x -----D
| |
|______|w
|
|
-----E
Where w,x,y,z are internal nodes.
d(A,B) = d(y,A) + d(y,B)
and
d(A, E) = d(z,A) + d(z, E) = {d(z,y) + d(y,A)} + {d(z,x) + d(x,w) + d(w,E)}
We use an idea inspired by the ete3 team: https://gist.github.com/jhcepas/279f9009f46bf675e3a890c19191158b :
For each node find its path to the root.
e.g.
A -> A, y, z
E -> E, w, x,z
and make these orderless sets. Then we XOR the two sets to only find the elements
that are in one or other sets but not both. In this case A, E, y, x, w.
The distance between the two nodes is the sum of the distances from each of those nodes
to the parent
One more optimization: since the distances are symmetric, and distance to itself is zero
we user itertools.combinations rather than itertools.permutations. This cuts our computes from theta(n^2)
1/2n^2 - n (= O(n^2), which is still not great, but in reality speeds things up for large trees).
For this tree, we will return the two dimensional array:
A B C D E
A 0 d(A-y) + d(B-y) d(A-z) + d(C-z) d(A-z) + d(D-z) d(A-z) + d(E-z)
B d(B-y) + d(A-y) 0 d(B-z) + d(C-z) d(B-z) + d(D-z) d(B-z) + d(E-z)
C d(C-z) + d(A-z) d(C-z) + d(B-z) 0 d(C-x) + d(D-x) d(C-x) + d(E-x)
D d(D-z) + d(A-z) d(D-z) + d(B-z) d(D-x) + d(C-x) 0 d(D-w) + d(E-w)
E d(E-z) + d(A-z) d(E-z) + d(B-z) d(E-x) + d(C-x) d(E-w) + d(D-w) 0
We will also return the one dimensional array with the leaves in the order in which they appear in the matrix
(i.e. the column and/or row headers).
:param filename: the optional file to write to. If not provided, output will be to standard output
:return: two-dimensional array and a one dimensional array
|
def cophenetic_matrix(self):
"""
.. versionadded: 3.1.1
Generate a cophenetic distance matrix of the treee to standard output
The `cophenetic matrix <https://en.wikipedia.org/wiki/Cophenetic>` is a matrix representation of the
distance between each node.
if we have a tree like
----A
_____________|y
| |
| ----B
________|z
| ----C
| |
|____________|x -----D
| |
|______|w
|
|
-----E
Where w,x,y,z are internal nodes.
d(A,B) = d(y,A) + d(y,B)
and
d(A, E) = d(z,A) + d(z, E) = {d(z,y) + d(y,A)} + {d(z,x) + d(x,w) + d(w,E)}
We use an idea inspired by the ete3 team: https://gist.github.com/jhcepas/279f9009f46bf675e3a890c19191158b :
For each node find its path to the root.
e.g.
A -> A, y, z
E -> E, w, x,z
and make these orderless sets. Then we XOR the two sets to only find the elements
that are in one or other sets but not both. In this case A, E, y, x, w.
The distance between the two nodes is the sum of the distances from each of those nodes
to the parent
One more optimization: since the distances are symmetric, and distance to itself is zero
we user itertools.combinations rather than itertools.permutations. This cuts our computes from theta(n^2)
1/2n^2 - n (= O(n^2), which is still not great, but in reality speeds things up for large trees).
For this tree, we will return the two dimensional array:
A B C D E
A 0 d(A-y) + d(B-y) d(A-z) + d(C-z) d(A-z) + d(D-z) d(A-z) + d(E-z)
B d(B-y) + d(A-y) 0 d(B-z) + d(C-z) d(B-z) + d(D-z) d(B-z) + d(E-z)
C d(C-z) + d(A-z) d(C-z) + d(B-z) 0 d(C-x) + d(D-x) d(C-x) + d(E-x)
D d(D-z) + d(A-z) d(D-z) + d(B-z) d(D-x) + d(C-x) 0 d(D-w) + d(E-w)
E d(E-z) + d(A-z) d(E-z) + d(B-z) d(E-x) + d(C-x) d(E-w) + d(D-w) 0
We will also return the one dimensional array with the leaves in the order in which they appear in the matrix
(i.e. the column and/or row headers).
:param filename: the optional file to write to. If not provided, output will be to standard output
:return: two-dimensional array and a one dimensional array
"""
leaves = self.get_leaves()
paths = {x: set() for x in leaves}
# get the paths going up the tree
# we get all the nodes up to the last one and store them in a set
for n in leaves:
if n.is_root():
continue
movingnode = n
while not movingnode.is_root():
paths[n].add(movingnode)
movingnode = movingnode.up
# now we want to get all pairs of nodes using itertools combinations. We need AB AC etc but don't need BA CA
leaf_distances = {x.name: {} for x in leaves}
for (leaf1, leaf2) in itertools.combinations(leaves, 2):
# figure out the unique nodes in the path
uniquenodes = paths[leaf1] ^ paths[leaf2]
distance = sum(x.dist for x in uniquenodes)
leaf_distances[leaf1.name][leaf2.name] = leaf_distances[leaf2.name][leaf1.name] = distance
allleaves = sorted(leaf_distances.keys()) # the leaves in order that we will return
output = [] # the two dimensional array that we will return
for i, n in enumerate(allleaves):
output.append([])
for m in allleaves:
if m == n:
output[i].append(0) # distance to ourself = 0
else:
output[i].append(leaf_distances[n][m])
return output, allleaves
|
(self)
|
720,285 |
ete3.coretype.tree
|
copy
|
.. versionadded: 2.1
Returns a copy of the current node.
:var cpickle method: Protocol used to copy the node
structure. The following values are accepted:
- "newick": Tree topology, node names, branch lengths and
branch support values will be copied by as represented in
the newick string (copy by newick string serialisation).
- "newick-extended": Tree topology and all node features
will be copied based on the extended newick format
representation. Only node features will be copied, thus
excluding other node attributes. As this method is also
based on newick serialisation, features will be converted
into text strings when making the copy.
- "cpickle": The whole node structure and its content is
cloned based on cPickle object serialisation (slower, but
recommended for full tree copying)
- "deepcopy": The whole node structure and its content is
copied based on the standard "copy" Python functionality
(this is the slowest method but it allows to copy complex
objects even if attributes point to lambda functions,
etc.)
|
def copy(self, method="cpickle"):
""".. versionadded: 2.1
Returns a copy of the current node.
:var cpickle method: Protocol used to copy the node
structure. The following values are accepted:
- "newick": Tree topology, node names, branch lengths and
branch support values will be copied by as represented in
the newick string (copy by newick string serialisation).
- "newick-extended": Tree topology and all node features
will be copied based on the extended newick format
representation. Only node features will be copied, thus
excluding other node attributes. As this method is also
based on newick serialisation, features will be converted
into text strings when making the copy.
- "cpickle": The whole node structure and its content is
cloned based on cPickle object serialisation (slower, but
recommended for full tree copying)
- "deepcopy": The whole node structure and its content is
copied based on the standard "copy" Python functionality
(this is the slowest method but it allows to copy complex
objects even if attributes point to lambda functions,
etc.)
"""
method = method.lower()
if method=="newick":
new_node = self.__class__(self.write(features=["name"], format_root_node=True))
elif method=="newick-extended":
self.write(features=[], format_root_node=True)
new_node = self.__class__(self.write(features=[]))
elif method == "deepcopy":
parent = self.up
self.up = None
new_node = copy.deepcopy(self)
self.up = parent
elif method == "cpickle":
parent = self.up
self.up = None
new_node = six.moves.cPickle.loads(six.moves.cPickle.dumps(self, 2))
self.up = parent
else:
raise TreeError("Invalid copy method")
return new_node
|
(self, method='cpickle')
|
720,286 |
ete3.coretype.tree
|
del_feature
|
Permanently deletes a node's feature.
|
def del_feature(self, pr_name):
"""
Permanently deletes a node's feature.
"""
if hasattr(self, pr_name):
delattr(self, pr_name)
self.features.remove(pr_name)
|
(self, pr_name)
|
720,287 |
ete3.coretype.tree
|
delete
|
Deletes node from the tree structure. Notice that this method
makes 'disappear' the node from the tree structure. This means
that children from the deleted node are transferred to the
next available parent.
:param True prevent_nondicotomic: When True (default), delete
function will be execute recursively to prevent
single-child nodes.
:param False preserve_branch_length: If True, branch lengths
of the deleted nodes are transferred (summed up) to its
parent's branch, thus keeping original distances among
nodes.
**Example:**
::
/ C
root-|
| / B
\--- H |
\ A
> H.delete() will produce this structure:
/ C
|
root-|--B
|
\ A
|
def delete(self, prevent_nondicotomic=True, preserve_branch_length=False):
"""
Deletes node from the tree structure. Notice that this method
makes 'disappear' the node from the tree structure. This means
that children from the deleted node are transferred to the
next available parent.
:param True prevent_nondicotomic: When True (default), delete
function will be execute recursively to prevent
single-child nodes.
:param False preserve_branch_length: If True, branch lengths
of the deleted nodes are transferred (summed up) to its
parent's branch, thus keeping original distances among
nodes.
**Example:**
::
/ C
root-|
| / B
\--- H |
\ A
> H.delete() will produce this structure:
/ C
|
root-|--B
|
\ A
"""
parent = self.up
if parent:
if preserve_branch_length:
if len(self.children) == 1:
self.children[0].dist += self.dist
elif len(self.children) > 1:
parent.dist += self.dist
for ch in self.children:
parent.add_child(ch)
parent.remove_child(self)
# Avoids parents with only one child
if prevent_nondicotomic and parent and\
len(parent.children) < 2:
parent.delete(prevent_nondicotomic=False,
preserve_branch_length=preserve_branch_length)
|
(self, prevent_nondicotomic=True, preserve_branch_length=False)
|
720,288 |
ete3.coretype.tree
|
describe
|
Prints general information about this node and its
connections.
|
def describe(self):
"""
Prints general information about this node and its
connections.
"""
if len(self.get_tree_root().children)==2:
rooting = "Yes"
elif len(self.get_tree_root().children)>2:
rooting = "No"
else:
rooting = "No children"
max_node, max_dist = self.get_farthest_leaf()
cached_content = self.get_cached_content()
print("Number of leaf nodes:\t%d" % len(cached_content[self]))
print("Total number of nodes:\t%d" % len(cached_content))
print("Rooted:\t%s" %rooting)
print("Most distant node:\t%s" %max_node.name)
print("Max. distance:\t%f" %max_dist)
|
(self)
|
720,289 |
ete3.coretype.tree
|
detach
|
Detachs this node (and all its descendants) from its parent
and returns the referent to itself.
Detached node conserves all its structure of descendants, and can
be attached to another node through the 'add_child' function. This
mechanism can be seen as a cut and paste.
|
def detach(self):
"""
Detachs this node (and all its descendants) from its parent
and returns the referent to itself.
Detached node conserves all its structure of descendants, and can
be attached to another node through the 'add_child' function. This
mechanism can be seen as a cut and paste.
"""
if self.up:
self.up.children.remove(self)
self.up = None
return self
|
(self)
|
720,290 |
ete3.coretype.tree
|
expand_polytomies
|
.. versionadded:: 2.3
Given a tree with one or more polytomies, this functions returns the
list of all trees (in newick format) resulting from the combination of
all possible solutions of the multifurcated nodes.
.. warning:
Please note that the number of of possible binary trees grows
exponentially with the number and size of polytomies. Using this
function with large multifurcations is not feasible:
polytomy size: 3 number of binary trees: 3
polytomy size: 4 number of binary trees: 15
polytomy size: 5 number of binary trees: 105
polytomy size: 6 number of binary trees: 945
polytomy size: 7 number of binary trees: 10395
polytomy size: 8 number of binary trees: 135135
polytomy size: 9 number of binary trees: 2027025
http://ajmonline.org/2010/darwin.php
|
def expand_polytomies(self, map_attr="name", polytomy_size_limit=5,
skip_large_polytomies=False):
'''
.. versionadded:: 2.3
Given a tree with one or more polytomies, this functions returns the
list of all trees (in newick format) resulting from the combination of
all possible solutions of the multifurcated nodes.
.. warning:
Please note that the number of of possible binary trees grows
exponentially with the number and size of polytomies. Using this
function with large multifurcations is not feasible:
polytomy size: 3 number of binary trees: 3
polytomy size: 4 number of binary trees: 15
polytomy size: 5 number of binary trees: 105
polytomy size: 6 number of binary trees: 945
polytomy size: 7 number of binary trees: 10395
polytomy size: 8 number of binary trees: 135135
polytomy size: 9 number of binary trees: 2027025
http://ajmonline.org/2010/darwin.php
'''
class TipTuple(tuple):
pass
def add_leaf(tree, label):
yield (label, tree)
if not isinstance(tree, TipTuple) and isinstance(tree, tuple):
for left in add_leaf(tree[0], label):
yield (left, tree[1])
for right in add_leaf(tree[1], label):
yield (tree[0], right)
def enum_unordered(labels):
if len(labels) == 1:
yield labels[0]
else:
for tree in enum_unordered(labels[1:]):
for new_tree in add_leaf(tree, labels[0]):
yield new_tree
n2subtrees = {}
for n in self.traverse("postorder"):
if n.is_leaf():
subtrees = [getattr(n, map_attr)]
else:
subtrees = []
if len(n.children) > polytomy_size_limit:
if skip_large_polytomies:
for childtrees in itertools.product(*[n2subtrees[ch] for ch in n.children]):
subtrees.append(TipTuple(childtrees))
else:
raise TreeError("Found polytomy larger than current limit: %s" %n)
else:
for childtrees in itertools.product(*[n2subtrees[ch] for ch in n.children]):
subtrees.extend([TipTuple(subtree) for subtree in enum_unordered(childtrees)])
n2subtrees[n] = subtrees
return ["%s;"%str(nw) for nw in n2subtrees[self]] # tuples are in newick format ^_^
|
(self, map_attr='name', polytomy_size_limit=5, skip_large_polytomies=False)
|
720,291 |
ete3.coretype.tree
|
from_parent_child_table
|
Converts a parent-child table into an ETE Tree instance.
:argument parent_child_table: a list of tuples containing parent-child
relationships. For example: [("A", "B", 0.1), ("A", "C", 0.2), ("C",
"D", 1), ("C", "E", 1.5)]. Where each tuple represents: [parent, child,
child-parent-dist]
:returns: A new Tree instance
:example:
>>> tree = Tree.from_parent_child_table([("A", "B", 0.1), ("A", "C", 0.2), ("C", "D", 1), ("C", "E", 1.5)])
>>> print tree
|
@staticmethod
def from_parent_child_table(parent_child_table):
"""Converts a parent-child table into an ETE Tree instance.
:argument parent_child_table: a list of tuples containing parent-child
relationships. For example: [("A", "B", 0.1), ("A", "C", 0.2), ("C",
"D", 1), ("C", "E", 1.5)]. Where each tuple represents: [parent, child,
child-parent-dist]
:returns: A new Tree instance
:example:
>>> tree = Tree.from_parent_child_table([("A", "B", 0.1), ("A", "C", 0.2), ("C", "D", 1), ("C", "E", 1.5)])
>>> print tree
"""
def get_node(nodename, dist=None):
if nodename not in nodes_by_name:
nodes_by_name[nodename] = Tree(name=nodename, dist=dist)
node = nodes_by_name[nodename]
if dist is not None:
node.dist = dist
node.name = nodename
return nodes_by_name[nodename]
nodes_by_name = {}
for columns in parent_child_table:
if len(columns) == 3:
parent_name, child_name, distance = columns
dist = float(distance)
else:
parent_name, child_name = columns
dist = None
parent = get_node(parent_name)
parent.add_child(get_node(child_name, dist=dist))
root = parent.get_tree_root()
return root
|
(parent_child_table)
|
720,292 |
ete3.coretype.tree
|
from_skbio
|
Converts a scikit-bio TreeNode object into ETE Tree object.
:argument skbio_tree: a scikit bio TreeNode instance
:argument None map_attributes: A list of attribute nanes in the
scikit-bio tree that should be mapped into the ETE tree
instance. (name, id and branch length are always mapped)
:returns: A new Tree instance
:example:
>>> tree = Tree.from_skibio(skbioTree, map_attributes=["value"])
|
@staticmethod
def from_skbio(skbio_tree, map_attributes=None):
"""Converts a scikit-bio TreeNode object into ETE Tree object.
:argument skbio_tree: a scikit bio TreeNode instance
:argument None map_attributes: A list of attribute nanes in the
scikit-bio tree that should be mapped into the ETE tree
instance. (name, id and branch length are always mapped)
:returns: A new Tree instance
:example:
>>> tree = Tree.from_skibio(skbioTree, map_attributes=["value"])
"""
from skbio import TreeNode as skbioTreeNode
def get_ete_node(skbio_node):
ete_node = all_nodes.get(skbio_node, Tree())
if skbio_node.length is not None:
ete_node.dist = float(skbio_node.length)
ete_node.name = skbio_node.name
ete_node.add_features(id=skbio_node.id)
if map_attributes:
for a in map_attributes:
ete_node.add_feature(a, getattr(skbio_node, a, None))
return ete_node
all_nodes = {}
if isinstance(skbio_tree, skbioTreeNode):
for node in skbio_tree.preorder(include_self=True):
all_nodes[node] = get_ete_node(node)
ete_node = all_nodes[node]
for ch in node.children:
ete_ch = get_ete_node(ch)
ete_node.add_child(ete_ch)
all_nodes[ch] = ete_ch
return ete_ch.get_tree_root()
|
(skbio_tree, map_attributes=None)
|
720,293 |
ete3.coretype.tree
|
get_ancestors
|
versionadded: 2.2
Returns the list of all ancestor nodes from current node to
the current tree root.
|
def get_ancestors(self):
'''versionadded: 2.2
Returns the list of all ancestor nodes from current node to
the current tree root.
'''
return [n for n in self.iter_ancestors()]
|
(self)
|
720,294 |
ete3.coretype.tree
|
get_ascii
|
Returns a string containing an ascii drawing of the tree.
:argument show_internal: includes internal edge names.
:argument compact: use exactly one line per tip.
:param attributes: A list of node attributes to shown in the
ASCII representation.
|
def get_ascii(self, show_internal=True, compact=False, attributes=None):
"""
Returns a string containing an ascii drawing of the tree.
:argument show_internal: includes internal edge names.
:argument compact: use exactly one line per tip.
:param attributes: A list of node attributes to shown in the
ASCII representation.
"""
(lines, mid) = self._asciiArt(show_internal=show_internal,
compact=compact, attributes=attributes)
return '\n'+'\n'.join(lines)
|
(self, show_internal=True, compact=False, attributes=None)
|
720,295 |
ete3.coretype.tree
|
get_cached_content
|
.. versionadded: 2.2
Returns a dictionary pointing to the preloaded content of each
internal node under this tree. Such a dictionary is intended
to work as a cache for operations that require many traversal
operations.
:param None store_attr: Specifies the node attribute that
should be cached (i.e. name, distance, etc.). When none,
the whole node instance is cached.
:param _store: (internal use)
|
def get_cached_content(self, store_attr=None, container_type=set, leaves_only=True, _store=None):
"""
.. versionadded: 2.2
Returns a dictionary pointing to the preloaded content of each
internal node under this tree. Such a dictionary is intended
to work as a cache for operations that require many traversal
operations.
:param None store_attr: Specifies the node attribute that
should be cached (i.e. name, distance, etc.). When none,
the whole node instance is cached.
:param _store: (internal use)
"""
if _store is None:
_store = {}
def get_value(_n):
if store_attr is None:
_val = [_n]
else:
if not isinstance(store_attr, six.string_types):
_val = [tuple(getattr(_n, attr, None) for attr in store_attr)]
else:
_val = [getattr(_n, store_attr, None)]
return _val
for ch in self.children:
ch.get_cached_content(store_attr=store_attr,
container_type=container_type,
leaves_only=leaves_only,
_store=_store)
if self.children:
if not leaves_only:
val = container_type(get_value(self))
else:
val = container_type()
for ch in self.children:
if type(val) == list:
val.extend(_store[ch])
if type(val) == set:
val.update(_store[ch])
if not leaves_only:
if type(val) == list:
val.extend(get_value(ch))
if type(val) == set:
val.update(get_value(ch))
_store[self] = val
else:
_store[self] = container_type(get_value(self))
return _store
|
(self, store_attr=None, container_type=<class 'set'>, leaves_only=True, _store=None)
|
720,296 |
ete3.coretype.tree
|
get_children
|
Returns an independent list of node's children.
|
def get_children(self):
"""
Returns an independent list of node's children.
"""
return [ch for ch in self.children]
|
(self)
|
720,297 |
ete3.coretype.tree
|
get_closest_leaf
|
Returns node's closest descendant leaf and the distance to
it.
:argument False topology_only: If set to True, distance
between nodes will be referred to the number of nodes
between them. In other words, topological distance will be
used instead of branch length distances.
:return: A tuple containing the closest leaf referred to the
current node and the distance to it.
|
def get_closest_leaf(self, topology_only=False, is_leaf_fn=None):
"""Returns node's closest descendant leaf and the distance to
it.
:argument False topology_only: If set to True, distance
between nodes will be referred to the number of nodes
between them. In other words, topological distance will be
used instead of branch length distances.
:return: A tuple containing the closest leaf referred to the
current node and the distance to it.
"""
min_node, min_dist, max_node, max_dist = self._get_farthest_and_closest_leaves(
topology_only=topology_only, is_leaf_fn=is_leaf_fn)
return min_node, min_dist
|
(self, topology_only=False, is_leaf_fn=None)
|
720,298 |
ete3.coretype.tree
|
get_common_ancestor
|
Returns the first common ancestor between this node and a given
list of 'target_nodes'.
**Examples:**
::
t = tree.Tree("(((A:0.1, B:0.01):0.001, C:0.0001):1.0[&&NHX:name=common], (D:0.00001):0.000001):2.0[&&NHX:name=root];")
A = t.get_descendants_by_name("A")[0]
C = t.get_descendants_by_name("C")[0]
common = A.get_common_ancestor(C)
print common.name
|
def get_common_ancestor(self, *target_nodes, **kargs):
"""
Returns the first common ancestor between this node and a given
list of 'target_nodes'.
**Examples:**
::
t = tree.Tree("(((A:0.1, B:0.01):0.001, C:0.0001):1.0[&&NHX:name=common], (D:0.00001):0.000001):2.0[&&NHX:name=root];")
A = t.get_descendants_by_name("A")[0]
C = t.get_descendants_by_name("C")[0]
common = A.get_common_ancestor(C)
print common.name
"""
get_path = kargs.get("get_path", False)
if len(target_nodes) == 1 and type(target_nodes[0]) \
in set([set, tuple, list, frozenset]):
target_nodes = target_nodes[0]
# Convert node names into node instances
target_nodes = _translate_nodes(self, *target_nodes)
if type(target_nodes) != list:
# If only one node is provided and is the same as the seed node,
# return itself
if target_nodes is self:
if get_path:
return self, {}
else:
return self
else:
#Otherwise find the common ancestor of current seed node and
#the target_node provided
target_nodes = [target_nodes, self]
n2path = {}
reference = []
ref_node = None
for n in target_nodes:
current = n
while current:
n2path.setdefault(n, set()).add(current)
if not ref_node:
reference.append(current)
current = current.up
if not ref_node:
ref_node = n
common = None
for n in reference:
broken = False
for node, path in six.iteritems(n2path):
if node is not ref_node and n not in path:
broken = True
break
if not broken:
common = n
break
if not common:
raise TreeError("Nodes are not connected!")
if get_path:
return common, n2path
else:
return common
|
(self, *target_nodes, **kargs)
|
720,299 |
ete3.coretype.tree
|
get_descendants
|
Returns a list of all (leaves and internal) descendant nodes.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
|
def get_descendants(self, strategy="levelorder", is_leaf_fn=None):
"""
Returns a list of all (leaves and internal) descendant nodes.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
"""
return [n for n in self.iter_descendants(strategy=strategy, \
is_leaf_fn=is_leaf_fn)]
|
(self, strategy='levelorder', is_leaf_fn=None)
|
720,300 |
ete3.coretype.tree
|
get_distance
|
Returns the distance between two nodes. If only one target is
specified, it returns the distance between the target and the
current node.
:argument target: a node within the same tree structure.
:argument target2: a node within the same tree structure. If
not specified, current node is used as target2.
:argument False topology_only: If set to True, distance will
refer to the number of nodes between target and target2.
:returns: branch length distance between target and
target2. If topology_only flag is True, returns the number
of nodes between target and target2.
|
def get_distance(self, target, target2=None, topology_only=False):
"""
Returns the distance between two nodes. If only one target is
specified, it returns the distance between the target and the
current node.
:argument target: a node within the same tree structure.
:argument target2: a node within the same tree structure. If
not specified, current node is used as target2.
:argument False topology_only: If set to True, distance will
refer to the number of nodes between target and target2.
:returns: branch length distance between target and
target2. If topology_only flag is True, returns the number
of nodes between target and target2.
"""
if target2 is None:
target2 = self
root = self.get_tree_root()
else:
# is target node under current node?
root = self
target, target2 = _translate_nodes(root, target, target2)
ancestor = root.get_common_ancestor(target, target2)
dist = 0.0
for n in [target2, target]:
current = n
while current != ancestor:
if topology_only:
if current!=target:
dist += 1
else:
dist += current.dist
current = current.up
return dist
|
(self, target, target2=None, topology_only=False)
|
720,301 |
ete3.clustering.clustertree
|
get_dunn
|
Calculates the Dunn index for the given set of descendant
nodes.
|
def get_dunn(self, clusters, fdist=None):
""" Calculates the Dunn index for the given set of descendant
nodes.
"""
if fdist is None:
fdist = self._fdist
nodes = _translate_nodes(self, *clusters)
return clustvalidation.get_dunn_index(fdist, *nodes)
|
(self, clusters, fdist=None)
|
720,302 |
ete3.coretype.tree
|
get_edges
|
.. versionadded:: 2.3
Returns the list of edges of a tree. Each edge is represented as a
tuple of two elements, each containing the list of nodes separated by
the edge.
|
def get_edges(self, cached_content = None):
'''
.. versionadded:: 2.3
Returns the list of edges of a tree. Each edge is represented as a
tuple of two elements, each containing the list of nodes separated by
the edge.
'''
return [edge for edge in self.iter_edges(cached_content)]
|
(self, cached_content=None)
|
720,303 |
ete3.coretype.tree
|
get_farthest_leaf
|
Returns node's farthest descendant node (which is always a leaf), and the
distance to it.
:argument False topology_only: If set to True, distance
between nodes will be referred to the number of nodes
between them. In other words, topological distance will be
used instead of branch length distances.
:return: A tuple containing the farthest leaf referred to the
current node and the distance to it.
|
def get_farthest_leaf(self, topology_only=False, is_leaf_fn=None):
"""
Returns node's farthest descendant node (which is always a leaf), and the
distance to it.
:argument False topology_only: If set to True, distance
between nodes will be referred to the number of nodes
between them. In other words, topological distance will be
used instead of branch length distances.
:return: A tuple containing the farthest leaf referred to the
current node and the distance to it.
"""
min_node, min_dist, max_node, max_dist = self._get_farthest_and_closest_leaves(
topology_only=topology_only, is_leaf_fn=is_leaf_fn)
return max_node, max_dist
|
(self, topology_only=False, is_leaf_fn=None)
|
720,304 |
ete3.coretype.tree
|
get_farthest_node
|
Returns the node's farthest descendant or ancestor node, and the
distance to it.
:argument False topology_only: If set to True, distance
between nodes will be referred to the number of nodes
between them. In other words, topological distance will be
used instead of branch length distances.
:return: A tuple containing the farthest node referred to the
current node and the distance to it.
|
def get_farthest_node(self, topology_only=False):
"""
Returns the node's farthest descendant or ancestor node, and the
distance to it.
:argument False topology_only: If set to True, distance
between nodes will be referred to the number of nodes
between them. In other words, topological distance will be
used instead of branch length distances.
:return: A tuple containing the farthest node referred to the
current node and the distance to it.
"""
# Init farthest node to current farthest leaf
farthest_node, farthest_dist = self.get_farthest_leaf(topology_only=topology_only)
prev = self
cdist = 0.0 if topology_only else prev.dist
current = prev.up
while current is not None:
for ch in current.children:
if ch != prev:
if not ch.is_leaf():
fnode, fdist = ch.get_farthest_leaf(topology_only=topology_only)
else:
fnode = ch
fdist = 0
if topology_only:
fdist += 1.0
else:
fdist += ch.dist
if cdist+fdist > farthest_dist:
farthest_dist = cdist + fdist
farthest_node = fnode
prev = current
if topology_only:
cdist += 1
else:
cdist += prev.dist
current = prev.up
return farthest_node, farthest_dist
|
(self, topology_only=False)
|
720,305 |
ete3.coretype.tree
|
get_leaf_names
|
Returns the list of terminal node names under the current
node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
|
def get_leaf_names(self, is_leaf_fn=None):
"""
Returns the list of terminal node names under the current
node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
"""
return [name for name in self.iter_leaf_names(is_leaf_fn=is_leaf_fn)]
|
(self, is_leaf_fn=None)
|
720,306 |
ete3.clustering.clustertree
|
get_leaf_profiles
|
Returns the list of all the profiles associated to the
leaves under this node.
|
def get_leaf_profiles(self):
""" Returns the list of all the profiles associated to the
leaves under this node."""
return [l.get_profile()[0] for l in self.iter_leaves()]
|
(self)
|
720,307 |
ete3.coretype.tree
|
get_leaves
|
Returns the list of terminal nodes (leaves) under this node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
|
def get_leaves(self, is_leaf_fn=None):
"""
Returns the list of terminal nodes (leaves) under this node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
"""
return [n for n in self.iter_leaves(is_leaf_fn=is_leaf_fn)]
|
(self, is_leaf_fn=None)
|
720,308 |
ete3.coretype.tree
|
get_leaves_by_name
|
Returns a list of leaf nodes matching a given name.
|
def get_leaves_by_name(self, name):
"""
Returns a list of leaf nodes matching a given name.
"""
return self.search_nodes(name=name, children=[])
|
(self, name)
|
720,309 |
ete3.coretype.tree
|
get_midpoint_outgroup
|
Returns the node that divides the current tree into two distance-balanced
partitions.
|
def get_midpoint_outgroup(self):
"""
Returns the node that divides the current tree into two distance-balanced
partitions.
"""
# Gets the farthest node to the current root
root = self.get_tree_root()
nA, r2A_dist = root.get_farthest_leaf()
nB, A2B_dist = nA.get_farthest_node()
outgroup = nA
middist = A2B_dist / 2.0
cdist = 0
current = nA
while current is not None:
cdist += current.dist
if cdist > (middist): # Deja de subir cuando se pasa del maximo
break
else:
current = current.up
# if we reached the root, the tree is already at midpoint. Return any child as valid outgroup
if current is None:
current = self.children[0]
return current
|
(self)
|
720,310 |
ete3.coretype.tree
|
get_monophyletic
|
.. versionadded:: 2.2
Returns a list of nodes matching the provided monophyly
criteria. For a node to be considered a match, all
`target_attr` values within and node, and exclusively them,
should be grouped.
:param values: a set of values for which monophyly is
expected.
:param target_attr: node attribute being used to check
monophyly (i.e. species for species trees, names for gene
family trees).
|
def get_monophyletic(self, values, target_attr):
"""
.. versionadded:: 2.2
Returns a list of nodes matching the provided monophyly
criteria. For a node to be considered a match, all
`target_attr` values within and node, and exclusively them,
should be grouped.
:param values: a set of values for which monophyly is
expected.
:param target_attr: node attribute being used to check
monophyly (i.e. species for species trees, names for gene
family trees).
"""
if type(values) != set:
values = set(values)
n2values = self.get_cached_content(store_attr=target_attr)
is_monophyletic = lambda node: n2values[node] == values
for match in self.iter_leaves(is_leaf_fn=is_monophyletic):
if is_monophyletic(match):
yield match
|
(self, values, target_attr)
|
720,311 |
ete3.clustering.clustertree
|
get_silhouette
|
Calculates the node's silhouette value by using a given
distance function. By default, euclidean distance is used. It
also calculates the deviation profile, mean profile, and
inter/intra-cluster distances.
It sets the following features into the analyzed node:
- node.intracluster
- node.intercluster
- node.silhouete
intracluster distances a(i) are calculated as the Centroid
Diameter
intercluster distances b(i) are calculated as the Centroid linkage distance
** Rousseeuw, P.J. (1987) Silhouettes: A graphical aid to the
interpretation and validation of cluster analysis.
J. Comput. Appl. Math., 20, 53-65.
|
def get_silhouette(self, fdist=None):
""" Calculates the node's silhouette value by using a given
distance function. By default, euclidean distance is used. It
also calculates the deviation profile, mean profile, and
inter/intra-cluster distances.
It sets the following features into the analyzed node:
- node.intracluster
- node.intercluster
- node.silhouete
intracluster distances a(i) are calculated as the Centroid
Diameter
intercluster distances b(i) are calculated as the Centroid linkage distance
** Rousseeuw, P.J. (1987) Silhouettes: A graphical aid to the
interpretation and validation of cluster analysis.
J. Comput. Appl. Math., 20, 53-65.
"""
if fdist is None:
fdist = self._fdist
# Updates internal values
self._silhouette, self._intracluster_dist, self._intercluster_dist = \
clustvalidation.get_silhouette_width(fdist, self)
# And returns them
return self._silhouette, self._intracluster_dist, self._intercluster_dist
|
(self, fdist=None)
|
720,312 |
ete3.coretype.tree
|
get_sisters
|
Returns an independent list of sister nodes.
|
def get_sisters(self):
"""
Returns an independent list of sister nodes.
"""
if self.up is not None:
return [ch for ch in self.up.children if ch!=self]
else:
return []
|
(self)
|
720,313 |
ete3.coretype.tree
|
get_topology_id
|
.. versionadded:: 2.3
Returns the unique ID representing the topology of the current tree. Two
trees with the same topology will produce the same id. If trees are
unrooted, make sure that the root node is not binary or use the
tree.unroot() function before generating the topology id.
This is useful to detect the number of unique topologies over a bunch of
trees, without requiring full distance methods.
The id is, by default, calculated based on the terminal node's names. Any
other node attribute could be used instead.
|
def get_topology_id(self, attr="name"):
'''
.. versionadded:: 2.3
Returns the unique ID representing the topology of the current tree. Two
trees with the same topology will produce the same id. If trees are
unrooted, make sure that the root node is not binary or use the
tree.unroot() function before generating the topology id.
This is useful to detect the number of unique topologies over a bunch of
trees, without requiring full distance methods.
The id is, by default, calculated based on the terminal node's names. Any
other node attribute could be used instead.
'''
edge_keys = []
for s1, s2 in self.get_edges():
k1 = sorted([getattr(e, attr) for e in s1])
k2 = sorted([getattr(e, attr) for e in s2])
edge_keys.append(sorted([k1, k2]))
return md5(str(sorted(edge_keys)).encode('utf-8')).hexdigest()
|
(self, attr='name')
|
720,314 |
ete3.coretype.tree
|
get_tree_root
|
Returns the absolute root node of current tree structure.
|
def get_tree_root(self):
"""
Returns the absolute root node of current tree structure.
"""
root = self
while root.up is not None:
root = root.up
return root
|
(self)
|
720,315 |
ete3.coretype.tree
|
is_leaf
|
Return True if current node is a leaf.
|
def is_leaf(self):
"""
Return True if current node is a leaf.
"""
return len(self.children) == 0
|
(self)
|
720,316 |
ete3.coretype.tree
|
is_root
|
Returns True if current node has no parent
|
def is_root(self):
"""
Returns True if current node has no parent
"""
if self.up is None:
return True
else:
return False
|
(self)
|
720,317 |
ete3.coretype.tree
|
iter_ancestors
|
versionadded: 2.2
Iterates over the list of all ancestor nodes from current node
to the current tree root.
|
def iter_ancestors(self):
'''versionadded: 2.2
Iterates over the list of all ancestor nodes from current node
to the current tree root.
'''
node = self
while node.up is not None:
yield node.up
node = node.up
|
(self)
|
720,318 |
ete3.coretype.tree
|
iter_descendants
|
Returns an iterator over all descendant nodes.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
|
def iter_descendants(self, strategy="levelorder", is_leaf_fn=None):
"""
Returns an iterator over all descendant nodes.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
"""
for n in self.traverse(strategy=strategy, is_leaf_fn=is_leaf_fn):
if n is not self:
yield n
|
(self, strategy='levelorder', is_leaf_fn=None)
|
720,319 |
ete3.coretype.tree
|
iter_edges
|
.. versionadded:: 2.3
Iterate over the list of edges of a tree. Each edge is represented as a
tuple of two elements, each containing the list of nodes separated by
the edge.
|
def iter_edges(self, cached_content = None):
'''
.. versionadded:: 2.3
Iterate over the list of edges of a tree. Each edge is represented as a
tuple of two elements, each containing the list of nodes separated by
the edge.
'''
if not cached_content:
cached_content = self.get_cached_content()
all_leaves = cached_content[self]
for n, side1 in six.iteritems(cached_content):
yield (side1, all_leaves-side1)
|
(self, cached_content=None)
|
720,320 |
ete3.coretype.tree
|
iter_leaf_names
|
Returns an iterator over the leaf names under this node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
|
def iter_leaf_names(self, is_leaf_fn=None):
"""
Returns an iterator over the leaf names under this node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
"""
for n in self.iter_leaves(is_leaf_fn=is_leaf_fn):
yield n.name
|
(self, is_leaf_fn=None)
|
720,321 |
ete3.clustering.clustertree
|
iter_leaf_profiles
|
Returns an iterator over all the profiles associated to
the leaves under this node.
|
def iter_leaf_profiles(self):
""" Returns an iterator over all the profiles associated to
the leaves under this node."""
for l in self.iter_leaves():
yield l.get_profile()[0]
|
(self)
|
720,322 |
ete3.coretype.tree
|
iter_leaves
|
Returns an iterator over the leaves under this node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
|
def iter_leaves(self, is_leaf_fn=None):
"""
Returns an iterator over the leaves under this node.
:argument None is_leaf_fn: See :func:`TreeNode.traverse` for
documentation.
"""
for n in self.traverse(strategy="preorder", is_leaf_fn=is_leaf_fn):
if not is_leaf_fn:
if n.is_leaf():
yield n
else:
if is_leaf_fn(n):
yield n
|
(self, is_leaf_fn=None)
|
720,323 |
ete3.coretype.tree
|
iter_prepostorder
|
Iterate over all nodes in a tree yielding every node in both
pre and post order. Each iteration returns a postorder flag
(True if node is being visited in postorder) and a node
instance.
|
def iter_prepostorder(self, is_leaf_fn=None):
"""
Iterate over all nodes in a tree yielding every node in both
pre and post order. Each iteration returns a postorder flag
(True if node is being visited in postorder) and a node
instance.
"""
to_visit = [self]
if is_leaf_fn is not None:
_leaf = is_leaf_fn
else:
_leaf = self.__class__.is_leaf
while to_visit:
node = to_visit.pop(-1)
try:
node = node[1]
except TypeError:
# PREORDER ACTIONS
yield (False, node)
if not _leaf(node):
# ADD CHILDREN
to_visit.extend(reversed(node.children + [[1, node]]))
else:
#POSTORDER ACTIONS
yield (True, node)
|
(self, is_leaf_fn=None)
|
720,324 |
ete3.coretype.tree
|
iter_search_nodes
|
Search nodes in an iterative way. Matches are yielded as they
are being found. This avoids needing to scan the full tree
topology before returning the first matches. Useful when
dealing with huge trees.
|
def iter_search_nodes(self, **conditions):
"""
Search nodes in an iterative way. Matches are yielded as they
are being found. This avoids needing to scan the full tree
topology before returning the first matches. Useful when
dealing with huge trees.
"""
for n in self.traverse():
conditions_passed = 0
for key, value in six.iteritems(conditions):
if hasattr(n, key) and getattr(n, key) == value:
conditions_passed +=1
if conditions_passed == len(conditions):
yield n
|
(self, **conditions)
|
720,325 |
ete3.coretype.tree
|
ladderize
|
.. versionadded: 2.1
Sort the branches of a given tree (swapping children nodes)
according to the size of each partition.
::
t = Tree("(f,((d, ((a,b),c)),e));")
print t
#
# /-f
# |
# | /-d
# ----| |
# | /---| /-a
# | | | /---|
# | | \---| \-b
# \---| |
# | \-c
# |
# \-e
t.ladderize()
print t
# /-f
# ----|
# | /-e
# \---|
# | /-d
# \---|
# | /-c
# \---|
# | /-a
# \---|
# \-b
|
def ladderize(self, direction=0):
"""
.. versionadded: 2.1
Sort the branches of a given tree (swapping children nodes)
according to the size of each partition.
::
t = Tree("(f,((d, ((a,b),c)),e));")
print t
#
# /-f
# |
# | /-d
# ----| |
# | /---| /-a
# | | | /---|
# | | \---| \-b
# \---| |
# | \-c
# |
# \-e
t.ladderize()
print t
# /-f
# ----|
# | /-e
# \---|
# | /-d
# \---|
# | /-c
# \---|
# | /-a
# \---|
# \-b
"""
if not self.is_leaf():
n2s = {}
for n in self.get_children():
s = n.ladderize(direction=direction)
n2s[n] = s
self.children.sort(key=lambda x: n2s[x])
if direction == 1:
self.children.reverse()
size = sum(n2s.values())
else:
size = 1
return size
|
(self, direction=0)
|
720,326 |
ete3.clustering.clustertree
|
link_to_arraytable
|
Allows to link a given arraytable object to the tree
structure under this node. Row names in the arraytable object
are expected to match leaf names.
Returns a list of nodes for with profiles could not been found
in arraytable.
|
def link_to_arraytable(self, arraytbl):
""" Allows to link a given arraytable object to the tree
structure under this node. Row names in the arraytable object
are expected to match leaf names.
Returns a list of nodes for with profiles could not been found
in arraytable.
"""
# Initialize tree with array data
if type(arraytbl) == ArrayTable:
array = arraytbl
else:
array = ArrayTable(arraytbl)
missing_leaves = []
matrix_values = [i for r in range(len(array.matrix))\
for i in array.matrix[r] if numpy.isfinite(i)]
array._matrix_min = min(matrix_values)
array._matrix_max = max(matrix_values)
for n in self.traverse():
n.arraytable = array
if n.is_leaf() and n.name in array.rowNames:
n._profile = array.get_row_vector(n.name)
elif n.is_leaf():
n._profile = [numpy.nan]*len(array.colNames)
missing_leaves.append(n)
if len(missing_leaves)>0:
print("""[%d] leaf names could not be mapped to the matrix rows.""" %\
len(missing_leaves), file=stderr)
self.arraytable = array
|
(self, arraytbl)
|
720,327 |
ete3.coretype.tree
|
phonehome
| null |
def phonehome(self):
from .. import _ph
_ph.call()
|
(self)
|
720,328 |
ete3.coretype.tree
|
populate
|
Generates a random topology by populating current node.
:argument None names_library: If provided, names library
(list, set, dict, etc.) will be used to name nodes.
:argument False reuse_names: If True, node names will not be
necessarily unique, which makes the process a bit more
efficient.
:argument False random_branches: If True, branch distances and support
values will be randomized.
:argument (0,1) branch_range: If random_branches is True, this
range of values will be used to generate random distances.
:argument (0,1) support_range: If random_branches is True,
this range of values will be used to generate random branch
support values.
|
def populate(self, size, names_library=None, reuse_names=False,
random_branches=False, branch_range=(0,1),
support_range=(0,1)):
"""
Generates a random topology by populating current node.
:argument None names_library: If provided, names library
(list, set, dict, etc.) will be used to name nodes.
:argument False reuse_names: If True, node names will not be
necessarily unique, which makes the process a bit more
efficient.
:argument False random_branches: If True, branch distances and support
values will be randomized.
:argument (0,1) branch_range: If random_branches is True, this
range of values will be used to generate random distances.
:argument (0,1) support_range: If random_branches is True,
this range of values will be used to generate random branch
support values.
"""
NewNode = self.__class__
if len(self.children) > 1:
connector = NewNode()
for ch in self.get_children():
ch.detach()
connector.add_child(child = ch)
root = NewNode()
self.add_child(child = connector)
self.add_child(child = root)
else:
root = self
next_deq = deque([root])
for i in range(size-1):
if random.randint(0, 1):
p = next_deq.pop()
else:
p = next_deq.popleft()
c1 = p.add_child()
c2 = p.add_child()
next_deq.extend([c1, c2])
if random_branches:
c1.dist = random.uniform(*branch_range)
c2.dist = random.uniform(*branch_range)
c1.support = random.uniform(*branch_range)
c2.support = random.uniform(*branch_range)
else:
c1.dist = 1.0
c2.dist = 1.0
c1.support = 1.0
c2.support = 1.0
# next contains leaf nodes
charset = "abcdefghijklmnopqrstuvwxyz"
if names_library:
names_library = deque(names_library)
else:
avail_names = itertools.combinations_with_replacement(charset, 10)
for n in next_deq:
if names_library:
if reuse_names:
tname = random.sample(names_library, 1)[0]
else:
tname = names_library.pop()
else:
tname = ''.join(next(avail_names))
n.name = tname
|
(self, size, names_library=None, reuse_names=False, random_branches=False, branch_range=(0, 1), support_range=(0, 1))
|
720,329 |
ete3.coretype.tree
|
prune
|
Prunes the topology of a node to conserve only the selected list of leaf
internal nodes. The minimum number of nodes that conserve the
topological relationships among the requested nodes will be
retained. Root node is always conserved.
:var nodes: a list of node names or node objects that should be retained
:param False preserve_branch_length: If True, branch lengths
of the deleted nodes are transferred (summed up) to its
parent's branch, thus keeping original distances among
nodes.
**Examples:**
::
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E
# | | /-A
#-root \-G -root
# | \-B
# | /-I
# \K|
# \-J
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B', 'C'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E
# | | /-A
#-root \-G -root- C|
# | \-B
# | /-I
# \K|
# \-J
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B', 'I'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E /-I
# | | -root
#-root \-G | /-A
# | \C|
# | /-I \-B
# \K|
# \-J
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B', 'F', 'H'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E
# | | /-A
#-root \-G -root-H /F|
# | \-B
# | /-I
# \K|
# \-J
|
def prune(self, nodes, preserve_branch_length=False):
"""Prunes the topology of a node to conserve only the selected list of leaf
internal nodes. The minimum number of nodes that conserve the
topological relationships among the requested nodes will be
retained. Root node is always conserved.
:var nodes: a list of node names or node objects that should be retained
:param False preserve_branch_length: If True, branch lengths
of the deleted nodes are transferred (summed up) to its
parent's branch, thus keeping original distances among
nodes.
**Examples:**
::
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E
# | | /-A
#-root \-G -root
# | \-B
# | /-I
# \K|
# \-J
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B', 'C'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E
# | | /-A
#-root \-G -root- C|
# | \-B
# | /-I
# \K|
# \-J
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B', 'I'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E /-I
# | | -root
#-root \-G | /-A
# | \C|
# | /-I \-B
# \K|
# \-J
t1 = Tree('(((((A,B)C)D,E)F,G)H,(I,J)K)root;', format=1)
t1.prune(['A', 'B', 'F', 'H'])
# /-A
# /D /C|
# /F| \-B
# | |
# /H| \-E
# | | /-A
#-root \-G -root-H /F|
# | \-B
# | /-I
# \K|
# \-J
"""
def cmp_nodes(x, y):
# if several nodes are in the same path of two kept nodes,
# only one should be maintained. This prioritize internal
# nodes that are already in the to_keep list and then
# deeper nodes (closer to the leaves).
if n2depth[x] > n2depth[y]:
return -1
elif n2depth[x] < n2depth[y]:
return 1
else:
return 0
to_keep = set(_translate_nodes(self, *nodes))
start, node2path = self.get_common_ancestor(to_keep, get_path=True)
to_keep.add(self)
# Calculate which kept nodes are visiting the same nodes in
# their path to the common ancestor.
n2count = {}
n2depth = {}
for seed, path in six.iteritems(node2path):
for visited_node in path:
if visited_node not in n2depth:
depth = visited_node.get_distance(start, topology_only=True)
n2depth[visited_node] = depth
if visited_node is not seed:
n2count.setdefault(visited_node, set()).add(seed)
# if several internal nodes are in the path of exactly the same kept
# nodes, only one (the deepest) should be maintain.
visitors2nodes = {}
for node, visitors in six.iteritems(n2count):
# keep nodes connection at least two other nodes
if len(visitors)>1:
visitor_key = frozenset(visitors)
visitors2nodes.setdefault(visitor_key, set()).add(node)
for visitors, nodes in six.iteritems(visitors2nodes):
if not (to_keep & nodes):
sorted_nodes = sorted(nodes, key=cmp_to_key(cmp_nodes))
to_keep.add(sorted_nodes[0])
for n in self.get_descendants('postorder'):
if n not in to_keep:
if preserve_branch_length:
if len(n.children) == 1:
n.children[0].dist += n.dist
elif len(n.children) > 1 and n.up:
n.up.dist += n.dist
n.delete(prevent_nondicotomic=False)
|
(self, nodes, preserve_branch_length=False)
|
720,330 |
ete3.coretype.tree
|
remove_child
|
Removes a child from this node (parent and child
nodes still exit but are no longer connected).
|
def remove_child(self, child):
"""
Removes a child from this node (parent and child
nodes still exit but are no longer connected).
"""
try:
self.children.remove(child)
except ValueError as e:
raise TreeError("child not found")
else:
child.up = None
return child
|
(self, child)
|
720,331 |
ete3.coretype.tree
|
remove_sister
|
Removes a sister node. It has the same effect as
**`TreeNode.up.remove_child(sister)`**
If a sister node is not supplied, the first sister will be deleted
and returned.
:argument sister: A node instance
:return: The node removed
|
def remove_sister(self, sister=None):
"""
Removes a sister node. It has the same effect as
**`TreeNode.up.remove_child(sister)`**
If a sister node is not supplied, the first sister will be deleted
and returned.
:argument sister: A node instance
:return: The node removed
"""
sisters = self.get_sisters()
if len(sisters) > 0:
if sister is None:
sister = sisters.pop(0)
return self.up.remove_child(sister)
|
(self, sister=None)
|
720,332 |
ete3.coretype.tree
|
render
|
Renders the node structure as an image.
:var file_name: path to the output image file. valid
extensions are .SVG, .PDF, .PNG
:var layout: a layout function or a valid layout function name
:var tree_style: a `TreeStyle` instance containing the image
properties
:var px units: "px": pixels, "mm": millimeters, "in": inches
:var None h: height of the image in :attr:`units`
:var None w: width of the image in :attr:`units`
:var 90 dpi: dots per inches.
|
def render(self, file_name, layout=None, w=None, h=None, \
tree_style=None, units="px", dpi=90):
"""
Renders the node structure as an image.
:var file_name: path to the output image file. valid
extensions are .SVG, .PDF, .PNG
:var layout: a layout function or a valid layout function name
:var tree_style: a `TreeStyle` instance containing the image
properties
:var px units: "px": pixels, "mm": millimeters, "in": inches
:var None h: height of the image in :attr:`units`
:var None w: width of the image in :attr:`units`
:var 90 dpi: dots per inches.
"""
from ..treeview import drawer
if file_name.startswith('%%return'):
return drawer.get_img(self, w=w, h=h,
layout=layout, tree_style=tree_style,
units=units, dpi=dpi, return_format=file_name)
else:
return drawer.render_tree(self, file_name, w=w, h=h,
layout=layout, tree_style=tree_style,
units=units, dpi=dpi)
|
(self, file_name, layout=None, w=None, h=None, tree_style=None, units='px', dpi=90)
|
720,333 |
ete3.coretype.tree
|
resolve_polytomy
|
.. versionadded: 2.2
Resolve all polytomies under current node by creating an
arbitrary dicotomic structure among the affected nodes. This
function randomly modifies current tree topology and should
only be used for compatibility reasons (i.e. programs
rejecting multifurcated node in the newick representation).
:param 0.0 default_dist: artificial branch distance of new
nodes.
:param 0.0 default_support: artificial branch support of new
nodes.
:param True recursive: Resolve any polytomy under this
node. When False, only current node will be checked and fixed.
|
def resolve_polytomy(self, default_dist=0.0, default_support=0.0,
recursive=True):
"""
.. versionadded: 2.2
Resolve all polytomies under current node by creating an
arbitrary dicotomic structure among the affected nodes. This
function randomly modifies current tree topology and should
only be used for compatibility reasons (i.e. programs
rejecting multifurcated node in the newick representation).
:param 0.0 default_dist: artificial branch distance of new
nodes.
:param 0.0 default_support: artificial branch support of new
nodes.
:param True recursive: Resolve any polytomy under this
node. When False, only current node will be checked and fixed.
"""
def _resolve(node):
if len(node.children) > 2:
children = list(node.children)
node.children = []
next_node = root = node
for i in range(len(children)-2):
next_node = next_node.add_child()
next_node.dist = default_dist
next_node.support = default_support
next_node = root
for ch in children:
next_node.add_child(ch)
if ch != children[-2]:
next_node = next_node.children[0]
target = [self]
if recursive:
target.extend([n for n in self.get_descendants()])
for n in target:
_resolve(n)
|
(self, default_dist=0.0, default_support=0.0, recursive=True)
|
720,334 |
ete3.coretype.tree
|
robinson_foulds
|
.. versionadded: 2.2
Returns the Robinson-Foulds symmetric distance between current
tree and a different tree instance.
:param t2: reference tree
:param name attr_t1: Compare trees using a custom node
attribute as a node name.
:param name attr_t2: Compare trees using a custom node
attribute as a node name in target tree.
:param False unrooted_trees: If True, consider trees as unrooted.
:param False expand_polytomies: If True, all polytomies in the reference
and target tree will be expanded into all possible binary
trees. Robinson-foulds distance will be calculated between all
tree combinations and the minimum value will be returned.
See also, :func:`NodeTree.expand_polytomy`.
:returns: (rf, rf_max, common_attrs, names, edges_t1, edges_t2, discarded_edges_t1, discarded_edges_t2)
|
def robinson_foulds(self, t2, attr_t1="name", attr_t2="name",
unrooted_trees=False, expand_polytomies=False,
polytomy_size_limit=5, skip_large_polytomies=False,
correct_by_polytomy_size=False, min_support_t1=0.0,
min_support_t2=0.0):
"""
.. versionadded: 2.2
Returns the Robinson-Foulds symmetric distance between current
tree and a different tree instance.
:param t2: reference tree
:param name attr_t1: Compare trees using a custom node
attribute as a node name.
:param name attr_t2: Compare trees using a custom node
attribute as a node name in target tree.
:param False unrooted_trees: If True, consider trees as unrooted.
:param False expand_polytomies: If True, all polytomies in the reference
and target tree will be expanded into all possible binary
trees. Robinson-foulds distance will be calculated between all
tree combinations and the minimum value will be returned.
See also, :func:`NodeTree.expand_polytomy`.
:returns: (rf, rf_max, common_attrs, names, edges_t1, edges_t2, discarded_edges_t1, discarded_edges_t2)
"""
ref_t = self
target_t = t2
if not unrooted_trees and (len(ref_t.children) > 2 or len(target_t.children) > 2):
raise TreeError("Unrooted tree found! You may want to activate the unrooted_trees flag.")
if expand_polytomies and correct_by_polytomy_size:
raise TreeError("expand_polytomies and correct_by_polytomy_size are mutually exclusive.")
if expand_polytomies and unrooted_trees:
raise TreeError("expand_polytomies and unrooted_trees arguments cannot be enabled at the same time")
attrs_t1 = set([getattr(n, attr_t1) for n in ref_t.iter_leaves() if hasattr(n, attr_t1)])
attrs_t2 = set([getattr(n, attr_t2) for n in target_t.iter_leaves() if hasattr(n, attr_t2)])
common_attrs = attrs_t1 & attrs_t2
# release mem
attrs_t1, attrs_t2 = None, None
# Check for duplicated items (is it necessary? can we optimize? what's the impact in performance?')
size1 = len([True for n in ref_t.iter_leaves() if getattr(n, attr_t1, None) in common_attrs])
size2 = len([True for n in target_t.iter_leaves() if getattr(n, attr_t2, None) in common_attrs])
if size1 > len(common_attrs):
raise TreeError('Duplicated items found in source tree')
if size2 > len(common_attrs):
raise TreeError('Duplicated items found in reference tree')
if expand_polytomies:
ref_trees = [Tree(nw) for nw in
ref_t.expand_polytomies(map_attr=attr_t1,
polytomy_size_limit=polytomy_size_limit,
skip_large_polytomies=skip_large_polytomies)]
target_trees = [Tree(nw) for nw in
target_t.expand_polytomies(map_attr=attr_t2,
polytomy_size_limit=polytomy_size_limit,
skip_large_polytomies=skip_large_polytomies)]
attr_t1, attr_t2 = "name", "name"
else:
ref_trees = [ref_t]
target_trees = [target_t]
polytomy_correction = 0
if correct_by_polytomy_size:
corr1 = sum([0]+[len(n.children) - 2 for n in ref_t.traverse() if len(n.children) > 2])
corr2 = sum([0]+[len(n.children) - 2 for n in target_t.traverse() if len(n.children) > 2])
if corr1 and corr2:
raise TreeError("Both trees contain polytomies! Try expand_polytomies=True instead")
else:
polytomy_correction = max([corr1, corr2])
min_comparison = None
for t1 in ref_trees:
t1_content = t1.get_cached_content()
t1_leaves = t1_content[t1]
if unrooted_trees:
edges1 = set([
tuple(sorted([tuple(sorted([getattr(n, attr_t1) for n in content if hasattr(n, attr_t1) and getattr(n, attr_t1) in common_attrs])),
tuple(sorted([getattr(n, attr_t1) for n in t1_leaves-content if hasattr(n, attr_t1) and getattr(n, attr_t1) in common_attrs]))]))
for content in six.itervalues(t1_content)])
edges1.discard(((),()))
else:
edges1 = set([
tuple(sorted([getattr(n, attr_t1) for n in content if hasattr(n, attr_t1) and getattr(n, attr_t1) in common_attrs]))
for content in six.itervalues(t1_content)])
edges1.discard(())
if min_support_t1:
support_t1 = dict([
(tuple(sorted([getattr(n, attr_t1) for n in content if hasattr(n, attr_t1) and getattr(n, attr_t1) in common_attrs])), branch.support)
for branch, content in six.iteritems(t1_content)])
for t2 in target_trees:
t2_content = t2.get_cached_content()
t2_leaves = t2_content[t2]
if unrooted_trees:
edges2 = set([
tuple(sorted([
tuple(sorted([getattr(n, attr_t2) for n in content if hasattr(n, attr_t2) and getattr(n, attr_t2) in common_attrs])),
tuple(sorted([getattr(n, attr_t2) for n in t2_leaves-content if hasattr(n, attr_t2) and getattr(n, attr_t2) in common_attrs]))]))
for content in six.itervalues(t2_content)])
edges2.discard(((),()))
else:
edges2 = set([
tuple(sorted([getattr(n, attr_t2) for n in content if hasattr(n, attr_t2) and getattr(n, attr_t2) in common_attrs]))
for content in six.itervalues(t2_content)])
edges2.discard(())
if min_support_t2:
support_t2 = dict([
(tuple(sorted(([getattr(n, attr_t2) for n in content if hasattr(n, attr_t2) and getattr(n, attr_t2) in common_attrs]))), branch.support)
for branch, content in six.iteritems(t2_content)])
# if a support value is passed as a constraint, discard lowly supported branches from the analysis
discard_t1, discard_t2 = set(), set()
if min_support_t1 and unrooted_trees:
discard_t1 = set([p for p in edges1 if support_t1.get(p[0], support_t1.get(p[1], 999999999)) < min_support_t1])
elif min_support_t1:
discard_t1 = set([p for p in edges1 if support_t1[p] < min_support_t1])
if min_support_t2 and unrooted_trees:
discard_t2 = set([p for p in edges2 if support_t2.get(p[0], support_t2.get(p[1], 999999999)) < min_support_t2])
elif min_support_t2:
discard_t2 = set([p for p in edges2 if support_t2[p] < min_support_t2])
#rf = len(edges1 ^ edges2) - (len(discard_t1) + len(discard_t2)) - polytomy_correction # poly_corr is 0 if the flag is not enabled
#rf = len((edges1-discard_t1) ^ (edges2-discard_t2)) - polytomy_correction
# the two root edges are never counted here, as they are always
# present in both trees because of the common attr filters
rf = len(((edges1 ^ edges2) - discard_t2) - discard_t1) - polytomy_correction
if unrooted_trees:
# thought this may work, but it does not, still I don't see why
#max_parts = (len(common_attrs)*2) - 6 - len(discard_t1) - len(discard_t2)
max_parts = (len([p for p in edges1 - discard_t1 if len(p[0])>1 and len(p[1])>1]) +
len([p for p in edges2 - discard_t2 if len(p[0])>1 and len(p[1])>1]))
else:
# thought this may work, but it does not, still I don't see why
#max_parts = (len(common_attrs)*2) - 4 - len(discard_t1) - len(discard_t2)
# Otherwise we need to count the actual number of valid
# partitions in each tree -2 is to avoid counting the root
# partition of the two trees (only needed in rooted trees)
max_parts = (len([p for p in edges1 - discard_t1 if len(p)>1]) +
len([p for p in edges2 - discard_t2 if len(p)>1])) - 2
# print max_parts
if not min_comparison or min_comparison[0] > rf:
min_comparison = [rf, max_parts, common_attrs, edges1, edges2, discard_t1, discard_t2]
return min_comparison
|
(self, t2, attr_t1='name', attr_t2='name', unrooted_trees=False, expand_polytomies=False, polytomy_size_limit=5, skip_large_polytomies=False, correct_by_polytomy_size=False, min_support_t1=0.0, min_support_t2=0.0)
|
720,335 |
ete3.coretype.tree
|
search_nodes
|
Returns the list of nodes matching a given set of conditions.
**Example:**
::
tree.search_nodes(dist=0.0, name="human")
|
def search_nodes(self, **conditions):
"""
Returns the list of nodes matching a given set of conditions.
**Example:**
::
tree.search_nodes(dist=0.0, name="human")
"""
matching_nodes = []
for n in self.iter_search_nodes(**conditions):
matching_nodes.append(n)
return matching_nodes
|
(self, **conditions)
|
720,336 |
ete3.clustering.clustertree
|
set_distance_function
|
Sets the distance function used to calculate cluster
distances and silouette index.
ARGUMENTS:
fn: a pointer to python function acepting two arrays (numpy) as
arguments.
EXAMPLE:
# A simple euclidean distance
my_dist_fn = lambda x,y: abs(x-y)
tree.set_distance_function(my_dist_fn)
|
def set_distance_function(self, fn):
""" Sets the distance function used to calculate cluster
distances and silouette index.
ARGUMENTS:
fn: a pointer to python function acepting two arrays (numpy) as
arguments.
EXAMPLE:
# A simple euclidean distance
my_dist_fn = lambda x,y: abs(x-y)
tree.set_distance_function(my_dist_fn)
"""
for n in self.traverse():
n._fdist = fn
n._silhouette = None
n._intercluster_dist = None
n._intracluster_dist = None
|
(self, fn)
|
720,337 |
ete3.coretype.tree
|
set_outgroup
|
Sets a descendant node as the outgroup of a tree. This function
can be used to root a tree or even an internal node.
:argument outgroup: a node instance within the same tree
structure that will be used as a basal node.
|
def set_outgroup(self, outgroup):
"""
Sets a descendant node as the outgroup of a tree. This function
can be used to root a tree or even an internal node.
:argument outgroup: a node instance within the same tree
structure that will be used as a basal node.
"""
outgroup = _translate_nodes(self, outgroup)
if self == outgroup:
raise TreeError("Cannot set myself as outgroup")
parent_outgroup = outgroup.up
# Detects (sub)tree root
n = outgroup
while n.up is not self:
n = n.up
# If outgroup is a child from root, but with more than one
# sister nodes, creates a new node to group them
self.children.remove(n)
if len(self.children) != 1:
down_branch_connector = self.__class__()
down_branch_connector.dist = 0.0
down_branch_connector.support = n.support
for ch in self.get_children():
down_branch_connector.children.append(ch)
ch.up = down_branch_connector
self.children.remove(ch)
else:
down_branch_connector = self.children[0]
# Connects down branch to myself or to outgroup
quien_va_ser_padre = parent_outgroup
if quien_va_ser_padre is not self:
# Parent-child swapping
quien_va_ser_hijo = quien_va_ser_padre.up
quien_fue_padre = None
buffered_dist = quien_va_ser_padre.dist
buffered_support = quien_va_ser_padre.support
while quien_va_ser_hijo is not self:
quien_va_ser_padre.children.append(quien_va_ser_hijo)
quien_va_ser_hijo.children.remove(quien_va_ser_padre)
buffered_dist2 = quien_va_ser_hijo.dist
buffered_support2 = quien_va_ser_hijo.support
quien_va_ser_hijo.dist = buffered_dist
quien_va_ser_hijo.support = buffered_support
buffered_dist = buffered_dist2
buffered_support = buffered_support2
quien_va_ser_padre.up = quien_fue_padre
quien_fue_padre = quien_va_ser_padre
quien_va_ser_padre = quien_va_ser_hijo
quien_va_ser_hijo = quien_va_ser_padre.up
quien_va_ser_padre.children.append(down_branch_connector)
down_branch_connector.up = quien_va_ser_padre
quien_va_ser_padre.up = quien_fue_padre
down_branch_connector.dist += buffered_dist
outgroup2 = parent_outgroup
parent_outgroup.children.remove(outgroup)
outgroup2.dist = 0
else:
outgroup2 = down_branch_connector
outgroup.up = self
outgroup2.up = self
# outgroup is always the first children. Some function my
# trust on this fact, so do no change this.
self.children = [outgroup,outgroup2]
middist = (outgroup2.dist + outgroup.dist)/2
outgroup.dist = middist
outgroup2.dist = middist
outgroup2.support = outgroup.support
|
(self, outgroup)
|
720,338 |
ete3.coretype.tree
|
set_style
|
.. versionadded: 2.1
Set 'node_style' as the fixed style for the current node.
|
def set_style(self, node_style):
"""
.. versionadded: 2.1
Set 'node_style' as the fixed style for the current node.
"""
if TREEVIEW:
if node_style is None:
node_style = NodeStyle()
if type(node_style) is NodeStyle:
self._img_style = node_style
else:
raise ValueError("Treeview module is disabled")
|
(self, node_style)
|
720,339 |
ete3.coretype.tree
|
show
|
Starts an interactive session to visualize current node
structure using provided layout and TreeStyle.
|
def show(self, layout=None, tree_style=None, name="ETE"):
"""
Starts an interactive session to visualize current node
structure using provided layout and TreeStyle.
"""
from ..treeview import drawer
drawer.show_tree(self, layout=layout,
tree_style=tree_style, win_name=name)
|
(self, layout=None, tree_style=None, name='ETE')
|
720,340 |
ete3.coretype.tree
|
sort_descendants
|
.. versionadded: 2.1
Sort the branches of a given tree by node names. After the
tree is sorted. Note that if duplicated names are present,
extra criteria should be added to sort nodes.
|
def sort_descendants(self, attr="name"):
"""
.. versionadded: 2.1
Sort the branches of a given tree by node names. After the
tree is sorted. Note that if duplicated names are present,
extra criteria should be added to sort nodes.
"""
node2content = self.get_cached_content(store_attr=attr, container_type=list)
for n in self.traverse():
if not n.is_leaf():
n.children.sort(key=lambda x: str(sorted(node2content[x])))
|
(self, attr='name')
|
720,341 |
ete3.coretype.tree
|
standardize
|
.. versionadded:: 2.3
process current tree structure to produce a standardized topology: nodes
with only one child are removed and multifurcations are automatically resolved.
|
def standardize(self, delete_orphan=True, preserve_branch_length=True):
"""
.. versionadded:: 2.3
process current tree structure to produce a standardized topology: nodes
with only one child are removed and multifurcations are automatically resolved.
"""
self.resolve_polytomy()
for n in self.get_descendants():
if len(n.children) == 1:
n.delete(prevent_nondicotomic=True,
preserve_branch_length=preserve_branch_length)
|
(self, delete_orphan=True, preserve_branch_length=True)
|
720,342 |
ete3.coretype.tree
|
swap_children
|
Swaps current children order.
|
def swap_children(self):
"""
Swaps current children order.
"""
if len(self.children)>1:
self.children.reverse()
|
(self)
|
720,343 |
ete3.coretype.tree
|
traverse
|
Returns an iterator to traverse the tree structure under this
node.
:argument "levelorder" strategy: set the way in which tree
will be traversed. Possible values are: "preorder" (first
parent and then children) 'postorder' (first children and
the parent) and "levelorder" (nodes are visited in order
from root to leaves)
:argument None is_leaf_fn: If supplied, ``is_leaf_fn``
function will be used to interrogate nodes about if they
are terminal or internal. ``is_leaf_fn`` function should
receive a node instance as first argument and return True
or False. Use this argument to traverse a tree by
dynamically collapsing internal nodes matching
``is_leaf_fn``.
|
def traverse(self, strategy="levelorder", is_leaf_fn=None):
"""
Returns an iterator to traverse the tree structure under this
node.
:argument "levelorder" strategy: set the way in which tree
will be traversed. Possible values are: "preorder" (first
parent and then children) 'postorder' (first children and
the parent) and "levelorder" (nodes are visited in order
from root to leaves)
:argument None is_leaf_fn: If supplied, ``is_leaf_fn``
function will be used to interrogate nodes about if they
are terminal or internal. ``is_leaf_fn`` function should
receive a node instance as first argument and return True
or False. Use this argument to traverse a tree by
dynamically collapsing internal nodes matching
``is_leaf_fn``.
"""
if strategy=="preorder":
return self._iter_descendants_preorder(is_leaf_fn=is_leaf_fn)
elif strategy=="levelorder":
return self._iter_descendants_levelorder(is_leaf_fn=is_leaf_fn)
elif strategy=="postorder":
return self._iter_descendants_postorder(is_leaf_fn=is_leaf_fn)
|
(self, strategy='levelorder', is_leaf_fn=None)
|
720,344 |
ete3.coretype.tree
|
unroot
|
Unroots current node. This function is expected to be used on
the absolute tree root node, but it can be also be applied to
any other internal node. It will convert a split into a
multifurcation.
:argument "legacy" mode: The value can be "legacy" or "keep".
If value is "keep", then function keeps the distance between
the leaves by adding the distance associated to the deleted
edge to the remaining edge. In the other case the distance
value of the deleted edge is dropped
|
def unroot(self, mode='legacy'):
"""
Unroots current node. This function is expected to be used on
the absolute tree root node, but it can be also be applied to
any other internal node. It will convert a split into a
multifurcation.
:argument "legacy" mode: The value can be "legacy" or "keep".
If value is "keep", then function keeps the distance between
the leaves by adding the distance associated to the deleted
edge to the remaining edge. In the other case the distance
value of the deleted edge is dropped
"""
if not (mode == 'legacy' or mode == 'keep'):
raise ValueError("The value of the mode parameter must be 'legacy' or 'keep'")
if len(self.children)==2:
if not self.children[0].is_leaf():
if mode == "keep":
self.children[1].dist+=self.children[0].dist
self.children[0].delete()
elif not self.children[1].is_leaf():
if mode == "keep":
self.children[0].dist+=self.children[1].dist
self.children[1].delete()
else:
raise TreeError("Cannot unroot a tree with only two leaves")
|
(self, mode='legacy')
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.