本文整理汇总了Python中ete2.TreeStyle.min_leaf_separation方法的典型用法代码示例。如果您正苦于以下问题:Python TreeStyle.min_leaf_separation方法的具体用法?Python TreeStyle.min_leaf_separation怎么用?Python TreeStyle.min_leaf_separation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ete2.TreeStyle
的用法示例。
在下文中一共展示了TreeStyle.min_leaf_separation方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_example_tree
# 需要导入模块: from ete2 import TreeStyle [as 别名]
# 或者: from ete2.TreeStyle import min_leaf_separation [as 别名]
def get_example_tree():
t = Tree()
ts = TreeStyle()
ts.layout_fn = layout
ts.mode = "c"
ts.show_leaf_name = True
ts.min_leaf_separation = 15
t.populate(100)
return t, ts
示例2: give_tree_layout
# 需要导入模块: from ete2 import TreeStyle [as 别名]
# 或者: from ete2.TreeStyle import min_leaf_separation [as 别名]
def give_tree_layout(t):
# for all nodes give them the weight = score
for n in t.traverse():
n.add_features(weight=n.dist/20.0)
# Create an empty TreeStyle
ts = TreeStyle()
# Set our custom layout function
ts.layout_fn = layout
ts.show_leaf_name = False
# Draw a tree
ts.mode = "c"
#ts.arc_start = -180
#ts.arc_span = 180
#ts.scale = 100
ts.min_leaf_separation = 10
ts.show_scale = False
return ts
示例3: main
# 需要导入模块: from ete2 import TreeStyle [as 别名]
# 或者: from ete2.TreeStyle import min_leaf_separation [as 别名]
#.........这里部分代码省略.........
for i in range(num_rows):
matrix.append([.5*(1-.5*ckm30_norm[i,j]-.5*ckm30_norm[j,i])+.5*(1-.5*ckm50_norm[i,j]-.5*ckm50_norm[j,i]) for j in range(i+1)])
#Construct the tree. Note I could use RapidNJ here, but a few tests have shown that the trees that RapidNJ creates are rubbish.
dm = _DistanceMatrix(names, matrix)
constructor = DistanceTreeConstructor()
tree = constructor.nj(dm)
t=Tree(tree.format('newick'),format=1)
#tree.format('newick')
#Phylo.draw_ascii(tree)
#Now I will put internal nodes in a certain phylogenetic distance between the root and a given node.
#Function to insert a node at a given distance
def insert_node(t, name_to_insert, insert_above, dist_along):
insert_at_node = t.search_nodes(name=insert_above)[0]
parent = (t&insert_above).up
orig_branch_length = t.get_distance(insert_at_node,parent)
if orig_branch_length < dist_along:
raise ValueError("error: dist_along larger than orig_branch_length")
removed_node = insert_at_node.detach()
removed_node.dist = orig_branch_length - dist_along
added_node = parent.add_child(name=name_to_insert, dist=dist_along)
added_node.add_child(removed_node)
#Function to insert a node some % along a branch
def insert_hyp_node(t, leaf_name, percent):
total_dist = t.get_distance(t.name,leaf_name)
percent_dist = percent*total_dist
child_node = (t&leaf_name)
ancestor_node = (t&child_node.name).up
while t.get_distance(t.name, ancestor_node) > percent_dist:
child_node = ancestor_node
ancestor_node = (t&child_node.name).up
insert_node(t, leaf_name+"_"+str(percent), child_node.name, percent_dist-t.get_distance(t.name, ancestor_node))
#Insert hypothetical nodes
hyp_node_names = dict()
cutoffs = [.9,.8,.7,.6,.5,.4,.3,.2,.1]
cutoffs = map(lambda y: y**1.5,cutoffs)
for i in range(len(x_file_names)):
xi = x[i:len(x):len(x_file_names)]
for j in range(1,len(cutoffs)+1):
if xi[j]>0:
insert_hyp_node(t, x_file_names[i], cutoffs[j-1])
hyp_node_names[x_file_names[i]+"_"+str(cutoffs[j-1])] = [x_file_names[i], cutoffs[j-1], j-1] #in case there are "_" in the file names
#insert_hyp_node(t, x_file_names[i],.5/t.get_distance(t.name,t&x_file_names[i])*cutoffs[j])
#Now put the bubbles on the nodes
def layout(node):
#print(node)
if node.is_leaf():
if node.name in x_file_names:
#make reconstructed bubble
size = x[x_file_names.index(node.name)]
F = CircleFace(radius=500*math.sqrt(size), color="RoyalBlue", style="sphere")
F.border.width = None
F.opacity = 0.6
faces.add_face_to_node(F,node, 0, position="branch-right")
if taxonomic_names_on_leaves:
nameFace = AttrFace("name", fsize=25, fgcolor='black',text_suffix="_"+taxonomy[x_file_names.index(node.name)])
faces.add_face_to_node(nameFace, node, 0, position="branch-right")
else:
nameFace = AttrFace("name", fsize=25, fgcolor='black')
faces.add_face_to_node(nameFace, node, 0, position="branch-right")
elif node.name in hyp_node_names: #Otherwise it's a hypothetical node, just use recon x
node_base_name = hyp_node_names[node.name][0]
percent = hyp_node_names[node.name][1]
if node_base_name in x_file_names:
idx = hyp_node_names[node.name][2]
size = x[x_file_names.index(node_base_name)+(idx+1)*len(x_file_names)]
F = CircleFace(radius=500*math.sqrt(size), color="RoyalBlue", style="sphere")
F.border.width = None
F.opacity = 0.6
faces.add_face_to_node(F,node, 0, position="branch-right")
#print node
#print size
else:
size=0
else:
size=0
#print(size)
ts = TreeStyle()
ts.layout_fn = layout
if plot_rectangular:
ts.mode = "r"
else:
ts.mode = "c"
ts.show_leaf_name = False
ts.min_leaf_separation = 50
#Export the tree to a png image
t.render(out_file, w=width, units="mm", tree_style=ts)
#Export the xml file
project = Phyloxml()
phylo = phyloxml.PhyloxmlTree(newick=t.write(format=0, features=[]))
phylo.phyloxml_phylogeny.set_name(title)
project.add_phylogeny(phylo)
project.export(open(out_file_xml,'w'))
示例4: MakePlot
# 需要导入模块: from ete2 import TreeStyle [as 别名]
# 或者: from ete2.TreeStyle import min_leaf_separation [as 别名]
#.........这里部分代码省略.........
if LCA.name==t.name:
percent_dist = percent*LCA_to_leaf_dist
if mean_dist <= percent:
child_node = (t&leaf_name)
else:
child_node = (t&nearby_names[0])#This means "go up from root" in the direction of the nearest guy
ancestor_node = (t&child_node.name).up
elif mean_dist <= percent:
percent_dist = t.get_distance(LCA) + abs(percent-mean_dist)*(LCA_to_leaf_dist)/(1-mean_dist)
child_node = (t&leaf_name)
ancestor_node = (t&child_node.name).up
else:
percent_dist = t.get_distance(LCA) - abs(percent-mean_dist)*(t.get_distance(LCA))/(mean_dist)
child_node = (t&leaf_name)
ancestor_node = (t&child_node.name).up
while t.get_distance(t.name, ancestor_node) > percent_dist:
child_node = ancestor_node
ancestor_node = (t&child_node.name).up
insert_node(t, leaf_name+"_"+str(percent), child_node.name, percent_dist-t.get_distance(t.name, ancestor_node))
#Set outgroup
if outgroup in names:
t.set_outgroup(t&outgroup) #I will need to check that this outgroup is actually one of the names...
else:
print("WARNING: the chosen outgroup " + outgroup + " is not in the given taxonomy: ")
print(names)
print("Proceeding without setting an outgroup. This may cause results to be uninterpretable.")
#Insert hypothetical nodes
hyp_node_names = dict()
cutoffs = [.9,.8,.7,.6,.5,.4,.3,.2,.1]
cutoffs = [-.5141*(val**3)+1.0932*(val**2)+0.3824*val for val in cutoffs]
for i in range(len(org_names)):
xi = x[i:len(x):len(org_names)]
for j in range(1,len(cutoffs)+1):
if xi[j]>0:
insert_hyp_node(t, org_names[i], cutoffs[j-1],ckm_ave_train_dist, org_names)
hyp_node_names[org_names[i]+"_"+str(cutoffs[j-1])] = [org_names[i], cutoffs[j-1], j-1] #in case there are "_" in the file names
size_factor=250
font_size=55
#Now put the bubbles on the nodes
def layout(node):
node_style = NodeStyle()
node_style["hz_line_width"] = 10
node_style["vt_line_width"] = 10
node.set_style(node_style)
#print(node)
if node.is_leaf():
if node.name in org_names:
#make reconstructed bubble
size = x[org_names.index(node.name)]
F = CircleFace(radius=size_factor*math.sqrt(size), color="RoyalBlue", style="sphere")
F.border.width = None
F.opacity = 0.6
faces.add_face_to_node(F,node, 0, position="branch-right")
#Denote that this was a training organism
nameFace = AttrFace("name", fsize=font_size, fgcolor='black')
faces.add_face_to_node(nameFace, node, 0, position="branch-right")
elif node.name in hyp_node_names: #Otherwise it's a hypothetical node, just use recon x
node_base_name = hyp_node_names[node.name][0]
percent = hyp_node_names[node.name][1]
if node_base_name in org_names:
idx = hyp_node_names[node.name][2]
size = x[org_names.index(node_base_name)+(idx+1)*len(org_names)]
F = CircleFace(radius=size_factor*math.sqrt(size), color="RoyalBlue", style="sphere")
F.border.width = None
F.opacity = 0.6
faces.add_face_to_node(F,node, 0, position="branch-right")
#This is if I want the names of the hypothetical nodes to be printed as well
#nameFace = AttrFace("name", fsize=font_size, fgcolor='black')
#faces.add_face_to_node(nameFace, node, 0, position="branch-right")
else:
size=0
else:
size=0
ts = TreeStyle()
ts.layout_fn = layout
ts.mode = "r"
#ts.mode = "c"
ts.scale = 2*1000
ts.show_leaf_name = False
ts.min_leaf_separation = 50
F = CircleFace(radius=.87*size_factor, color="RoyalBlue", style="sphere")
F.border.width = None
F.opacity = 0.6
ts.legend.add_face(F,0)
ts.legend.add_face(TextFace(" Inferred relative abundance",fsize=1.5*font_size,fgcolor="Blue"),1)
ts.legend.add_face(TextFace(" Total absolute abundance depicted " + str(sum_x)[0:8], fsize=1.5*font_size,fgcolor="Black"),1)
ts.legend_position=4
#t.show(tree_style=ts)
t.render(outfile, w=550, units="mm", tree_style=ts)
#Redner the XML file
project = Phyloxml()
phylo = phyloxml.PhyloxmlTree(newick=t.write(format=0, features=[]))
project.add_phylogeny(phylo)
project.export(open(outfilexml,'w'))
示例5: main
# 需要导入模块: from ete2 import TreeStyle [as 别名]
# 或者: from ete2.TreeStyle import min_leaf_separation [as 别名]
def main(argv):
input_file=''
title='Title'
label_internal_nodes = False
label_leaves = False
out_file=''
width=750
out_file_xml=''
try:
opts, args = getopt.getopt(argv,"h:i:t:lno:w:x:",["Help=","InputFile=","Title=","LabelLeaves=", "LabelInternalNodes=","OutFile=","Width=","OutFileXML="])
except getopt.GetoptError:
print 'Unknown option, call using: ./PlotTree.py -i <InputCAMIFile> -t <Title> -l <LabelLeavesFlag> -n <LabelInternalNodesFlag> -o <OutFile.png> -x <Outfile.xml> -w <Width>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print './PlotTree.py -i <InputCAMIFile> -t <Title> -l <LabelLeavesFlag> -n <LabelInternalNodesFlag> -o <OutFile> -x <OutFile.xml> -w <Width>'
sys.exit(2)
elif opt in ("-i", "--InputFile"):
input_file = arg
elif opt in ("-t", "--Title"):
title = arg
elif opt in ("-l", "--LabelLeaves"):
label_leaves = True
elif opt in ("-n","--LabelInternalNodes"):
label_internal_nodes = True
elif opt in ("-o", "--OutFile"):
out_file = arg
elif opt in ("-w", "--Width"):
width = int(arg)
elif opt in ("-x", "--OutFileXML"):
out_file_xml = arg
schema_names = COLOR_SCHEMES.keys()
#Read the common kmer profile
ckm_tax_paths = []
ckm_name_to_perc = dict()
fid = open(input_file,'r')
file = fid.readlines()
fid.close()
#Put placeholders in for missing names like: "||" -> "|NA1|"
file_noblank = list()
i=0
for line in file:
while "||" in line:
line = line.replace("||","|NONAME|",1)
i = i+1
file_noblank.append(line)
#Get the names and weights
for line in file_noblank:
if line[0]!='#' and line[0]!='@' and line[0]!='\n': #Don't parse comments or blank lines
temp = line.split()[3] #Get the names
ckm_tax_paths.append(temp)
ckm_name_to_perc[temp.split("|")[-1]] = line.split()[-1] #Get the weights
#Create the tree
t=Tree()
names_to_nodes = dict()
for i in range(0,len(ckm_tax_paths)):
split_tax_path = ckm_tax_paths[i].split("|")
if len(split_tax_path)==1: #If len==1, then it's a superkingdom
names_to_nodes[split_tax_path[0]] = t.add_child(name=split_tax_path[0]) #connect directly to tree
else:
if split_tax_path[-2] in names_to_nodes: #If the parent is already in the tree, add to tree
names_to_nodes[split_tax_path[-1]] = names_to_nodes[split_tax_path[-2]].add_child(name=split_tax_path[-1])
else: #Otherwise iterate up until we have something that is in the tree
j=2
while split_tax_path[-j]=="NONAME":
j = j + 1
#This skips over the NONAMES
names_to_nodes[split_tax_path[-1]] = names_to_nodes[split_tax_path[-j]].add_child(name=split_tax_path[-1])
#Show the tree
#print t.get_ascii(show_internal=True)
#scheme = random.sample(schema_names, 1)[0] #'set2' is nice,
scheme = 'set2'
def layout(node):
if node.name in ckm_name_to_perc:
ckm_perc = float(ckm_name_to_perc[node.name])
else:
ckm_perc = 0
F = CircleFace(radius=3.14*math.sqrt(ckm_perc), color="RoyalBlue", style="sphere")
F.border.width = None
F.opacity = 0.6
faces.add_face_to_node(F,node, 0, position="branch-right")
if label_internal_nodes:
faces.add_face_to_node(TextFace(node.name, fsize=7),node, 0, position="branch-top")
ts = TreeStyle()
ts.layout_fn = layout
ts.mode = "r"
ts.show_leaf_name = label_leaves
ts.min_leaf_separation = 50
ts.title.add_face(TextFace(title, fsize=20), column=0)
#Export the tree to a png image
#.........这里部分代码省略.........