예제 #1
0
    def render(self, fname, layout=None, tree_style=None, palette=None):
        """
        Renders the locus tree and writes the image to file.
        :param fname: str
            Output file path
        :param layout:
        :param tree_style:
        :param palette:
        :return: None
        """
        if not palette:
            palette = ['#1F77B4', '#AEC7E8', '#FF7F0E',
                       '#FFBB78', '#2CA02C', '#98DF8A',
                       '#D62728', '#FF9896', '#9467BD',
                       '#C5B0D5', '#8C564B', '#C49C94',
                       '#E377C2', '#F7B6D2', '#7F7F7F',
                       '#C7C7C7', '#BCBD22', '#DBDB8D',
                       '#17BECF', '#9EDAE5']
        if not self.colorized:
            self._colorize(palette)
            self.colorized = True
        if not tree_style:
            from ete2 import TreeStyle
            tree_style = TreeStyle()  # imported during colorizing tree
            # tstyle.show_leaf_name = False
            tree_style.scale = 28
            tree_style.branch_vertical_margin = 6
            tree_style.show_branch_length = False

            # tstyle.show_branch_support = True
            tree_style.show_scale = False
        self.L.convert_to_ultrametric()
        self.L.render(file_name=fname, tree_style=tree_style)
예제 #2
0
    def render_annotate(newick_path, output_path):
        """Render the annotated tree, showing internal node names.
           The output_path should end in .PNG, .PDF or .SVG: this will determine the format.

           To aid in testing, if output_path is None, the tree is shown rather than rendered.
        """
        tree = Tree(newick_path, format=1)

        ts = TreeStyle()
        ts.show_leaf_name = True
        ts.branch_vertical_margin = 15

        ns = NodeStyle()
        ns["size"] = 1

        edge = 0
        for node in tree.traverse():
            node.name = node.name.replace("'", "")
            node.name = node.name.replace("+", ",")
            if not node.is_leaf() and node.name != "NoName":
                f = TextFace(node.name)
                f.margin_top = 5
                f.margin_bottom = 5
                f.margin_right = 10
                f.margin_left = 10
                edge += 1
                node.add_face(f, column=0, position="branch-top")

        if output_path is None:
            tree.show(tree_style=ts)
        else:
            tree.render(output_path)
예제 #3
0
def make_tree(treefile, image_file, clone_info):
    colour_list = ['MidnightBlue','RoyalBlue', 'LightSkyBlue', 'Aquamarine', 'SpringGreen', 'GreenYellow',\
                   'Gold','DarkOrange']
    weeks = ['16', '30', '38', '48', '59', '119', '176', '206']
    weeks = ['6', '14', '53', '92','144']
    t = Tree(treefile,format = 1)
    ts = TreeStyle()
    for i in range(5):
        ts.legend.add_face(CircleFace(20, colour_list[i]), column=0)
        ts.legend.add_face(TextFace('week' + weeks[i]), column=1)
    ts.legend_position = 2
    ts.show_leaf_name = True
    ts.branch_vertical_margin = 15
    ts.rotation = 90
    ns = NodeStyle()
    ns["size"] = 1
    ns.hz_line_width = 10
    ns.vt_line_width = 10
    edge = 0
    for node in t.traverse():
        node.name = node.name.replace("'", "")
        node.name = node.name.replace(".", ",")
        name = node.name.split(' ')[0]
        print name
        if name in clone_info.keys():
            style_node(node, colour_list[int(clone_info[name][0])-1], int(int(clone_info[name][1])/10)+5)
        if not node.is_leaf() and node.name != 'NoName':
                f = TextFace(node.name)
                f.margin_top = 2.5
                f.margin_bottom = 2.5
                f.margin_right = 2.5
                f.margin_left = 2.5
                node.add_face(f, column=0, position="branch-top")
    t.render(image_file, tree_style = ts)
예제 #4
0
파일: RenderTree.py 프로젝트: yanmc/TRIgS
    def render_annotate(newick_path, output_path):
        """Render the annotated tree, showing internal node names.
           The output_path should end in .PNG, .PDF or .SVG: this will determine the format.

           To aid in testing, if output_path is None, the tree is shown rather than rendered.
        """
        tree = Tree(newick_path, format=1)

        ts = TreeStyle()
        ts.show_leaf_name = True
        ts.branch_vertical_margin = 15

        ns = NodeStyle()
        ns["size"] = 1

        edge = 0
        for node in tree.traverse():
            node.name = node.name.replace("'", "")
            node.name = node.name.replace("+", ",")
            if not node.is_leaf() and node.name != 'NoName':
                f = TextFace(node.name)
                f.margin_top = 5
                f.margin_bottom = 5
                f.margin_right = 10
                f.margin_left = 10
                edge += 1
                node.add_face(f, column=0, position="branch-top")

        if output_path is None:
            tree.show(tree_style=ts)
        else:
            tree.render(output_path)
예제 #5
0
def draw_tree(tree, file):
    root = tree.get_midpoint_outgroup()
    try:
      tree.set_outgroup(root)
    except:
      pass
    root = tree.get_tree_root()
    root.dist = 0
    add_sig(tree)
    ts = TreeStyle()
    ts.branch_vertical_margin = 1
    #ts.scale = 1500
    ts.show_leaf_name = False
    #ts.show_branch_support = True
    leg_file = path.join(path.expanduser('~'), 'Perl', 'Modules', 'TreeLegend.png')   
    leg_face= ImgFace(img_file=leg_file)
    leg_face.margin_left, leg_face.margin_top = 5, 5
    ts.legend.add_face(leg_face, column=1)
    ts.legend_position=1

    title_face = TextFace(text=file.split('.')[0])
    title_face.margin_left, title_face.margin_top = 10, 5
    ts.title.add_face(title_face, column=1)
    (ts.margin_left, ts.margin_right) = (5,5)
    tree.render(file, tree_style=ts, w=6000, units='mm')
예제 #6
0
    def show(self, tree_style=None, palette=None):
        """
        Starts an interactive session to visualize the decomposition.
        :return: None
        """
        if not palette:
            palette = ['#1F77B4', '#AEC7E8', '#FF7F0E',
                       '#FFBB78', '#2CA02C', '#98DF8A',
                       '#D62728', '#FF9896', '#9467BD',
                       '#C5B0D5', '#8C564B', '#C49C94',
                       '#E377C2', '#F7B6D2', '#7F7F7F',
                       '#C7C7C7', '#BCBD22', '#DBDB8D',
                       '#17BECF', '#9EDAE5']
        if not self.colorized:
            self._colorize(palette)
            self.colorized = True
        if not tree_style:
            from ete2 import TreeStyle
            tree_style = TreeStyle()
            # tstyle.show_leaf_name = False
            tree_style.scale = 28
            tree_style.branch_vertical_margin = 6
            tree_style.show_branch_length = False

            # tstyle.show_branch_support = True
            tree_style.show_scale = False
        self.L.convert_to_ultrametric()
        self.L.show(tree_style=tree_style)
예제 #7
0
 def plotTree(self, tree, out_fn=None, rotation=270, show_leaf_name=False, 
               show_branch_length=False, circularTree=False, show_division_nodes=True, 
               distance_between_branches=4, show_border=False, width=None, height=None):            
     from ete2 import TreeStyle        
     from PyQt4 import QtSvg, QtCore, QtGui
     from ete2.treeview import qt4_render, drawer, main
     
       
     ts = TreeStyle()   
     ts.show_scale = False
     ts.show_border = show_border
     ts.orientation = 1 # 0, tree is drawn from left-to-right. 1, tree is drawn from right-to-left
     ts.rotation = rotation
     ts.show_leaf_name = show_leaf_name
     ts.show_branch_length = show_branch_length
     if circularTree:
         ts.mode = 'c'
     else:
         ts.mode = 'r'
     ts.branch_vertical_margin = distance_between_branches
     
     
     def hideInternalNodesLayout(node):
         if not node.is_leaf():
             node.img_style["size"] = 0
     
     if show_division_nodes is False:
         ts.layout_fn = hideInternalNodesLayout
     
     if out_fn is not None:        
         scene  = qt4_render._TreeScene()
         img = ts
         
         tree_item, n2i, n2f = qt4_render.render(tree, img)
         scene.init_data(tree, img, n2i, n2f)
         tree_item.setParentItem(scene.master_item)
         scene.master_item.setPos(0,0)
         scene.addItem(scene.master_item)      
         main.save(scene, out_fn, w=width, h=height, dpi=600)    
           
     else:
         scene, img = drawer.init_scene(tree, None, ts)
         tree_item, n2i, n2f = qt4_render.render(tree, img)
         scene.init_data(tree, img, n2i, n2f)
     
         tree_item.setParentItem(scene.master_item)
         scene.addItem(scene.master_item)
     
         size = tree_item.rect()
         w, h = size.width(), size.height()
     
         svg = QtSvg.QSvgGenerator()
         svg.setFileName("test.svg")
         svg.setSize(QtCore.QSize(w, h))
         svg.setViewBox(size)
         pp = QtGui.QPainter()
         pp.begin(svg)            
         scene.render(pp, tree_item.rect(), tree_item.rect(), QtCore.Qt.KeepAspectRatio)
예제 #8
0
 def plotTree(self, tree, out_fn=None, rotation=270, show_leaf_name=False, 
               show_branch_length=False, circularTree=False, show_division_nodes=True, 
               distance_between_branches=4, show_border=False, width=None, height=None):            
     from ete2 import TreeStyle        
     from PyQt4 import QtSvg, QtCore, QtGui
     from ete2.treeview import qt4_render, drawer, main
     
       
     ts = TreeStyle()   
     ts.show_scale = False
     ts.show_border = show_border
     ts.orientation = 1 # 0, tree is drawn from left-to-right. 1, tree is drawn from right-to-left
     ts.rotation = rotation
     ts.show_leaf_name = show_leaf_name
     ts.show_branch_length = show_branch_length
     if circularTree:
         ts.mode = 'c'
     else:
         ts.mode = 'r'
     ts.branch_vertical_margin = distance_between_branches
     
     
     def hideInternalNodesLayout(node):
         if not node.is_leaf():
             node.img_style["size"] = 0
     
     if show_division_nodes is False:
         ts.layout_fn = hideInternalNodesLayout
     
     if out_fn is not None:        
         scene  = qt4_render._TreeScene()
         img = ts
         
         tree_item, n2i, n2f = qt4_render.render(tree, img)
         scene.init_data(tree, img, n2i, n2f)
         tree_item.setParentItem(scene.master_item)
         scene.master_item.setPos(0,0)
         scene.addItem(scene.master_item)      
         main.save(scene, out_fn, w=width, h=height, dpi=600)    
           
     else:
         scene, img = drawer.init_scene(tree, None, ts)
         tree_item, n2i, n2f = qt4_render.render(tree, img)
         scene.init_data(tree, img, n2i, n2f)
     
         tree_item.setParentItem(scene.master_item)
         scene.addItem(scene.master_item)
     
         size = tree_item.rect()
         w, h = size.width(), size.height()
     
         svg = QtSvg.QSvgGenerator()
         svg.setFileName("test.svg")
         svg.setSize(QtCore.QSize(w, h))
         svg.setViewBox(size)
         pp = QtGui.QPainter()
         pp.begin(svg)            
         scene.render(pp, tree_item.rect(), tree_item.rect(), QtCore.Qt.KeepAspectRatio)
