def get_tree_style(tree_file, abund, rownames):

    with open("matrix.txt", "w") as temp:
        cols = len(abund[0])
        header = "#Names"
        for i in xrange(cols):
            header += "\tOTU%d" % i
        temp.write("%s\n" % header)
        for i, row in enumerate(abund):
            temp.write("%s\t%s\n" % (rownames[i], '\t'.join([str(i) for i in row])))

    t = Tree(tree_file)
    t.convert_to_ultrametric(10)

    assert isinstance(abund, numpy.ndarray)
    assert isinstance(rownames, numpy.ndarray)
    ts = TreeStyle()
    ts.mode = "r"
    ts.show_leaf_name = False
    ts.show_scale = False
    ts.show_branch_length = False
    ts.branch_vertical_margin = 20
    ts.force_topology = True
    ts.optimal_scale_level = "full"
    ts.scale = 50
    ts.draw_guiding_lines = True
    ts.guiding_lines_type = 0
    ts.guiding_lines_color = "black"
    for n in t.traverse():
        if not n.is_leaf():
            nstyle = NodeStyle()
            n.set_style(nstyle)
            nstyle['size'] = 0
            nstyle['hz_line_width'] = 3
            nstyle['vt_line_width'] = 3
        else:
            nstyle = NodeStyle()
            n.set_style(nstyle)
            nstyle['size'] = 0
            nstyle['hz_line_width'] = 3
            nstyle['vt_line_width'] = 3
            nstyle['fgcolor'] = "Black"
            nstyle['shape'] = "square"
            name_face = AttrFace("name", fsize=14, ftype="Arial", fgcolor="black", penwidth=10, text_prefix=" ", text_suffix=" ")
            n.add_face(name_face, column=0, position="aligned")
            row_index = rownames.tolist().index(n.name)
            col = 1
            for i in xrange(10):
                col += 1
                n.add_face(CircleFace(5, color=get_color(abund, row_index, i)), column=col, position="aligned")
    return t, ts
Esempio n. 2
0
def setTreeStyle(style, layoutfunction):
    #consolidate options for showing trees
    #pass in string "circle" or "rect" and a layout function
    I = TreeStyle()
    if style == "circle":
        I.tree_width = 1200
        I.layout_fn = layoutfunction
        I.show_branch_length = False
        #I.show_branch_support = True
        I.show_leaf_name = False
        I.mode = "c"
        I.force_topology = True
        #I.legend_position = 3
        I.extra_branch_line_type = 1
        I.guiding_lines_type = 1
        I.guiding_lines_color = "#666666"
        I.extra_branch_line_color = "#666666"
        I.optimal_scale_level = "full"
        I.root_opening_factor = 0
        
    elif style =="rect":
        I = TreeStyle()
        I.layout_fn = layoutfunction
        I.show_leaf_name = False
        I.force_topology = True
        I.optimal_scale_level = "semi"
        
    else:
        I.layout_fn = layoutfunction
        I.show_leaf_name = False
        I.force_topology = True
        I.optimal_scale_level = "semi"
    return I

    

    
Esempio n. 3
0
from ete2 import EvolTree
from ete2 import faces


tree = EvolTree("data/S_example/measuring_S_tree.nw")
tree.link_to_alignment("data/S_example/alignment_S_measuring_evol.fasta")

print tree

print "\n Running free-ratio model with calculation of ancestral sequences..."

tree.run_model("fb_anc")
# tree.link_to_evol_model('/tmp/ete2-codeml/fb_anc/out', 'fb_anc')

I = TreeStyle()
I.force_topology = False
I.draw_aligned_faces_as_table = True
I.draw_guiding_lines = True
I.guiding_lines_type = 2
I.guiding_lines_color = "#CCCCCC"
for n in sorted(tree.get_descendants() + [tree], key=lambda x: x.node_id):
    if n.is_leaf():
        continue
    anc_face = faces.SequenceFace(n.sequence, "aa", fsize=10, bg_colors={})
    I.aligned_foot.add_face(anc_face, 1)
    I.aligned_foot.add_face(faces.TextFace("node_id: #%d " % (n.node_id), fsize=8), 0)
print "display result of bs_anc model, with ancestral amino acid sequences."
tree.show(tree_style=I)

print "\nThe End."
Esempio n. 4
0
def doWork(args):
	# read clustering data
	geneIdToClusterId = {}
	clusterIds  = []
	for line in open(args.cluster):
		if line[0] == '%':
			clusterId = line[1:].strip()
			clusterIds.append(clusterId)
		else:
			geneIdToClusterId[line.strip()] = clusterId

	# create colour map for clusters
	clusterIdToColour = {}
	index = 0
	for clusterId in clusterIds:
		rgb = tuple([int(c*255) for c in hsv_to_rgb(float(index)/len(clusterIds), 0.4, 1.0)])
		clusterIdToColour[clusterId] = '#%02X%02X%02X' % rgb
		index += 1

	# read tree
	tree = Tree(args.tree)

	# define node properties
	for node in tree.traverse('levelorder'):
		if node.is_leaf():
			name = node.name.replace("'", '')
			clusterId = geneIdToClusterId[name]
			node.add_feature('clusterId', clusterId)
			
			if args.label:
				node.name = ' ' + name + ' [' + clusterId + ']'
			
			ns = NodeStyle()
			ns['shape'] = 'circle'
			ns['fgcolor'] = 'black'
			ns['size'] = 4
			node.set_style(ns)
		else:
			ns = NodeStyle()
			ns['size'] = 0
			node.set_style(ns)
		
	# determine colour for each node
	assignedColor = set()
	for node in tree.traverse('levelorder'):
		if node.is_leaf() or node.is_root():
			continue
			
		# determine clusters descendent from this node
		clusterSet = set()
		for leaf in node.get_leaves():
			clusterSet.add(leaf.clusterId)
			
		# check if parent has already been assigned a colour
		bContinue = False
		parentNode = node.up
		while not parentNode.is_root():
			if parentNode in assignedColor:
				bContinue = True
				break
				
			parentNode = parentNode.up
			
		if bContinue:
			continue

		# colour node if all descendants are from the same cluster
		if len(clusterSet) == 1:
			clusters = list(clusterSet)
			ns = NodeStyle()
			ns['bgcolor'] = clusterIdToColour[clusters[0]]
			ns['size'] = 0
			node.set_style(ns)
			assignedColor.add(node)

	# create tree style for plotting consisteny index trees
	treeStyle = TreeStyle()
	treeStyle.margin_left = 10
	treeStyle.margin_right = 10
	treeStyle.margin_top = 10
	treeStyle.margin_bottom = 10
	treeStyle.guiding_lines_type = 1
	treeStyle.legend_position=1
	treeStyle.scale = 500
	
	# add legend
	rowIndex = 0
	for clusterId in sorted(clusterIdToColour.keys()):
		treeStyle.legend.add_face(CircleFace(4, clusterIdToColour[clusterId]), column=0)
		treeStyle.legend.add_face(TextFace(' ' + clusterId), column=1)
		rowIndex += 1

	tree.render(args.output, dpi=args.dpi, w = int(args.width*args.dpi + 0.5), tree_style=treeStyle)