本文整理汇总了Python中ete3.Tree.get_leaves方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.get_leaves方法的具体用法?Python Tree.get_leaves怎么用?Python Tree.get_leaves使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ete3.Tree
的用法示例。
在下文中一共展示了Tree.get_leaves方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tree_distances
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
def tree_distances(file):
t = Tree(file)
branch_len_out = open(file + ".patristic-dist.tsv", "w")
avg_distance_leaves = 0
# Computing patristic distance matrix
header = ""
all_leaves = t.get_leaves()
for i in all_leaves:
header = header + "\t" + i.name
nb_of_distances = 0
max_len = 0
min_len = 9999999999999999
branch_len_out.write(header+"\n")
for leaf1 in all_leaves:
row = ""
row += str(leaf1.name)
for leaf2 in all_leaves:
distance = np.clip(leaf1.get_distance(leaf2), 0.0, 99999999999999999999999999)
avg_distance_leaves += distance
row += "\t%f" % distance
nb_of_distances += 1
if distance > max_len:
max_len = distance
if distance < min_len and distance > 0:
min_len = distance
branch_len_out.write(row+"\n")
branch_len_out.close()
示例2: smart_reroot
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
def smart_reroot(treefile, outgroupfile, outfile, format=0):
"""
simple function to reroot Newick format tree using ete2
Tree reading format options see here:
http://packages.python.org/ete2/tutorial/tutorial_trees.html#reading-newick-trees
"""
tree = Tree(treefile, format=format)
leaves = [t.name for t in tree.get_leaves()][::-1]
outgroup = []
for o in must_open(outgroupfile):
o = o.strip()
for leaf in leaves:
if leaf[:len(o)] == o:
outgroup.append(leaf)
if outgroup:
break
if not outgroup:
print("Outgroup not found. Tree {0} cannot be rerooted.".format(treefile), file=sys.stderr)
return treefile
try:
tree.set_outgroup(tree.get_common_ancestor(*outgroup))
except ValueError:
assert type(outgroup) == list
outgroup = outgroup[0]
tree.set_outgroup(outgroup)
tree.write(outfile=outfile, format=format)
logging.debug("Rerooted tree printed to {0}".format(outfile))
return outfile
示例3: tree_distances_info
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
def tree_distances_info(file,scale,seq_len):
t = Tree(file)
# branch_len_matrix_f = file + ".branches-len.tsv"
branch_len_out = open(file + ".%d.patristic-dist.tsv" % seq_len, "w")
tree_info = open(file + ".%d.tree-info.txt" % seq_len, "w")
avg_distance_leaves = 0
# Computing patristic distance matrix
header = ""
all_leaves = t.get_leaves()
for i in all_leaves:
header = header + "\t" + i.name
nb_of_distances = 0
max_len = 0
min_len = 99999999999999
branch_len_out.write(header+"\n")
for leaf1 in all_leaves:
row = ""
row += str(leaf1.name)
for leaf2 in all_leaves:
avg_distance_leaves += leaf1.get_distance(leaf2)
distance = leaf1.get_distance(leaf2)
row += "\t%f" % distance
nb_of_distances += 1
if distance > max_len:
max_len = distance
if distance < min_len and distance > 0:
min_len = distance
branch_len_out.write(row+"\n")
tree_info.write("Scale_factor(1=original-tree)\t%f\n" % scale)
tree_info.write("Seq_Length\t%d\n" % seq_len)
tree_info.write("Number_of_leaves_(taxa)\t%d\n" % len(all_leaves))
tree_info.write("Minimal_patristic_distance\t%f\n" % min_len)
tree_info.write("Maximal_patristic_distance\t%f\n" % max_len)
tree_info.write("Average_patristic_distance\t%f\n" % (avg_distance_leaves/(nb_of_distances*scale)))
print("Scale_factor(1=original-tree)\t%f" % scale)
print("Seq_Length\t%d" % seq_len)
print("Number_of_leaves_(taxa)\t%d" % len(all_leaves))
print("Minimal_patristic_distance\t%f" % min_len)
print("Maximal_patristic_distance\t%f" % max_len)
print("Average_patristic_distance\t%f" % (avg_distance_leaves/(nb_of_distances*scale)))
branch_len_out.close()
tree_info.close()
示例4: make_matrix
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [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")
示例5: make_dists
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [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))
示例6: is_proper_newick
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
def is_proper_newick(newick_data, dont_raise=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)
return True
示例7:
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
for fasta in rec_dict[strain]:
concat_dict[strain]=concat_dict[strain]+str(fasta.seq)
#write out concatenated fasta
handle=open("all_concat.fasta", "w")
for rec in concat_dict:
handle.write(">"+rec+"\n"+concat_dict[rec]+"\n")
handle.close()
#SeqIO.write(list(SeqIO.parse(open("all_concat.fasta"), "fasta")), "all_concat.phy", "phylip")
#now write out tree
for node in tree_old.traverse():
if node.is_leaf():
temp=node.name.replace('p','plate')
node.name=temp
tree_old.prune(strains, preserve_branch_length=T)
write.tree(tree_old, "all_concat.newick", formatrue=1)
for leaf in collapsed.get_leaves():
temp=leaf.name.replace('p', 'plate').split("_")[0]
leaf.name=temp
collapsed.write(outfile="concat_107.newick", format=1)
test=subprocess(Popen(["/ebio/abt6_projects9/Pseudomonas_diversity/Programs/bin/ClonalFrameML", "/ebio/abt6_projects9/Pseudomonas_diversity/data/post_assembly_analysis/pan_genome/data/vis/clonalframe/concat_107.newick", "/ebio/abt6_projects9/Pseudomonas_diversity/data/post_assembly_analysis/pan_genome/data/vis/clonalframe/all_concat.fasta"]), stdout=subprocess.PIPE))
output = test.communicate()[0]
示例8: Tree
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
import time
from ete3 import Tree
# Creates a random tree with 10,000 leaf nodes
tree = Tree()
tree.populate(10000)
# This code should be faster
t1 = time.time()
for leaf in tree.iter_leaves():
if "aw" in leaf.name:
print "found a match:", leaf.name,
break
print "Iterating: ellapsed time:", time.time() - t1
# This slower
t1 = time.time()
for leaf in tree.get_leaves():
if "aw" in leaf.name:
print "found a match:", leaf.name,
break
print "Getting: ellapsed time:", time.time() - t1
# Results in something like:
# found a match: guoaw Iterating: ellapsed time: 0.00436091423035 secs
# found a match: guoaw Getting: ellapsed time: 0.124316930771 secs
示例9: open
# 需要导入模块: from ete3 import Tree [as 别名]
# 或者: from ete3.Tree import get_leaves [as 别名]
inh = open(sys.argv[1])
treestring = inh.readline()
treestr = treestring.replace(';','')
treestr = treestr + ";"
inh.close()
if len(treestr) == 0:
print sys.argv[1] + "\tEmpty tree"
quit()
t = Tree(treestr)
#define basic tree style
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_support = True
#for n in t.traverse()
# if n.is_leaf():
#Here, we set up the annotations we want on the tree. For example, let's make the leaves with eukaryote sequences large red balls.
for leaf in t.get_leaves():
if re.search('Eukaryota', leaf.name):
leaf_style = NodeStyle()
leaf_style["fgcolor"] = "red"
leaf_style["size"] = 15
leaf.set_style(leaf_style)
t.show(tree_style=ts)