예제 #9
0
    def draw_ete_tree(self,
                      corpus,
                      fontsize=5,
                      color_leafs=False,
                      save_newick=True,
                      mode='c',
                      outputfile=None,
                      return_svg=True,
                      show=False,
                      save=False):
        root = self.to_ete(labels=corpus.titles)

        def layout(node):
            if node.is_leaf():
                N = AttrFace("name", fsize=7)
                faces.add_face_to_node(faces.AttrFace("name", "Arial", 10,
                                                      None),
                                       node,
                                       0,
                                       position='branch-right')
                # problems: aligment of labels to branch, left padding of labels

        ts = TreeStyle()
        ts.mode = mode
        ts.show_leaf_name = False
        ts.scale = 120
        ts.show_scale = False
        ts.branch_vertical_margin = 10

        nstyle = NodeStyle()
        nstyle["fgcolor"] = "#0f0f0f"
        nstyle["size"] = 0
        nstyle["vt_line_color"] = "#0f0f0f"
        nstyle["hz_line_color"] = "#0f0f0f"
        nstyle["vt_line_width"] = 1
        nstyle["hz_line_width"] = 1
        nstyle["vt_line_type"] = 0
        nstyle["hz_line_type"] = 0

        for n in root.traverse():
            n.set_style(nstyle)

        ts.layout_fn = layout

        if outputfile:
            outputfile = os.path.expanduser(outputfile)

        if save_newick:  # save tree in newick format for later manipulation in e.g. FigTree:
            root.write(outfile=os.path.splitext(outputfile)[0] + '.newick')

        if save:
            root.render(outputfile, tree_style=ts)
        if show:
            root.show(tree_style=ts)
        if return_svg:  # return the SVG as a string
            return root.render("%%return")[0]
예제 #10
0
def draw_tree(tree, file):
    ts = TreeStyle()
    ts.branch_vertical_margin = 1
    ts.show_leaf_name = False
    if '.svg' in file:
      ts.scale = 3000
      tree.render(file, tree_style=ts, h=300, units='mm')
    else:
      ts.scale = 1500 
      tree.render(file, tree_style=ts, w=3000, units='mm')
def load_tree(f_tree, f_align):
    # Tree style
    ts = TreeStyle()
    ts.show_leaf_name = True
    ts.show_branch_length = True
    ts.show_branch_support = True
    ts.branch_vertical_margin = 10    

    # Load phylogenetic tree
    tree = PhyloTree(f_tree.read())
    if f_align is not None:    
        tree.link_to_alignment(f_align.read())
    return tree, ts
예제 #12
0
파일: treeCommon.py 프로젝트: robsyme/hal
def drawTree(nwfile, outfile):
    from ete2 import Tree, TreeStyle, TextFace
    ts = TreeStyle()
    ts.show_leaf_name = False
    ts.layout_fn = my_layout
    ts.branch_vertical_margin = 12.75
    ts.orientation = 1
    titleFace = TextFace("Phylogenetic Tree", fsize=18, fgcolor="white")
    titleFace.margin_top = 15
    ts.title.add_face(titleFace, column=1)

    t = Tree(nwfile)
    t.render(outfile, tree_style=ts)
예제 #13
0
파일: treeCommon.py 프로젝트: dayin1989/hal
def drawTree(nwfile, outfile):
    from ete2 import Tree, TreeStyle, TextFace
    ts = TreeStyle()
    ts.show_leaf_name = False
    ts.layout_fn = my_layout
    ts.branch_vertical_margin = 12.75
    ts.orientation = 1
    titleFace = TextFace("Phylogenetic Tree", fsize=18, fgcolor="white")
    titleFace.margin_top = 15
    ts.title.add_face(titleFace, column=1)

    t = Tree(nwfile)
    t.render(outfile, tree_style=ts)
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
예제 #15
0
def get_tree_style(scale=None, vertical_margin=None):
    """Setups the tree-style used when rendering the tree"""

    style = TreeStyle()
    style.show_leaf_name = False

    if scale:
        style.scale = scale

    style.show_border = True

    if vertical_margin:
        style.branch_vertical_margin = vertical_margin

    return style
예제 #16
0
    def draw_ete_tree(self, corpus, fontsize=5,
                      color_leafs=False,
                      save_newick=True, mode='c',
                      outputfile=None,
                      return_svg=True, show=False,
                      save=False):
        root = self.to_ete(labels=corpus.titles)

        def layout(node):
            if node.is_leaf():
                N = AttrFace("name", fsize=7)
                faces.add_face_to_node(faces.AttrFace("name","Arial",10, None), node, 0, position='branch-right')
                # problems: aligment of labels to branch, left padding of labels

        ts = TreeStyle()
        ts.mode = mode
        ts.show_leaf_name = False
        ts.scale = 120
        ts.show_scale = False
        ts.branch_vertical_margin = 10

        nstyle = NodeStyle()
        nstyle["fgcolor"] = "#0f0f0f"
        nstyle["size"] = 0
        nstyle["vt_line_color"] = "#0f0f0f"
        nstyle["hz_line_color"] = "#0f0f0f"
        nstyle["vt_line_width"] = 1
        nstyle["hz_line_width"] = 1
        nstyle["vt_line_type"] = 0
        nstyle["hz_line_type"] = 0

        for n in root.traverse():
           n.set_style(nstyle)
        
        ts.layout_fn = layout

        if outputfile:
            outputfile = os.path.expanduser(outputfile)

        if save_newick: # save tree in newick format for later manipulation in e.g. FigTree:
            root.write(outfile=os.path.splitext(outputfile)[0]+'.newick')

        if save:
            root.render(outputfile, tree_style=ts)
        if show:
            root.show(tree_style=ts) 
        if return_svg: # return the SVG as a string
            return root.render("%%return")[0]
예제 #17
0
파일: run_ete.py 프로젝트: Jakob37/RASP
def get_tree_style(scale=None, vertical_margin=None):

    """Setups the tree-style used when rendering the tree"""

    style = TreeStyle()
    style.show_leaf_name = False

    if scale:
        style.scale = scale

    style.show_border = True

    if vertical_margin:
        style.branch_vertical_margin = vertical_margin

    return style
예제 #18
0
    def draw_significant_expansion_contraction(
            self, outfile_prefix="significant_expansion_contraction_tree"):

        tree = self.tree
        for node in tree.traverse():
            node.img_style["size"] = 0

        def layout(node):
            if node.up is not None:
                attr = AttrFace("significant_expansion",
                                fsize=7,
                                fgcolor="green",
                                text_prefix="+")
                faces.add_face_to_node(attr, node, 0, position="branch-top")

                attr = AttrFace("significant_contraction",
                                fsize=7,
                                fgcolor="red",
                                text_prefix="-")
                faces.add_face_to_node(attr, node, 0, position="branch-bottom")

            if node.is_leaf():
                attr = AttrFace("name",
                                fsize=10,
                                fgcolor="black",
                                text_prefix="  ",
                                fstyle="italic")
                faces.add_face_to_node(attr, node, 0, position="aligned")

        ts = TreeStyle()
        ts.layout_fn = layout
        ts.optimal_scale_level = "full"
        ts.branch_vertical_margin = 10
        ts.show_leaf_name = False
        #ts.allow_face_overlap =True
        tree.render("%s.png" % outfile_prefix,
                    w=200,
                    units='mm',
                    tree_style=ts,
                    dpi=1200)
        tree.render("%s.svg" % outfile_prefix,
                    w=200,
                    units='mm',
                    tree_style=ts,
                    dpi=1200)
def drawete2PhylTree(treeFilePath):
    tree = Tree(treeFilePath)
    treeStyle = TreeStyle()
    treeStyle.show_scale = False
    #treeStyle.margin_left = 0
    #treeStyle.margin_right = 0
    #treeStyle.margin_top = 0
    #treeStyle.margin_bottom = 0
    #treeStyle.tree_width = treeWidth
    no_of_nodes = countNodes(tree)
    treeStyle.scale = 120#finalImageHeightinMM/no_of_nodes
    treeStyle.branch_vertical_margin = 10#finalImageHeightinMM/no_of_nodes
    #treeStyle.draw_aligned_faces_as_table = False
    tree = changeNodeStyle(tree)
    #tree.img_style["size"] = 30
    #tree.img_style["fgcolor"] = "blue"
    #tree.show(tree_style=treeStyle)
    tree.render("tree.PNG",tree_style=treeStyle,units="mm")
예제 #20
0
	def render_tree_image(self, filename):
		def my_layout(node):
			name_faces = AttrFace("name", fsize=10, fgcolor = "#0000FF")
			faces.add_face_to_node(name_faces, node, column=0, position="branch-right")
		t = Tree("%s;" % self.newick_string(), format = 1)
		s = "0"	
		for n in t.iter_descendants():
			text = TextFace(s)
			if s == "0": s = "1"
			else: s = "0"
			text.fgcolor = "#FF0000";
			nstyle = NodeStyle()
			nstyle['size'] = 15
			nstyle['fgcolor'] = "#333"
			n.set_style(nstyle)
			n.add_face(text, column = 0, position = "branch-top")
		ts = TreeStyle()
		ts.rotation = 90
		ts.show_leaf_name = False
		ts.layout_fn = my_layout
		ts.margin_left = 0
		ts.branch_vertical_margin = 50
		ts.show_scale = False
		t.render(filename, tree_style = ts, w = 2000)
