本文整理匯總了Python中ete3.Tree方法的典型用法代碼示例。如果您正苦於以下問題:Python ete3.Tree方法的具體用法?Python ete3.Tree怎麽用?Python ete3.Tree使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ete3
的用法示例。
在下文中一共展示了ete3.Tree方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: closest_dna_dist
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def closest_dna_dist(treefile):
"""
Using get closest leaf in ete which according to the description gets the closest descendent leaf but may or may not!
Note that this may not be symmetric.
:param treefile: The tree file to read
:return: a dict of a node and its closest leaf
"""
global verbose
if verbose:
sys.stderr.write("Getting closest distances\n")
tree = Tree(treefile)
dist = {}
leaves = tree.get_leaves()
# prepopulate the hash
for l in leaves:
dist[l.name] = {}
for i in range(len(leaves)):
closest, distance = leaves[i].get_closest_leaf()
dist[leaves[i].name][closest.name] = distance
if verbose:
sys.stderr.write("{} -> {} : {}\n".format(leaves[i].name, closest.name, distance))
if verbose:
sys.stderr.write("\tDone\n")
return dist
示例2: parse_jplacer_tree
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def parse_jplacer_tree(data):
"""
Extract the tree from the jplacer data structure and make it into an ete3 object
:param data: the jplacer data structure
:return:
"""
try:
tree = Tree(data['tree'], quoted_node_names=True, format=1)
except NewickError as n:
tt = re.sub(r'(\:[\d\.]+){\d+}', r'\1', data['tree'])
tt = re.sub(r'{\d+};$', ';', tt)
tree = Tree(tt, quoted_node_names=True, format=1)
return tree
示例3: readTree
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def readTree(newick_tree):
try:
if newick_tree[0] == "[": return ete3.Tree(newick_tree[newick_tree.index("]")+1:])
else: return ete3.Tree(newick_tree)
except:
return None
示例4: allTopos
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def allTopos(branches, _topos=None, _topo_IDs=None):
if _topos is None or _topo_IDs is None:
_topos = []
_topo_IDs = set([])
assert 4 <= len(branches) <= 8, "Please specify between 4 and 8 unique taxon names."
#print("topos contains", len(_topos), "topologies.")
#print("current tree is:", branches)
for x in range(len(branches)-1):
for y in range(x+1,len(branches)):
#print("Joining branch", x, branches[x], "with branch", y, branches[y])
new_branches = list(branches)
new_branches[x] = [new_branches[x],new_branches.pop(y)]
#print("New tree is:", new_branches)
if len(new_branches) == 3:
#print("Tree has three branches, so appending to topos.")
#now check that the topo doesn't match a topology already in trees, and if not add it
t = ete3.Tree(listToNwk(new_branches))
ID = t.get_topology_id()
if ID not in _topo_IDs:
_topos.append(t)
_topo_IDs.add(ID)
else:
#print("Tree still unresolved, so re-calling function.")
_topos = allTopos(new_branches, _topos, _topo_IDs)
#print(_topo_IDs)
return(_topos)
示例5: get_tree
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def get_tree(hal):
"""
Extracts a Tree object from a HAL
:param hal: HAL file.
:return: Tree object
"""
cmd = ['halStats', '--tree', hal]
newick = call_proc_lines(cmd)[0]
return ete3.Tree(newick, format=1)
示例6: migrate
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def migrate(db_path):
if db_path is None:
raise ConfigError("No database path is given.")
# make sure someone is not being funny
utils.is_profile_db(db_path)
# make sure the version is accurate
profile_db = db.DB(db_path, None, ignore_version = True)
if str(profile_db.get_version()) != current_version:
raise ConfigError("Version of this profile database is not %s (hence, this script cannot really do anything)." % current_version)
# migrate item orders
item_orders = profile_db.get_table_as_dict(item_orders_table_name)
for order_name in item_orders:
if item_orders[order_name]['type'] == 'newick':
newick = Tree(item_orders[order_name]['data'], format=1)
newick = newick.write(format=2)
profile_db._exec("""UPDATE %s SET "data" = ? WHERE "name" LIKE ?""" % item_orders_table_name, (newick, order_name))
# migrate layer orders
layer_orders = profile_db.get_table_as_dict(layer_orders_table_name)
for order_name in layer_orders:
if layer_orders[order_name]['data_type'] == 'newick':
newick = Tree(layer_orders[order_name]['data_value'], format=1)
newick = newick.write(format=2)
profile_db._exec("""UPDATE %s SET "data_value" = ? WHERE "data_key" LIKE ?""" % layer_orders_table_name, (newick, order_name))
# set the version
profile_db.remove_meta_key_value_pair('version')
profile_db.set_version(next_version)
# bye
profile_db.disconnect()
progress.end()
run.info_single('Your profile db is now %s. Aww, yisss.' % next_version, nl_after=1, nl_before=1, mc='green')
示例7: migrate
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def migrate(db_path):
if db_path is None:
raise ConfigError("No database path is given.")
# make sure someone is not being funny
utils.is_pan_db(db_path)
# make sure the version is accurate
pan_db = db.DB(db_path, None, ignore_version = True)
if str(pan_db.get_version()) != current_version:
raise ConfigError("Version of this pan database is not %s (hence, this script cannot really do anything)." % current_version)
# migrate item orders
item_orders = pan_db.get_table_as_dict(item_orders_table_name)
for order_name in item_orders:
if item_orders[order_name]['type'] == 'newick':
newick = Tree(item_orders[order_name]['data'], format=1)
newick = newick.write(format=2)
pan_db._exec("""UPDATE %s SET "data" = ? WHERE "name" LIKE ?""" % item_orders_table_name, (newick, order_name))
# migrate layer orders
layer_orders = pan_db.get_table_as_dict(layer_orders_table_name)
for order_name in layer_orders:
if layer_orders[order_name]['data_type'] == 'newick':
newick = Tree(layer_orders[order_name]['data_value'], format=1)
newick = newick.write(format=2)
pan_db._exec("""UPDATE %s SET "data_value" = ? WHERE "data_key" LIKE ?""" % layer_orders_table_name, (newick, order_name))
# set the version
pan_db.remove_meta_key_value_pair('version')
pan_db.set_version(next_version)
# now bye for real!
pan_db.disconnect()
progress.end()
run.info_single('Your pan db is now %s.' % next_version, nl_after=1, nl_before=1, mc='green')
示例8: is_proper_newick
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def is_proper_newick(newick_data, dont_raise=False, names_with_only_digits_ok=False):
try:
tree = Tree(newick_data, format=1)
seen = set([])
duplicates = set([])
for leaf in tree.get_leaves():
name = leaf.name
if name in seen:
duplicates.add(name)
seen.add(name)
if len(duplicates):
raise Exception("Your newick tree contains duplicate leaves, here is a list of them: %s" % ", ".join(duplicates))
except Exception as e:
if dont_raise:
return False
else:
raise FilesNPathsError("Your tree doesn't seem to be properly formatted. Here is what ETE had "
"to say about this: '%s'. Pity :/" % e)
names_with_only_digits = [n.name for n in tree.get_leaves() if n.name.isdigit()]
if len(names_with_only_digits) and not names_with_only_digits_ok:
raise FilesNPathsError("Your tree contains names that are composed of only digits (like this one: '%s'). Sadly, anvi'o "
"is not happy with such names in newick trees or clustering dendrograms :( Anvi'o developers "
"apologize for the inconvenience." % (names_with_only_digits[0]))
return True
示例9: get_names_order_from_newick_tree
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def get_names_order_from_newick_tree(newick_tree, newick_format=1, reverse=False, names_with_only_digits_ok=False):
filesnpaths.is_proper_newick(newick_tree, names_with_only_digits_ok=names_with_only_digits_ok)
tree = Tree(newick_tree, format=newick_format)
names = [n.name for n in tree.get_leaves()]
return list(reversed(names)) if reverse else names
示例10: save_tree
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def save_tree(self):
try:
order_full_name = request.forms.get('name')
order_data = request.forms.get('data')
tree_type = request.forms.get('tree_type')
additional = request.forms.get('additional')
if tree_type == 'samples':
order_name = order_full_name
distance = 'NA'
linkage = 'NA'
if order_name in self.interactive.layers_order_data_dict:
raise ConfigError("Tree name '%s' already exists, overwriting currently not supported." % order_name)
self.interactive.layers_order_data_dict[order_name] = {'newick': order_data, 'basic': ''}
TableForLayerOrders(self.interactive.args).add({order_name: {'data_type': 'newick', 'data_value': order_data}})
else:
self.interactive.p_meta['item_orders'][order_full_name] = {'type': 'newick', 'data': order_data, 'additional': additional}
order_name, distance, linkage = order_full_name.split(':')
anvio_db_path = self.interactive.pan_db_path or self.interactive.profile_db_path
dbops.add_items_order_to_db(anvio_db_path, order_name, order_data, order_data_type_newick=True, distance=distance, linkage=linkage, additional_data=additional, dont_overwrite=True)
return json.dumps({'status': 0, 'message': 'New order "%s (D: %s; L: %s)" successfully saved to the database.' % (order_name, distance, linkage)})
except ConfigError as e:
message = str(e.clear_text()) if hasattr(e, 'clear_text') else str(e)
return json.dumps({'status': 1, 'message': message})
示例11: load_tree
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def load_tree(treefile):
"""
Just read the tree file
:param treefile:
:return:
"""
return Tree(treefile, quoted_node_names=True, format=1)
示例12: make_dists
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def make_dists(treefile, printone, verbose):
"""
Create pairwise distances from a tree file
:param treefile: the tree file to parse
:param printone: if true we only print one copy of the pair (ie. A -> B). If false we print A->B and B->A
:param verbose: make some additional output
:return:
"""
tree = Tree(treefile)
leaves = tree.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
if verbose:
sys.stderr.write("Precalculating distances\n")
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}
if verbose:
sys.stderr.write("Iterating over the leaves\n")
for (leaf1, leaf2) in combinations(leaves, 2):
# figure out the unique nodes in the path
uniquenodes = paths[leaf1] ^ paths[leaf2]
distance = sum(x.dist for x in uniquenodes)
if printone:
if leaf1.name < leaf2.name:
print("{}\t{}\t{}".format(leaf1.name, leaf2.name, distance))
else:
print("{}\t{}\t{}".format(leaf2.name, leaf1.name, distance))
else:
print("{}\t{}\t{}".format(leaf1.name, leaf2.name, distance))
print("{}\t{}\t{}".format(leaf2.name, leaf1.name, distance))
示例13: make_matrix
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def make_matrix(treefile):
"""
Create a matrix from a tree file
:param treefile:
:return:
"""
tree = Tree(treefile)
leaves = tree.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
sys.stderr.write("Precalculating distances\n")
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}
sys.stderr.write("Iterating over the leaves\n")
for (leaf1, leaf2) in 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())
sys.stdout.write("\t".join([""] + allleaves) + "\n")
for n in allleaves:
sys.stdout.write(n + "\t")
for m in allleaves:
if m == n:
sys.stdout.write("0\t")
else:
sys.stdout.write("{}\t".format(leaf_distances[n][m]))
sys.stdout.write("\n")
示例14: phylogenetic_tree_to_cluster_format
# 需要導入模塊: import ete3 [as 別名]
# 或者: from ete3 import Tree [as 別名]
def phylogenetic_tree_to_cluster_format(tree, pairwise_estimates):
"""
Convert a phylogenetic tree to a 'cluster' data structure as in
``fastcluster``. The first two columns indicate the nodes that are joined by
the relevant node, the third indicates the distance (calculated from branch
lengths in the case of a phylogenetic tree) and the fourth the number of
leaves underneath the node. Note that the trees are rooted using
midpoint-rooting.
Example of the data structure (output from ``fastcluster``)::
[[ 3. 7. 4.26269776 2. ]
[ 0. 5. 26.75703595 2. ]
[ 2. 8. 56.16007598 2. ]
[ 9. 12. 78.91813609 3. ]
[ 1. 11. 87.91756528 3. ]
[ 4. 6. 93.04790855 2. ]
[ 14. 15. 114.71302639 5. ]
[ 13. 16. 137.94616373 8. ]
[ 10. 17. 157.29055403 10. ]]
:param tree: newick tree file
:param pairwise_estimates: pairwise Ks estimates data frame (pandas)
(only the index is used)
:return: clustering data structure, pairwise distances dictionary
"""
id_map = {
pairwise_estimates.index[i]: i for i in range(len(pairwise_estimates))}
t = Tree(tree)
# midpoint rooting
midpoint = t.get_midpoint_outgroup()
if not midpoint: # midpoint = None when their are only two leaves
midpoint = list(t.get_leaves())[0]
t.set_outgroup(midpoint)
logging.debug('Tree after rooting:\n{}'.format(t.get_ascii()))
# algorithm for getting cluster data structure
n = len(id_map)
out = []
pairwise_distances = {}
for node in t.traverse('postorder'):
if node.is_leaf():
node.name = id_map[node.name]
id_map[node.name] = node.name # add identity map for renamed nodes
# to id_map for line below
pairwise_distances[node.name] = {
id_map[x.name]: node.get_distance(x) for x in t.get_leaves()
}
else:
node.name = n
n += 1
children = node.get_children()
out.append(
[children[0].name, children[1].name,
children[0].get_distance(children[1]),
len(node.get_leaves())])
return np.array(out), pairwise_distances