예제 #21
0
def main():
    args = parse_args()

    if args.data:
        print "\nReading tree from " + args.tree + " and data matrix from " + args.data
        tree = ClusterTree(args.tree, text_array=args.data)
    else:
        print "\nReading tree from " + args.tree
        tree = Tree(args.tree)

    if args.midpoint:
        R = tree.get_midpoint_outgroup()
        tree.set_outgroup(R)
        print "- Applying midpoint rooting"
    elif args.outgroup:
        tree.set_outgroup(tree & args.outgroup)
        print "- Rooting using outgroup " + args.outgroup

    if not args.no_ladderize:
        tree.ladderize()
        print "- Ladderizing tree"

    table, column_list, column_values = readtable(args, tree.get_leaf_names())

    labels = []
    if args.labels:
        print "\nThese labels will be printed next to each strain:"
        for label in args.labels:
            if label in column_list:
                labels.append(label)
                print " " + label
            else:
                print "WARNING: specified label " + label + " was not found in the columns of the info file provided, " + args.info

    # set node styles
    # start by setting all to no shapes, black labels
    for n in tree.traverse():
        nstyle = NodeStyle()
        nstyle["fgcolor"] = "black"
        nstyle["size"] = 0
        n.set_style(nstyle)

    # add colour tags next to nodes
    if args.colour_tags:
        colour_tags = []
        print "\nThese columns will be used to generate colour tags:"
        for label in args.colour_tags:
            if label in column_list:
                colour_tags.append(label)
                print " " + label
            else:
                print "\tWARNING: specified label for colour tagging, " + label + ", was not found in the columns of the info file provided, " + args.info
        print(colour_tags)
        print("here")
        for i in range(0, len(colour_tags)):
            label = colour_tags[i]
            print(label)
            colour_dict = getColourPalette(column_values[label], args, label)

            print "- Adding colour tag for " + label

            for node in tree.get_leaves():
                this_face = Face()
                this_face.margin_left = args.padding
                node.add_face(this_face, column=0, position="aligned")

                if node.name in table:
                    this_label = table[node.name][label]
                    this_colour = colour_dict[this_label]
                else:
                    this_colour = "white"
                this_face = Face()
                this_face.background.color = this_colour
                this_face.margin_right = args.margin_right
                this_face.margin_left = args.margin_left
                this_face.margin_top = args.margin_top
                this_face.margin_bottom = args.margin_bottom
                this_face.border.width = args.border_width
                this_face.border.color = "white"
                node.add_face(this_face, column=i + 1, position="aligned")
        print
    else:
        colour_tags = []

    # add labels as columns
    for i in range(0, len(labels)):

        label = labels[i]

        print "- Adding label " + label
        if label == args.colour_nodes_by:
            print "  also colouring nodes by these values"
        print(column_values)
        print(args)
        colour_dict = getColourPalette(column_values[label], args, label)

        for node in tree.get_leaves():
            if node.name in table:
                this_label = table[node.name][label]
                this_colour = colour_dict[this_label]
            else:
                this_label = ""
                this_colour = "black"

            this_face = TextFace(text=this_label, fsize=args.font_size)
            if args.tags:
                this_face.background.color = this_colour
            elif label == args.colour_nodes_by:
                this_face.fgcolor = this_colour
            this_face.margin_right = args.padding
            if i == 0:
                this_face.margin_left = args.padding
            node.add_face(this_face,
                          column=i + len(colour_tags) + 1,
                          position="aligned")

            # set leaves to coloured circles
            node.img_style["size"] = args.node_size
            if label == args.colour_nodes_by:
                node.img_style["fgcolor"] = this_colour

    if args.colour_branches_by or args.colour_backgrounds_by or args.branch_support_colour:
        if args.colour_branches_by:
            print "- Colouring branches by label " + args.colour_branches_by
            colour_dict_br = getColourPalette(
                column_values[args.colour_branches_by], args,
                args.colour_branches_by)
        if args.colour_backgrounds_by:
            print "- Colouring node backgrounds by label " + args.colour_backgrounds_by
            colour_dict_bg = getColourPalette(
                column_values[args.colour_backgrounds_by], args,
                args.colour_backgrounds_by)
        if args.branch_support_colour:
            print "- Colouring branches by support values"
            # colours extracted from R using rgb( colorRamp(c("white","red", "black"))(seq(0, 1, length = 100)), max = 255)
            # support_colours = {0.0:"#FFFFFF",0.01:"#FFFFFF", 0.02:"#FFF9F9", 0.03:"#FFF4F4", 0.04:"#FFEFEF", 0.05:"#FFEAEA", 0.06:"#FFE5E5", 0.07:"#FFE0E0", 0.08:"#FFDADA", 0.09:"#FFD5D5", 0.1:"#FFD0D0", 0.11:"#FFCBCB", 0.12:"#FFC6C6", 0.13:"#FFC1C1", 0.14:"#FFBCBC", 0.15:"#FFB6B6", 0.16:"#FFB1B1", 0.17:"#FFACAC", 0.18:"#FFA7A7", 0.19:"#FFA2A2", 0.2:"#FF9D9D", 0.21:"#FF9797", 0.22:"#FF9292", 0.23:"#FF8D8D", 0.24:"#FF8888", 0.25:"#FF8383", 0.26:"#FF7E7E", 0.27:"#FF7979", 0.28:"#FF7373", 0.29:"#FF6E6E", 0.3:"#FF6969", 0.31:"#FF6464", 0.32:"#FF5F5F", 0.33:"#FF5A5A", 0.34:"#FF5454", 0.35:"#FF4F4F", 0.36:"#FF4A4A", 0.37:"#FF4545", 0.38:"#FF4040", 0.39:"#FF3B3B", 0.4:"#FF3636", 0.41:"#FF3030", 0.42:"#FF2B2B", 0.43:"#FF2626", 0.44:"#FF2121", 0.45:"#FF1C1C", 0.46:"#FF1717", 0.47:"#FF1212", 0.48:"#FF0C0C", 0.49:"#FF0707", 0.5:"#FF0202", 0.51:"#FC0000", 0.52:"#F70000", 0.53:"#F20000", 0.54:"#EC0000", 0.55:"#E70000", 0.56:"#E20000", 0.57:"#DD0000", 0.58:"#D80000", 0.59:"#D30000", 0.6:"#CE0000", 0.61:"#C80000", 0.62:"#C30000", 0.63:"#BE0000", 0.64:"#B90000", 0.65:"#B40000", 0.66:"#AF0000", 0.67:"#A90000", 0.68:"#A40000", 0.69:"#9F0000", 0.7:"#9A0000", 0.71:"#950000", 0.72:"#900000", 0.73:"#8B0000", 0.74:"#850000", 0.75:"#800000", 0.76:"#7B0000", 0.77:"#760000", 0.78:"#710000", 0.79:"#6C0000", 0.8:"#670000", 0.81:"#610000", 0.82:"#5C0000", 0.83:"#570000", 0.84:"#520000", 0.85:"#4D0000", 0.86:"#480000", 0.87:"#420000", 0.88:"#3D0000", 0.89:"#380000", 0.9:"#330000", 0.91:"#2E0000", 0.92:"#290000", 0.93:"#240000", 0.94:"#1E0000", 0.95:"#190000", 0.96:"#140000", 0.97:"#0F0000", 0.98:"#0A0000", 0.99:"#050000", 1:"#000000"}
            # rgb( colorRamp(c("red", "black"))(seq(0, 1, length = 100)), max = 255))
            support_colours = {}

            if args.branch_support_cutoff:
                for i in range(0, args.branch_support_cutoff):
                    support_colours[i] = "#FF0000"
                for i in range(args.branch_support_cutoff, 101):
                    support_colours[i] = "#000000"
            else:
                if args.branch_support_percent:
                    support_colours = {
                        0: "#FF0000",
                        1: "#FF0000",
                        2: "#FC0000",
                        3: "#F90000",
                        4: "#F70000",
                        5: "#F40000",
                        6: "#F20000",
                        7: "#EF0000",
                        8: "#EC0000",
                        9: "#EA0000",
                        10: "#E70000",
                        11: "#E50000",
                        12: "#E20000",
                        13: "#E00000",
                        14: "#DD0000",
                        15: "#DA0000",
                        16: "#D80000",
                        17: "#D50000",
                        18: "#D30000",
                        19: "#D00000",
                        20: "#CE0000",
                        21: "#CB0000",
                        22: "#C80000",
                        23: "#C60000",
                        24: "#C30000",
                        25: "#C10000",
                        26: "#BE0000",
                        27: "#BC0000",
                        28: "#B90000",
                        29: "#B60000",
                        30: "#B40000",
                        31: "#B10000",
                        32: "#AF0000",
                        33: "#AC0000",
                        34: "#AA0000",
                        35: "#A70000",
                        36: "#A40000",
                        37: "#A20000",
                        38: "#9F0000",
                        39: "#9D0000",
                        40: "#9A0000",
                        41: "#970000",
                        42: "#950000",
                        43: "#920000",
                        44: "#900000",
                        45: "#8D0000",
                        46: "#8B0000",
                        47: "#880000",
                        48: "#850000",
                        49: "#830000",
                        50: "#800000",
                        51: "#7E0000",
                        52: "#7B0000",
                        53: "#790000",
                        54: "#760000",
                        55: "#730000",
                        56: "#710000",
                        57: "#6E0000",
                        58: "#6C0000",
                        59: "#690000",
                        60: "#670000",
                        61: "#640000",
                        62: "#610000",
                        63: "#5F0000",
                        64: "#5C0000",
                        65: "#5A0000",
                        66: "#570000",
                        67: "#540000",
                        68: "#520000",
                        69: "#4F0000",
                        70: "#4D0000",
                        71: "#4A0000",
                        72: "#480000",
                        73: "#450000",
                        74: "#420000",
                        75: "#400000",
                        76: "#3D0000",
                        77: "#3B0000",
                        78: "#380000",
                        79: "#360000",
                        80: "#330000",
                        81: "#300000",
                        82: "#2E0000",
                        83: "#2B0000",
                        84: "#290000",
                        85: "#260000",
                        86: "#240000",
                        87: "#210000",
                        88: "#1E0000",
                        89: "#1C0000",
                        90: "#190000",
                        91: "#170000",
                        92: "#140000",
                        93: "#120000",
                        94: "#0F0000",
                        95: "#0C0000",
                        96: "#0A0000",
                        97: "#070000",
                        98: "#050000",
                        99: "#020000",
                        100: "#000000"
                    }
                else:
                    support_colours = {
                        0.0: "#FF0000",
                        0.01: "#FF0000",
                        0.02: "#FC0000",
                        0.03: "#F90000",
                        0.04: "#F70000",
                        0.05: "#F40000",
                        0.06: "#F20000",
                        0.07: "#EF0000",
                        0.08: "#EC0000",
                        0.09: "#EA0000",
                        0.1: "#E70000",
                        0.11: "#E50000",
                        0.12: "#E20000",
                        0.13: "#E00000",
                        0.14: "#DD0000",
                        0.15: "#DA0000",
                        0.16: "#D80000",
                        0.17: "#D50000",
                        0.18: "#D30000",
                        0.19: "#D00000",
                        0.2: "#CE0000",
                        0.21: "#CB0000",
                        0.22: "#C80000",
                        0.23: "#C60000",
                        0.24: "#C30000",
                        0.25: "#C10000",
                        0.26: "#BE0000",
                        0.27: "#BC0000",
                        0.28: "#B90000",
                        0.29: "#B60000",
                        0.3: "#B40000",
                        0.31: "#B10000",
                        0.32: "#AF0000",
                        0.33: "#AC0000",
                        0.34: "#AA0000",
                        0.35: "#A70000",
                        0.36: "#A40000",
                        0.37: "#A20000",
                        0.38: "#9F0000",
                        0.39: "#9D0000",
                        0.4: "#9A0000",
                        0.41: "#970000",
                        0.42: "#950000",
                        0.43: "#920000",
                        0.44: "#900000",
                        0.45: "#8D0000",
                        0.46: "#8B0000",
                        0.47: "#880000",
                        0.48: "#850000",
                        0.49: "#830000",
                        0.5: "#800000",
                        0.51: "#7E0000",
                        0.52: "#7B0000",
                        0.53: "#790000",
                        0.54: "#760000",
                        0.55: "#730000",
                        0.56: "#710000",
                        0.57: "#6E0000",
                        0.58: "#6C0000",
                        0.59: "#690000",
                        0.6: "#670000",
                        0.61: "#640000",
                        0.62: "#610000",
                        0.63: "#5F0000",
                        0.64: "#5C0000",
                        0.65: "#5A0000",
                        0.66: "#570000",
                        0.67: "#540000",
                        0.68: "#520000",
                        0.69: "#4F0000",
                        0.7: "#4D0000",
                        0.71: "#4A0000",
                        0.72: "#480000",
                        0.73: "#450000",
                        0.74: "#420000",
                        0.75: "#400000",
                        0.76: "#3D0000",
                        0.77: "#3B0000",
                        0.78: "#380000",
                        0.79: "#360000",
                        0.8: "#330000",
                        0.81: "#300000",
                        0.82: "#2E0000",
                        0.83: "#2B0000",
                        0.84: "#290000",
                        0.85: "#260000",
                        0.86: "#240000",
                        0.87: "#210000",
                        0.88: "#1E0000",
                        0.89: "#1C0000",
                        0.9: "#190000",
                        0.91: "#170000",
                        0.92: "#140000",
                        0.93: "#120000",
                        0.94: "#0F0000",
                        0.95: "#0C0000",
                        0.96: "#0A0000",
                        0.97: "#070000",
                        0.98: "#050000",
                        0.99: "#020000",
                        1.0: "#000000"
                    }
        for node in tree.traverse():
            nstyle = NodeStyle()
            nstyle["size"] = 0
            if node.name in table:
                #print "Colouring individual " + node.name
                if args.colour_branches_by:
                    nstyle["vt_line_color"] = colour_dict_br[table[node.name][
                        args.colour_branches_by]]  # set branch colour
                    nstyle["hz_line_color"] = colour_dict_br[table[node.name][
                        args.colour_branches_by]]
                if args.colour_backgrounds_by:
                    if args.colour_branches_by in table[node.name]:
                        if table[node.name][args.colour_branches_by] != "none":
                            if not args.colour_branches_by:
                                nstyle["hz_line_color"] = "black"
                                nstyle["vt_line_color"] = "black"
                            nstyle["bgcolor"] = colour_dict_bg[table[node.name][
                                args.
                                colour_backgrounds_by]]  # set background colour
                node.set_style(nstyle)
            else:
                # internal node
                descendants = node.get_leaves()
                descendant_labels_br = []
                descendant_labels_bg = []
                for d in descendants:
                    if args.colour_branches_by:
                        if d.name in table:
                            this_label_br = table[d.name][
                                args.colour_branches_by]
                            if this_label_br not in descendant_labels_br:
                                descendant_labels_br.append(this_label_br)
                        elif "none" not in descendant_labels_br:
                            descendant_labels_br.append("none")
                    if args.colour_backgrounds_by:
                        if d.name in table:
                            this_label_bg = table[d.name][
                                args.colour_backgrounds_by]
                            if this_label_bg not in descendant_labels_bg:
                                descendant_labels_bg.append(this_label_bg)
                        elif "none" not in descendant_labels_bg:
                            descendant_labels_bg.append("none")
#				nstyle = NodeStyle()
#				nstyle["size"] = 0
                if len(descendant_labels_br
                       ) == 1 and descendant_labels_br[0] != "none":
                    this_colour = colour_dict_br[descendant_labels_br[0]]
                    nstyle["vt_line_color"] = this_colour  # set branch colour
                    nstyle["hz_line_color"] = this_colour
                elif args.branch_support_colour and not node.is_leaf():
                    if int(node.support) in support_colours:
                        nstyle["vt_line_color"] = support_colours[int(
                            node.support)]  # take colour from support value
                        nstyle["hz_line_color"] = support_colours[int(
                            node.support)]
                    else:
                        print "  WARNING support values don't make sense. Note scale is assumed to be 0-1 unless using the --branch_support_percent flag."
                if len(descendant_labels_bg
                       ) == 1 and descendant_labels_bg[0] != "none":
                    this_colour = colour_dict_bg[descendant_labels_bg[0]]
                    nstyle["bgcolor"] = this_colour  # set background colour
                node.set_style(nstyle)

    if args.colour_nodes_by:
        if args.colour_nodes_by not in labels:
            print "- Colouring nodes by label " + args.colour_nodes_by
            colour_dict = getColourPalette(column_values[args.colour_nodes_by],
                                           args, args.colour_nodes_by)
            for node in tree.get_leaves():
                if node.name in table:
                    this_label = table[node.name][args.colour_nodes_by]
                    this_colour = colour_dict[this_label]
                    if this_colour != "None":
                        node.img_style["fgcolor"] = this_colour
                        node.img_style["size"] = args.node_size

    for node in tree.traverse():
        node.img_style["hz_line_width"] = args.branch_thickness
        node.img_style["vt_line_width"] = args.branch_thickness

    # matches = search_by_size(tree, 75)
    # print("matches")
    # node = matches[0]
    # node.swap_children()


########### COMENTADO PARA VER COMO QUEDA CON EL BARCODE. PORQUE ESE NO TIENE TODOS LOS NODOS

# rotate specific nodes
# matches = search_by_size(tree, 463)
# print("matches")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 601)
# print("matches")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(node, 7)
# print("matches")
# node = matches[1]
# node.swap_children()

# matches = search_by_size(node, 5)
# print("matches 5")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 8)
# print("matches 8")
# node = matches[5]
# node.swap_children()

# matches = search_by_size(tree, 349)
# print("matches 349")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 192)
# print("matches 192")
# node = matches[1]
# node.swap_children()

# matches = search_by_size(tree, 182)
# print("matches 182")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 180)
# print("matches 180")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 175)
# print("matches 175")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 34)
# print("matches 34")
# node = matches[0]
# node.swap_children()

#### FIN ####

#for n in matches:
#	print n

# set tree style
    ts = TreeStyle()

    ts.draw_guiding_lines = True
    ts.guiding_lines_color = 'black'

    if args.show_leaf_names:
        ts.show_leaf_name = True
    else:
        ts.show_leaf_name = False

    if args.length_scale:
        ts.scale = args.length_scale

    if args.branch_padding:
        ts.branch_vertical_margin = args.branch_padding

    if args.branch_support_print:
        ts.show_branch_support = True

    if args.fan:
        ts.mode = "c"
        print "\nPrinting circular tree (--fan)"
    else:
        print "\nPrinting rectangular tree, to switch to circular use --fan"

    if args.title:
        title = TextFace(args.title, fsize=20)
        title.margin_left = 20
        title.margin_top = 20
        ts.title.add_face(title, column=1)

    if args.no_guiding_lines:
        ts.draw_guiding_lines = False

    if args.data:
        print "\nPrinting data matrix as " + args.data_type + " with range (" + str(
            args.mindata) + "->" + str(args.maxdata) + ";" + str(
                args.centervalue) + "), height " + str(
                    args.data_height) + ", width " + str(args.data_width)
        profileFace = ProfileFace(min_v=args.mindata,
                                  max_v=args.maxdata,
                                  center_v=args.centervalue,
                                  width=args.data_width,
                                  height=args.data_height,
                                  style=args.data_type)

        def mylayout(node):
            if node.is_leaf():
                add_face_to_node(profileFace, node, 0, aligned=True)

        ts.layout_fn = mylayout

    # set root branch length to zero
    tree.dist = 0

    if args.delete_branches:
        #print "Branches "+ args.delete_branches + " will not be shown"
        for branch in args.delete_branches:
            leaf = tree.get_leaves_by_name(branch)[0]  #SRR1173284
            leaf.delete()

    # render tree
    tree.render(args.output, w=args.width, dpi=400, units="mm", tree_style=ts)
    print "\n FINISHED! Tree plot printed to file " + args.output
    print

    if args.print_colour_dict:
        print colour_dict
        if args.colour_branches_by:
            print colour_dict_br
        if args.colour_backgrounds_by:
            print colour_dict_bg

    if args.interactive:
        print "\nEntering interactive mode..."
        tree.show(tree_style=ts)
예제 #22
0
def main():
	args = parse_args()
	
	if args.data:
		print "\nReading tree from " + args.tree + " and data matrix from " + args.data
		tree = ClusterTree(args.tree, text_array=args.data)
	else:
		print "\nReading tree from " + args.tree
		tree = Tree(args.tree)	
		
	if args.midpoint:
		R = tree.get_midpoint_outgroup()
		tree.set_outgroup(R)
		print "- Applying midpoint rooting"
	elif args.outgroup:
		tree.set_outgroup( tree&args.outgroup )
		print "- Rooting using outgroup " + args.outgroup
		
	if not args.no_ladderize:
		tree.ladderize()
		print "- Ladderizing tree"

	table, column_list, column_values = readtable(args, tree.get_leaf_names())
	
	labels = []
	if args.labels:
		print "\nThese labels will be printed next to each strain:"
		for label in args.labels:
			if label in column_list:
				labels.append(label)
				print " " + label
			else:
				print "WARNING: specified label " + label + " was not found in the columns of the info file provided, " + args.info

	# set node styles
	# start by setting all to no shapes, black labels
	for n in tree.traverse():
		nstyle = NodeStyle()
		nstyle["fgcolor"] = "black"
		nstyle["size"] = 0
		n.set_style(nstyle)
	
	# add colour tags next to nodes
	if args.colour_tags:
		colour_tags = []
		print "\nThese columns will be used to generate colour tags:"
		for label in args.colour_tags:
			if label in column_list:
				colour_tags.append(label)
				print " " + label
			else:
				print "\tWARNING: specified label for colour tagging, " + label + ", was not found in the columns of the info file provided, " + args.info
				
		for i in range(0,len(colour_tags)):
			label = colour_tags[i]
			colour_dict = getColourPalette(column_values[label],args,label)
			
			print "- Adding colour tag for " + label
			
			for node in tree.get_leaves():
				this_face = Face()
				this_face.margin_left = args.padding
				node.add_face(this_face, column=0, position = "aligned")
				
				if node.name in table:
					this_label = table[node.name][label]
					this_colour = colour_dict[this_label]
				else:
					this_colour = "white"
				this_face = Face()
				this_face.background.color = this_colour
				this_face.margin_right = args.margin_right
				this_face.margin_left = args.margin_left
				this_face.margin_top = args.margin_top
				this_face.margin_bottom = args.margin_bottom
				this_face.border.width = args.border_width
				this_face.border.color="white"
				node.add_face(this_face, column=i+1, position = "aligned")
		print
	else:
		colour_tags = []
	
	# add labels as columns
	for i in range(0,len(labels)):
		
		label = labels[i]
		
		print "- Adding label " + label
		if label == args.colour_nodes_by:
			print "  also colouring nodes by these values"
		
		colour_dict = getColourPalette(column_values[label],args,label)
		
		for node in tree.get_leaves():
			if node.name in table:
				this_label = table[node.name][label]
				this_colour = colour_dict[this_label]
			else:
				this_label = ""
				this_colour = "black"

			this_face = TextFace(text=this_label, fsize = args.font_size)
			if args.tags:
				this_face.background.color = this_colour
			elif label == args.colour_nodes_by:
				this_face.fgcolor = this_colour
			this_face.margin_right = args.padding
			if i == 0:
				this_face.margin_left = args.padding
			node.add_face(this_face, column=i+len(colour_tags)+1, position = "aligned")
			
			# set leaves to coloured circles
			node.img_style["size"] = args.node_size
			if label == args.colour_nodes_by:
				node.img_style["fgcolor"] = this_colour
					
	if args.colour_branches_by or args.colour_backgrounds_by or args.branch_support_colour:
		if args.colour_branches_by:
			print "- Colouring branches by label " + args.colour_branches_by
			colour_dict_br = getColourPalette(column_values[args.colour_branches_by],args,args.colour_branches_by)
		if args.colour_backgrounds_by:
			print "- Colouring node backgrounds by label " + args.colour_backgrounds_by
			colour_dict_bg = getColourPalette(column_values[args.colour_backgrounds_by],args,args.colour_backgrounds_by)
		if args.branch_support_colour:
			print "- Colouring branches by support values"
			# colours extracted from R using rgb( colorRamp(c("white","red", "black"))(seq(0, 1, length = 100)), max = 255)
			# support_colours = {0.0:"#FFFFFF",0.01:"#FFFFFF", 0.02:"#FFF9F9", 0.03:"#FFF4F4", 0.04:"#FFEFEF", 0.05:"#FFEAEA", 0.06:"#FFE5E5", 0.07:"#FFE0E0", 0.08:"#FFDADA", 0.09:"#FFD5D5", 0.1:"#FFD0D0", 0.11:"#FFCBCB", 0.12:"#FFC6C6", 0.13:"#FFC1C1", 0.14:"#FFBCBC", 0.15:"#FFB6B6", 0.16:"#FFB1B1", 0.17:"#FFACAC", 0.18:"#FFA7A7", 0.19:"#FFA2A2", 0.2:"#FF9D9D", 0.21:"#FF9797", 0.22:"#FF9292", 0.23:"#FF8D8D", 0.24:"#FF8888", 0.25:"#FF8383", 0.26:"#FF7E7E", 0.27:"#FF7979", 0.28:"#FF7373", 0.29:"#FF6E6E", 0.3:"#FF6969", 0.31:"#FF6464", 0.32:"#FF5F5F", 0.33:"#FF5A5A", 0.34:"#FF5454", 0.35:"#FF4F4F", 0.36:"#FF4A4A", 0.37:"#FF4545", 0.38:"#FF4040", 0.39:"#FF3B3B", 0.4:"#FF3636", 0.41:"#FF3030", 0.42:"#FF2B2B", 0.43:"#FF2626", 0.44:"#FF2121", 0.45:"#FF1C1C", 0.46:"#FF1717", 0.47:"#FF1212", 0.48:"#FF0C0C", 0.49:"#FF0707", 0.5:"#FF0202", 0.51:"#FC0000", 0.52:"#F70000", 0.53:"#F20000", 0.54:"#EC0000", 0.55:"#E70000", 0.56:"#E20000", 0.57:"#DD0000", 0.58:"#D80000", 0.59:"#D30000", 0.6:"#CE0000", 0.61:"#C80000", 0.62:"#C30000", 0.63:"#BE0000", 0.64:"#B90000", 0.65:"#B40000", 0.66:"#AF0000", 0.67:"#A90000", 0.68:"#A40000", 0.69:"#9F0000", 0.7:"#9A0000", 0.71:"#950000", 0.72:"#900000", 0.73:"#8B0000", 0.74:"#850000", 0.75:"#800000", 0.76:"#7B0000", 0.77:"#760000", 0.78:"#710000", 0.79:"#6C0000", 0.8:"#670000", 0.81:"#610000", 0.82:"#5C0000", 0.83:"#570000", 0.84:"#520000", 0.85:"#4D0000", 0.86:"#480000", 0.87:"#420000", 0.88:"#3D0000", 0.89:"#380000", 0.9:"#330000", 0.91:"#2E0000", 0.92:"#290000", 0.93:"#240000", 0.94:"#1E0000", 0.95:"#190000", 0.96:"#140000", 0.97:"#0F0000", 0.98:"#0A0000", 0.99:"#050000", 1:"#000000"}
			# rgb( colorRamp(c("red", "black"))(seq(0, 1, length = 100)), max = 255))
			support_colours = {}
			
			if args.branch_support_cutoff:
				for i in range(0,args.branch_support_cutoff):
					support_colours[i] = "#FF0000"
				for i in range(args.branch_support_cutoff,101):
					support_colours[i] = "#000000"
			else:
				if args.branch_support_percent:
					support_colours = {0:"#FF0000",1:"#FF0000",2:"#FC0000",3:"#F90000",4:"#F70000",5:"#F40000",6:"#F20000",7:"#EF0000",8:"#EC0000",9:"#EA0000",10:"#E70000",11:"#E50000",12:"#E20000",13:"#E00000",14:"#DD0000",15:"#DA0000",16:"#D80000",17:"#D50000",18:"#D30000",19:"#D00000",20:"#CE0000",21:"#CB0000",22:"#C80000",23:"#C60000",24:"#C30000",25:"#C10000",26:"#BE0000",27:"#BC0000",28:"#B90000",29:"#B60000",30:"#B40000",31:"#B10000",32:"#AF0000",33:"#AC0000",34:"#AA0000",35:"#A70000",36:"#A40000",37:"#A20000",38:"#9F0000",39:"#9D0000",40:"#9A0000",41:"#970000",42:"#950000",43:"#920000",44:"#900000",45:"#8D0000",46:"#8B0000",47:"#880000",48:"#850000",49:"#830000",50:"#800000",51:"#7E0000",52:"#7B0000",53:"#790000",54:"#760000",55:"#730000",56:"#710000",57:"#6E0000",58:"#6C0000",59:"#690000",60:"#670000",61:"#640000",62:"#610000",63:"#5F0000",64:"#5C0000",65:"#5A0000",66:"#570000",67:"#540000",68:"#520000",69:"#4F0000",70:"#4D0000",71:"#4A0000",72:"#480000",73:"#450000",74:"#420000",75:"#400000",76:"#3D0000",77:"#3B0000",78:"#380000",79:"#360000",80:"#330000",81:"#300000",82:"#2E0000",83:"#2B0000",84:"#290000",85:"#260000",86:"#240000",87:"#210000",88:"#1E0000",89:"#1C0000",90:"#190000",91:"#170000",92:"#140000",93:"#120000",94:"#0F0000",95:"#0C0000",96:"#0A0000",97:"#070000",98:"#050000",99:"#020000",100:"#000000"}
				else:
					support_colours = {0.0:"#FF0000", 0.01:"#FF0000", 0.02:"#FC0000", 0.03:"#F90000", 0.04:"#F70000", 0.05:"#F40000", 0.06:"#F20000", 0.07:"#EF0000", 0.08:"#EC0000", 0.09:"#EA0000", 0.1:"#E70000", 0.11:"#E50000", 0.12:"#E20000", 0.13:"#E00000", 0.14:"#DD0000", 0.15:"#DA0000", 0.16:"#D80000", 0.17:"#D50000", 0.18:"#D30000", 0.19:"#D00000", 0.2:"#CE0000", 0.21:"#CB0000", 0.22:"#C80000", 0.23:"#C60000", 0.24:"#C30000", 0.25:"#C10000", 0.26:"#BE0000", 0.27:"#BC0000", 0.28:"#B90000", 0.29:"#B60000", 0.3:"#B40000", 0.31:"#B10000", 0.32:"#AF0000", 0.33:"#AC0000", 0.34:"#AA0000", 0.35:"#A70000", 0.36:"#A40000", 0.37:"#A20000", 0.38:"#9F0000", 0.39:"#9D0000", 0.4:"#9A0000", 0.41:"#970000", 0.42:"#950000", 0.43:"#920000", 0.44:"#900000", 0.45:"#8D0000", 0.46:"#8B0000", 0.47:"#880000", 0.48:"#850000", 0.49:"#830000", 0.5:"#800000", 0.51:"#7E0000", 0.52:"#7B0000", 0.53:"#790000", 0.54:"#760000", 0.55:"#730000", 0.56:"#710000", 0.57:"#6E0000", 0.58:"#6C0000", 0.59:"#690000", 0.6:"#670000", 0.61:"#640000", 0.62:"#610000", 0.63:"#5F0000", 0.64:"#5C0000", 0.65:"#5A0000", 0.66:"#570000", 0.67:"#540000", 0.68:"#520000", 0.69:"#4F0000", 0.7:"#4D0000", 0.71:"#4A0000", 0.72:"#480000", 0.73:"#450000", 0.74:"#420000", 0.75:"#400000", 0.76:"#3D0000", 0.77:"#3B0000", 0.78:"#380000", 0.79:"#360000", 0.8:"#330000", 0.81:"#300000", 0.82:"#2E0000", 0.83:"#2B0000", 0.84:"#290000", 0.85:"#260000", 0.86:"#240000", 0.87:"#210000", 0.88:"#1E0000", 0.89:"#1C0000", 0.9:"#190000", 0.91:"#170000", 0.92:"#140000", 0.93:"#120000", 0.94:"#0F0000", 0.95:"#0C0000", 0.96:"#0A0000", 0.97:"#070000", 0.98:"#050000", 0.99:"#020000", 1.0:"#000000"}
		for node in tree.traverse():			
			nstyle = NodeStyle()
			nstyle["size"] = 0
			if node.name in table:
				#print "Colouring individual " + node.name
				if args.colour_branches_by:
					nstyle["vt_line_color"] = colour_dict_br[table[node.name][args.colour_branches_by]] # set branch colour
					nstyle["hz_line_color"] = colour_dict_br[table[node.name][args.colour_branches_by]]
				if args.colour_backgrounds_by:
					if args.colour_branches_by in table[node.name]:
						if table[node.name][args.colour_branches_by] != "none":
							nstyle["bgcolor"] = colour_dict_bg[table[node.name][args.colour_backgrounds_by]] # set background colour
				node.set_style(nstyle)
			else:
				# internal node
				descendants = node.get_leaves()
				descendant_labels_br = []
				descendant_labels_bg = []
				for d in descendants:
					if args.colour_branches_by:
						if d.name in table:
							this_label_br = table[d.name][args.colour_branches_by]
							if this_label_br not in descendant_labels_br:
								descendant_labels_br.append(this_label_br)
						elif "none" not in descendant_labels_br:
							descendant_labels_br.append("none")
					if args.colour_backgrounds_by:
						if d.name in table:
							this_label_bg = table[d.name][args.colour_backgrounds_by]
							if this_label_bg not in descendant_labels_bg:
								descendant_labels_bg.append(this_label_bg)
						elif "none" not in descendant_labels_bg:
							descendant_labels_bg.append("none")
#				nstyle = NodeStyle()
#				nstyle["size"] = 0
				if len(descendant_labels_br) == 1 and descendant_labels_br[0] != "none":
					this_colour = colour_dict_br[descendant_labels_br[0]]
					nstyle["vt_line_color"] = this_colour # set branch colour
					nstyle["hz_line_color"] = this_colour
				elif args.branch_support_colour and not node.is_leaf():
					if int(node.support) in support_colours:
						nstyle["vt_line_color"] = support_colours[int(node.support)] # take colour from support value
						nstyle["hz_line_color"] = support_colours[int(node.support)]
					else:
						print "  WARNING support values don't make sense. Note scale is assumed to be 0-1 unless using the --branch_support_percent flag."
				if len(descendant_labels_bg) == 1 and descendant_labels_bg[0] != "none":
					this_colour = colour_dict_bg[descendant_labels_bg[0]]
					nstyle["bgcolor"] = this_colour # set background colour
				node.set_style(nstyle)
					
	if args.colour_nodes_by:
		if args.colour_nodes_by not in labels:
			print "- Colouring nodes by label " + args.colour_nodes_by
			colour_dict = getColourPalette(column_values[args.colour_nodes_by],args,args.colour_nodes_by)
			for node in tree.get_leaves():
				if node.name in table:
					this_label = table[node.name][args.colour_nodes_by]
					this_colour = colour_dict[this_label]
					if this_colour != "None":
						node.img_style["fgcolor"] = this_colour
						node.img_style["size"] = args.node_size
				

	for node in tree.traverse():
		node.img_style["hz_line_width"] = args.branch_thickness
		node.img_style["vt_line_width"] = args.branch_thickness
			
	# set tree style
	ts = TreeStyle()
	
	if args.show_leaf_names:
		ts.show_leaf_name = True
	else:
		ts.show_leaf_name = False

	if args.length_scale:
		ts.scale = args.length_scale
		
	if args.branch_padding:
		ts.branch_vertical_margin = args.branch_padding
		
	if args.branch_support_print:
		ts.show_branch_support = True
		
	if args.fan:
		ts.mode = "c"
		print "\nPrinting circular tree (--fan)"
	else:
		print "\nPrinting rectangular tree, to switch to circular use --fan"
		
	if args.title:
		title = TextFace(args.title, fsize=20)
		title.margin_left = 20
		title.margin_top = 20
		ts.title.add_face(title, column=1)
		
	if args.no_guiding_lines:
		ts.draw_guiding_lines = False
		
	if args.data:
		print "\nPrinting data matrix as " + args.data_type + " with range (" +  str(args.mindata) + "->" +  str(args.maxdata) + ";" +  str(args.centervalue) + "), height " + str(args.data_height) + ", width " + str(args.data_width)
		profileFace  = ProfileFace(min_v=args.mindata, max_v=args.maxdata, center_v=args.centervalue, width=args.data_width, height=args.data_height, style=args.data_type)
		def mylayout(node):
			if node.is_leaf():
				add_face_to_node(profileFace, node, 0, aligned=True)
		ts.layout_fn = mylayout
		
	# set root branch length to zero
	tree.dist=0
	
	# render tree
	tree.render(args.output, w=args.width, dpi=300, units="mm", tree_style=ts)
	
	print "\n FINISHED! Tree plot printed to file " + args.output
	print
	
	if args.print_colour_dict:
		print colour_dict
		if args.colour_branches_by:
			print colour_dict_br
		if args.colour_backgrounds_by:
			print colour_dict_bg
	
	if args.interactive:
		print "\nEntering interactive mode..."
		tree.show(tree_style=ts)
예제 #23
0
                        fgcolor="green",
                        text_prefix="")
        faces.add_face_to_node(attr, node, 0, position="branch-top")


#ts.show_branch_support = True
tree_index = 1
with open(args.input_tree_file, "r") as in_fd:
    for line in in_fd:
        tree_line = line.strip()
        tree = Tree(tree_line, format=args.input_tree_format)
        for node in tree.traverse():
            node.img_style["size"] = 0

        ts = TreeStyle()
        ts.layout_fn = layout
        ts.optimal_scale_level = "full"
        ts.branch_vertical_margin = 10
        ts.show_leaf_name = False
        #print(tree.write(format=args.input_tree_format, features=[args.feature_name]))
        if args.draw_mode == "render":
            tree.render("%s_%i.png" %
                        (args.output_tree_file_prefix, tree_index),
                        tree_style=ts,
                        w=args.width,
                        units=args.width_units)
        else:
            tree.show()

        tree_index += 1
예제 #24
0
                                     # configuration

# We extend the minimum WebTreeApplication with our own WSGI
# application
application.set_external_app_handler(example_app)

# Lets now apply our custom tree loader function to the main
# application 
application.set_tree_loader(my_tree_loader)

# And our layout as the default one to render trees
ts = TreeStyle()
ts.show_leaf_name = False
ts.layout_fn.append(main_layout)
ts.mode = "r"
ts.branch_vertical_margin = 5
#ts.scale = 20
application.set_tree_style(ts)
#application.set_default_layout_fn(main_layout)
application.set_tree_size(None, None)
# I want to make up how tree image in shown using a custrom tree
# renderer that adds much more HTML code
application.set_external_tree_renderer(tree_renderer)


# ==============================================================================
# ADD CUSTOM ACTIONS TO THE APPLICATION
#
# The function "register_action" allows to attach functionality to
# nodes in the image. All registered accions will be shown in the
# popup menu bound to the nodes and faces in the web image.
예제 #25
0
def bct_dendrogram(corpus,
                   tree,
                   outputfile=None,
                   fontsize=5,
                   save_newick=True,
                   mode='c',
                   show=False,
                   color_leafs=False,
                   save=False,
                   return_svg=True):
    """
    Draw a dendrogram of the texts in the corpus using ETE.
    
    Parameters
    ----------
    corpus : `Corpus` instance
        The corpus to be plotted.
    tree : `(VN)Clusterer` object
        The clusterer object which was
        applied to the corpus.
    outputfile : str
        The path where the plot should be saved.
    color_leafs: boolean, default=True,
        If true, will color the text labels
        according to their category.
    fontsize : int, default=5
        The fontsize of the labels on the axes.
    save_newick : boolean, default=True
        Whether to dump a representation of the
        tree in newick-format, which can later
        be modified using software like FigTree:
        http://tree.bio.ed.ac.uk/software/figtree/
    mode : str, default='c'
        The type of tree to be drawn. Supports:
        - 'c': circular dendrogram
        - 'r': traditional, rectangular dendrogram
    save : boolean, default=False
        Whether to save the plot to `outputfile`.
    return_svg : boolean, default=True
        Whether to return the plot in SVG-format.
        Useful for the GUI.
    """

    for leaf in tree.get_leaves():
        leaf.name = leaf.name.replace("'", '')

    def layout(node):
        if node.is_leaf():
            N = AttrFace("name", fsize=7)
            faces.add_face_to_node(faces.AttrFace("name", "Arial", 10, None),
                                   node,
                                   0,
                                   position='branch-right')
            # problems: aligment of labels to branch, left padding of labels

    ts = TreeStyle()
    ts.mode = mode
    ts.show_leaf_name = False
    ts.scale = 120
    ts.show_scale = False
    ts.branch_vertical_margin = 10

    nstyle = NodeStyle()
    nstyle["fgcolor"] = "#0f0f0f"
    nstyle["size"] = 0
    nstyle["vt_line_color"] = "#0f0f0f"
    nstyle["hz_line_color"] = "#0f0f0f"
    nstyle["vt_line_width"] = 1
    nstyle["hz_line_width"] = 1
    nstyle["vt_line_type"] = 0
    nstyle["hz_line_type"] = 0

    for n in tree.traverse():
        n.set_style(nstyle)

    ts.layout_fn = layout

    if outputfile:
        outputfile = os.path.expanduser(outputfile)

    if save_newick:  # save tree in newick format for later manipulation in e.g. FigTree:
        tree.write(outfile=os.path.splitext(outputfile)[0] + '.newick')

    if save:
        tree.render(outputfile, tree_style=ts)
    if show:
        tree.show(tree_style=ts)
    if return_svg:  # return the SVG as a string
        return tree.render("%%return")[0]
예제 #26
0
def cluster():
    fileManager = managers.utility.loadFileManager()
    leq = '≤'.decode('utf-8')

    if request.method == "GET":
        # "GET" request occurs when the page is first loaded.
        if 'analyoption' not in session:
            session['analyoption'] = constants.DEFAULT_ANALIZE_OPTIONS
        if 'hierarchyoption' not in session:
            session['hierarchyoption'] = constants.DEFAULT_HIERARCHICAL_OPTIONS
        labels = fileManager.getActiveLabels()
        thresholdOps = {}
        return render_template('cluster.html', labels=labels, thresholdOps=thresholdOps)

    if 'getdendro' in request.form:
        labelDict = fileManager.getActiveLabels()
        labels = []
        for ind, label in labelDict.items():
            labels.append(label)
        # Apply re-tokenisation and filters to DTM 
        #countMatrix = fileManager.getMatrix(ARGUMENTS OMITTED)

        # Get options from request.form
        orientation = str(request.form['orientation'])
        title = request.form['title']
        pruning = request.form['pruning']
        pruning = int(request.form['pruning']) if pruning else 0
        linkage = str(request.form['linkage'])
        metric = str(request.form['metric'])

        # Get active files
        allContents = []  # list of strings-of-text for each segment
        tempLabels = []  # list of labels for each segment
        for lFile in fileManager.files.values():
            if lFile.active:
                contentElement = lFile.loadContents()
                allContents.append(contentElement)

                if request.form["file_" + str(lFile.id)] == lFile.label:
                    tempLabels.append(lFile.label.encode("utf-8"))
                else:
                    newLabel = request.form["file_" + str(lFile.id)].encode("utf-8")
                    tempLabels.append(newLabel)

        # More options
        ngramSize = int(request.form['tokenSize'])
        useWordTokens = request.form['tokenType'] == 'word'
        try:
            useFreq = request.form['normalizeType'] == 'freq'

            useTfidf = request.form['normalizeType'] == 'tfidf'  # if use TF/IDF
            normOption = "N/A"  # only applicable when using "TF/IDF", set default value to N/A
            if useTfidf:
                if request.form['norm'] == 'l1':
                    normOption = u'l1'
                elif request.form['norm'] == 'l2':
                    normOption = u'l2'
                else:
                    normOption = None
        except:
            useFreq = useTfidf = False
            normOption = None

        onlyCharGramsWithinWords = False
        if not useWordTokens:  # if using character-grams
            # this option is disabled on the GUI, because countVectorizer count front and end markers as ' ' if this is true
            onlyCharGramsWithinWords = 'inWordsOnly' in request.form

        greyWord = 'greyword' in request.form
        MostFrequenWord = 'mfwcheckbox' in request.form
        Culling = 'cullcheckbox' in request.form

        showDeletedWord = False
        if 'greyword' or 'mfwcheckbox' or 'cullcheckbox' in request.form:
            if 'onlygreyword' in request.form:
                showDeletedWord = True

        if useWordTokens:
            tokenType = u'word'
        else:
            tokenType = u'char'
            if onlyCharGramsWithinWords:
                tokenType = u'char_wb'

        from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
        vectorizer = CountVectorizer(input=u'content', encoding=u'utf-8', min_df=1,
                                      analyzer=tokenType, token_pattern=ur'(?u)\b[\w\']+\b',
                                      ngram_range=(ngramSize, ngramSize),
                                      stop_words=[], dtype=float, max_df=1.0)

        # make a (sparse) Document-Term-Matrix (DTM) to hold all counts
        DocTermSparseMatrix = vectorizer.fit_transform(allContents)
        dtm = DocTermSparseMatrix.toarray()

        from sklearn.metrics.pairwise import euclidean_distances
        from scipy.cluster.hierarchy import ward

        import matplotlib.pyplot as plt
        from scipy.cluster.hierarchy import average, weighted, ward, single, complete, dendrogram
        from scipy.cluster import hierarchy
        from scipy.spatial.distance import pdist

        if orientation == "left":
            orientation = "right"
        if orientation == "top":
            LEAF_ROTATION_DEGREE = 90
        else:
            LEAF_ROTATION_DEGREE = 0

        if linkage == "ward":
            dist = euclidean_distances(dtm)
            np.round(dist, 1)
            linkage_matrix = ward(dist)
            dendrogram(linkage_matrix, orientation=orientation, leaf_rotation=LEAF_ROTATION_DEGREE, labels=labels)
            Z = linkage_matrix
        else:
            Y = pdist(dtm, metric)
            Z = hierarchy.linkage(Y, method=linkage)
            dendrogram(Z, orientation=orientation, leaf_rotation=LEAF_ROTATION_DEGREE, labels=labels)

        plt.tight_layout()  # fixes margins

        ## Conversion to Newick/ETE
        # Stuff we need
        from scipy.cluster.hierarchy import average, linkage, to_tree
        #from hcluster import linkage, to_tree
        from ete2 import Tree, TreeStyle, NodeStyle

        # Change it to a distance matrix
        T = to_tree(Z)

        # ete2 section
        root = Tree()
        root.dist = 0
        root.name = "root"
        item2node = {T: root}

        to_visit = [T]
        while to_visit:
            node = to_visit.pop()
            cl_dist = node.dist /2.0
            for ch_node in [node.left, node.right]:
                if ch_node:
                    ch = Tree()
                    ch.dist = cl_dist
                    ch.name = str(ch_node.id)
                    item2node[node].add_child(ch)
                    item2node[ch_node] = ch
                    to_visit.append(ch_node)

        # This is the ETE tree structure
        tree = root
        ts = TreeStyle()
        ts.show_leaf_name = True
        ts.show_branch_length = True
        ts.show_scale = False
        ts.scale =  None
        if orientation == "top":
            ts.rotation = 90
            ts.branch_vertical_margin = 10 # 10 pixels between adjacent branches

        # Draws nodes as small red spheres of diameter equal to 10 pixels
        nstyle = NodeStyle()
        nstyle["size"] = 0

        # Replace the node labels
        for leaf in tree:
            k = leaf.name
            k = int(k)
            leaf.name = labels[k]

        # Apply node styles to nodes
        for n in tree.traverse():
           n.set_style(nstyle)

        # Convert the ETE tree to Newick
        newick = tree.write()
        f = open('C:\\Users\\Scott\\Documents\\GitHub\\d3-dendro\\newickStr.txt', 'w')
        f.write(newick)
        f.close()

        # Save the image as .png...
        from os import path, makedirs

        # Using ETE
        folder = pathjoin(session_manager.session_folder(), constants.RESULTS_FOLDER)
        if (not os.path.isdir(folder)):
            makedirs(folder)

        # saves dendrogram as a .png with pyplot
        plt.savefig(path.join(folder, constants.DENDROGRAM_PNG_FILENAME))
        plt.close()
        # if orientation == "top":
        #     plt.figure(figsize=(20,80))
        # else:
        #     plt.figure(figsize=(80,20))

        pdfPageNumber, score, inconsistentMax, maxclustMax, distanceMax, distanceMin, monocritMax, monocritMin, threshold = utility.generateDendrogram(
            fileManager)
        session['dengenerated'] = True
        labels = fileManager.getActiveLabels()

        inconsistentOp = "0 " + leq + " t " + leq + " " + str(inconsistentMax)
        maxclustOp = "2 " + leq + " t " + leq + " " + str(maxclustMax)
        distanceOp = str(distanceMin) + " " + leq + " t " + leq + " " + str(distanceMax)
        monocritOp = str(monocritMin) + " " + leq + " t " + leq + " " + str(monocritMax)

        thresholdOps = {"inconsistent": inconsistentOp, "maxclust": maxclustOp, "distance": distanceOp,
                        "monocrit": monocritOp}

        managers.utility.saveFileManager(fileManager)
        session_manager.cacheAnalysisOption()
        session_manager.cacheHierarchyOption()
        import random
        ver = random.random() * 100
        return render_template('cluster.html', labels=labels, pdfPageNumber=pdfPageNumber, score=score,
                               inconsistentMax=inconsistentMax, maxclustMax=maxclustMax, distanceMax=distanceMax,
                               distanceMin=distanceMin, monocritMax=monocritMax, monocritMin=monocritMin,
                               threshold=threshold, thresholdOps=thresholdOps, ver=ver)
예제 #27
0
def bct_dendrogram(corpus, tree, outputfile=None,
                   fontsize=5, save_newick=True, mode='c', show=False,
                   color_leafs=False, save=False, return_svg=True):
    """
    Draw a dendrogram of the texts in the corpus using ETE.
    
    Parameters
    ----------
    corpus : `Corpus` instance
        The corpus to be plotted.
    tree : `(VN)Clusterer` object
        The clusterer object which was
        applied to the corpus.
    outputfile : str
        The path where the plot should be saved.
    color_leafs: boolean, default=True,
        If true, will color the text labels
        according to their category.
    fontsize : int, default=5
        The fontsize of the labels on the axes.
    save_newick : boolean, default=True
        Whether to dump a representation of the
        tree in newick-format, which can later
        be modified using software like FigTree:
        http://tree.bio.ed.ac.uk/software/figtree/
    mode : str, default='c'
        The type of tree to be drawn. Supports:
        - 'c': circular dendrogram
        - 'r': traditional, rectangular dendrogram
    save : boolean, default=False
        Whether to save the plot to `outputfile`.
    return_svg : boolean, default=True
        Whether to return the plot in SVG-format.
        Useful for the GUI.
    """

    for leaf in tree.get_leaves():
        leaf.name = leaf.name.replace("'", '')
    
    def layout(node):
        if node.is_leaf():
            N = AttrFace("name", fsize=7)
            faces.add_face_to_node(faces.AttrFace("name","Arial",10, None), node, 0, position='branch-right')
            # problems: aligment of labels to branch, left padding of labels

    ts = TreeStyle()
    ts.mode = mode
    ts.show_leaf_name = False
    ts.scale = 120
    ts.show_scale = False
    ts.branch_vertical_margin = 10

    nstyle = NodeStyle()
    nstyle["fgcolor"] = "#0f0f0f"
    nstyle["size"] = 0
    nstyle["vt_line_color"] = "#0f0f0f"
    nstyle["hz_line_color"] = "#0f0f0f"
    nstyle["vt_line_width"] = 1
    nstyle["hz_line_width"] = 1
    nstyle["vt_line_type"] = 0
    nstyle["hz_line_type"] = 0

    for n in tree.traverse():
       n.set_style(nstyle)
        
    ts.layout_fn = layout

    if outputfile:
        outputfile = os.path.expanduser(outputfile)

    if save_newick: # save tree in newick format for later manipulation in e.g. FigTree:
        tree.write(outfile=os.path.splitext(outputfile)[0]+'.newick')

    if save:
        tree.render(outputfile, tree_style=ts)
    if show:
        tree.show(tree_style=ts) 
    if return_svg: # return the SVG as a string
        return tree.render("%%return")[0]
예제 #28
0
def showTree(delimitation, scale = 500, render = False, fout = "", form = "svg", show_support = False):
	"""delimitation: species_setting class"""
	tree = delimitation.root
	style0 = NodeStyle()
	style0["fgcolor"] = "#000000"
	style0["vt_line_color"] = "#0000aa"
	style0["hz_line_color"] = "#0000aa"
	style0["vt_line_width"] = 2
	style0["hz_line_width"] = 2
	style0["vt_line_type"] = 0 
	style0["hz_line_type"] = 0
	style0["size"] = 0
	
	tree.clear_face()
	for node in tree.get_descendants():
		node.set_style(style0)
		node.img_style["size"] = 0
		node.clear_face()
	
	tree.set_style(style0)
	tree.img_style["size"] = 0
	
	style1 = NodeStyle()
	style1["fgcolor"] = "#000000"
	style1["vt_line_color"] = "#ff0000"
	style1["hz_line_color"] = "#0000aa"
	style1["vt_line_width"] = 2
	style1["hz_line_width"] = 2
	style1["vt_line_type"] = 0 
	style1["hz_line_type"] = 0
	style1["size"] = 0
	
	style2 = NodeStyle()
	style2["fgcolor"] = "#0f0f0f"
	style2["vt_line_color"] = "#ff0000"
	style2["hz_line_color"] = "#ff0000"
	style2["vt_line_width"] = 2
	style2["hz_line_width"] = 2
	style2["vt_line_type"] = 0 
	style2["hz_line_type"] = 0
	style2["size"] = 0
	
	for node in delimitation.active_nodes:
		node.set_style(style1)
		node.img_style["size"] = 0
		for des in node.get_descendants():
			des.set_style(style2)
			des.img_style["size"] = 0
	
	for node in delimitation.root.traverse(strategy='preorder'):
		if show_support and hasattr(node, "bs"):
			if node.bs == 0.0:
				node.add_face(TextFace("0", fsize = 8), column=0, position = "branch-top")
			else:
				node.add_face(TextFace("{0:.2f}".format(node.bs), fsize = 8), column=0, position = "branch-top")
			
	ts = TreeStyle()
	"""scale pixels per branch length unit"""
	ts.scale =  scale 
	ts.branch_vertical_margin = 7
	if render:
		tree.render(fout+"."+form, tree_style=ts)
	else:
		tree.show(tree_style=ts)
예제 #29
0
파일: ete_view.py 프로젝트: F4L2/devoirs
def run(args):
    if args.text_mode:
        from ete2 import Tree
        for tindex, tfile in enumerate(args.src_tree_iterator):
            #print tfile
            if args.raxml:
                nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]", open(tfile).read())
                t = Tree(nw)
            else:
                t = Tree(tfile)
            
            print t.get_ascii(show_internal=args.show_internal_names,
                              attributes=args.show_attributes)
        return
        
    import random
    import re
    import colorsys
    from collections import defaultdict
    from ete2 import (Tree, PhyloTree, TextFace, RectFace, faces, TreeStyle,
                         add_face_to_node, random_color)
    
    global FACES
    
    if args.face:
        FACES = parse_faces(args.face)
    else:
        FACES = []

    # VISUALIZATION
    ts = TreeStyle()
    ts.mode = args.mode
    ts.show_leaf_name = True
    ts.tree_width = args.tree_width

    
    for f in FACES:
        if f["value"] == "@name":
            ts.show_leaf_name = False
            break

    if args.as_ncbi:
        ts.show_leaf_name = False
        FACES.extend(parse_faces(
            ['value:@sci_name, size:10, fstyle:italic',
             'value:@taxid, color:grey, size:6, format:" - %s"',
             'value:@sci_name, color:steelblue, size:7, pos:b-top, nodetype:internal',
             'value:@rank, color:indianred, size:6, pos:b-bottom, nodetype:internal',
         ]))


    if args.alg:
        FACES.extend(parse_faces(
            ['value:@sequence, size:10, pos:aligned, ftype:%s' %args.alg_type]
         ))

    if args.heatmap:
        FACES.extend(parse_faces(
            ['value:@name, size:10, pos:aligned, ftype:heatmap']
         ))
        
    if args.bubbles:
        for bubble in args.bubbles:
            FACES.extend(parse_faces(
                ['value:@%s, pos:float, ftype:bubble, opacity:0.4' %bubble,
             ]))
                        
    ts.branch_vertical_margin = args.branch_separation
    if args.show_support:
        ts.show_branch_support = True
    if args.show_branch_length:
        ts.show_branch_length = True
    if args.force_topology:
        ts.force_topology = True
    ts.layout_fn = lambda x: None
        
    for tindex, tfile in enumerate(args.src_tree_iterator):
        #print tfile
        if args.raxml:
            nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]", open(tfile).read())
            t = PhyloTree(nw)
        else:
            t = PhyloTree(tfile)

            
        if args.alg:
            t.link_to_alignment(args.alg, alg_format=args.alg_format)

        if args.heatmap:
            DEFAULT_COLOR_SATURATION = 0.3
            BASE_LIGHTNESS = 0.7
            def gradient_color(value, max_value, saturation=0.5, hue=0.1):    
                def rgb2hex(rgb):
                    return '#%02x%02x%02x' % rgb
                def hls2hex(h, l, s):
                    return rgb2hex( tuple(map(lambda x: int(x*255), colorsys.hls_to_rgb(h, l, s))))

                lightness = 1 - (value * BASE_LIGHTNESS) / max_value
                return hls2hex(hue, lightness, DEFAULT_COLOR_SATURATION)

            
            heatmap_data = {}
            max_value, min_value = None, None
            for line in open(args.heatmap):
                if line.startswith('#COLNAMES'):
                    pass
                elif line.startswith('#') or not line.strip():
                    pass
                else:
                    fields = line.split('\t')
                    name = fields[0].strip()

                    values = map(lambda x: float(x) if x else None, fields[1:])

                    maxv = max(values)
                    minv = min(values)
                    if max_value is None or maxv > max_value:
                        max_value = maxv
                    if min_value is None or minv < min_value:
                        min_value = minv                        
                    heatmap_data[name] = values

            heatmap_center_value = 0
            heatmap_color_center = "white"
            heatmap_color_up = 0.3
            heatmap_color_down = 0.7
            heatmap_color_missing = "black"
            
            heatmap_max_value = abs(heatmap_center_value - max_value)
            heatmap_min_value = abs(heatmap_center_value - min_value)
                    
            if heatmap_center_value <= min_value:
                heatmap_max_value = heatmap_min_value + heatmap_max_value
            else:
                heatmap_max_value = max(heatmap_min_value, heatmap_max_value)

                    
                    
        # scale the tree
        if not args.height: 
            args.height = None
        if not args.width: 
            args.width = None

        f2color = {}
        f2last_seed = {}
        for node in t.traverse():
            node.img_style['size'] = 0                
            if len(node.children) == 1:
                node.img_style['size'] = 2                
                node.img_style['shape'] = "square"
                node.img_style['fgcolor'] = "steelblue"

            ftype_pos = defaultdict(int)

            for findex, f in enumerate(FACES):
                if (f['nodetype'] == 'any' or 
                    (f['nodetype'] == 'leaf' and node.is_leaf()) or
                    (f['nodetype'] == 'internal' and not node.is_leaf())):


                    # if node passes face filters
                    if node_matcher(node, f["filters"]):
                        if f["value"].startswith("@"):
                            fvalue = getattr(node, f["value"][1:], None)
                        else:
                            fvalue = f["value"]

                        # if node's attribute has content, generate face
                        if fvalue is not None:
                            fsize = f["size"]
                            fbgcolor = f["bgcolor"]
                            fcolor = f['color']

                            if fcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr :
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fcolor = f2color.setdefault(color_bin, random_color(h=seed))

                            if fbgcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fbgcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr :
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fbgcolor = f2color.setdefault(color_bin, random_color(h=seed))

                            if f["ftype"] == "text":
                                if f.get("format", None):
                                    fvalue = f["format"] % fvalue

                                F = TextFace(fvalue,
                                             fsize = fsize,
                                             fgcolor = fcolor or "black",
                                             fstyle = f.get('fstyle', None))

                            elif f["ftype"] == "fullseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="seq",
                                                       seqtail_format="seq",
                                                       height=fsize)
                            elif f["ftype"] == "compactseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="compactseq",
                                                       seqtail_format="compactseq",
                                                       height=fsize)
                            elif f["ftype"] == "blockseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="blockseq",
                                                   seqtail_format="blockseq",
                                                       height=fsize,
                                                       fgcolor=fcolor or "slategrey",
                                                       bgcolor=fbgcolor or "slategrey",
                                                       scale_factor = 1.0)
                                fbgcolor = None
                            elif f["ftype"] == "bubble":
                                try:
                                    v = float(fvalue)
                                except ValueError:
                                    rad = fsize
                                else:
                                    rad = fsize * v
                                F = faces.CircleFace(radius=rad, style="sphere",
                                                     color=fcolor or "steelblue")

                            elif f["ftype"] == "heatmap":
                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                else:
                                    col = f["column"]

                                for i, value in enumerate(heatmap_data.get(node.name, [])):
                                    ftype_pos[f["pos"]] += 1

                                    if value is None:
                                        color = heatmap_color_missing
                                    elif value > heatmap_center_value:
                                        color = gradient_color(abs(heatmap_center_value - value), heatmap_max_value, hue=heatmap_color_up)
                                    elif value < heatmap_center_value:
                                        color = gradient_color(abs(heatmap_center_value - value), heatmap_max_value, hue=heatmap_color_down)
                                    else:
                                        color = heatmap_color_center
                                    node.add_face(RectFace(20, 20, color, color), position="aligned", column=col + i)
                                    # Add header 
                                    # for i, name in enumerate(header):
                                    #    nameF = TextFace(name, fsize=7)
                                    #    nameF.rotation = -90
                                    #    tree_style.aligned_header.add_face(nameF, column=i)
                                F = None

                            elif f["ftype"] == "profile":
                                # internal profiles?
                                F = None
                            elif f["ftype"] == "barchart":
                                F = None
                            elif f["ftype"] == "piechart":
                                F = None



                            # Add the Face
                            if F:
                                F.opacity = f['opacity'] or 1.0

                                # Set face general attributes
                                if fbgcolor:
                                    F.background.color = fbgcolor

                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                    ftype_pos[f["pos"]] += 1    
                                else:
                                    col = f["column"]
                                node.add_face(F, column=col, position=f["pos"])

        if args.image:
            t.render("t%d.%s" %(tindex, args.image),
                     tree_style=ts, w=args.width, h=args.height, units=args.size_units)
        else:
            t.show(None, tree_style=ts)
예제 #30
0
def cluster():
    fileManager = managers.utility.loadFileManager()
    leq = '≤'.decode('utf-8')

    if request.method == "GET":
        # "GET" request occurs when the page is first loaded.
        if 'analyoption' not in session:
            session['analyoption'] = constants.DEFAULT_ANALIZE_OPTIONS
        if 'hierarchyoption' not in session:
            session['hierarchyoption'] = constants.DEFAULT_HIERARCHICAL_OPTIONS
        labels = fileManager.getActiveLabels()
        thresholdOps = {}
        return render_template('cluster.html',
                               labels=labels,
                               thresholdOps=thresholdOps)

    if 'getdendro' in request.form:
        labelDict = fileManager.getActiveLabels()
        labels = []
        for ind, label in labelDict.items():
            labels.append(label)
        # Apply re-tokenisation and filters to DTM
        #countMatrix = fileManager.getMatrix(ARGUMENTS OMITTED)

        # Get options from request.form
        orientation = str(request.form['orientation'])
        title = request.form['title']
        pruning = request.form['pruning']
        pruning = int(request.form['pruning']) if pruning else 0
        linkage = str(request.form['linkage'])
        metric = str(request.form['metric'])

        # Get active files
        allContents = []  # list of strings-of-text for each segment
        tempLabels = []  # list of labels for each segment
        for lFile in fileManager.files.values():
            if lFile.active:
                contentElement = lFile.loadContents()
                allContents.append(contentElement)

                if request.form["file_" + str(lFile.id)] == lFile.label:
                    tempLabels.append(lFile.label.encode("utf-8"))
                else:
                    newLabel = request.form["file_" +
                                            str(lFile.id)].encode("utf-8")
                    tempLabels.append(newLabel)

        # More options
        ngramSize = int(request.form['tokenSize'])
        useWordTokens = request.form['tokenType'] == 'word'
        try:
            useFreq = request.form['normalizeType'] == 'freq'

            useTfidf = request.form['normalizeType'] == 'tfidf'  # if use TF/IDF
            normOption = "N/A"  # only applicable when using "TF/IDF", set default value to N/A
            if useTfidf:
                if request.form['norm'] == 'l1':
                    normOption = u'l1'
                elif request.form['norm'] == 'l2':
                    normOption = u'l2'
                else:
                    normOption = None
        except:
            useFreq = useTfidf = False
            normOption = None

        onlyCharGramsWithinWords = False
        if not useWordTokens:  # if using character-grams
            # this option is disabled on the GUI, because countVectorizer count front and end markers as ' ' if this is true
            onlyCharGramsWithinWords = 'inWordsOnly' in request.form

        greyWord = 'greyword' in request.form
        MostFrequenWord = 'mfwcheckbox' in request.form
        Culling = 'cullcheckbox' in request.form

        showDeletedWord = False
        if 'greyword' or 'mfwcheckbox' or 'cullcheckbox' in request.form:
            if 'onlygreyword' in request.form:
                showDeletedWord = True

        if useWordTokens:
            tokenType = u'word'
        else:
            tokenType = u'char'
            if onlyCharGramsWithinWords:
                tokenType = u'char_wb'

        from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
        vectorizer = CountVectorizer(input=u'content',
                                     encoding=u'utf-8',
                                     min_df=1,
                                     analyzer=tokenType,
                                     token_pattern=ur'(?u)\b[\w\']+\b',
                                     ngram_range=(ngramSize, ngramSize),
                                     stop_words=[],
                                     dtype=float,
                                     max_df=1.0)

        # make a (sparse) Document-Term-Matrix (DTM) to hold all counts
        DocTermSparseMatrix = vectorizer.fit_transform(allContents)
        dtm = DocTermSparseMatrix.toarray()

        from sklearn.metrics.pairwise import euclidean_distances
        from scipy.cluster.hierarchy import ward

        import matplotlib.pyplot as plt
        from scipy.cluster.hierarchy import average, weighted, ward, single, complete, dendrogram
        from scipy.cluster import hierarchy
        from scipy.spatial.distance import pdist

        if orientation == "left":
            orientation = "right"
        if orientation == "top":
            LEAF_ROTATION_DEGREE = 90
        else:
            LEAF_ROTATION_DEGREE = 0

        if linkage == "ward":
            dist = euclidean_distances(dtm)
            np.round(dist, 1)
            linkage_matrix = ward(dist)
            dendrogram(linkage_matrix,
                       orientation=orientation,
                       leaf_rotation=LEAF_ROTATION_DEGREE,
                       labels=labels)
            Z = linkage_matrix
        else:
            Y = pdist(dtm, metric)
            Z = hierarchy.linkage(Y, method=linkage)
            dendrogram(Z,
                       orientation=orientation,
                       leaf_rotation=LEAF_ROTATION_DEGREE,
                       labels=labels)

        plt.tight_layout()  # fixes margins

        ## Conversion to Newick/ETE
        # Stuff we need
        from scipy.cluster.hierarchy import average, linkage, to_tree
        #from hcluster import linkage, to_tree
        from ete2 import Tree, TreeStyle, NodeStyle

        # Change it to a distance matrix
        T = to_tree(Z)

        # ete2 section
        root = Tree()
        root.dist = 0
        root.name = "root"
        item2node = {T: root}

        to_visit = [T]
        while to_visit:
            node = to_visit.pop()
            cl_dist = node.dist / 2.0
            for ch_node in [node.left, node.right]:
                if ch_node:
                    ch = Tree()
                    ch.dist = cl_dist
                    ch.name = str(ch_node.id)
                    item2node[node].add_child(ch)
                    item2node[ch_node] = ch
                    to_visit.append(ch_node)

        # This is the ETE tree structure
        tree = root
        ts = TreeStyle()
        ts.show_leaf_name = True
        ts.show_branch_length = True
        ts.show_scale = False
        ts.scale = None
        if orientation == "top":
            ts.rotation = 90
            ts.branch_vertical_margin = 10  # 10 pixels between adjacent branches

        # Draws nodes as small red spheres of diameter equal to 10 pixels
        nstyle = NodeStyle()
        nstyle["size"] = 0

        # Replace the node labels
        for leaf in tree:
            k = leaf.name
            k = int(k)
            leaf.name = labels[k]

        # Apply node styles to nodes
        for n in tree.traverse():
            n.set_style(nstyle)

        # Convert the ETE tree to Newick
        newick = tree.write()
        f = open(
            'C:\\Users\\Scott\\Documents\\GitHub\\d3-dendro\\newickStr.txt',
            'w')
        f.write(newick)
        f.close()

        # Save the image as .png...
        from os import path, makedirs

        # Using ETE
        folder = pathjoin(session_manager.session_folder(),
                          constants.RESULTS_FOLDER)
        if (not os.path.isdir(folder)):
            makedirs(folder)

        # saves dendrogram as a .png with pyplot
        plt.savefig(path.join(folder, constants.DENDROGRAM_PNG_FILENAME))
        plt.close()
        # if orientation == "top":
        #     plt.figure(figsize=(20,80))
        # else:
        #     plt.figure(figsize=(80,20))

        pdfPageNumber, score, inconsistentMax, maxclustMax, distanceMax, distanceMin, monocritMax, monocritMin, threshold = utility.generateDendrogram(
            fileManager)
        session['dengenerated'] = True
        labels = fileManager.getActiveLabels()

        inconsistentOp = "0 " + leq + " t " + leq + " " + str(inconsistentMax)
        maxclustOp = "2 " + leq + " t " + leq + " " + str(maxclustMax)
        distanceOp = str(distanceMin) + " " + leq + " t " + leq + " " + str(
            distanceMax)
        monocritOp = str(monocritMin) + " " + leq + " t " + leq + " " + str(
            monocritMax)

        thresholdOps = {
            "inconsistent": inconsistentOp,
            "maxclust": maxclustOp,
            "distance": distanceOp,
            "monocrit": monocritOp
        }

        managers.utility.saveFileManager(fileManager)
        session_manager.cacheAnalysisOption()
        session_manager.cacheHierarchyOption()
        import random
        ver = random.random() * 100
        return render_template('cluster.html',
                               labels=labels,
                               pdfPageNumber=pdfPageNumber,
                               score=score,
                               inconsistentMax=inconsistentMax,
                               maxclustMax=maxclustMax,
                               distanceMax=distanceMax,
                               distanceMin=distanceMin,
                               monocritMax=monocritMax,
                               monocritMin=monocritMin,
                               threshold=threshold,
                               thresholdOps=thresholdOps,
                               ver=ver)
예제 #31
0
width = 600  # For other output args, see http://etetoolkit.org/docs/latest/tutorial/tutorial_drawing.html#rendering-trees-as-images

# Configure tree styles
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_length = True
ts.show_scale = False
ts.scale = None

ts.mode = "c"  # draw tree in circular style
ts.arc_start = 0
ts.arc_span = 360

if orientation == "top":
    ts.rotation = 90
    ts.branch_vertical_margin = 10  # 10 pixels between adjacent branches

# Remove the default small red spheres for each node
nstyle = NodeStyle()
nstyle["size"] = 0
for leaf in tree:
    k = leaf.name
    k = int(k)
    leaf.name = labels[k]

# Apply node styles to nodes
for n in tree.traverse():
    n.set_style(nstyle)

# Plot the tree
tree.show(tree_style=ts)
예제 #32
0
width = 600 # For other output args, see http://etetoolkit.org/docs/latest/tutorial/tutorial_drawing.html#rendering-trees-as-images

# Configure tree styles
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_length = True
ts.show_scale = False
ts.scale =  None

ts.mode = "c" # draw tree in circular style
ts.arc_start = 0
ts.arc_span = 360

if orientation == "top":
    ts.rotation = 90
    ts.branch_vertical_margin = 10 # 10 pixels between adjacent branches

# Remove the default small red spheres for each node
nstyle = NodeStyle()
nstyle["size"] = 0
for leaf in tree:
    k = leaf.name
    k = int(k)
    leaf.name = labels[k]

# Apply node styles to nodes
for n in tree.traverse():
   n.set_style(nstyle)

# Plot the tree
tree.show(tree_style=ts)
예제 #33
0
common_tree_file = args.tree_with_general_data

general_tree_file = "%s.png" % args.output_prefix
general_significant_tree_file = "%s_significant.png" % args.output_prefix
general_tree_file_no_labels = "%s_no_labels.png" % args.output_prefix
with open(common_tree_file, "r") as tree_fd:
    tree = Tree(tree_fd.readline().strip(), format=1)

for node in tree.traverse():
    node.img_style["size"] = 0

ts = TreeStyle()
ts.layout_fn = layout
ts.optimal_scale_level = "full"
ts.branch_vertical_margin = 10
ts.show_leaf_name = False
#ts.allow_face_overlap =True

tree.render(general_tree_file, w=200, units='mm', tree_style=ts, dpi=300)

ts_significant = TreeStyle()
ts_significant.layout_fn = layout_significant
ts_significant.optimal_scale_level = "full"
ts_significant.branch_vertical_margin = 10
ts_significant.show_leaf_name = False
#ts.allow_face_overlap =True

tree.render(general_significant_tree_file,
            w=200,
            units='mm',